def h_authorize(self, _): """ Bus message handler """ ahandler=AuthResponseAgent() Bus.publish(self, "method_call", {"method":"auth.getToken"}, {"c":ahandler.h_apicb, "e":ahandler.h_apieb})
def on_cenable_toggled(self, wcenable): """ DBus API enable checkbox """ state=wcenable.props.active Bus.publish(self, "user_params", {"dbus_enable": str(state)}) return True
def h_apieb(self, response): """ API response errback """ _,code, msg=response tcode=self._msgTable.get(code, "unknown") Bus.publish(self, "error", tcode) Bus.publish(self, "log", "Authorization error, code(%s) msg(%s)" % (code, msg))
def _publishParams(self, params={}): """ Publish any change """ for param in self._paramsList: wgt=self.builder.get_object("e%s"%param) value=wgt.get_text() params[param]=value Bus.publish(self, "user_params", params)
def on_bauth_clicked(self, wbauth): Bus.publish(self, "authorize") return True
def show(self): Bus.publish(self, "user_params?") self.bapply.props.sensitive=False self.window.present()
Bus.publish(self, "user_params", params) def _resetChanged(self): self.username_changed=False self.api_key_changed=False self.secret_key_changed=False self.params_changed=False def _applyState(self, state): self.bapply.props.sensitive=state cfg=Config() Bus.subscribe("user_params", cfg.h_user_params) class App: ICON_PATH="/usr/share/icons" ICON_FILE="dlastfm.png" def __init__(self, curdir): self.curdir=curdir self._statusIcon=gtk.StatusIcon() self._statusIcon.set_from_stock(gtk.STOCK_ABOUT) self._statusIcon.set_visible(True) self._statusIcon.set_tooltip("DBus Last.fm")
def gatePublish(self, *pa): if self._enable: Bus.publish(self, *pa)
def __init__(self): bus_name = dbus.service.BusName('fm.lastfm.api', bus=dbus.SessionBus()) dbus.service.Object.__init__(self, bus_name, '/account') self._cache={} self._enable=False Bus.publish(self, "user_params?")
@dbus.service.method('fm.last.api.account', in_signature="s") def setSecretKey(self, secret_key): self.gatePublish("user_params", {"secret_key":secret_key}) ## ================================================================== Authentication @dbus.service.method('fm.last.api.account', out_signature="v", async_callbacks=("_callback", "_errback")) def getAuthUrl(self, _callback, _errback): """ Returns an URL pointing to an authorization page An "authorization token" must first be retrieved from Last.fm and thus the current session (if any) will be lost. """ self.gatePublish("method_call", {"method":"auth.getToken"}, {"c":_callback, "e":_errback}) @dbus.service.method('fm.last.api.account') def clearSession(self): """ Clears all session related user parameters """ self.gatePublish("user_params", {"auth_token":"", "token":""}) api=DbusApiAccount() Bus.subscribe("user_params", api._snif_user_params)
If a valid 'url' is received, open a browser tab for the user to 'authorize' the application """ _status, url=response webbrowser.open_new_tab(url) def h_apieb(self, response): """ API response errback """ _,code, msg=response tcode=self._msgTable.get(code, "unknown") Bus.publish(self, "error", tcode) Bus.publish(self, "log", "Authorization error, code(%s) msg(%s)" % (code, msg)) class AuthorizeAgent(object): def h_authorize(self, _): """ Bus message handler """ ahandler=AuthResponseAgent() Bus.publish(self, "method_call", {"method":"auth.getToken"}, {"c":ahandler.h_apicb, "e":ahandler.h_apieb}) aagent=AuthorizeAgent() Bus.subscribe("authorize", aagent.h_authorize)
""" _msgs={"api_access_error":"Error whilst accessing Last.FM web api. <br/>Is the service authentified?" ,"conn_error":"Connection error. <br/>Is Internet access functional?" ,"auth_error":"Authorization error. <br/>Are the 'api_key' and 'secret_key' valid?" ,"api_error":"Web API error. <br/>Has Last.fm's Web API changed?" ,"unknown":"Unknown error!" } def h_error(self, _, errorType, *pa, **kwa): errmsg=self._msgs.get(errorType, None) if not errmsg: errmsg=self._msgs.get("unknown") self._doNotify("DBus Last.fm - Error", errmsg) def _doNotify(self, summary, msg): pynotify.init("DBus Last.fm") n=pynotify.Notification(summary, msg) n.show() usermsg=UserMessenger() Bus.subscribe("error", usermsg.h_error) if __name__=="__main__": u=UserMessenger() u.h_error("", "api_access_error") #u.h_error("", "conn_error") #u.h_error("", "foo")