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) (side-by-side diff) | |
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,153 +1,155 @@ 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(); document.getElementById('setslist').view = this.photosets; document.getElementById('setphotos').view = this.photoset; document.getElementById('uploadlist').view = this.uploads; 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( - function() { + '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' ]); }, 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') return p.dateupload; /* TODO: unixtime conversion */ 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) { this.tree.beginUpdateBatch(); this.photos = new Array(); var n; while(n=xp.iterateNext()) { this.photos.push(new Photo(n)); } this.rowCount = this.photos.length; this.tree.endUpdateBatch(); }, load_photos: function(psid) { var _this = this; this.fireflix.flickr.api_call( { 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,s,c,m) { _this.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'); diff --git a/content/flickr.js b/content/flickr.js index add628a..b5bfa43 100644 --- a/content/flickr.js +++ b/content/flickr.js @@ -52,207 +52,201 @@ Photo.prototype = { 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'); }, fromNode_: function(n) { var t; // TODO: @rotation @isfavorite 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; } // 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/', 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: 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(on_s, on_f) { + 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: 'delete' }, _this.auth_url ); - 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(u); - b.selectedTab = t; - if(on_s) on_s(); + { 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( o.server, (o instanceof Photoset)? o.primary : o.id, o.secret, |