-rw-r--r-- | content/fireflix.js | 96 | ||||
-rw-r--r-- | content/flickr.js | 65 |
2 files changed, 69 insertions, 92 deletions
diff --git a/content/fireflix.js b/content/fireflix.js index 225e21c..283e930 100644 --- a/content/fireflix.js +++ b/content/fireflix.js @@ -1,417 +1,405 @@ function splitascii(s) { var rv=''; for(var i=0;i<s.length;++i) { var w = s.charCodeAt(i); rv += String.fromCharCode( w&0xff, (w>>8)&0xff ); } return rv; } var fireflix = { flickr: new Flickr(), init: function() { pull_elements(this,document,[ 'cmd_auth_auth','cmd_auth_done','cmd_auth_unauth', 'menu_auth_done','b_auth','b_auth_done','auth_info', 'loc_strings','cmd_set_props' ]); this.build_menus(); this.foundphotos.init(this); this.photosets.init(this); this.photoset.init(this); this.uploads.init(this); this.uploadObserver.init(this); this.flickr.api_key = '9c43cd66947a57e6f29db1a9da3f72e3'; this.flickr.api_shs = '9c33c9e2f0f0cfd5'; this.flickr.prefs_root = 'net.klever.kin.fireflix'; this.flickr.load_token(); this.no_auth_info_label = this.auth_info.value; this.set_auth_state(this.flickr.token,false); if(this.flickr.token) { this.refresh_stuff(); }else{ this.on_cmd_auth(); } }, set_auth_state: function(au,inp) { /* authorized, in progress */ this.cmd_auth_unauth.disabled = !au; this.b_auth.hidden = au || inp; this.b_auth_done.hidden = !inp; this.menu_auth_done.hidden = !inp; this.cmd_auth_done.setAttribute('disabled',!inp); this.auth_info.disabled = !au; if(au) { this.auth_info.value = this.flickr.user.fullname+' ['+this.flickr.user.username+']'; /* TODO: move to locale */ }else{ this.auth_info.value = this.no_auth_info_label; } }, on_cmd_auth: function() { var _this = this; this.flickr.authorize_0( 'delete', function(x,f,u) { _this.openTab(u); _this.set_auth_state(_this.flickr.token,true); }, function(x,s,c,m) { _this.flickr_failure(x,s,c,m); } ); }, on_cmd_auth_done: function() { this.set_auth_state(this.flickr.token,false); var _this = this; this.flickr.authorize_1( function() { _this.flickr.save_token(); _this.refresh_stuff(); _this.set_auth_state(_this.flickr.token,false); _this.auth_info.value = _this.flickr.user.fullname+' ['+_this.flickr.user.username+']'; }, function(x,s,c,m) { _this.set_auth_state(_this.flickr.token,false); /* XXX: no reset token? */ _this.flickr_failure(x,s,c,m); } ); }, on_cmd_auth_unauth: function() { this.flickr.reset_token(); this.set_auth_state(false,false); }, refresh_sets: function() { this.photosets.refresh_sets(); }, refresh_stuff: function() { this.refresh_sets(); this.refresh_user_tags(); }, /* photoset treeview */ photoset: { photos: new Array(), fireflix: null, init: function(f) { this.fireflix = f; pull_elements(this,document,[ 'set_photo', 'set_photo_props' ]); document.getElementById('setphotos').view = this; }, rowCount: 0, getCellText: function(r,c) { var p = this.photos[r]; if(c.id=='sp_title') return p.title; if(c.id=='sp_taken') return p.datetaken; if(c.id=='sp_upload') { var du = new Date(p.dateupload*1000); var rv = du.getFullYear()+'-'+(du.getMonth()+1)+'-'+du.getDate() +' '+ du.getHours()+':'+du.getMinutes()+':'+du.getSeconds(); return rv.replace(/(\D)(\d)(\D)/,'$10$2$3'); } return c.id; }, setTree: function(t) { this.tree = t }, isContainer: function(r) { return false; }, isSeparator: function(r) { return false; }, isSorted: function(r) { return false; }, getLevel: function(r) { return 0; }, getImageSrc: function(r,c) { return null }, getRowProperties: function(r,p) {}, getCellProperties: function(cid,cel,p) {}, getColumnProperties: function(cid,cel,p) { }, cycleHeader: function(cid,e) { }, getParentIndex: function(r) { return -1; }, drop: function(r,o) { }, canDropBeforeAfter: function(r,b) { return false }, - importXPR: function(xp) { + import_json: function(jp) { this.tree.beginUpdateBatch(); this.photos = new Array(); - var n; while(n=xp.iterateNext()) { - this.photos.push(new Photo(n)); - } - this.rowCount = this.photos.length; + for(var i in jp) this.photos.push(new Photo(jp[i])); + this.rowCount =this.photos.length; this.tree.endUpdateBatch(); this.selection.clearSelection(); }, load_photos: function(psid) { - var _this = this; - this.fireflix.flickr.api_call( + var that = this; + this.fireflix.flickr.api_call_json( { method: 'flickr.photosets.getPhotos', auth_token: 'default', photoset_id: psid, extras: 'license,date_upload,date_taken,owner_name,icon_server,original_format,last_update' - }, function(xr) { - var x = xr.responseXML; - var xp = x.evaluate( - '/rsp/photoset/photo', x, null, - XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); - _this.importXPR(xp); + }, function(x,j) { + that.import_json(j.photoset.photo); }, function(x,s,c,m) { - _this.fireflix.flickr_failure(x,s,c,m); + that.fireflix.flickr_failure(x,s,c,m); } ); }, on_select: function() { if(this.selection.count==1) { var p = this.photos[this.selection.currentIndex]; this.set_photo.src = this.fireflix.flickr.get_photo_url(p.server,p.id,p.secret,'t'); this.set_photo_props.hidden = false; }else{ this.set_photo_props.hidden = true; } }, on_cmd_open: function(ev) { if(this.selection.currentIndex<0) return; var p = this.photos[this.selection.currentIndex]; if(!p.id) return; this.fireflix.openTab(this.fireflix.flickr.make_photo_url(p,'p')); } }, /* photosets treeview */ photosets: { sets: new Array(), fireflix: null, init: function(f) { this.fireflix = f; document.getElementById('setslist').view = this; }, rowCount: 0, getCellText: function(r,c) { var s = this.sets[r]; if(c.id=='sl_name') return s.title; if(c.id=='sl_photos') return s.photos; return c.id; }, setTree: function(t) { this.tree = t }, isContainer: function(r) { return false; }, isSeparator: function(r) { return false; }, isSorted: function() { return false; }, getLevel: function(r) { return 0; }, getImageSrc: function(r,c) { return null }, getRowProperties: function(r,p) {}, getCellProperties: function(cid,cel,p) { }, getColumnProperties: function(cid,cel,p) { }, cycleHeader: function(cid,e) { }, getParentIndex: function(r) { return -1; }, drop: function(r,o) { }, canDropBeforeAfter: function(r,b) { return false }, - importXPR: function(xp) { + import_json: function(j) { this.tree.beginUpdateBatch(); this.sets = new Array(); - var n; while(n=xp.iterateNext()) { - this.sets.push(new Photoset(n)); - } + for(var i in j) this.sets.push(new Photoset(j[i])); this.rowCount = this.sets.length; this.tree.endUpdateBatch(); }, refresh_sets: function() { - var _this = this; - this.fireflix.flickr.api_call( + var that = this; + this.fireflix.flickr.api_call_json( { method: 'flickr.photosets.getList', auth_token: 'default' - }, function(xr) { - var x = xr.responseXML; - var xp = x.evaluate( - '/rsp/photosets/photoset', x, null, - XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); - _this.importXPR(xp); + }, function(x,j) { + that.import_json(j.photosets.photoset); }, function(x,s,c,m) { - _this.fireflix.flickr_failure(x,s,c,m); + that.fireflix.flickr_failure(x,s,c,m); } ); }, on_select: function() { if(this.selection.count==1) { this.fireflix.cmd_set_props.setAttribute('disabled','false'); var s = this.sets[this.selection.currentIndex]; this.fireflix.photoset.load_photos(s.id); }else{ this.fireflix.cmd_set_props.setAttribute('disabled','true'); } }, on_cmd_open_in_flickr: function(ev) { if(this.selection.currentIndex<0) return; var p = this.sets[this.selection.currentIndex]; if(!p.id) return; this.fireflix.openTab(this.fireflix.flickr.make_photoset_url(p)); } }, refresh_user_tags: function() { var lb = document.getElementById('tagslist'); var _this = this; this.flickr.api_call( { method: 'flickr.tags.getListUser', auth_token: 'default', }, function(xr) { var x = xr.responseXML; var xp = x.evaluate( '/rsp/who/tags/tag', x, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); // TODO: clear list var n; while(n=xp.iterateNext()) { lb.appendItem(n.firstChild.nodeValue); } }, function(x,s,c,m) { _this.flickr_failure(x,s,c,m); } ); }, uploadObserver: { fireflix: null, init: function(f) { this.fireflix = f; }, getSupportedFlavours: function() { var rv = new FlavourSet(); rv.appendFlavour('application/x-moz-file','nsIFile'); rv.appendFlavour('application/x-moz-url'); rv.appendFlavour('text/uri-list'); rv.appendFlavour('text/unicode'); return rv; }, canHandleMultipleItems: true, onDragOver: function(ev,fl,sess) { return true; }, onDrop: function(ev,dd,s) { var ldf = null; for(var i in dd.dataList) { var di = dd.dataList[i]; var dif = di.first; if( ldf==null || ldf.flavour.contentType!=dif.flavour.contentType || ldf.contentLength!=dif.contentLength || ldf.data!=dif.data ) this.drop_item(ev,di,s); ldf = dif; } }, drop_item: function(ev,di,s) { var d = di.first; switch(d.flavour.contentType) { case 'text/unicode': this.drop_urilist(ev,d.data,s); break; case 'application/x-moz-file': this.fireflix.uploads.add(d.data.path); document.getElementById('fireflix_tabs').selectedTab = document.getElementById('tab_upload'); break; case 'text/uri-list': // is it ascii or could it be utf8? this.drop_urilist(ev,splitascii(d.data),s); break; default: alert(d.flavour.contentType+':'+d.data); break; }; }, drop_urilist: function(ev,ul,s) { // TODO: check for being a file? var us = decodeURIComponent(ul).split(/[\r\n]/); for(var ui in us) if(/\S/.test(us[ui])) this.fireflix.uploads.add(us[ui]); document.getElementById('fireflix_tabs').selectedTab = document.getElementById('tab_upload'); } }, uploads: { fireflix: null, init: function(f) { this.fireflix=f; pull_elements(this,document,[ 'upload_filename','upload_title','upload_file_preview', 'upload_file_props','upload_progress','upload_tags', 'cmd_uploads_upload', 'upload_failure', 'upload_is_public', 'upload_is_friends', 'upload_is_family' ]); document.getElementById('uploadlist').view = this; this.upload_is_public.addEventListener( 'CheckboxStateChange', { that: this, handleEvent: function(ev) { this.that.propsToSel('is_public'); } }, false ); this.upload_is_friends.addEventListener( 'CheckboxStateChange', { that: this, handleEvent: function(ev) { this.that.propsToSel('is_friends'); } }, false ); this.upload_is_family.addEventListener( 'CheckboxStateChange', { that: this, handleEvent: function(ev) { this.that.propsToSel('is_family'); } }, false ); }, files: new Array(), rowCount: 0, getCellText: function(r,c) { var f = this.files[r]; if(c.id=='up_file') return f.file; if(c.id=='up_title') return f.title; if(c.id=='up_status') return f.state; return c.id; }, setTree: function(t) { this.tree = t }, isContainer: function(r) { return false; }, isSeparator: function(r) { return false; }, isSorted: function(r) { return false; }, getLevel: function(r) { return 0; }, getImageSrc: function(r,c) { return null }, getRowProperties: function(r,p) { try { if(!Components) return; }catch(e) { return } var f = this.files[r]; var as = Components.classes['@mozilla.org/atom-service;1']. getService(Components.interfaces.nsIAtomService); p.AppendElement(as.getAtom(f.state)); }, getCellProperties: function(r,c,p) { this.getRowProperties(r,p); }, getColumnProperties: function(c,p) { }, cycleHeader: function(cid,e) { }, getParentIndex: function(r) { return -1; }, drop: function(r,o) { }, canDropBeforeAfter: function(r,b) { return false }, add: function(f) { if(f.indexOf('file:/')==0) { f = f.substr(5); while(f.substr(0,2)=='//') { // XXX: not very performant, is it? ;-) f = f.substr(1); } } var t = f; var ls = t.lastIndexOf('/'); if(ls>0) t = t.substr(ls+1); ls = t.lastIndexOf('\\'); if(ls>0) t = t.substr(ls+1); var ld = t.lastIndexOf('.'); if(ld>0) t = t.substr(0,ld); this.files.push( { file: f, title: t, tags: '', is_public: true, is_friend: false, is_family: false, state: 'pending' } ); this.rowCount = this.files.length; this.tree.rowCountChanged(this.rowCount-1,1); }, upload_worker: function() { for(var f in this.files) { if(this.files[f].state=='pending') { var ff = this.files[f]; this.on_file_upload(ff); ff.state='uploading'; this.tree.invalidate(); var _this = this; this.fireflix.flickr.upload_file( ff.file, { @@ -598,427 +586,423 @@ var fireflix = { this.upload_progress.setAttribute('hidden','false'); this.upload_worker(); }, on_clear: function() { this.clear_list(); }, on_remove: function() { if(this.selection.count) { this.tree.beginUpdateBatch(); for(var i=this.files.length-1;i>=0;--i) { if(this.selection.isSelected(i)) { this.files.splice(i,1); this.rowCount--; } } this.tree.endUpdateBatch(); this.selection.clearSelection(); } }, on_add: function() { var ifp = Components.interfaces.nsIFilePicker; var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(ifp); fp.init(window, "Select a File", ifp.modeOpenMultiple); fp.appendFilters(ifp.filterImages); var rv = fp.show(); if(rv==ifp.returnOK) { var ff = fp.files; while(ff.hasMoreElements()) { var f = ff.getNext(); f.QueryInterface(Components.interfaces.nsIFile); this.add(f.path); } } }, on_cmd_open: function(ev) { if(this.selection.currentIndex<0) return; var f = this.files[this.selection.currentIndex]; if(f.photoid) { this.fireflix.openTab( this.fireflix.flickr.make_uploader_edit_url(f.photoid) ); }else{ this.fireflix.openTab( 'file://'+f.file); } } }, on_set_props: function() { var pset = this.photosets.sets[this.photosets.selection.currentIndex]; window.openDialog( "chrome://fireflix/content/photoset-props.xul", null, "dependent,modal,dialog,chrome", this, pset ); if(pset.dirty) { var _this = this; this.flickr.api_call( { method: 'flickr.photosets.editMeta', auth_token: 'default', photoset_id: pset.id, title: pset.title, description: pset.description }, function(xr) { pset.dirty = false; _this.flickr.api_call( { method: 'flickr.photosets.getPhotos', auth_token: 'default', photoset_id: pset.id }, function(xr) { var x = xr.responseXML; var xp = x.evaluate( '/rsp/photoset/photo', x, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ); var phids = new Array(); var priph = null; var n; while(n=xp.iterateNext()) { var pid = n.getAttribute('id'); phids.push( pid ); if(pid==pset.primary && n.getAttribute('isprimary')!='1') priph = pid; } if(priph) { _this.flickr.api_call( { method: 'flickr.photosets.editPhotos', auth_token: 'default', photoset_id: pset.id, primary_photo_id: priph, photo_ids: phids.join(',') }, function() { }, function(x,s,c,m) { /* flickr.photosets.editPhotos */ _this.flickr_failure(x,s,c,m); } ); } }, function(x,s,c,m) { /* flickr.photosets.getPhotos */ _this.flickr_failure(x,s,c,m); } ); }, function(x,s,c,m) { /* flickr.photosets.editMeta */ _this.flickr_failure(x,s,c,m); } ); } }, on_refresh_sets: function() { this.refresh_sets(); }, on_cmd_sets_html: function(csfx,ev) { var uti = csfx.charAt(0); var utl = csfx.charAt(1); var rv = this.build_html(this.photoset.photos,uti,utl); this.popup_content(rv); }, on_cmd_setphotos_html: function(csfx,ev) { var uti = csfx.charAt(0); var utl = csfx.charAt(1); var rv = ''; for(var p in this.photoset.photos) { if(this.photoset.selection.isSelected(p)) rv += this.photo_html(this.photoset.photos[p],uti,utl)+'\n'; } this.popup_content(rv); }, on_cmd_uploads_html: function(csfx,ev) { var uti = csfx.charAt(0); var utl = csfx.charAt(1); var pids = new Array(); for(var f in this.uploads.files) { if(this.uploads.selection.isSelected(f)) if(this.uploads.files[f].photoid) pids.push(this.uploads.files[f].photoid); } var pp = this.uploads.rowCount*2; if(pp>500) pp = 500; var _this = this; this.flickr.api_call( { method: 'flickr.photos.search', auth_token: 'default', extras: 'original_format', user_id: 'me', per_page: pp }, function(xr) { var x = xr.responseXML; var rv = ''; for(var pn in pids) { var p = pids[pn]; var pp = new Photo(xp_node('/rsp/photos/photo[@id='+p+']',x)); rv += _this.photo_html(pp,uti,utl)+'\n'; } _this.popup_content(rv); }, function(x,s,c,m) { _this.flickr_failure(x,s,c,m); } ); }, /* * */ foundphotos: { fireflix: null, init: function(f) { this.fireflix = f; pull_elements(this,document,[ 'search_for','search_tags','search_mine', 'searchresult_props','search_photo', 'searchresult_title','searchresult_description', 'search_page','cmd_search_prev_page','cmd_search_next_page' ]); document.getElementById('searchresults').view = this; }, photos: new Array(), rowCount: 0, getCellText: function(r,c) { var p = this.photos[r]; if(c.id=='sr_title') return p.title; return c.id; }, setTree: function(t) { this.tree = t }, isContainer: function(r) { return false }, isSeparator: function(r) { return false }, isSorted: function(r) { return false }, getLevel: function(r) { return 0 }, getImageSrc: function(r,c) { return null }, getRowProperties: function(r,p) { }, getCellProperties: function(cid,cel,p) { }, getColumnProperties: function(cid,cel,p) { }, cycleHeader: function(cid,e) { }, getParentIndex: function(r) { return -1 }, drop: function(r,o) { }, canDropBeforeAfter: function(r,b) { return false }, - importXPR: function(xp) { + import_json: function(jp) { this.selection.clearSelection(); - this.selection.currentIndex = -1; - this.searchresult_props.hidden = true; + this.selection.currentIndex=-1; + this.searchresult_props.hidden=true; this.tree.beginUpdateBatch(); this.photos = new Array(); - var n; while(n=xp.iterateNext()) { - this.photos.push(new Photo(n)); + for(var i in jp) { + this.photos.push(new Photo(jp[i])); } this.rowCount = this.photos.length; this.tree.endUpdateBatch(); }, paging: { pars: null, page: null, pages: null, perpage: null, total: null }, search_photos: function() { var pars = { method: 'flickr.photos.search', auth_token: 'default', extras: 'license,date_upload,date_taken,owner_name,icon_server,original_format,last_update,geo' }; if(this.search_mine.checked) pars.user_id='me'; if(this.search_tags.checked) { pars.tags=this.search_for.value.split(/ +/).join(','); }else{ pars.text=this.search_for.value; } this.paging.pars = new Object(); this.paging.page = null; this.paging.pages = null; this.paging.perpage = null; this.paging.total = null; for(var p in pars) this.paging.pars[p] = pars[p]; this.perform_search(pars); }, perform_search: function(p) { - var _this = this; - this.fireflix.flickr.api_call( p, - function(xr) { - var x = xr.responseXML; - var xp = xp_nodes('/rsp/photos/photo',x); - _this.importXPR(xp); - _this.tree.ensureRowIsVisible(0); - xp = xp_node('/rsp/photos',x); - _this.paging.page = parseInt(xp.getAttribute('page')); - _this.paging.pages = parseInt(xp.getAttribute('pages')); - _this.paging.perpage = parseInt(xp.getAttribute('perpage')); - _this.paging.total = parseInt(xp.getAttribute('total')); - _this.update_paging(); - _this.on_select(); + var that = this; + this.fireflix.flickr.api_call_json( p, + function(x,j) { + that.import_json(j.photos.photo); + that.tree.ensureRowIsVisible(0); + var pp=j.photos; + that.paging.page = pp.page; that.paging.pages = pp.pages; + that.paging.perpage = pp.perpage; + that.paging.total = pp.total; + that.update_paging(); + that.on_select(); }, function(x,s,c,m) { - _this.fireflix.flickr_failure(x,s,c,m); + that.fireflix.flickr_failure(x,s,c,m); } ); }, on_cmd_prev: function(ev) { var pars = new Object(); for(var p in this.paging.pars) pars[p] = this.paging.pars[p]; pars.page=this.paging.page-1; pars.per_page=this.paging.perpage; this.perform_search(pars); }, on_cmd_next: function(ev) { var pars = new Object(); for(var p in this.paging.pars) pars[p] = this.paging.pars[p]; pars.page=this.paging.page+1; pars.per_page=this.paging.perpage; this.perform_search(pars); }, update_paging: function() { if(! (this.paging.pars && this.paging.page && this.paging.pages) ) { this.search_page.value=''; this.search_page.hidden = true; this.cmd_search_prev_page.setAttribute('disabled','true'); this.cmd_search_next_page.setAttribute('disabled','true'); }else{ this.search_page.value=this.fireflix.loc_strings.getFormattedString('search_page',[this.paging.page,this.paging.pages]); this.search_page.hidden=false; this.cmd_search_prev_page.setAttribute('disabled',(this.paging.page>1)?'false':'true'); this.cmd_search_next_page.setAttribute('disabled',(this.paging.page<this.paging.pages)?'false':'true'); } }, render_description_frame: function(content) { this.searchresult_description.innerHTML = ''; if(content) { var dp = new DOMParser(); var pd = dp.parseFromString( '<div xmlns="http://www.w3.org/1999/xhtml">'+content+'</div>', 'text/xml' ); var de = pd.documentElement; if(de.tagName=='parsererror') this.searchresult_description.innerHTML=this.fireflix.loc_strings.getString('broken_description'); else this.searchresult_description.appendChild(de); /* of all linking elements flickr only allows a */ var as = this.searchresult_description.getElementsByTagName('a'); for(var a=0;a<as.length;++a) as.item(a).setAttribute('target','_blank'); } }, on_select: function() { if(this.selection.currentIndex<0) { this.searchresult_props.hidden = true; }else{ var p = this.photos[this.selection.currentIndex]; if(!p) { this.searchresult_props.hidden = true; }else{ this.search_photo.src = this.fireflix.flickr.make_photo_url(p,'t'); this.searchresult_title.value = p.title; this.searchresult_title.tooltipText = p.title; this.render_description_frame(null); if(p.description==null && p.description==undefined) { var pid = p.id; var ci = this.selection.currentIndex; - var _this = this; - this.fireflix.flickr.api_call( + var that = this; + this.fireflix.flickr.api_call_json( { method: 'flickr.photos.getInfo', auth_token: 'default', photo_id: p.id, secret: p.secret - }, function(xr) { - var pp = _this.photos[ci]; - if(ci==_this.selection.currentIndex && pp.id==pid) { - var n = xp_node('/rsp/photo',xr.responseXML); - pp.fromNode_(n); - _this.render_description_frame(pp.description); + }, function(x,j) { + var pp = that.photos[ci]; + if(ci==that.selection.currentIndex && pp.id==pid) { + pp.fromJSON_(j.photo); + that.render_description_frame(pp.description); } }, function(x,s,c,m) { - _this.fireflix.flickr_failure(x,s,c,m); + that.fireflix.flickr_failure(x,s,c,m); } ); this.searchresult_props.hidden = false; }else{ this.render_description_frame(p.description); } } } }, on_cmd_open: function(ev) { if(this.selection.currentIndex<0) return; var p = this.photos[this.selection.currentIndex]; if(!p.id) return; this.fireflix.openTab(this.fireflix.flickr.make_photo_url(p,'p')); } }, photo_html: function(p,i,l) { // TODO: add alt/title when possible var rv = '<a href="'+this.flickr.make_photo_url(p,l)+'">' + '<img src="'+this.flickr.make_photo_url(p,i)+'" />'+ '</a>'; return rv; }, build_html: function(photos,uti,utl) { var rv = ''; for(var i in photos) { var p = photos[i]; rv += this.photo_html(p,uti,utl)+'\n'; } return rv; }, popup_content: function(s) { window.openDialog( "chrome://fireflix/content/generated-content.xul", null, "dialog,chrome", this, s ); }, copy_to_clipboard: function(s) { var ch = Components.classes["@mozilla.org/widget/clipboardhelper;1"] .getService(Components.interfaces.nsIClipboardHelper); ch.copyString(s); }, openTab: function(l) { var wm = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService( Components.interfaces.nsIWindowMediator ); var bw = wm.getMostRecentWindow('navigator:browser'); var b = bw.getBrowser(); var t = b.addTab(l); b.selectedTab = t; }, build_menus: function() { this.append_html_menu( document.getElementById('sets_html_menu'), 'stm_','m_bop','cmdset_sets','cmd_sets_html' ); this.append_html_menu( document.getElementById('setphotos_html_menu'), 'stm_','m_bop','cmdset_setphotos','cmd_setphotos_html' ); this.append_html_menu( document.getElementById('uploads_html_menu'), 'stm_','m_bop','cmdset_uploads','cmd_uploads_html' ); return; }, append_html_menu: function(m,imgt,lnkt,csid,cpfx) { var mp = m.appendChild(document.createElement('menupopup')); var t; t=mp.appendChild(document.createElement('menuitem')); t.setAttribute('label',this.loc_strings.getString('menutitle_Images')); t.setAttribute('class','menuhead');t.setAttribute('disabled','true'); mp.appendChild(document.createElement('menuseparator')); var cs = document.getElementById(csid); for(var iti=0;iti<imgt.length;++iti) { t = mp.appendChild(document.createElement('menu')); t.setAttribute('label',this.loc_strings.getString('urltype_'+imgt.charAt(iti))); var smp = t.appendChild(document.createElement('menupopup')); t=smp.appendChild(document.createElement('menuitem')); t.setAttribute('label',this.loc_strings.getString('menutitle_Links')); t.setAttribute('class','menuhead');t.setAttribute('disabled','true'); smp.appendChild(document.createElement('menuseparator')); for(var lti=0;lti<lnkt.length;++lti) { var csfx = imgt.charAt(iti)+lnkt.charAt(lti); t=smp.appendChild(document.createElement('menuitem')); t.setAttribute('label',this.loc_strings.getString('urltype_'+lnkt.charAt(lti))); t.setAttribute('command',cpfx+'_'+csfx); t=cs.appendChild(document.createElement('command')); t.setAttribute('id',cpfx+'_'+csfx); t.setAttribute('oncommand','fireflix.on_'+cpfx+"('"+csfx+"',event)"); } } return mp; }, flickr_failure: function(x,s,c,m) { if(c==98) { // Invalid auth token this.flickr.reset_token(); this.set_auth_state(false,false); return; } // TODO: is that beauty? alert('flickr api call failed\n'+c+' '+m); } }; diff --git a/content/flickr.js b/content/flickr.js index 545144c..716874b 100644 --- a/content/flickr.js +++ b/content/flickr.js @@ -1,270 +1,263 @@ /* * Photoset */ function Photoset(s) { if(s instanceof Photoset) { for(var p in s) this[p]=s[p]; }else - this.fromNode(s); + this.fromJSON(s); } Photoset.prototype = { id: null, primary: null, secret: null, server: null, photos: null, title: null, description: null, - fromNode: function(n) { - this.id = n.getAttribute('id'); - this.primary = n.getAttribute('primary'); - this.secret = n.getAttribute('secret'); - this.server = n.getAttribute('server'); - this.photos = n.getAttribute('photos'); - this.title = n.getElementsByTagName('title').item(0).firstChild.nodeValue; - this.description = n.getElementsByTagName('description').item(0).firstChild; - if(this.description) this.description = this.description.nodeValue; + fromJSON: function(j) { + this.id=j.id; + this.primary = j.primary; + this.secret = j.secret; this.server = j.server; + this.photos = j.photos; + this.title = j.title._content; + this.description = j.description._content; } }; /* * Photo */ function Photo(s) { if(s instanceof Photo) { for(var p in s) this[p]=s[p]; }else - this.fromNode(s); + this.fromJSON(s); } Photo.prototype = { id: null, secret: null, server: null, title: null, isprimary: null, license: null, dateupload: null, datetaken: null, datetakengranularity: null, ownername: null, iconserver: null, originalformat: null, lastupdate: null, - fromNode: function(n) { - this.id = n.getAttribute('id'); this.secret = n.getAttribute('secret'); - this.server = n.getAttribute('server'); - this.title = n.getAttribute('title'); - this.isprimary = n.getAttribute('isprimary'); - this.license = n.getAttribute('license'); - this.dateupload = n.getAttribute('dateupload'); - this.datetaken = n.getAttribute('datetaken'); this.datetakengranularity = n.getAttribute('datetakengranularity'); - this.ownername = n.getAttribute('ownername'); - this.iconserver = n.getAttribute('iconserver'); - this.originalformat = n.getAttribute('originalformat'); - this.lastupdate = n.getAttribute('lastupdate'); + fromJSON: function(j) { + this.id = j.id; this.secret = j.secret; + this.server = j.server; + this.title=j.title; + this.isprimary = j.isprimary; + this.license = j.license; + this.dateupload = j.dateupload; + this.datetaken=j.datetaken; j.datetakengranularity=j.datetakengranularity; + this.ownername=j.ownername; + this.iconserver=j.iconserver; + this.originalformat=j.originalformat; + this.lastupdate=j.lastupdate; }, - fromNode_: function(n) { - var t; - // TODO: @rotation @isfavorite + fromJSON_: function(j) { this.owner = {}; - t = n.getElementsByTagName('owner').item(0); - if(t) { - this.owner.nsid=t.getAttribute('nsid'); - this.owner.username=t.getAttribute('username'); - this.owner.realname=t.getAttribute('realname'); - this.owner.location=t.getAttribute.location; - } - t = n.getElementsByTagName('description').item(0); - if(t && t.firstChild) { - this.description = t.firstChild.nodeValue; + var t; + if((t=j.owner)) { + this.owner.nsid=t.nsid; + this.owner.username=t.username; this.owner.realname=t.realname; + this.owner.location=t.location; } + if((t=j.description)) this.description=t._content; + // TODO: @rotation @isfavorite // TODO: visibility/@ispublic visibility/@isfriend visibility/@isfamily // TODO: dates/@posted dates/@taken dates/@takengranularity dates/@lastupdate // TODO: permissions/@permcomment permsiions/@permaddmeta // TODO: editability/@canaddcomment editability/@canaddmeta // TODO: comments // TODO: notes/note/@id notes/note/@author notes/note/@authorname // TODO: notes/note/@x notes/note/@y notes/note/@w notes/note/@h // TODO: notes/note // TODO: tags/tag/@id tags/tag/@author tags/tag/@raw tags/tag // TODO: urls/url/@type urls/url } }; function Flickr() { } Flickr.prototype = { rest_url: 'http://www.flickr.com/services/rest/', auth_url: 'http://flickr.com/services/auth/', photo_url: 'http://static.flickr.com/', photos_url: 'http://www.flickr.com/photos/', upload_url: 'http://www.flickr.com/services/upload/', uploader_edit_url: 'http://www.flickr.com/tools/uploader_edit.gne', api_sig: function(paramstr) { return MD5(toutf8(this.api_shs+paramstr)); }, api_call_url: function(params,url) { params.api_key = this.api_key; var pp = new Array(); for(var p in params) { pp.push(p); } var pstr = ''; var rv = (url?url:this.rest_url)+'?'; for(var p in pp.sort()) { var pn = pp[p]; pstr += pn+params[pn]; rv += pn+'='+params[pn]+'&'; } rv += 'api_sig='+this.api_sig(pstr); return rv; }, api_call_json: function(params,on_success,on_failure) { if(params.auth_token=='default') params.auth_token=this.token; params.format = 'json'; params.nojsoncallback=1; var x = new XMLHttpRequest(); x.open("GET",this.api_call_url(params)); x.onreadystatechange=function() { if(x.readyState!=4) return false; if(x.status==200) { var rsp=JSON.parse(x.responseText); if(rsp.stat=='ok') on_success && on_success(x,rsp); else on_failure && on_failure(rsp,rsp.stat,rsp.code,rsp.message); }else on_failure && on_failure(x); return true; }; x.send(null); return true; }, api_call: function(params, on_success, on_failure) { if(params.auth_token == 'default') params.auth_token = this.token; var x = new XMLHttpRequest(); x.open("GET",this.api_call_url(params)); x.onreadystatechange=function() { if(x.readyState!=4) return false; if(x.status==200) { var stat = x.responseXML.firstChild.getAttribute('stat'); if(stat=='ok') { if(on_success) on_success(x); }else{ var e = x.responseXML.getElementsByTagName('err').item(0); var ecode = e.getAttribute('code'); var emsg = e.getAttribute('msg'); dump(params.method+' failed: '+ecode+' '+emsg+'\n'); if(on_failure) on_failure(x,stat,ecode,emsg); } }else{ if(on_failure) on_failure(x); } return true; } x.send(null); return true; }, frob: null, authorize_0: function(perms, on_s, on_f) { var _this = this; this.api_call( { method: 'flickr.auth.getFrob' }, function(x) { _this.frob = xp_str('/rsp/frob',x.responseXML); var u = _this.api_call_url( { frob: _this.frob, perms: perms?perms:'delete' }, _this.auth_url ); if(on_s) on_s(x,_this.frob,u); }, function(x,s,c,m) { if(on_f) on_f(x,s,c,m); } ); }, token: null, perms: null, user: null, authorize_1: function(on_s, on_f) { var _this = this; this.api_call( { method: 'flickr.auth.getToken', frob: this.frob }, function(x) { _this.token = xp_str('/rsp/auth/token',x.responseXML); _this.perms = xp_str('/rsp/auth/perms',x.responseXML); var u = xp_node('/rsp/auth/user',x.responseXML); _this.user = { nsid: u.getAttribute('nsid'), username: u.getAttribute('username'), fullname: u.getAttribute('fullname') }; if(on_s) on_s(x); }, function(x,s,c,m) { if(on_f) on_f(x,s,c,m); } ); }, prefs: Components.classes['@mozilla.org/preferences-service;1'].getService( Components.interfaces.nsIPrefBranch ), prefs_root: 'net.klever.kin.flickr', save_token: function() { // TODO: don't clear when there's nothing to clear or catch exceptions if(this.token) this.prefs.setCharPref(this.prefs_root+'.auth_token',this.token); else this.prefs.clearUserPref(this.prefs_root+'.auth_token'); if(this.perms) this.prefs.setCharPref(this.prefs_root+'.auth_perms',this.perms); else this.prefs.clearUserPref(this.prefs_root+'.auth_perms'); if(this.user && this.user.nsid!=null && this.user.nsid!=undefined) this.prefs.setCharPref(this.prefs_root+'.auth_user.nsid',this.user.nsid); else this.prefs.clearUserPref(this.prefs_root+'.auth_user.nsid'); if(this.user && this.user.username!=null && this.user.username!=undefined) this.prefs.setCharPref(this.prefs_root+'.auth_user.username',this.user.username); else this.prefs.clearUserPref(this.prefs_root+'.auth_user.username'); if(this.user && this.user.fullname!=null && this.user.fullname!=undefined) this.prefs.setCharPref(this.prefs_root+'.auth_user.fullname',this.user.fullname); else this.prefs.clearUserPref(this.prefs_root+'.auth_user.fullname'); }, _reset_token: function() { this.token = null; this.perms = null; this.user = null; return false; }, load_token: function() { try { if(this.prefs.getPrefType(this.prefs_root+'.auth_token')!=this.prefs.PREF_STRING) return this._reset_token(); this.token = this.prefs.getCharPref(this.prefs_root+'.auth_token'); if(this.prefs.getPrefType(this.prefs_root+'.auth_perms')!=this.prefs.PREF_STRING) return this._reset_token(); this.perms = this.prefs.getCharPref(this.prefs_root+'.auth_perms'); if(this.prefs.getPrefType(this.prefs_root+'.auth_user.nsid')!=this.prefs.PREF_STRING) return this._reset_token(); this.user = new Object(); this.user.nsid = this.prefs.getCharPref(this.prefs_root+'.auth_user.nsid'); if(this.prefs.getPrefType(this.prefs_root+'.auth_user.username')!=this.prefs.PREF_STRING) return this._reset_token(); this.user.username = this.prefs.getCharPref(this.prefs_root+'.auth_user.username'); if(this.prefs.getPrefType(this.prefs_root+'.auth_user.fullname')!=this.prefs.PREF_STRING) return this._reset_token(); this.user.fullname = this.prefs.getCharPref(this.prefs_root+'.auth_user.fullname'); }catch(e) { return this._reset_token(); } return true; }, reset_token: function() { this._reset_token(); this.save_token(); }, get_photo_url: function(ser,id,sec,sfx,ext) { var rv = this.photo_url + ser + '/' + id + '_' + sec; if(sfx && sfx!='_') rv += '_'+sfx; rv += ext?'.'+ext:'.jpg'; return rv; }, get_image_url: function(o,sfx) { return this.get_photo_url( |