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 /content | |
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 | |||
@@ -1,904 +1,906 @@ | |||
1 | function splitascii(s) { | 1 | function splitascii(s) { |
2 | var rv=''; | 2 | var rv=''; |
3 | for(var i=0;i<s.length;++i) { | 3 | for(var i=0;i<s.length;++i) { |
4 | var w = s.charCodeAt(i); | 4 | var w = s.charCodeAt(i); |
5 | rv += String.fromCharCode( | 5 | rv += String.fromCharCode( |
6 | w&0xff, (w>>8)&0xff ); | 6 | w&0xff, (w>>8)&0xff ); |
7 | } | 7 | } |
8 | return rv; | 8 | return rv; |
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; |
106 | }, | 108 | }, |
107 | setTree: function(t) { this.tree = t }, | 109 | setTree: function(t) { this.tree = t }, |
108 | isContainer: function(r) { return false; }, | 110 | isContainer: function(r) { return false; }, |
109 | isSeparator: function(r) { return false; }, | 111 | isSeparator: function(r) { return false; }, |
110 | isSorted: function(r) { return false; }, | 112 | isSorted: function(r) { return false; }, |
111 | getLevel: function(r) { return 0; }, | 113 | getLevel: function(r) { return 0; }, |
112 | getImageSrc: function(r,c) { return null }, | 114 | getImageSrc: function(r,c) { return null }, |
113 | getRowProperties: function(r,p) {}, | 115 | getRowProperties: function(r,p) {}, |
114 | getCellProperties: function(cid,cel,p) {}, | 116 | getCellProperties: function(cid,cel,p) {}, |
115 | getColumnProperties: function(cid,cel,p) { }, | 117 | getColumnProperties: function(cid,cel,p) { }, |
116 | cycleHeader: function(cid,e) { }, | 118 | cycleHeader: function(cid,e) { }, |
117 | getParentIndex: function(r) { return -1; }, | 119 | getParentIndex: function(r) { return -1; }, |
118 | drop: function(r,o) { }, | 120 | drop: function(r,o) { }, |
119 | canDropBeforeAfter: function(r,b) { return false }, | 121 | canDropBeforeAfter: function(r,b) { return false }, |
120 | 122 | ||
121 | importXPR: function(xp) { | 123 | importXPR: function(xp) { |
122 | this.tree.beginUpdateBatch(); | 124 | this.tree.beginUpdateBatch(); |
123 | this.photos = new Array(); | 125 | this.photos = new Array(); |
124 | var n; while(n=xp.iterateNext()) { | 126 | var n; while(n=xp.iterateNext()) { |
125 | this.photos.push(new Photo(n)); | 127 | this.photos.push(new Photo(n)); |
126 | } | 128 | } |
127 | this.rowCount = this.photos.length; | 129 | this.rowCount = this.photos.length; |
128 | this.tree.endUpdateBatch(); | 130 | this.tree.endUpdateBatch(); |
129 | }, | 131 | }, |
130 | load_photos: function(psid) { | 132 | load_photos: function(psid) { |
131 | var _this = this; | 133 | var _this = this; |
132 | this.fireflix.flickr.api_call( | 134 | this.fireflix.flickr.api_call( |
133 | { | 135 | { |
134 | method: 'flickr.photosets.getPhotos', | 136 | method: 'flickr.photosets.getPhotos', |
135 | auth_token: 'default', | 137 | auth_token: 'default', |
136 | photoset_id: psid, | 138 | photoset_id: psid, |
137 | extras: 'license,date_upload,date_taken,owner_name,icon_server,original_format,last_update' | 139 | extras: 'license,date_upload,date_taken,owner_name,icon_server,original_format,last_update' |
138 | }, function(xr) { | 140 | }, function(xr) { |
139 | var x = xr.responseXML; | 141 | var x = xr.responseXML; |
140 | var xp = x.evaluate( | 142 | var xp = x.evaluate( |
141 | '/rsp/photoset/photo', x, null, | 143 | '/rsp/photoset/photo', x, null, |
142 | XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); | 144 | XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); |
143 | _this.importXPR(xp); | 145 | _this.importXPR(xp); |
144 | }, function(x,s,c,m) { | 146 | }, function(x,s,c,m) { |
145 | _this.fireflix.flickr_failure(x,s,c,m); | 147 | _this.fireflix.flickr_failure(x,s,c,m); |
146 | } | 148 | } |
147 | ); | 149 | ); |
148 | }, | 150 | }, |
149 | on_select: function() { | 151 | on_select: function() { |
150 | if(this.selection.count==1) { | 152 | if(this.selection.count==1) { |
151 | var p = this.photos[this.selection.currentIndex]; | 153 | var p = this.photos[this.selection.currentIndex]; |
152 | this.set_photo.src = | 154 | this.set_photo.src = |
153 | this.fireflix.flickr.get_photo_url(p.server,p.id,p.secret,'t'); | 155 | this.fireflix.flickr.get_photo_url(p.server,p.id,p.secret,'t'); |
154 | this.set_photo.hidden = false; | 156 | this.set_photo.hidden = false; |
155 | }else{ | 157 | }else{ |
156 | this.set_photo.hidden = true; | 158 | this.set_photo.hidden = true; |
157 | } | 159 | } |
158 | } | 160 | } |
159 | }, | 161 | }, |
160 | 162 | ||
161 | /* photosets treeview */ | 163 | /* photosets treeview */ |
162 | photosets: { | 164 | photosets: { |
163 | sets: new Array(), | 165 | sets: new Array(), |
164 | fireflix: null, | 166 | fireflix: null, |
165 | init: function(f) { | 167 | init: function(f) { |
166 | this.fireflix = f; | 168 | this.fireflix = f; |
167 | }, | 169 | }, |
168 | rowCount: 0, | 170 | rowCount: 0, |
169 | getCellText: function(r,c) { | 171 | getCellText: function(r,c) { |
170 | var s = this.sets[r]; | 172 | var s = this.sets[r]; |
171 | if(c.id=='sl_name') return s.title; | 173 | if(c.id=='sl_name') return s.title; |
172 | if(c.id=='sl_photos') return s.photos; | 174 | if(c.id=='sl_photos') return s.photos; |
173 | return c.id; | 175 | return c.id; |
174 | }, | 176 | }, |
175 | setTree: function(t) { this.tree = t }, | 177 | setTree: function(t) { this.tree = t }, |
176 | isContainer: function(r) { return false; }, | 178 | isContainer: function(r) { return false; }, |
177 | isSeparator: function(r) { return false; }, | 179 | isSeparator: function(r) { return false; }, |
178 | isSorted: function() { return false; }, | 180 | isSorted: function() { return false; }, |
179 | getLevel: function(r) { return 0; }, | 181 | getLevel: function(r) { return 0; }, |
180 | getImageSrc: function(r,c) { return null }, | 182 | getImageSrc: function(r,c) { return null }, |
181 | getRowProperties: function(r,p) {}, | 183 | getRowProperties: function(r,p) {}, |
182 | getCellProperties: function(cid,cel,p) { }, | 184 | getCellProperties: function(cid,cel,p) { }, |
183 | getColumnProperties: function(cid,cel,p) { }, | 185 | getColumnProperties: function(cid,cel,p) { }, |
184 | cycleHeader: function(cid,e) { }, | 186 | cycleHeader: function(cid,e) { }, |
185 | getParentIndex: function(r) { return -1; }, | 187 | getParentIndex: function(r) { return -1; }, |
186 | drop: function(r,o) { }, | 188 | drop: function(r,o) { }, |
187 | canDropBeforeAfter: function(r,b) { return false }, | 189 | canDropBeforeAfter: function(r,b) { return false }, |
188 | 190 | ||
189 | importXPR: function(xp) { | 191 | importXPR: function(xp) { |
190 | this.tree.beginUpdateBatch(); | 192 | this.tree.beginUpdateBatch(); |
191 | this.sets = new Array(); | 193 | this.sets = new Array(); |
192 | var n; while(n=xp.iterateNext()) { | 194 | var n; while(n=xp.iterateNext()) { |
193 | this.sets.push(new Photoset(n)); | 195 | this.sets.push(new Photoset(n)); |
194 | } | 196 | } |
195 | this.rowCount = this.sets.length; | 197 | this.rowCount = this.sets.length; |
196 | this.tree.endUpdateBatch(); | 198 | this.tree.endUpdateBatch(); |
197 | }, | 199 | }, |
198 | refresh_sets: function() { | 200 | refresh_sets: function() { |
199 | var _this = this; | 201 | var _this = this; |
200 | this.fireflix.flickr.api_call( | 202 | this.fireflix.flickr.api_call( |
201 | { | 203 | { |
202 | method: 'flickr.photosets.getList', | 204 | method: 'flickr.photosets.getList', |
203 | auth_token: 'default' | 205 | auth_token: 'default' |
204 | }, function(xr) { | 206 | }, function(xr) { |
205 | var x = xr.responseXML; | 207 | var x = xr.responseXML; |
206 | var xp = x.evaluate( | 208 | var xp = x.evaluate( |
207 | '/rsp/photosets/photoset', x, null, | 209 | '/rsp/photosets/photoset', x, null, |
208 | XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); | 210 | XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); |
209 | _this.importXPR(xp); | 211 | _this.importXPR(xp); |
210 | }, function(x,s,c,m) { | 212 | }, function(x,s,c,m) { |
211 | _this.fireflix.flickr_failure(x,s,c,m); | 213 | _this.fireflix.flickr_failure(x,s,c,m); |
212 | } | 214 | } |
213 | ); | 215 | ); |
214 | }, | 216 | }, |
215 | on_select: function() { | 217 | on_select: function() { |
216 | if(this.selection.count==1) { | 218 | if(this.selection.count==1) { |
217 | this.fireflix.cmd_set_props.setAttribute('disabled','false'); | 219 | this.fireflix.cmd_set_props.setAttribute('disabled','false'); |
218 | var s = this.sets[this.selection.currentIndex]; | 220 | var s = this.sets[this.selection.currentIndex]; |
219 | this.fireflix.photoset.load_photos(s.id); | 221 | this.fireflix.photoset.load_photos(s.id); |
220 | }else{ | 222 | }else{ |
221 | this.fireflix.cmd_set_props.setAttribute('disabled','true'); | 223 | this.fireflix.cmd_set_props.setAttribute('disabled','true'); |
222 | } | 224 | } |
223 | } | 225 | } |
224 | }, | 226 | }, |
225 | 227 | ||
226 | refresh_user_tags: function() { | 228 | refresh_user_tags: function() { |
227 | var lb = document.getElementById('tagslist'); | 229 | var lb = document.getElementById('tagslist'); |
228 | var _this = this; | 230 | var _this = this; |
229 | this.flickr.api_call( | 231 | this.flickr.api_call( |
230 | { | 232 | { |
231 | method: 'flickr.tags.getListUser', | 233 | method: 'flickr.tags.getListUser', |
232 | auth_token: 'default', | 234 | auth_token: 'default', |
233 | }, function(xr) { | 235 | }, function(xr) { |
234 | var x = xr.responseXML; | 236 | var x = xr.responseXML; |
235 | var xp = x.evaluate( | 237 | var xp = x.evaluate( |
236 | '/rsp/who/tags/tag', x, null, | 238 | '/rsp/who/tags/tag', x, null, |
237 | XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); | 239 | XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); |
238 | // TODO: clear list | 240 | // TODO: clear list |
239 | var n; while(n=xp.iterateNext()) { | 241 | var n; while(n=xp.iterateNext()) { |
240 | lb.appendItem(n.firstChild.nodeValue); | 242 | lb.appendItem(n.firstChild.nodeValue); |
241 | } | 243 | } |
242 | }, function(x,s,c,m) { | 244 | }, function(x,s,c,m) { |
243 | _this.flickr_failure(x,s,c,m); | 245 | _this.flickr_failure(x,s,c,m); |
244 | } | 246 | } |
245 | ); | 247 | ); |
246 | }, | 248 | }, |
247 | 249 | ||
248 | uploadObserver: { | 250 | uploadObserver: { |
249 | fireflix: null, | 251 | fireflix: null, |
250 | init: function(f) { | 252 | init: function(f) { |
251 | this.fireflix = f; | 253 | this.fireflix = f; |
252 | }, | 254 | }, |
253 | getSupportedFlavours: function() { | 255 | getSupportedFlavours: function() { |
254 | var rv = new FlavourSet(); | 256 | var rv = new FlavourSet(); |
255 | rv.appendFlavour('application/x-moz-file','nsIFile'); | 257 | rv.appendFlavour('application/x-moz-file','nsIFile'); |
256 | rv.appendFlavour('application/x-moz-url'); | 258 | rv.appendFlavour('application/x-moz-url'); |
257 | rv.appendFlavour('text/uri-list'); | 259 | rv.appendFlavour('text/uri-list'); |
258 | rv.appendFlavour('text/unicode'); | 260 | rv.appendFlavour('text/unicode'); |
259 | return rv; | 261 | return rv; |
260 | }, | 262 | }, |
261 | canHandleMultipleItems: true, | 263 | canHandleMultipleItems: true, |
262 | onDragOver: function(ev,fl,sess) { | 264 | onDragOver: function(ev,fl,sess) { |
263 | return true; | 265 | return true; |
264 | }, | 266 | }, |
265 | onDrop: function(ev,dd,s) { | 267 | onDrop: function(ev,dd,s) { |
266 | var ldf = null; | 268 | var ldf = null; |
267 | for(var i in dd.dataList) { | 269 | for(var i in dd.dataList) { |
268 | var di = dd.dataList[i]; | 270 | var di = dd.dataList[i]; |
269 | var dif = di.first; | 271 | var dif = di.first; |
270 | if( | 272 | if( |
271 | ldf==null | 273 | ldf==null |
272 | || ldf.flavour.contentType!=dif.flavour.contentType | 274 | || ldf.flavour.contentType!=dif.flavour.contentType |
273 | || ldf.contentLength!=dif.contentLength | 275 | || ldf.contentLength!=dif.contentLength |
274 | || ldf.data!=dif.data ) | 276 | || ldf.data!=dif.data ) |
275 | this.drop_item(ev,di,s); | 277 | this.drop_item(ev,di,s); |
276 | ldf = dif; | 278 | ldf = dif; |
277 | } | 279 | } |
278 | }, | 280 | }, |
279 | drop_item: function(ev,di,s) { | 281 | drop_item: function(ev,di,s) { |
280 | var d = di.first; | 282 | var d = di.first; |
281 | switch(d.flavour.contentType) { | 283 | switch(d.flavour.contentType) { |
282 | case 'text/unicode': | 284 | case 'text/unicode': |
283 | this.drop_urilist(ev,d.data,s); | 285 | this.drop_urilist(ev,d.data,s); |
284 | break; | 286 | break; |
285 | case 'application/x-moz-file': | 287 | case 'application/x-moz-file': |
286 | this.fireflix.uploads.add(d.data.path); | 288 | this.fireflix.uploads.add(d.data.path); |
287 | document.getElementById('fireflix_tabs').selectedTab | 289 | document.getElementById('fireflix_tabs').selectedTab |
288 | = document.getElementById('tab_upload'); | 290 | = document.getElementById('tab_upload'); |
289 | break; | 291 | break; |
290 | case 'text/uri-list': | 292 | case 'text/uri-list': |
291 | // is it ascii or could it be utf8? | 293 | // is it ascii or could it be utf8? |
292 | this.drop_urilist(ev,splitascii(d.data),s); | 294 | this.drop_urilist(ev,splitascii(d.data),s); |
293 | break; | 295 | break; |
294 | default: alert(d.flavour.contentType+':'+d.data); break; | 296 | default: alert(d.flavour.contentType+':'+d.data); break; |
295 | }; | 297 | }; |
296 | }, | 298 | }, |
297 | drop_urilist: function(ev,ul,s) { | 299 | drop_urilist: function(ev,ul,s) { |
298 | // TODO: check for being a file? | 300 | // TODO: check for being a file? |
299 | var us = decodeURIComponent(ul).split(/[\r\n]/); | 301 | var us = decodeURIComponent(ul).split(/[\r\n]/); |
300 | for(var ui in us) | 302 | for(var ui in us) |
301 | if(/\S/.test(us[ui])) | 303 | if(/\S/.test(us[ui])) |
302 | this.fireflix.uploads.add(us[ui]); | 304 | this.fireflix.uploads.add(us[ui]); |
303 | document.getElementById('fireflix_tabs').selectedTab | 305 | document.getElementById('fireflix_tabs').selectedTab |
304 | = document.getElementById('tab_upload'); | 306 | = document.getElementById('tab_upload'); |
305 | } | 307 | } |
306 | }, | 308 | }, |
307 | 309 | ||
308 | uploads: { | 310 | uploads: { |
309 | fireflix: null, | 311 | fireflix: null, |
310 | init: function(f) { | 312 | init: function(f) { |
311 | this.fireflix=f; | 313 | this.fireflix=f; |
312 | pull_elements(this,document,[ | 314 | pull_elements(this,document,[ |
313 | 'upload_filename','upload_title','upload_file_preview', | 315 | 'upload_filename','upload_title','upload_file_preview', |
314 | 'upload_file_props','upload_progress','upload_tags', | 316 | 'upload_file_props','upload_progress','upload_tags', |
315 | 'cmd_uploads_upload' | 317 | 'cmd_uploads_upload' |
316 | ]); | 318 | ]); |
317 | }, | 319 | }, |
318 | files: new Array(), | 320 | files: new Array(), |
319 | rowCount: 0, | 321 | rowCount: 0, |
320 | getCellText: function(r,c) { | 322 | getCellText: function(r,c) { |
321 | var f = this.files[r]; | 323 | var f = this.files[r]; |
322 | if(c.id=='up_file') return f.file; | 324 | if(c.id=='up_file') return f.file; |
323 | if(c.id=='up_title') return f.title; | 325 | if(c.id=='up_title') return f.title; |
324 | if(c.id=='up_status') return f.state; | 326 | if(c.id=='up_status') return f.state; |
325 | return c.id; | 327 | return c.id; |
326 | }, | 328 | }, |
327 | setTree: function(t) { this.tree = t }, | 329 | setTree: function(t) { this.tree = t }, |
328 | isContainer: function(r) { return false; }, | 330 | isContainer: function(r) { return false; }, |
329 | isSeparator: function(r) { return false; }, | 331 | isSeparator: function(r) { return false; }, |
330 | isSorted: function(r) { return false; }, | 332 | isSorted: function(r) { return false; }, |
331 | getLevel: function(r) { return 0; }, | 333 | getLevel: function(r) { return 0; }, |
332 | getImageSrc: function(r,c) { return null }, | 334 | getImageSrc: function(r,c) { return null }, |
333 | getRowProperties: function(r,p) { | 335 | getRowProperties: function(r,p) { |
334 | try { | 336 | try { |
335 | if(!Components) return; | 337 | if(!Components) return; |
336 | }catch(e) { return } | 338 | }catch(e) { return } |
337 | var f = this.files[r]; | 339 | var f = this.files[r]; |
338 | var as = Components.classes['@mozilla.org/atom-service;1']. | 340 | var as = Components.classes['@mozilla.org/atom-service;1']. |
339 | getService(Components.interfaces.nsIAtomService); | 341 | getService(Components.interfaces.nsIAtomService); |
340 | p.AppendElement(as.getAtom(f.state)); | 342 | p.AppendElement(as.getAtom(f.state)); |
341 | }, | 343 | }, |
342 | getCellProperties: function(r,c,p) { this.getRowProperties(r,p); }, | 344 | getCellProperties: function(r,c,p) { this.getRowProperties(r,p); }, |
343 | getColumnProperties: function(c,p) { }, | 345 | getColumnProperties: function(c,p) { }, |
344 | cycleHeader: function(cid,e) { }, | 346 | cycleHeader: function(cid,e) { }, |
345 | getParentIndex: function(r) { return -1; }, | 347 | getParentIndex: function(r) { return -1; }, |
346 | drop: function(r,o) { }, | 348 | drop: function(r,o) { }, |
347 | canDropBeforeAfter: function(r,b) { return false }, | 349 | canDropBeforeAfter: function(r,b) { return false }, |
348 | 350 | ||
349 | add: function(f) { | 351 | add: function(f) { |
350 | if(f.indexOf('file:/')==0) { | 352 | if(f.indexOf('file:/')==0) { |
351 | f = f.substr(5); | 353 | f = f.substr(5); |
352 | while(f.substr(0,2)=='//') { // XXX: not very performant, is it? ;-) | 354 | while(f.substr(0,2)=='//') { // XXX: not very performant, is it? ;-) |
353 | f = f.substr(1); | 355 | f = f.substr(1); |
354 | } | 356 | } |
355 | } | 357 | } |
356 | var t = f; | 358 | var t = f; |
357 | var ls = t.lastIndexOf('/'); | 359 | var ls = t.lastIndexOf('/'); |
358 | if(ls>0) t = t.substr(ls+1); | 360 | if(ls>0) t = t.substr(ls+1); |
359 | ls = t.lastIndexOf('\\'); | 361 | ls = t.lastIndexOf('\\'); |
360 | if(ls>0) t = t.substr(ls+1); | 362 | if(ls>0) t = t.substr(ls+1); |
361 | var ld = t.lastIndexOf('.'); | 363 | var ld = t.lastIndexOf('.'); |
362 | if(ld>0) t = t.substr(0,ld); | 364 | if(ld>0) t = t.substr(0,ld); |
363 | this.files.push( { | 365 | this.files.push( { |
364 | file: f, | 366 | file: f, |
365 | title: t, | 367 | title: t, |
366 | tags: '', | 368 | tags: '', |
367 | state: 'pending' | 369 | state: 'pending' |
368 | } ); | 370 | } ); |
369 | this.rowCount = this.files.length; | 371 | this.rowCount = this.files.length; |
370 | this.tree.rowCountChanged(this.rowCount-1,1); | 372 | this.tree.rowCountChanged(this.rowCount-1,1); |
371 | }, | 373 | }, |
372 | 374 | ||
373 | upload_worker: function() { | 375 | upload_worker: function() { |
374 | for(var f in this.files) { | 376 | for(var f in this.files) { |
375 | if(this.files[f].state=='pending') { | 377 | if(this.files[f].state=='pending') { |
376 | var ff = this.files[f]; | 378 | var ff = this.files[f]; |
377 | dump('upload '+ff.file+'\n'); | 379 | dump('upload '+ff.file+'\n'); |
378 | this.on_file_upload(ff); | 380 | this.on_file_upload(ff); |
379 | ff.state='uploading'; | 381 | ff.state='uploading'; |
380 | this.tree.invalidate(); | 382 | this.tree.invalidate(); |
381 | var _this = this; | 383 | var _this = this; |
382 | this.fireflix.flickr.upload_file( | 384 | this.fireflix.flickr.upload_file( |
383 | ff.file, { title: ff.title, tags: ff.tags }, | 385 | ff.file, { title: ff.title, tags: ff.tags }, |
384 | function(x,p) { | 386 | function(x,p) { |
385 | ff.photoid = p; | 387 | ff.photoid = p; |
386 | _this.batch_ids.push(p); | 388 | _this.batch_ids.push(p); |
387 | ff.state='completed'; | 389 | ff.state='completed'; |
388 | _this.tree.invalidate(); | 390 | _this.tree.invalidate(); |
389 | window.setTimeout(_this.upload_to,0,_this); | 391 | window.setTimeout(_this.upload_to,0,_this); |
390 | }, function(x,s,c,m) { | 392 | }, function(x,s,c,m) { |
391 | ff.state='failed'; | 393 | ff.state='failed'; |
392 | ff.flickr_errcode = c; | 394 | ff.flickr_errcode = c; |
393 | ff.flickr_errmsg = m; | 395 | ff.flickr_errmsg = m; |
394 | _this.tree.invalidate(); | 396 | _this.tree.invalidate(); |
395 | window.setTimeout(_this.upload_to,0,_this); | 397 | window.setTimeout(_this.upload_to,0,_this); |
396 | } | 398 | } |
397 | ); | 399 | ); |
398 | return; | 400 | return; |
399 | } | 401 | } |
400 | } | 402 | } |
401 | dump('uploading done\n'); | 403 | dump('uploading done\n'); |
402 | this.on_finish_upload(); | 404 | this.on_finish_upload(); |
403 | }, | 405 | }, |
404 | upload_to: function(_this) { _this.upload_worker(); }, | 406 | upload_to: function(_this) { _this.upload_worker(); }, |
405 | on_file_upload: function(f) { | 407 | on_file_upload: function(f) { |
406 | this.cmd_uploads_upload.setAttribute('disabled','true'); | 408 | this.cmd_uploads_upload.setAttribute('disabled','true'); |
407 | for(var fi in this.files) { | 409 | for(var fi in this.files) { |
408 | if(this.files[fi].file==f.file) { | 410 | if(this.files[fi].file==f.file) { |
409 | this.tree.ensureRowIsVisible(fi); | 411 | this.tree.ensureRowIsVisible(fi); |
410 | this.selection.rangedSelect(fi,fi,false); | 412 | this.selection.rangedSelect(fi,fi,false); |
411 | this.selection.currentIndex = fi; | 413 | this.selection.currentIndex = fi; |
412 | this.selToProps(); | 414 | this.selToProps(); |
413 | break; | 415 | break; |
414 | } | 416 | } |
415 | } | 417 | } |
416 | }, | 418 | }, |
417 | on_finish_upload: function() { | 419 | on_finish_upload: function() { |
418 | if(this.batch_ids.length) { | 420 | if(this.batch_ids.length) { |
419 | var psn = prompt(this.fireflix.loc_strings.getString('postUploadPhotoset')); | 421 | var psn = prompt(this.fireflix.loc_strings.getString('postUploadPhotoset')); |
420 | if(psn!=null) { | 422 | if(psn!=null) { |
421 | var pids = this.batch_ids.join(','); | 423 | var pids = this.batch_ids.join(','); |
422 | var ppid = this.batch_ids[0]; | 424 | var ppid = this.batch_ids[0]; |
423 | var _this = this; | 425 | var _this = this; |
424 | this.fireflix.flickr.api_call( | 426 | this.fireflix.flickr.api_call( |
425 | { | 427 | { |
426 | method: 'flickr.photosets.create', | 428 | method: 'flickr.photosets.create', |
427 | auth_token: 'default', | 429 | auth_token: 'default', |
428 | title: psn, | 430 | title: psn, |
429 | primary_photo_id: ppid | 431 | primary_photo_id: ppid |
430 | }, function(x) { | 432 | }, function(x) { |
431 | var npid = | 433 | var npid = |
432 | x.responseXML.getElementsByTagName('photoset').item(0).getAttribute('id'); | 434 | x.responseXML.getElementsByTagName('photoset').item(0).getAttribute('id'); |
433 | _this.fireflix.flickr.api_call( | 435 | _this.fireflix.flickr.api_call( |
434 | { | 436 | { |
435 | method: 'flickr.photosets.editPhotos', | 437 | method: 'flickr.photosets.editPhotos', |
436 | auth_token: 'default', | 438 | auth_token: 'default', |
437 | photoset_id: npid, | 439 | photoset_id: npid, |
438 | primary_photo_id: ppid, | 440 | primary_photo_id: ppid, |
439 | photo_ids: pids | 441 | photo_ids: pids |
440 | }, function(x) { | 442 | }, function(x) { |
441 | _this.fireflix.refresh_sets(); | 443 | _this.fireflix.refresh_sets(); |
442 | }, function(x,s,c,m) { | 444 | }, function(x,s,c,m) { |
443 | _this.fireflix.flickr_failure(x,s,c,m); | 445 | _this.fireflix.flickr_failure(x,s,c,m); |
444 | } | 446 | } |
445 | ); | 447 | ); |
446 | }, function(x,s,c,m) { | 448 | }, function(x,s,c,m) { |
447 | _this.fireflix.flickr_failure(x,s,c,m); | 449 | _this.fireflix.flickr_failure(x,s,c,m); |
448 | } | 450 | } |
449 | ); | 451 | ); |
450 | } | 452 | } |
451 | } | 453 | } |
452 | this.selection.clearSelection(); | 454 | this.selection.clearSelection(); |
453 | this.cmd_uploads_upload.setAttribute('disabled','false'); | 455 | this.cmd_uploads_upload.setAttribute('disabled','false'); |
454 | this.upload_progress.setAttribute('hidden','true'); | 456 | this.upload_progress.setAttribute('hidden','true'); |
455 | }, | 457 | }, |
456 | 458 | ||
457 | clear_list: function() { | 459 | clear_list: function() { |
458 | this.tree.beginUpdateBatch(); | 460 | this.tree.beginUpdateBatch(); |
459 | this.rowCount = 0; | 461 | this.rowCount = 0; |
460 | this.files = new Array(); | 462 | this.files = new Array(); |
461 | this.tree.endUpdateBatch(); | 463 | this.tree.endUpdateBatch(); |
462 | this.selToProps(); | 464 | this.selToProps(); |
463 | }, | 465 | }, |
464 | selectionChanged: function() { | 466 | selectionChanged: function() { |
465 | this.selToProps(); | 467 | this.selToProps(); |
466 | }, | 468 | }, |
467 | disableProps: function() { | 469 | disableProps: function() { |
468 | this.upload_filename.value=''; | 470 | this.upload_filename.value=''; |
469 | this.upload_filename.disabled = true; | 471 | this.upload_filename.disabled = true; |
470 | this.upload_title.value=''; | 472 | this.upload_title.value=''; |
471 | this.upload_title.disabled = true; | 473 | this.upload_title.disabled = true; |
472 | this.upload_file_preview.src = null; | 474 | this.upload_file_preview.src = null; |
473 | this.upload_file_props.hidden = true; | 475 | this.upload_file_props.hidden = true; |
474 | this.upload_tags.value=''; | 476 | this.upload_tags.value=''; |
475 | this.upload_tags.disabled = true; | 477 | this.upload_tags.disabled = true; |
476 | }, | 478 | }, |
477 | selToProps: function() { | 479 | selToProps: function() { |
478 | if(!this.selection.count) { | 480 | if(!this.selection.count) { |
479 | this.disableProps(); | 481 | this.disableProps(); |
480 | }else if(this.selection.count==1) { | 482 | }else if(this.selection.count==1) { |
481 | var f=this.files[this.selection.currentIndex]; | 483 | var f=this.files[this.selection.currentIndex]; |
482 | if(f==null || f.state!='pending') { | 484 | if(f==null || f.state!='pending') { |
483 | this.disableProps(); | 485 | this.disableProps(); |
484 | }else{ | 486 | }else{ |
485 | this.upload_filename.value = f.file; | 487 | this.upload_filename.value = f.file; |
486 | this.upload_filename.disabled = false; | 488 | this.upload_filename.disabled = false; |
487 | this.upload_title.value = f.title; | 489 | this.upload_title.value = f.title; |
488 | this.upload_title.disabled = false; | 490 | this.upload_title.disabled = false; |
489 | this.upload_file_preview.src = 'file:///'+f.file; | 491 | this.upload_file_preview.src = 'file:///'+f.file; |
490 | this.upload_file_props.hidden = false; | 492 | this.upload_file_props.hidden = false; |
491 | this.upload_tags.value = f.tags; | 493 | this.upload_tags.value = f.tags; |
492 | this.upload_tags.disabled = false; | 494 | this.upload_tags.disabled = false; |
493 | } | 495 | } |
494 | }else{ | 496 | }else{ |
495 | var ftitle = null; var onetitle = true; | 497 | var ftitle = null; var onetitle = true; |
496 | var ftags = null; var onetag = true; | 498 | var ftags = null; var onetag = true; |
497 | var fs = 0; | 499 | var fs = 0; |
498 | for(var ff in this.files) { | 500 | for(var ff in this.files) { |
499 | if(this.selection.isSelected(ff) && this.files[ff].state=='pending' ) { | 501 | if(this.selection.isSelected(ff) && this.files[ff].state=='pending' ) { |
500 | ++fs; | 502 | ++fs; |
501 | if(ftitle==null) { | 503 | if(ftitle==null) { |
502 | ftitle = this.files[ff].title; | 504 | ftitle = this.files[ff].title; |
503 | }else if(ftitle!=this.files[ff].title) { | 505 | }else if(ftitle!=this.files[ff].title) { |
504 | onetitle = false; | 506 | onetitle = false; |
505 | } | 507 | } |
506 | if(ftags==null) { | 508 | if(ftags==null) { |
507 | ftags = this.files[ff].tags; | 509 | ftags = this.files[ff].tags; |
508 | }else if(ftags!=this.files[ff].tags) { | 510 | }else if(ftags!=this.files[ff].tags) { |
509 | onetag = false; | 511 | onetag = false; |
510 | } | 512 | } |
511 | } | 513 | } |
512 | } | 514 | } |
513 | if(fs) { | 515 | if(fs) { |
514 | this.upload_filename.value=''; | 516 | this.upload_filename.value=''; |
515 | this.upload_filename.disabled = true; | 517 | this.upload_filename.disabled = true; |
516 | if(onetitle) | 518 | if(onetitle) |
517 | this.upload_title.value = ftitle; | 519 | this.upload_title.value = ftitle; |
518 | this.upload_title.disabled = false; | 520 | this.upload_title.disabled = false; |
519 | if(onetag) | 521 | if(onetag) |
520 | this.upload_tags.value = ftags; | 522 | this.upload_tags.value = ftags; |
521 | this.upload_tags.disabled = false; | 523 | this.upload_tags.disabled = false; |
522 | this.upload_file_preview.src = null; | 524 | this.upload_file_preview.src = null; |
523 | this.upload_file_props.hidden = false; | 525 | this.upload_file_props.hidden = false; |
524 | }else | 526 | }else |
525 | this.disableProps(); | 527 | this.disableProps(); |
526 | } | 528 | } |
527 | }, | 529 | }, |
528 | propsToSel: function(prop) { | 530 | propsToSel: function(prop) { |
529 | if(this.selection.count<=0) return; | 531 | if(this.selection.count<=0) return; |
530 | for(var ff in this.files) { | 532 | for(var ff in this.files) { |
531 | if(this.selection.isSelected(ff) && this.files[ff].state=='pending') { | 533 | if(this.selection.isSelected(ff) && this.files[ff].state=='pending') { |
532 | if(prop=='filename') | 534 | if(prop=='filename') |
533 | this.files[ff].file = this.upload_filename.value; | 535 | this.files[ff].file = this.upload_filename.value; |
534 | if(prop=='title') | 536 | if(prop=='title') |
535 | this.files[ff].title = this.upload_title.value; | 537 | this.files[ff].title = this.upload_title.value; |
536 | if(prop=='tags') | 538 | if(prop=='tags') |
537 | this.files[ff].tags = this.upload_tags.value; | 539 | this.files[ff].tags = this.upload_tags.value; |
538 | this.tree.invalidateRow(ff); | 540 | this.tree.invalidateRow(ff); |
539 | } | 541 | } |
540 | } | 542 | } |
541 | }, | 543 | }, |
542 | 544 | ||
543 | on_upload: function() { | 545 | on_upload: function() { |
544 | this.selToProps(); | 546 | this.selToProps(); |
545 | this.batch_ids = new Array(); | 547 | this.batch_ids = new Array(); |
546 | this.upload_progress.value=0; | 548 | this.upload_progress.value=0; |
547 | this.upload_progress.setAttribute('hidden','false'); | 549 | this.upload_progress.setAttribute('hidden','false'); |
548 | this.upload_worker(); | 550 | this.upload_worker(); |
549 | }, | 551 | }, |
550 | on_clear: function() { | 552 | on_clear: function() { |
551 | this.clear_list(); | 553 | this.clear_list(); |
552 | }, | 554 | }, |
553 | on_remove: function() { | 555 | on_remove: function() { |
554 | if(this.selection.count) { | 556 | if(this.selection.count) { |
555 | this.tree.beginUpdateBatch(); | 557 | this.tree.beginUpdateBatch(); |
556 | for(var i=this.files.length-1;i>=0;--i) { | 558 | for(var i=this.files.length-1;i>=0;--i) { |
557 | if(this.selection.isSelected(i)) { | 559 | if(this.selection.isSelected(i)) { |
558 | this.files.splice(i,1); | 560 | this.files.splice(i,1); |
559 | this.rowCount--; | 561 | this.rowCount--; |
560 | } | 562 | } |
561 | } | 563 | } |
562 | this.tree.endUpdateBatch(); | 564 | this.tree.endUpdateBatch(); |
563 | this.selection.clearSelection(); | 565 | this.selection.clearSelection(); |
564 | } | 566 | } |
565 | }, | 567 | }, |
566 | on_add: function() { | 568 | on_add: function() { |
567 | var ifp = Components.interfaces.nsIFilePicker; | 569 | var ifp = Components.interfaces.nsIFilePicker; |
568 | var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(ifp); | 570 | var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(ifp); |
569 | fp.init(window, "Select a File", ifp.modeOpenMultiple); | 571 | fp.init(window, "Select a File", ifp.modeOpenMultiple); |
570 | fp.appendFilters(ifp.filterImages); | 572 | fp.appendFilters(ifp.filterImages); |
571 | var rv = fp.show(); | 573 | var rv = fp.show(); |
572 | if(rv==ifp.returnOK) { | 574 | if(rv==ifp.returnOK) { |
573 | var ff = fp.files; | 575 | var ff = fp.files; |
574 | while(ff.hasMoreElements()) { | 576 | while(ff.hasMoreElements()) { |
575 | var f = ff.getNext(); | 577 | var f = ff.getNext(); |
576 | f.QueryInterface(Components.interfaces.nsIFile); | 578 | f.QueryInterface(Components.interfaces.nsIFile); |
577 | this.add(f.path); | 579 | this.add(f.path); |
578 | } | 580 | } |
579 | } | 581 | } |
580 | } | 582 | } |
581 | }, | 583 | }, |
582 | 584 | ||
583 | on_set_props: function() { | 585 | on_set_props: function() { |
584 | var pset = this.photosets.sets[this.photosets.selection.currentIndex]; | 586 | var pset = this.photosets.sets[this.photosets.selection.currentIndex]; |
585 | window.openDialog( | 587 | window.openDialog( |
586 | "chrome://fireflix/content/photoset-props.xul", | 588 | "chrome://fireflix/content/photoset-props.xul", |
587 | null, "dependent,modal,dialog,chrome", this, | 589 | null, "dependent,modal,dialog,chrome", this, |
588 | pset ); | 590 | pset ); |
589 | if(pset.dirty) { | 591 | if(pset.dirty) { |
590 | var _this = this; | 592 | var _this = this; |
591 | this.flickr.api_call( | 593 | this.flickr.api_call( |
592 | { | 594 | { |
593 | method: 'flickr.photosets.editMeta', | 595 | method: 'flickr.photosets.editMeta', |
594 | auth_token: 'default', | 596 | auth_token: 'default', |
595 | photoset_id: pset.id, | 597 | photoset_id: pset.id, |
596 | title: pset.title, | 598 | title: pset.title, |
597 | description: pset.description | 599 | description: pset.description |
598 | }, function(xr) { | 600 | }, function(xr) { |
599 | pset.dirty = false; | 601 | pset.dirty = false; |
600 | _this.flickr.api_call( | 602 | _this.flickr.api_call( |
601 | { | 603 | { |
602 | method: 'flickr.photosets.getPhotos', | 604 | method: 'flickr.photosets.getPhotos', |
603 | auth_token: 'default', | 605 | auth_token: 'default', |
604 | photoset_id: pset.id | 606 | photoset_id: pset.id |
605 | }, function(xr) { | 607 | }, function(xr) { |
606 | var x = xr.responseXML; | 608 | var x = xr.responseXML; |
607 | var xp = x.evaluate( | 609 | var xp = x.evaluate( |
608 | '/rsp/photoset/photo', x, null, | 610 | '/rsp/photoset/photo', x, null, |
609 | XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); | 611 | XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); |
610 | var phids = new Array(); | 612 | var phids = new Array(); |
611 | var priph = null; | 613 | var priph = null; |
612 | var n; while(n=xp.iterateNext()) { | 614 | var n; while(n=xp.iterateNext()) { |
613 | var pid = n.getAttribute('id'); | 615 | var pid = n.getAttribute('id'); |
614 | phids.push( pid ); | 616 | phids.push( pid ); |
615 | if(pid==pset.primary && n.getAttribute('isprimary')!='1') | 617 | if(pid==pset.primary && n.getAttribute('isprimary')!='1') |
616 | priph = pid; | 618 | priph = pid; |
617 | } | 619 | } |
618 | if(priph) { | 620 | if(priph) { |
619 | _this.flickr.api_call( | 621 | _this.flickr.api_call( |
620 | { | 622 | { |
621 | method: 'flickr.photosets.editPhotos', | 623 | method: 'flickr.photosets.editPhotos', |
622 | auth_token: 'default', | 624 | auth_token: 'default', |
623 | photoset_id: pset.id, | 625 | photoset_id: pset.id, |
624 | primary_photo_id: priph, | 626 | primary_photo_id: priph, |
625 | photo_ids: phids.join(',') | 627 | photo_ids: phids.join(',') |
626 | }, function() { }, function(x,s,c,m) { /* flickr.photosets.editPhotos */ | 628 | }, function() { }, function(x,s,c,m) { /* flickr.photosets.editPhotos */ |
627 | _this.flickr_failure(x,s,c,m); | 629 | _this.flickr_failure(x,s,c,m); |
628 | } | 630 | } |
629 | ); | 631 | ); |
630 | } | 632 | } |
631 | }, function(x,s,c,m) { /* flickr.photosets.getPhotos */ | 633 | }, function(x,s,c,m) { /* flickr.photosets.getPhotos */ |
632 | _this.flickr_failure(x,s,c,m); | 634 | _this.flickr_failure(x,s,c,m); |
633 | } | 635 | } |
634 | ); | 636 | ); |
635 | }, function(x,s,c,m) { /* flickr.photosets.editMeta */ | 637 | }, function(x,s,c,m) { /* flickr.photosets.editMeta */ |
636 | _this.flickr_failure(x,s,c,m); | 638 | _this.flickr_failure(x,s,c,m); |
637 | } | 639 | } |
638 | ); | 640 | ); |
639 | } | 641 | } |
640 | }, | 642 | }, |
641 | on_refresh_sets: function() { | 643 | on_refresh_sets: function() { |
642 | this.refresh_sets(); | 644 | this.refresh_sets(); |
643 | }, | 645 | }, |
644 | on_cmd_sets_html: function(csfx,ev) { | 646 | on_cmd_sets_html: function(csfx,ev) { |
645 | var uti = csfx.charAt(0); var utl = csfx.charAt(1); | 647 | var uti = csfx.charAt(0); var utl = csfx.charAt(1); |
646 | var rv = this.build_html(this.photoset.photos,uti,utl); | 648 | var rv = this.build_html(this.photoset.photos,uti,utl); |
647 | this.popup_content(rv); | 649 | this.popup_content(rv); |
648 | }, | 650 | }, |
649 | 651 | ||
650 | on_cmd_uploads_html: function(csfx,ev) { | 652 | on_cmd_uploads_html: function(csfx,ev) { |
651 | var uti = csfx.charAt(0); var utl = csfx.charAt(1); | 653 | var uti = csfx.charAt(0); var utl = csfx.charAt(1); |
652 | var pids = new Array(); | 654 | var pids = new Array(); |
653 | for(var f in this.uploads.files) { | 655 | for(var f in this.uploads.files) { |
654 | if(this.uploads.selection.isSelected(f)) | 656 | if(this.uploads.selection.isSelected(f)) |
655 | if(this.uploads.files[f].photoid) | 657 | if(this.uploads.files[f].photoid) |
656 | pids.push(this.uploads.files[f].photoid); | 658 | pids.push(this.uploads.files[f].photoid); |
657 | } | 659 | } |
658 | var pp = this.uploads.rowCount*2; if(pp>500) pp = 500; | 660 | var pp = this.uploads.rowCount*2; if(pp>500) pp = 500; |
659 | var _this = this; | 661 | var _this = this; |
660 | this.flickr.api_call( | 662 | this.flickr.api_call( |
661 | { | 663 | { |
662 | method: 'flickr.photos.search', | 664 | method: 'flickr.photos.search', |
663 | auth_token: 'default', | 665 | auth_token: 'default', |
664 | extras: 'original_format', | 666 | extras: 'original_format', |
665 | user_id: 'me', | 667 | user_id: 'me', |
666 | per_page: pp | 668 | per_page: pp |
667 | }, | 669 | }, |
668 | function(xr) { | 670 | function(xr) { |
669 | var x = xr.responseXML; | 671 | var x = xr.responseXML; |
670 | var rv = ''; | 672 | var rv = ''; |
671 | for(var pn in pids) { | 673 | for(var pn in pids) { |
672 | var p = pids[pn]; | 674 | var p = pids[pn]; |
673 | var pp = new Photo(xp_node('/rsp/photos/photo[@id='+p+']',x)); | 675 | var pp = new Photo(xp_node('/rsp/photos/photo[@id='+p+']',x)); |
674 | rv += _this.photo_html(pp,uti,utl)+'\n'; | 676 | rv += _this.photo_html(pp,uti,utl)+'\n'; |
675 | } | 677 | } |
676 | _this.popup_content(rv); | 678 | _this.popup_content(rv); |
677 | }, function(x,s,c,m) { | 679 | }, function(x,s,c,m) { |
678 | _this.flickr_failure(x,s,c,m); | 680 | _this.flickr_failure(x,s,c,m); |
679 | } | 681 | } |
680 | ); | 682 | ); |
681 | }, | 683 | }, |
682 | 684 | ||
683 | /* | 685 | /* |
684 | * | 686 | * |
685 | */ | 687 | */ |
686 | foundphotos: { | 688 | foundphotos: { |
687 | fireflix: null, | 689 | fireflix: null, |
688 | init: function(f) { | 690 | init: function(f) { |
689 | this.fireflix = f; | 691 | this.fireflix = f; |
690 | pull_elements(this,document,[ | 692 | pull_elements(this,document,[ |
691 | 'search_for','search_tags','search_mine', | 693 | 'search_for','search_tags','search_mine', |
692 | 'searchresult_props','search_photo', | 694 | 'searchresult_props','search_photo', |
693 | 'searchresult_title','searchresult_description' | 695 | 'searchresult_title','searchresult_description' |
694 | ]); | 696 | ]); |
695 | document.getElementById('searchresults').view = this; | 697 | document.getElementById('searchresults').view = this; |
696 | }, | 698 | }, |
697 | photos: new Array(), | 699 | photos: new Array(), |
698 | rowCount: 0, | 700 | rowCount: 0, |
699 | getCellText: function(r,c) { | 701 | getCellText: function(r,c) { |
700 | var p = this.photos[r]; | 702 | var p = this.photos[r]; |
701 | if(c.id=='sr_title') return p.title; | 703 | if(c.id=='sr_title') return p.title; |
702 | return c.id; | 704 | return c.id; |
703 | }, | 705 | }, |
704 | setTree: function(t) { this.tree = t }, | 706 | setTree: function(t) { this.tree = t }, |
705 | isContainer: function(r) { return false }, | 707 | isContainer: function(r) { return false }, |
706 | isSeparator: function(r) { return false }, | 708 | isSeparator: function(r) { return false }, |
707 | isSorted: function(r) { return false }, | 709 | isSorted: function(r) { return false }, |
708 | getLevel: function(r) { return 0 }, | 710 | getLevel: function(r) { return 0 }, |
709 | getImageSrc: function(r,c) { return null }, | 711 | getImageSrc: function(r,c) { return null }, |
710 | getRowProperties: function(r,p) { }, | 712 | getRowProperties: function(r,p) { }, |
711 | getCellProperties: function(cid,cel,p) { }, | 713 | getCellProperties: function(cid,cel,p) { }, |
712 | getColumnProperties: function(cid,cel,p) { }, | 714 | getColumnProperties: function(cid,cel,p) { }, |
713 | cycleHeader: function(cid,e) { }, | 715 | cycleHeader: function(cid,e) { }, |
714 | getParentIndex: function(r) { return -1 }, | 716 | getParentIndex: function(r) { return -1 }, |
715 | drop: function(r,o) { }, | 717 | drop: function(r,o) { }, |
716 | canDropBeforeAfter: function(r,b) { return false }, | 718 | canDropBeforeAfter: function(r,b) { return false }, |
717 | 719 | ||
718 | importXPR: function(xp) { | 720 | importXPR: function(xp) { |
719 | this.selection.clearSelection(); | 721 | this.selection.clearSelection(); |
720 | this.selection.currentIndex = -1; | 722 | this.selection.currentIndex = -1; |
721 | this.searchresult_props.hidden = true; | 723 | this.searchresult_props.hidden = true; |
722 | this.tree.beginUpdateBatch(); | 724 | this.tree.beginUpdateBatch(); |
723 | this.photos = new Array(); | 725 | this.photos = new Array(); |
724 | var n; while(n=xp.iterateNext()) { | 726 | var n; while(n=xp.iterateNext()) { |
725 | this.photos.push(new Photo(n)); | 727 | this.photos.push(new Photo(n)); |
726 | } | 728 | } |
727 | this.rowCount = this.photos.length; | 729 | this.rowCount = this.photos.length; |
728 | this.tree.endUpdateBatch(); | 730 | this.tree.endUpdateBatch(); |
729 | }, | 731 | }, |
730 | search_photos: function() { | 732 | search_photos: function() { |
731 | var pars = { | 733 | var pars = { |
732 | method: 'flickr.photos.search', | 734 | method: 'flickr.photos.search', |
733 | auth_token: 'default', | 735 | auth_token: 'default', |
734 | extras: 'license,date_upload,date_taken,owner_name,icon_server,original_format,last_update,geo' | 736 | extras: 'license,date_upload,date_taken,owner_name,icon_server,original_format,last_update,geo' |
735 | }; | 737 | }; |
736 | if(this.search_mine.checked) | 738 | if(this.search_mine.checked) |
737 | pars.user_id='me'; | 739 | pars.user_id='me'; |
738 | if(this.search_tags.checked) { | 740 | if(this.search_tags.checked) { |
739 | pars.tags=this.search_for.value.split(/ +/).join(','); | 741 | pars.tags=this.search_for.value.split(/ +/).join(','); |
740 | }else{ | 742 | }else{ |
741 | pars.text=this.search_for.value; | 743 | pars.text=this.search_for.value; |
742 | } | 744 | } |
743 | var _this = this; | 745 | var _this = this; |
744 | this.fireflix.flickr.api_call( pars, | 746 | this.fireflix.flickr.api_call( pars, |
745 | function(xr) { | 747 | function(xr) { |
746 | var x = xr.responseXML; | 748 | var x = xr.responseXML; |
747 | var xp = x.evaluate( | 749 | var xp = x.evaluate( |
748 | '/rsp/photos/photo', x, null, | 750 | '/rsp/photos/photo', x, null, |
749 | XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); | 751 | XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); |
750 | _this.importXPR(xp); | 752 | _this.importXPR(xp); |
751 | _this.tree.ensureRowIsVisible(0); | 753 | _this.tree.ensureRowIsVisible(0); |
752 | _this.on_select(); | 754 | _this.on_select(); |
753 | }, function(x,s,c,m) { | 755 | }, function(x,s,c,m) { |
754 | _this.fireflix.flickr_failure(x,s,c,m); | 756 | _this.fireflix.flickr_failure(x,s,c,m); |
755 | } | 757 | } |
756 | ); | 758 | ); |
757 | }, | 759 | }, |
758 | render_description_frame: function(content) { | 760 | render_description_frame: function(content) { |
759 | if(!content) { | 761 | if(!content) { |
760 | this.searchresult_description.innerHTML = ''; | 762 | this.searchresult_description.innerHTML = ''; |
761 | }else{ | 763 | }else{ |
762 | this.searchresult_description.innerHTML = content?content:''; | 764 | this.searchresult_description.innerHTML = content?content:''; |
763 | /* of all linking elements flickr only allows a */ | 765 | /* of all linking elements flickr only allows a */ |
764 | var as = this.searchresult_description.getElementsByTagName('a'); | 766 | var as = this.searchresult_description.getElementsByTagName('a'); |
765 | for(var a=0;a<as.length;++a) | 767 | for(var a=0;a<as.length;++a) |
766 | as.item(a).setAttribute('target','_blank'); | 768 | as.item(a).setAttribute('target','_blank'); |
767 | } | 769 | } |
768 | }, | 770 | }, |
769 | on_select: function() { | 771 | on_select: function() { |
770 | if(this.selection.currentIndex<0) { | 772 | if(this.selection.currentIndex<0) { |
771 | this.searchresult_props.hidden = true; | 773 | this.searchresult_props.hidden = true; |
772 | }else{ | 774 | }else{ |
773 | var p = this.photos[this.selection.currentIndex]; | 775 | var p = this.photos[this.selection.currentIndex]; |
774 | if(!p) { | 776 | if(!p) { |
775 | this.searchresult_props.hidden = true; | 777 | this.searchresult_props.hidden = true; |
776 | }else{ | 778 | }else{ |
777 | this.search_photo.src = this.fireflix.flickr.make_photo_url(p,'t'); | 779 | this.search_photo.src = this.fireflix.flickr.make_photo_url(p,'t'); |
778 | this.searchresult_title.value = p.title; | 780 | this.searchresult_title.value = p.title; |
779 | this.render_description_frame(null); | 781 | this.render_description_frame(null); |
780 | if(p.description==null && p.description==undefined) { | 782 | if(p.description==null && p.description==undefined) { |
781 | var pid = p.id; | 783 | var pid = p.id; |
782 | var ci = this.selection.currentIndex; | 784 | var ci = this.selection.currentIndex; |
783 | var _this = this; | 785 | var _this = this; |
784 | this.fireflix.flickr.api_call( | 786 | this.fireflix.flickr.api_call( |
785 | { | 787 | { |
786 | method: 'flickr.photos.getInfo', | 788 | method: 'flickr.photos.getInfo', |
787 | auth_token: 'default', | 789 | auth_token: 'default', |
788 | photo_id: p.id, | 790 | photo_id: p.id, |
789 | secret: p.secret | 791 | secret: p.secret |
790 | }, function(xr) { | 792 | }, function(xr) { |
791 | var pp = _this.photos[ci]; | 793 | var pp = _this.photos[ci]; |
792 | if(ci==_this.selection.currentIndex && pp.id==pid) { | 794 | if(ci==_this.selection.currentIndex && pp.id==pid) { |
793 | var n = xp_node('/rsp/photo',xr.responseXML); | 795 | var n = xp_node('/rsp/photo',xr.responseXML); |
794 | pp.fromNode_(n); | 796 | pp.fromNode_(n); |
795 | _this.render_description_frame(pp.description); | 797 | _this.render_description_frame(pp.description); |
796 | } | 798 | } |
797 | }, function(x,s,c,m) { | 799 | }, function(x,s,c,m) { |
798 | _this.fireflix.flickr_failure(x,s,c,m); | 800 | _this.fireflix.flickr_failure(x,s,c,m); |
799 | } | 801 | } |
800 | ); | 802 | ); |
801 | this.searchresult_props.hidden = false; | 803 | this.searchresult_props.hidden = false; |
802 | }else{ | 804 | }else{ |
803 | this.render_description_frame(p.description); | 805 | this.render_description_frame(p.description); |
804 | } | 806 | } |
805 | } | 807 | } |
806 | } | 808 | } |
807 | }, | 809 | }, |
808 | on_cmd_open: function(ev) { | 810 | on_cmd_open: function(ev) { |
809 | if(this.selection.currentIndex<0) | 811 | if(this.selection.currentIndex<0) |
810 | return; | 812 | return; |
811 | var p = this.photos[this.selection.currentIndex]; | 813 | var p = this.photos[this.selection.currentIndex]; |
812 | if(!p.id) | 814 | if(!p.id) |
813 | return; | 815 | return; |
814 | this.fireflix.openTab(this.fireflix.flickr.make_photo_url(p,'p')); | 816 | this.fireflix.openTab(this.fireflix.flickr.make_photo_url(p,'p')); |
815 | } | 817 | } |
816 | }, | 818 | }, |
817 | 819 | ||
818 | photo_html: function(p,i,l) { | 820 | photo_html: function(p,i,l) { |
819 | // TODO: add alt/title when possible | 821 | // TODO: add alt/title when possible |
820 | var rv = | 822 | var rv = |
821 | '<a href="'+this.flickr.make_photo_url(p,l)+'">' + | 823 | '<a href="'+this.flickr.make_photo_url(p,l)+'">' + |
822 | '<img src="'+this.flickr.make_photo_url(p,i)+'" />'+ | 824 | '<img src="'+this.flickr.make_photo_url(p,i)+'" />'+ |
823 | '</a>'; | 825 | '</a>'; |
824 | return rv; | 826 | return rv; |
825 | }, | 827 | }, |
826 | build_html: function(photos,uti,utl) { | 828 | build_html: function(photos,uti,utl) { |
827 | var rv = ''; | 829 | var rv = ''; |
828 | for(var i in photos) { | 830 | for(var i in photos) { |
829 | var p = photos[i]; | 831 | var p = photos[i]; |
830 | rv += this.photo_html(p,utl,uti)+'\n'; | 832 | rv += this.photo_html(p,utl,uti)+'\n'; |
831 | } | 833 | } |
832 | return rv; | 834 | return rv; |
833 | }, | 835 | }, |
834 | 836 | ||
835 | popup_content: function(s) { | 837 | popup_content: function(s) { |
836 | window.openDialog( | 838 | window.openDialog( |
837 | "chrome://fireflix/content/generated-content.xul", | 839 | "chrome://fireflix/content/generated-content.xul", |
838 | null, "dialog,chrome", this, s ); | 840 | null, "dialog,chrome", this, s ); |
839 | }, | 841 | }, |
840 | copy_to_clipboard: function(s) { | 842 | copy_to_clipboard: function(s) { |
841 | var ch = Components.classes["@mozilla.org/widget/clipboardhelper;1"] | 843 | var ch = Components.classes["@mozilla.org/widget/clipboardhelper;1"] |
842 | .getService(Components.interfaces.nsIClipboardHelper); | 844 | .getService(Components.interfaces.nsIClipboardHelper); |
843 | ch.copyString(s); | 845 | ch.copyString(s); |
844 | }, | 846 | }, |
845 | openTab: function(l) { | 847 | openTab: function(l) { |
846 | var wm = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService( | 848 | var wm = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService( |
847 | Components.interfaces.nsIWindowMediator ); | 849 | Components.interfaces.nsIWindowMediator ); |
848 | var bw = wm.getMostRecentWindow('navigator:browser'); | 850 | var bw = wm.getMostRecentWindow('navigator:browser'); |
849 | var b = bw.getBrowser(); | 851 | var b = bw.getBrowser(); |
850 | var t = b.addTab(l); | 852 | var t = b.addTab(l); |
851 | b.selectedTab = t; | 853 | b.selectedTab = t; |
852 | }, | 854 | }, |
853 | 855 | ||
854 | build_menus: function() { | 856 | build_menus: function() { |
855 | this.append_html_menu( | 857 | this.append_html_menu( |
856 | document.getElementById('sets_html_menu'), | 858 | document.getElementById('sets_html_menu'), |
857 | 'stm_','m_bop','cmdset_sets','cmd_sets_html' | 859 | 'stm_','m_bop','cmdset_sets','cmd_sets_html' |
858 | ); | 860 | ); |
859 | this.append_html_menu( | 861 | this.append_html_menu( |
860 | document.getElementById('uploads_html_menu'), | 862 | document.getElementById('uploads_html_menu'), |
861 | 'stm_','m_bop','cmdset_uploads','cmd_uploads_html' | 863 | 'stm_','m_bop','cmdset_uploads','cmd_uploads_html' |
862 | ); | 864 | ); |
863 | return; | 865 | return; |
864 | }, | 866 | }, |
865 | append_html_menu: function(m,imgt,lnkt,csid,cpfx) { | 867 | append_html_menu: function(m,imgt,lnkt,csid,cpfx) { |
866 | var mp = m.appendChild(document.createElement('menupopup')); | 868 | var mp = m.appendChild(document.createElement('menupopup')); |
867 | var t; | 869 | var t; |
868 | t=mp.appendChild(document.createElement('menuitem')); | 870 | t=mp.appendChild(document.createElement('menuitem')); |
869 | t.setAttribute('label',this.loc_strings.getString('menutitle_Images')); | 871 | t.setAttribute('label',this.loc_strings.getString('menutitle_Images')); |
870 | t.setAttribute('class','menuhead');t.setAttribute('disabled','true'); | 872 | t.setAttribute('class','menuhead');t.setAttribute('disabled','true'); |
871 | mp.appendChild(document.createElement('menuseparator')); | 873 | mp.appendChild(document.createElement('menuseparator')); |
872 | var cs = document.getElementById(csid); | 874 | var cs = document.getElementById(csid); |
873 | for(var iti=0;iti<imgt.length;++iti) { | 875 | for(var iti=0;iti<imgt.length;++iti) { |
874 | t = mp.appendChild(document.createElement('menu')); | 876 | t = mp.appendChild(document.createElement('menu')); |
875 | t.setAttribute('label',this.loc_strings.getString('urltype_'+imgt.charAt(iti))); | 877 | t.setAttribute('label',this.loc_strings.getString('urltype_'+imgt.charAt(iti))); |
876 | var smp = t.appendChild(document.createElement('menupopup')); | 878 | var smp = t.appendChild(document.createElement('menupopup')); |
877 | t=smp.appendChild(document.createElement('menuitem')); | 879 | t=smp.appendChild(document.createElement('menuitem')); |
878 | t.setAttribute('label',this.loc_strings.getString('menutitle_Links')); | 880 | t.setAttribute('label',this.loc_strings.getString('menutitle_Links')); |
879 | t.setAttribute('class','menuhead');t.setAttribute('disabled','true'); | 881 | t.setAttribute('class','menuhead');t.setAttribute('disabled','true'); |
880 | smp.appendChild(document.createElement('menuseparator')); | 882 | smp.appendChild(document.createElement('menuseparator')); |
881 | for(var lti=0;lti<lnkt.length;++lti) { | 883 | for(var lti=0;lti<lnkt.length;++lti) { |
882 | var csfx = imgt.charAt(iti)+lnkt.charAt(lti); | 884 | var csfx = imgt.charAt(iti)+lnkt.charAt(lti); |
883 | t=smp.appendChild(document.createElement('menuitem')); | 885 | t=smp.appendChild(document.createElement('menuitem')); |
884 | t.setAttribute('label',this.loc_strings.getString('urltype_'+lnkt.charAt(lti))); | 886 | t.setAttribute('label',this.loc_strings.getString('urltype_'+lnkt.charAt(lti))); |
885 | t.setAttribute('command',cpfx+'_'+csfx); | 887 | t.setAttribute('command',cpfx+'_'+csfx); |
886 | t=cs.appendChild(document.createElement('command')); | 888 | t=cs.appendChild(document.createElement('command')); |
887 | t.setAttribute('id',cpfx+'_'+csfx); | 889 | t.setAttribute('id',cpfx+'_'+csfx); |
888 | t.setAttribute('oncommand','fireflix.on_'+cpfx+"('"+csfx+"',event)"); | 890 | t.setAttribute('oncommand','fireflix.on_'+cpfx+"('"+csfx+"',event)"); |
889 | } | 891 | } |
890 | } | 892 | } |
891 | return mp; | 893 | return mp; |
892 | }, | 894 | }, |
893 | 895 | ||
894 | flickr_failure: function(x,s,c,m) { | 896 | flickr_failure: function(x,s,c,m) { |
895 | if(c==98) { // Invalid auth token | 897 | if(c==98) { // Invalid auth token |
896 | this.flickr.reset_token(); | 898 | this.flickr.reset_token(); |
897 | this.set_auth_state(false,false); | 899 | this.set_auth_state(false,false); |
898 | return; | 900 | return; |
899 | } | 901 | } |
900 | // TODO: is that beauty? | 902 | // TODO: is that beauty? |
901 | alert('flickr api call failed\n'+c+' '+m); | 903 | alert('flickr api call failed\n'+c+' '+m); |
902 | } | 904 | } |
903 | 905 | ||
904 | }; | 906 | }; |
diff --git a/content/flickr.js b/content/flickr.js index add628a..b5bfa43 100644 --- a/content/flickr.js +++ b/content/flickr.js | |||
@@ -1,353 +1,347 @@ | |||
1 | /* | 1 | /* |
2 | * Photoset | 2 | * Photoset |
3 | */ | 3 | */ |
4 | 4 | ||
5 | function Photoset(s) { | 5 | function Photoset(s) { |
6 | if(s instanceof Photoset) { | 6 | if(s instanceof Photoset) { |
7 | for(var p in s) this[p]=s[p]; | 7 | for(var p in s) this[p]=s[p]; |
8 | }else | 8 | }else |
9 | this.fromNode(s); | 9 | this.fromNode(s); |
10 | } | 10 | } |
11 | Photoset.prototype = { | 11 | Photoset.prototype = { |
12 | id: null, | 12 | id: null, |
13 | primary: null, | 13 | primary: null, |
14 | secret: null, | 14 | secret: null, |
15 | server: null, | 15 | server: null, |
16 | photos: null, | 16 | photos: null, |
17 | title: null, | 17 | title: null, |
18 | description: null, | 18 | description: null, |
19 | fromNode: function(n) { | 19 | fromNode: function(n) { |
20 | this.id = n.getAttribute('id'); | 20 | this.id = n.getAttribute('id'); |
21 | this.primary = n.getAttribute('primary'); | 21 | this.primary = n.getAttribute('primary'); |
22 | this.secret = n.getAttribute('secret'); | 22 | this.secret = n.getAttribute('secret'); |
23 | this.server = n.getAttribute('server'); | 23 | this.server = n.getAttribute('server'); |
24 | this.photos = n.getAttribute('photos'); | 24 | this.photos = n.getAttribute('photos'); |
25 | this.title = n.getElementsByTagName('title').item(0).firstChild.nodeValue; | 25 | this.title = n.getElementsByTagName('title').item(0).firstChild.nodeValue; |
26 | this.description = n.getElementsByTagName('description').item(0).firstChild; | 26 | this.description = n.getElementsByTagName('description').item(0).firstChild; |
27 | if(this.description) this.description = this.description.nodeValue; | 27 | if(this.description) this.description = this.description.nodeValue; |
28 | } | 28 | } |
29 | }; | 29 | }; |
30 | 30 | ||
31 | /* | 31 | /* |
32 | * Photo | 32 | * Photo |
33 | */ | 33 | */ |
34 | function Photo(s) { | 34 | function Photo(s) { |
35 | if(s instanceof Photo) { | 35 | if(s instanceof Photo) { |
36 | for(var p in s) this[p]=s[p]; | 36 | for(var p in s) this[p]=s[p]; |
37 | }else | 37 | }else |
38 | this.fromNode(s); | 38 | this.fromNode(s); |
39 | } | 39 | } |
40 | Photo.prototype = { | 40 | Photo.prototype = { |
41 | id: null, secret: null, | 41 | id: null, secret: null, |
42 | server: null, | 42 | server: null, |
43 | title: null, | 43 | title: null, |
44 | isprimary: null, | 44 | isprimary: null, |
45 | license: null, | 45 | license: null, |
46 | dateupload: null, datetaken: null, datetakengranularity: null, | 46 | dateupload: null, datetaken: null, datetakengranularity: null, |
47 | ownername: null, | 47 | ownername: null, |
48 | iconserver: null, | 48 | iconserver: null, |
49 | originalformat: null, | 49 | originalformat: null, |
50 | lastupdate: null, | 50 | lastupdate: null, |
51 | fromNode: function(n) { | 51 | fromNode: function(n) { |
52 | this.id = n.getAttribute('id'); this.secret = n.getAttribute('secret'); | 52 | this.id = n.getAttribute('id'); this.secret = n.getAttribute('secret'); |
53 | this.server = n.getAttribute('server'); | 53 | this.server = n.getAttribute('server'); |
54 | this.title = n.getAttribute('title'); | 54 | this.title = n.getAttribute('title'); |
55 | this.isprimary = n.getAttribute('isprimary'); | 55 | this.isprimary = n.getAttribute('isprimary'); |
56 | this.license = n.getAttribute('license'); | 56 | this.license = n.getAttribute('license'); |
57 | this.dateupload = n.getAttribute('dateupload'); | 57 | this.dateupload = n.getAttribute('dateupload'); |
58 | this.datetaken = n.getAttribute('datetaken'); this.datetakengranularity = n.getAttribute('datetakengranularity'); | 58 | this.datetaken = n.getAttribute('datetaken'); this.datetakengranularity = n.getAttribute('datetakengranularity'); |
59 | this.ownername = n.getAttribute('ownername'); | 59 | this.ownername = n.getAttribute('ownername'); |
60 | this.iconserver = n.getAttribute('iconserver'); | 60 | this.iconserver = n.getAttribute('iconserver'); |
61 | this.originalformat = n.getAttribute('originalformat'); | 61 | this.originalformat = n.getAttribute('originalformat'); |
62 | this.lastupdate = n.getAttribute('lastupdate'); | 62 | this.lastupdate = n.getAttribute('lastupdate'); |
63 | }, | 63 | }, |
64 | fromNode_: function(n) { | 64 | fromNode_: function(n) { |
65 | var t; | 65 | var t; |
66 | // TODO: @rotation @isfavorite | 66 | // TODO: @rotation @isfavorite |
67 | this.owner = {}; | 67 | this.owner = {}; |
68 | t = n.getElementsByTagName('owner').item(0); | 68 | t = n.getElementsByTagName('owner').item(0); |
69 | if(t) { | 69 | if(t) { |
70 | this.owner.nsid=t.getAttribute('nsid'); | 70 | this.owner.nsid=t.getAttribute('nsid'); |
71 | this.owner.username=t.getAttribute('username'); | 71 | this.owner.username=t.getAttribute('username'); |
72 | this.owner.realname=t.getAttribute('realname'); | 72 | this.owner.realname=t.getAttribute('realname'); |
73 | this.owner.location=t.getAttribute.location; | 73 | this.owner.location=t.getAttribute.location; |
74 | } | 74 | } |
75 | t = n.getElementsByTagName('description').item(0); | 75 | t = n.getElementsByTagName('description').item(0); |
76 | if(t && t.firstChild) { | 76 | if(t && t.firstChild) { |
77 | this.description = t.firstChild.nodeValue; | 77 | this.description = t.firstChild.nodeValue; |
78 | } | 78 | } |
79 | // TODO: visibility/@ispublic visibility/@isfriend visibility/@isfamily | 79 | // TODO: visibility/@ispublic visibility/@isfriend visibility/@isfamily |
80 | // TODO: dates/@posted dates/@taken dates/@takengranularity dates/@lastupdate | 80 | // TODO: dates/@posted dates/@taken dates/@takengranularity dates/@lastupdate |
81 | // TODO: permissions/@permcomment permsiions/@permaddmeta | 81 | // TODO: permissions/@permcomment permsiions/@permaddmeta |
82 | // TODO: editability/@canaddcomment editability/@canaddmeta | 82 | // TODO: editability/@canaddcomment editability/@canaddmeta |
83 | // TODO: comments | 83 | // TODO: comments |
84 | // TODO: notes/note/@id notes/note/@author notes/note/@authorname | 84 | // TODO: notes/note/@id notes/note/@author notes/note/@authorname |
85 | // TODO: notes/note/@x notes/note/@y notes/note/@w notes/note/@h | 85 | // TODO: notes/note/@x notes/note/@y notes/note/@w notes/note/@h |
86 | // TODO: notes/note | 86 | // TODO: notes/note |
87 | // TODO: tags/tag/@id tags/tag/@author tags/tag/@raw tags/tag | 87 | // TODO: tags/tag/@id tags/tag/@author tags/tag/@raw tags/tag |
88 | // TODO: urls/url/@type urls/url | 88 | // TODO: urls/url/@type urls/url |
89 | } | 89 | } |
90 | }; | 90 | }; |
91 | 91 | ||
92 | function Flickr() { } | 92 | function Flickr() { } |
93 | Flickr.prototype = { | 93 | Flickr.prototype = { |
94 | 94 | ||
95 | rest_url: 'http://www.flickr.com/services/rest/', | 95 | rest_url: 'http://www.flickr.com/services/rest/', |
96 | auth_url: 'http://flickr.com/services/auth/', | 96 | auth_url: 'http://flickr.com/services/auth/', |
97 | photo_url: 'http://static.flickr.com/', | 97 | photo_url: 'http://static.flickr.com/', |
98 | photos_url: 'http://www.flickr.com/photos/', | 98 | photos_url: 'http://www.flickr.com/photos/', |
99 | upload_url: 'http://www.flickr.com/services/upload/', | 99 | upload_url: 'http://www.flickr.com/services/upload/', |
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); |
211 | else | 205 | else |
212 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.username'); | 206 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.username'); |
213 | if(this.user && this.user.fullname!=null && this.user.fullname!=undefined) | 207 | if(this.user && this.user.fullname!=null && this.user.fullname!=undefined) |
214 | this.prefs.setCharPref(this.prefs_root+'.auth_user.fullname',this.user.fullname); | 208 | this.prefs.setCharPref(this.prefs_root+'.auth_user.fullname',this.user.fullname); |
215 | else | 209 | else |
216 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname'); | 210 | this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname'); |
217 | }, | 211 | }, |
218 | _reset_token: function() { | 212 | _reset_token: function() { |
219 | this.token = null; this.perms = null; this.user = null; | 213 | this.token = null; this.perms = null; this.user = null; |
220 | return false; | 214 | return false; |
221 | }, | 215 | }, |
222 | load_token: function() { | 216 | load_token: function() { |
223 | try { | 217 | try { |
224 | if(this.prefs.getPrefType(this.prefs_root+'.auth_token')!=this.prefs.PREF_STRING) | 218 | if(this.prefs.getPrefType(this.prefs_root+'.auth_token')!=this.prefs.PREF_STRING) |
225 | return this._reset_token(); | 219 | return this._reset_token(); |
226 | this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token'); | 220 | this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token'); |
227 | if(this.prefs.getPrefType(this.prefs_root+'.auth_perms')!=this.prefs.PREF_STRING) | 221 | if(this.prefs.getPrefType(this.prefs_root+'.auth_perms')!=this.prefs.PREF_STRING) |
228 | return this._reset_token(); | 222 | return this._reset_token(); |
229 | this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms'); | 223 | this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms'); |
230 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.nsid')!=this.prefs.PREF_STRING) | 224 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.nsid')!=this.prefs.PREF_STRING) |
231 | return this._reset_token(); | 225 | return this._reset_token(); |
232 | this.user = new Object(); | 226 | this.user = new Object(); |
233 | this.user.nsid = this.prefs.getCharPref(this.prefs_root+'.auth_user.nsid'); | 227 | this.user.nsid = this.prefs.getCharPref(this.prefs_root+'.auth_user.nsid'); |
234 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.username')!=this.prefs.PREF_STRING) | 228 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.username')!=this.prefs.PREF_STRING) |
235 | return this._reset_token(); | 229 | return this._reset_token(); |
236 | this.user.username = this.prefs.getCharPref(this.prefs_root+'.auth_user.username'); | 230 | this.user.username = this.prefs.getCharPref(this.prefs_root+'.auth_user.username'); |
237 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.fullname')!=this.prefs.PREF_STRING) | 231 | if(this.prefs.getPrefType(this.prefs_root+'.auth_user.fullname')!=this.prefs.PREF_STRING) |
238 | return this._reset_token(); | 232 | return this._reset_token(); |
239 | this.user.fullname = this.prefs.getCharPref(this.prefs_root+'.auth_user.fullname'); | 233 | this.user.fullname = this.prefs.getCharPref(this.prefs_root+'.auth_user.fullname'); |
240 | }catch(e) { return this._reset_token(); } | 234 | }catch(e) { return this._reset_token(); } |
241 | return true; | 235 | return true; |
242 | }, | 236 | }, |
243 | reset_token: function() { | 237 | reset_token: function() { |
244 | this._reset_token(); | 238 | this._reset_token(); |
245 | this.save_token(); | 239 | this.save_token(); |
246 | }, | 240 | }, |
247 | 241 | ||
248 | get_photo_url: function(ser,id,sec,sfx,ext) { | 242 | get_photo_url: function(ser,id,sec,sfx,ext) { |
249 | var rv = this.photo_url + ser + '/' + id + '_' + sec; | 243 | var rv = this.photo_url + ser + '/' + id + '_' + sec; |
250 | if(sfx && sfx!='_') rv += '_'+sfx; | 244 | if(sfx && sfx!='_') rv += '_'+sfx; |
251 | rv += ext?'.'+ext:'.jpg'; | 245 | rv += ext?'.'+ext:'.jpg'; |
252 | return rv; | 246 | return rv; |
253 | }, | 247 | }, |
254 | get_image_url: function(o,sfx) { | 248 | get_image_url: function(o,sfx) { |
255 | return this.get_photo_url( | 249 | return this.get_photo_url( |
256 | o.server, | 250 | o.server, |
257 | (o instanceof Photoset)? o.primary : o.id, | 251 | (o instanceof Photoset)? o.primary : o.id, |
258 | o.secret, | 252 | o.secret, |
259 | sfx, | 253 | sfx, |
260 | (sfx=='o')?o.originalformat:null | 254 | (sfx=='o')?o.originalformat:null |
261 | ); | 255 | ); |
262 | }, | 256 | }, |
263 | get_photo_page_url: function(p) { | 257 | get_photo_page_url: function(p) { |
264 | if(p instanceof Photo) // TODO: half wrong, what if no owner? | 258 | if(p instanceof Photo) // TODO: half wrong, what if no owner? |
265 | return this.photos_url + (p.owner.nsid?p.owner.nsid:this.user.nsid) + '/' + p.id; | 259 | return this.photos_url + (p.owner.nsid?p.owner.nsid:this.user.nsid) + '/' + p.id; |
266 | else // TODO: take owner into account? | 260 | else // TODO: take owner into account? |
267 | return this.photos_url + this.user.nsid + '/' + p; | 261 | return this.photos_url + this.user.nsid + '/' + p; |
268 | }, | 262 | }, |
269 | make_photo_url: function(p,sfx) { | 263 | make_photo_url: function(p,sfx) { |
270 | if(sfx=='p') | 264 | if(sfx=='p') |
271 | return this.get_photo_page_url(p); | 265 | return this.get_photo_page_url(p); |
272 | else | 266 | else |
273 | return this.get_image_url(p,sfx); | 267 | return this.get_image_url(p,sfx); |
274 | }, | 268 | }, |
275 | 269 | ||
276 | upload_file: function(f,fa,on_success,on_failure) { | 270 | upload_file: function(f,fa,on_success,on_failure) { |
277 | try { | 271 | try { |
278 | var fi = Components.classes["@mozilla.org/file/local;1"] | 272 | var fi = Components.classes["@mozilla.org/file/local;1"] |
279 | .createInstance(Components.interfaces.nsILocalFile); | 273 | .createInstance(Components.interfaces.nsILocalFile); |
280 | fi.initWithPath( f ); | 274 | fi.initWithPath( f ); |
281 | var st = Components.classes["@mozilla.org/network/file-input-stream;1"] | 275 | var st = Components.classes["@mozilla.org/network/file-input-stream;1"] |
282 | .createInstance(Components.interfaces.nsIFileInputStream); | 276 | .createInstance(Components.interfaces.nsIFileInputStream); |
283 | st.init(fi,0x01,00004,null); | 277 | st.init(fi,0x01,00004,null); |
284 | var bis = Components.classes["@mozilla.org/binaryinputstream;1"] | 278 | var bis = Components.classes["@mozilla.org/binaryinputstream;1"] |
285 | .createInstance(Components.interfaces.nsIBinaryInputStream); | 279 | .createInstance(Components.interfaces.nsIBinaryInputStream); |
286 | bis.setInputStream(st); | 280 | bis.setInputStream(st); |
287 | 281 | ||
288 | // allocate and initialize temp storage string | 282 | // allocate and initialize temp storage string |
289 | var pbs = Components.classes["@mozilla.org/storagestream;1"] | 283 | var pbs = Components.classes["@mozilla.org/storagestream;1"] |
290 | .createInstance(Components.interfaces.nsIStorageStream); | 284 | .createInstance(Components.interfaces.nsIStorageStream); |
291 | pbs.init(1024,10000000,null); | 285 | pbs.init(1024,10000000,null); |
292 | // create output stream | 286 | // create output stream |
293 | var pbos = pbs.getOutputStream(0); | 287 | var pbos = pbs.getOutputStream(0); |
294 | // and a binaryoutputstream interface | 288 | // and a binaryoutputstream interface |
295 | var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"] | 289 | var pbbos = Components.classes["@mozilla.org/binaryoutputstream;1"] |
296 | .createInstance(Components.interfaces.nsIBinaryOutputStream); | 290 | .createInstance(Components.interfaces.nsIBinaryOutputStream); |
297 | pbbos.setOutputStream(pbos); | 291 | pbbos.setOutputStream(pbos); |
298 | 292 | ||
299 | /* create POST body */ | 293 | /* create POST body */ |
300 | var boundarytoken = 'kadaroloongazaduviaxamma'; | 294 | var boundarytoken = 'kadaroloongazaduviaxamma'; |
301 | var boundary = '--'+boundarytoken; | 295 | var boundary = '--'+boundarytoken; |
302 | var b = ''; | 296 | var b = ''; |
303 | 297 | ||
304 | var parms = { api_key: this.api_key, auth_token: this.token }; | 298 | var parms = { api_key: this.api_key, auth_token: this.token }; |
305 | for(var p in fa) parms[p] = fa[p]; | 299 | for(var p in fa) parms[p] = fa[p]; |
306 | var pns = new Array(); | 300 | var pns = new Array(); |
307 | for(var p in parms) pns.push(p); | 301 | for(var p in parms) pns.push(p); |
308 | var pstr = ''; | 302 | var pstr = ''; |
309 | for(var p in pns.sort()) { | 303 | for(var p in pns.sort()) { |
310 | var pn = pns[p]; | 304 | var pn = pns[p]; |
311 | pstr += pn+parms[pn]; | 305 | pstr += pn+parms[pn]; |
312 | b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n'; | 306 | b += boundary+'\nContent-Disposition: form-data; name="'+pn+'"\n\n'+toutf8(parms[pn])+'\n'; |
313 | } | 307 | } |
314 | b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n'; | 308 | b += boundary+'\nContent-Disposition: form-data; name="api_sig"\n\n'+this.api_sig(pstr)+'\n'; |
315 | b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n'; | 309 | b += boundary+'\nContent-Disposition: form-data; name="photo"; filename="'+f+'"\nContent-Type: image/jpeg\nContent-Transfer-Encoding: binary\n\n'; |
316 | pbbos.writeBytes(b,b.length); | 310 | pbbos.writeBytes(b,b.length); |
317 | var bisbytes = bis.available(); | 311 | var bisbytes = bis.available(); |
318 | pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes); | 312 | pbbos.writeBytes(bis.readBytes(bisbytes),bisbytes); |
319 | pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close(); | 313 | pbbos.writeBytes('\n'+boundary+'--',3+boundary.length); bis.close(); st.close(); |
320 | 314 | ||
321 | pbbos.close(); pbos.close(); | 315 | pbbos.close(); pbos.close(); |
322 | 316 | ||
323 | var x = new XMLHttpRequest(); | 317 | var x = new XMLHttpRequest(); |
324 | x.open("POST",this.upload_url); | 318 | x.open("POST",this.upload_url); |
325 | x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"'); | 319 | x.setRequestHeader('Content-Type', 'multipart/form-data; boundary="'+boundarytoken+'"'); |
326 | x.setRequestHeader('Connection','close'); | 320 | x.setRequestHeader('Connection','close'); |
327 | x.setRequestHeader('Content-Length',b.length); | 321 | x.setRequestHeader('Content-Length',b.length); |
328 | x.onreadystatechange=function() { | 322 | x.onreadystatechange=function() { |
329 | if(x.readyState!=4) return false; | 323 | if(x.readyState!=4) return false; |
330 | if(x.status==200) { | 324 | if(x.status==200) { |
331 | var stat = x.responseXML.firstChild.getAttribute('stat'); | 325 | var stat = x.responseXML.firstChild.getAttribute('stat'); |
332 | if(stat=='ok') { | 326 | if(stat=='ok') { |
333 | var pid = xp_str('/rsp/photoid',x.responseXML); | 327 | var pid = xp_str('/rsp/photoid',x.responseXML); |
334 | if(on_success) on_success(x,pid); | 328 | if(on_success) on_success(x,pid); |
335 | }else{ | 329 | }else{ |
336 | var e = x.responseXML.getElementsByTagName('err').item(0); | 330 | var e = x.responseXML.getElementsByTagName('err').item(0); |
337 | var ecode = e.getAttribute('code'); | 331 | var ecode = e.getAttribute('code'); |
338 | var emsg = e.getAttribute('msg'); | 332 | var emsg = e.getAttribute('msg'); |
339 | dump('upload failed: '+ecode+' '+emsg+'\n'); | 333 | dump('upload failed: '+ecode+' '+emsg+'\n'); |
340 | if(on_failure) on_failure(x,stat,ecode,emsg); | 334 | if(on_failure) on_failure(x,stat,ecode,emsg); |
341 | } | 335 | } |
342 | }else{ | 336 | }else{ |
343 | if(on_failure) on_failure(x); | 337 | if(on_failure) on_failure(x); |
344 | } | 338 | } |
345 | return true; | 339 | return true; |
346 | }; | 340 | }; |
347 | x.send(pbs.newInputStream(0)); | 341 | x.send(pbs.newInputStream(0)); |
348 | }catch(e) { | 342 | }catch(e) { |
349 | if(on_failure) on_failure(e,null,-1,e.message); | 343 | if(on_failure) on_failure(e,null,-1,e.message); |
350 | } | 344 | } |
351 | } | 345 | } |
352 | 346 | ||
353 | }; | 347 | }; |