author | Michael Krelin <hacker@klever.net> | 2006-10-01 21:12:03 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2006-10-01 21:12:03 (UTC) |
commit | d11f973311060020c6cc760f7304488155f40dd7 (patch) (unidiff) | |
tree | 9a6f65554efd5ad3ffda7ac8bef4a188093ff96b | |
parent | 6f8e1b5f544a41f492ca42dc407d0580cfc67bc0 (diff) | |
download | fireflix-d11f973311060020c6cc760f7304488155f40dd7.zip fireflix-d11f973311060020c6cc760f7304488155f40dd7.tar.gz fireflix-d11f973311060020c6cc760f7304488155f40dd7.tar.bz2 |
moved UI authorization code from flickr object
git-svn-id: http://svn.klever.net/kin/fireflix/trunk@176 fe716a7a-6dde-0310-88d9-d003556173a8
-rw-r--r-- | content/fireflix.js | 4 | ||||
-rw-r--r-- | content/flickr.js | 12 |
2 files changed, 6 insertions, 10 deletions
diff --git a/content/fireflix.js b/content/fireflix.js index 0f01d26..f180379 100644 --- a/content/fireflix.js +++ b/content/fireflix.js | |||
@@ -9,97 +9,99 @@ function splitascii(s) { | |||
9 | } | 9 | } |
10 | 10 | ||
11 | 11 | ||
12 | var fireflix = { | 12 | var fireflix = { |
13 | flickr: new Flickr(), | 13 | flickr: new Flickr(), |
14 | init: function() { | 14 | init: function() { |
15 | pull_elements(this,document,[ | 15 | pull_elements(this,document,[ |
16 | 'cmd_auth_auth','cmd_auth_done','cmd_auth_unauth', | 16 | 'cmd_auth_auth','cmd_auth_done','cmd_auth_unauth', |
17 | 'menu_auth_done','b_auth','b_auth_done','auth_info', | 17 | 'menu_auth_done','b_auth','b_auth_done','auth_info', |
18 | 'loc_strings','cmd_set_props' | 18 | 'loc_strings','cmd_set_props' |
19 | ]); | 19 | ]); |
20 | this.build_menus(); | 20 | this.build_menus(); |
21 | this.foundphotos.init(this); | 21 | this.foundphotos.init(this); |
22 | this.photosets.init(this); | 22 | this.photosets.init(this); |
23 | this.photoset.init(this); | 23 | this.photoset.init(this); |
24 | this.uploads.init(this); | 24 | this.uploads.init(this); |
25 | this.uploadObserver.init(this); | 25 | this.uploadObserver.init(this); |
26 | this.flickr.api_key = '9c43cd66947a57e6f29db1a9da3f72e3'; | 26 | this.flickr.api_key = '9c43cd66947a57e6f29db1a9da3f72e3'; |
27 | this.flickr.api_shs = '9c33c9e2f0f0cfd5'; | 27 | this.flickr.api_shs = '9c33c9e2f0f0cfd5'; |
28 | this.flickr.prefs_root = 'net.klever.kin.fireflix'; | 28 | this.flickr.prefs_root = 'net.klever.kin.fireflix'; |
29 | this.flickr.load_token(); | 29 | this.flickr.load_token(); |
30 | document.getElementById('setslist').view = this.photosets; | 30 | document.getElementById('setslist').view = this.photosets; |
31 | document.getElementById('setphotos').view = this.photoset; | 31 | document.getElementById('setphotos').view = this.photoset; |
32 | document.getElementById('uploadlist').view = this.uploads; | 32 | document.getElementById('uploadlist').view = this.uploads; |
33 | this.no_auth_info_label = this.auth_info.value; | 33 | this.no_auth_info_label = this.auth_info.value; |
34 | this.set_auth_state(this.flickr.token,false); | 34 | this.set_auth_state(this.flickr.token,false); |
35 | if(this.flickr.token) { | 35 | if(this.flickr.token) { |
36 | this.refresh_stuff(); | 36 | this.refresh_stuff(); |
37 | }else{ | 37 | }else{ |
38 | this.on_cmd_auth(); | 38 | this.on_cmd_auth(); |
39 | } | 39 | } |
40 | }, | 40 | }, |
41 | set_auth_state: function(au,inp) { /* authorized, in progress */ | 41 | set_auth_state: function(au,inp) { /* authorized, in progress */ |
42 | this.cmd_auth_unauth.disabled = !au; | 42 | this.cmd_auth_unauth.disabled = !au; |
43 | this.b_auth.hidden = au || inp; | 43 | this.b_auth.hidden = au || inp; |
44 | this.b_auth_done.hidden = !inp; | 44 | this.b_auth_done.hidden = !inp; |
45 | this.menu_auth_done.hidden = !inp; | 45 | this.menu_auth_done.hidden = !inp; |
46 | this.cmd_auth_done.setAttribute('disabled',!inp); | 46 | this.cmd_auth_done.setAttribute('disabled',!inp); |
47 | this.auth_info.disabled = !au; | 47 | this.auth_info.disabled = !au; |
48 | if(au) { | 48 | if(au) { |
49 | this.auth_info.value = this.flickr.user.fullname+' ['+this.flickr.user.username+']'; /* TODO: move to locale */ | 49 | this.auth_info.value = this.flickr.user.fullname+' ['+this.flickr.user.username+']'; /* TODO: move to locale */ |
50 | }else{ | 50 | }else{ |
51 | this.auth_info.value = this.no_auth_info_label; | 51 | this.auth_info.value = this.no_auth_info_label; |
52 | } | 52 | } |
53 | }, | 53 | }, |
54 | on_cmd_auth: function() { | 54 | on_cmd_auth: function() { |
55 | var _this = this; | 55 | var _this = this; |
56 | this.flickr.authorize_0( | 56 | this.flickr.authorize_0( |
57 | function() { | 57 | 'delete', |
58 | function(x,f,u) { | ||
59 | _this.openTab(u); | ||
58 | _this.set_auth_state(_this.flickr.token,true); | 60 | _this.set_auth_state(_this.flickr.token,true); |
59 | }, function(x,s,c,m) { | 61 | }, function(x,s,c,m) { |
60 | _this.flickr_failure(x,s,c,m); | 62 | _this.flickr_failure(x,s,c,m); |
61 | } | 63 | } |
62 | ); | 64 | ); |
63 | }, | 65 | }, |
64 | on_cmd_auth_done: function() { | 66 | on_cmd_auth_done: function() { |
65 | this.set_auth_state(this.flickr.token,false); | 67 | this.set_auth_state(this.flickr.token,false); |
66 | var _this = this; | 68 | var _this = this; |
67 | this.flickr.authorize_1( | 69 | this.flickr.authorize_1( |
68 | function() { | 70 | function() { |
69 | _this.flickr.save_token(); | 71 | _this.flickr.save_token(); |
70 | _this.refresh_stuff(); | 72 | _this.refresh_stuff(); |
71 | _this.set_auth_state(_this.flickr.token,false); | 73 | _this.set_auth_state(_this.flickr.token,false); |
72 | _this.auth_info.value = | 74 | _this.auth_info.value = |
73 | _this.flickr.user.fullname+' ['+_this.flickr.user.username+']'; | 75 | _this.flickr.user.fullname+' ['+_this.flickr.user.username+']'; |
74 | }, function(x,s,c,m) { | 76 | }, function(x,s,c,m) { |
75 | _this.set_auth_state(_this.flickr.token,false); /* XXX: no reset token? */ | 77 | _this.set_auth_state(_this.flickr.token,false); /* XXX: no reset token? */ |
76 | _this.flickr_failure(x,s,c,m); | 78 | _this.flickr_failure(x,s,c,m); |
77 | } | 79 | } |
78 | ); | 80 | ); |
79 | }, | 81 | }, |
80 | on_cmd_auth_unauth: function() { | 82 | on_cmd_auth_unauth: function() { |
81 | this.flickr.reset_token(); | 83 | this.flickr.reset_token(); |
82 | this.set_auth_state(false,false); | 84 | this.set_auth_state(false,false); |
83 | }, | 85 | }, |
84 | 86 | ||
85 | refresh_sets: function() { this.photosets.refresh_sets(); }, | 87 | refresh_sets: function() { this.photosets.refresh_sets(); }, |
86 | refresh_stuff: function() { | 88 | refresh_stuff: function() { |
87 | this.refresh_sets(); | 89 | this.refresh_sets(); |
88 | this.refresh_user_tags(); | 90 | this.refresh_user_tags(); |
89 | }, | 91 | }, |
90 | 92 | ||
91 | /* photoset treeview */ | 93 | /* photoset treeview */ |
92 | photoset: { | 94 | photoset: { |
93 | photos: new Array(), | 95 | photos: new Array(), |
94 | fireflix: null, | 96 | fireflix: null, |
95 | init: function(f) { | 97 | init: function(f) { |
96 | this.fireflix = f; | 98 | this.fireflix = f; |
97 | pull_elements(this,document,[ 'set_photo' ]); | 99 | pull_elements(this,document,[ 'set_photo' ]); |
98 | }, | 100 | }, |
99 | rowCount: 0, | 101 | rowCount: 0, |
100 | getCellText: function(r,c) { | 102 | getCellText: function(r,c) { |
101 | var p = this.photos[r]; | 103 | var p = this.photos[r]; |
102 | if(c.id=='sp_title') return p.title; | 104 | if(c.id=='sp_title') return p.title; |
103 | if(c.id=='sp_taken') return p.datetaken; | 105 | if(c.id=='sp_taken') return p.datetaken; |
104 | if(c.id=='sp_upload') return p.dateupload; /* TODO: unixtime conversion */ | 106 | if(c.id=='sp_upload') return p.dateupload; /* TODO: unixtime conversion */ |
105 | return c.id; | 107 | return c.id; |
diff --git a/content/flickr.js b/content/flickr.js index add628a..b5bfa43 100644 --- a/content/flickr.js +++ b/content/flickr.js | |||
@@ -100,111 +100,105 @@ Flickr.prototype = { | |||
100 | 100 | ||
101 | api_sig: function(paramstr) { | 101 | api_sig: function(paramstr) { |
102 | return MD5(toutf8(this.api_shs+paramstr)); | 102 | return MD5(toutf8(this.api_shs+paramstr)); |
103 | }, | 103 | }, |
104 | api_call_url: function(params,url) { | 104 | api_call_url: function(params,url) { |
105 | params.api_key = this.api_key; | 105 | params.api_key = this.api_key; |
106 | var pp = new Array(); | 106 | var pp = new Array(); |
107 | for(var p in params) { | 107 | for(var p in params) { |
108 | pp.push(p); | 108 | pp.push(p); |
109 | } | 109 | } |
110 | var pstr = ''; | 110 | var pstr = ''; |
111 | var rv = (url?url:this.rest_url)+'?'; | 111 | var rv = (url?url:this.rest_url)+'?'; |
112 | for(var p in pp.sort()) { | 112 | for(var p in pp.sort()) { |
113 | var pn = pp[p]; | 113 | var pn = pp[p]; |
114 | pstr += pn+params[pn]; | 114 | pstr += pn+params[pn]; |
115 | rv += pn+'='+params[pn]+'&'; | 115 | rv += pn+'='+params[pn]+'&'; |
116 | } | 116 | } |
117 | rv += 'api_sig='+this.api_sig(pstr); | 117 | rv += 'api_sig='+this.api_sig(pstr); |
118 | return rv; | 118 | return rv; |
119 | }, | 119 | }, |
120 | api_call: function(params, on_success, on_failure) { | 120 | api_call: function(params, on_success, on_failure) { |
121 | if(params.auth_token == 'default') | 121 | if(params.auth_token == 'default') |
122 | params.auth_token = this.token; | 122 | params.auth_token = this.token; |
123 | var x = new XMLHttpRequest(); | 123 | var x = new XMLHttpRequest(); |
124 | x.open("GET",this.api_call_url(params)); | 124 | x.open("GET",this.api_call_url(params)); |
125 | x.onreadystatechange=function() { | 125 | x.onreadystatechange=function() { |
126 | if(x.readyState!=4) return false; | 126 | if(x.readyState!=4) return false; |
127 | if(x.status==200) { | 127 | if(x.status==200) { |
128 | var stat = x.responseXML.firstChild.getAttribute('stat'); | 128 | var stat = x.responseXML.firstChild.getAttribute('stat'); |
129 | if(stat=='ok') { | 129 | if(stat=='ok') { |
130 | if(on_success) on_success(x); | 130 | if(on_success) on_success(x); |
131 | }else{ | 131 | }else{ |
132 | var e = x.responseXML.getElementsByTagName('err').item(0); | 132 | var e = x.responseXML.getElementsByTagName('err').item(0); |
133 | var ecode = e.getAttribute('code'); | 133 | var ecode = e.getAttribute('code'); |
134 | var emsg = e.getAttribute('msg'); | 134 | var emsg = e.getAttribute('msg'); |
135 | dump(params.method+' failed: '+ecode+' '+emsg+'\n'); | 135 | dump(params.method+' failed: '+ecode+' '+emsg+'\n'); |
136 | if(on_failure) on_failure(x,stat,ecode,emsg); | 136 | if(on_failure) on_failure(x,stat,ecode,emsg); |
137 | } | 137 | } |
138 | }else{ | 138 | }else{ |
139 | if(on_failure) on_failure(x); | 139 | if(on_failure) on_failure(x); |
140 | } | 140 | } |
141 | return true; | 141 | return true; |
142 | } | 142 | } |
143 | x.send(null); | 143 | x.send(null); |
144 | return true; | 144 | return true; |
145 | }, | 145 | }, |
146 | 146 | ||
147 | frob: null, | 147 | frob: null, |
148 | authorize_0: function(on_s, on_f) { | 148 | authorize_0: function(perms, on_s, on_f) { |
149 | var _this = this; | 149 | var _this = this; |
150 | this.api_call( | 150 | this.api_call( |
151 | { method: 'flickr.auth.getFrob' }, | 151 | { method: 'flickr.auth.getFrob' }, |
152 | function(x) { | 152 | function(x) { |
153 | _this.frob = xp_str('/rsp/frob',x.responseXML); | 153 | _this.frob = xp_str('/rsp/frob',x.responseXML); |
154 | var u = _this.api_call_url( | 154 | var u = _this.api_call_url( |
155 | { frob: _this.frob, perms: 'delete' }, _this.auth_url ); | 155 | { frob: _this.frob, perms: perms?perms:'delete' }, _this.auth_url ); |
156 | var wm = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService( | 156 | if(on_s) on_s(x,_this.frob,u); |
157 | Components.interfaces.nsIWindowMediator ); | ||
158 | var bw = wm.getMostRecentWindow('navigator:browser'); | ||
159 | var b = bw.getBrowser(); | ||
160 | var t = b.addTab(u); | ||
161 | b.selectedTab = t; | ||
162 | if(on_s) on_s(); | ||
163 | }, function(x,s,c,m) { | 157 | }, function(x,s,c,m) { |
164 | if(on_f) on_f(x,s,c,m); | 158 | if(on_f) on_f(x,s,c,m); |
165 | } | 159 | } |
166 | ); | 160 | ); |
167 | }, | 161 | }, |
168 | token: null, | 162 | token: null, |
169 | perms: null, | 163 | perms: null, |
170 | user: null, | 164 | user: null, |
171 | authorize_1: function(on_s, on_f) { | 165 | authorize_1: function(on_s, on_f) { |
172 | var _this = this; | 166 | var _this = this; |
173 | this.api_call( | 167 | this.api_call( |
174 | { method: 'flickr.auth.getToken', frob: this.frob }, | 168 | { method: 'flickr.auth.getToken', frob: this.frob }, |
175 | function(x) { | 169 | function(x) { |
176 | _this.token = xp_str('/rsp/auth/token',x.responseXML); | 170 | _this.token = xp_str('/rsp/auth/token',x.responseXML); |
177 | _this.perms = xp_str('/rsp/auth/perms',x.responseXML); | 171 | _this.perms = xp_str('/rsp/auth/perms',x.responseXML); |
178 | var u = xp_node('/rsp/auth/user',x.responseXML); | 172 | var u = xp_node('/rsp/auth/user',x.responseXML); |
179 | _this.user = { | 173 | _this.user = { |
180 | nsid: u.getAttribute('nsid'), | 174 | nsid: u.getAttribute('nsid'), |
181 | username: u.getAttribute('username'), | 175 | username: u.getAttribute('username'), |
182 | fullname: u.getAttribute('fullname') | 176 | fullname: u.getAttribute('fullname') |
183 | }; | 177 | }; |
184 | if(on_s) on_s(x); | 178 | if(on_s) on_s(x); |
185 | }, function(x,s,c,m) { | 179 | }, function(x,s,c,m) { |
186 | if(on_f) on_f(x,s,c,m); | 180 | if(on_f) on_f(x,s,c,m); |
187 | } | 181 | } |
188 | ); | 182 | ); |
189 | }, | 183 | }, |
190 | 184 | ||
191 | prefs: Components.classes['@mozilla.org/preferences-service;1'].getService( | 185 | prefs: Components.classes['@mozilla.org/preferences-service;1'].getService( |
192 | Components.interfaces.nsIPrefBranch | 186 | Components.interfaces.nsIPrefBranch |
193 | ), | 187 | ), |
194 | prefs_root: 'net.klever.kin.flickr', | 188 | prefs_root: 'net.klever.kin.flickr', |
195 | save_token: function() { | 189 | save_token: function() { |
196 | // TODO: don't clear when there's nothing to clear or catch exceptions | 190 | // TODO: don't clear when there's nothing to clear or catch exceptions |
197 | if(this.token) | 191 | if(this.token) |
198 | this.prefs.setCharPref(this.prefs_root+'.auth_token',this.token); | 192 | this.prefs.setCharPref(this.prefs_root+'.auth_token',this.token); |
199 | else | 193 | else |
200 | this.prefs.clearUserPref(this.prefs_root+'.auth_token'); | 194 | this.prefs.clearUserPref(this.prefs_root+'.auth_token'); |
201 | if(this.perms) | 195 | if(this.perms) |
202 | this.prefs.setCharPref(this.prefs_root+'.auth_perms',this.perms); | 196 | this.prefs.setCharPref(this.prefs_root+'.auth_perms',this.perms); |
203 | else | 197 | else |
204 | this.prefs.clearUserPref(this.prefs_root+'.auth_perms'); | 198 | this.prefs.clearUserPref(this.prefs_root+'.auth_perms'); |
205 | if(this.user && this.user.nsid!=null && this.user.nsid!=undefined) | 199 | if(this.user && this.user.nsid!=null && this.user.nsid!=undefined) |
206 | this.prefs.setCharPref(this.prefs_root+'.auth_user.nsid',this.user.nsid); | 200 | this.prefs.setCharPref(this.prefs_root+'.auth_user.nsid',this.user.nsid); |
207 | else | 201 | else |
208 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.nsid'); | 202 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.nsid'); |
209 | if(this.user && this.user.username!=null && this.user.username!=undefined) | 203 | if(this.user && this.user.username!=null && this.user.username!=undefined) |
210 | this.prefs.setCharPref(this.prefs_root+'.auth_user.username',this.user.username); | 204 | this.prefs.setCharPref(this.prefs_root+'.auth_user.username',this.user.username); |