# HG changeset patch # User Goffi # Date 1277716775 -28800 # Node ID e24e080e6b1656e464173f86f2f3e18f18c20c4a # Parent 5ae370c718038db5875bb72b113877e263d186c9 CS plugin: unread messages can now be openned in external web browser diff -r 5ae370c71803 -r e24e080e6b16 plugins/plugin_misc_cs.py --- a/plugins/plugin_misc_cs.py Mon Jun 28 15:18:59 2010 +0800 +++ b/plugins/plugin_misc_cs.py Mon Jun 28 17:19:35 2010 +0800 @@ -35,6 +35,7 @@ from wokkel import disco, iwokkel, data_form from tools.xml_tools import XMLUI import urllib +import webbrowser from BeautifulSoup import BeautifulSoup import re @@ -75,6 +76,7 @@ host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface")) self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while self.host.registerGeneralCB("plugin_CS_sendMessage", self.sendMessage) + self.host.registerGeneralCB("plugin_CS_showUnreadMessages", self.showUnreadMessages) def erroCB(self, e, id): """Called when something is going wrong when contacting CS website""" @@ -164,6 +166,8 @@ interface = XMLUI('window','tabs', title='CouchSurfing management') interface.addCategory(_("Messages"), "vertical") interface.addText(_("G'day %(name)s, you have %(nb_message)i unread message%(plural_mess)s and %(unread_CR_mess)s unread couch request message%(plural_CR)s\nIf you want to send a message, select the recipient(s) in the list below") % {'name':user_name, 'nb_message':unread_mess, 'plural_mess':'s' if unread_mess>1 else '', 'unread_CR_mess': unread_CR_mess, 'plural_CR':'s' if unread_CR_mess>1 else ''}) + if unread_mess: + interface.addButton('plugin_CS_showUnreadMessages', 'showUnreadMessages', _('Show unread message%(plural)s in external web browser') % {'plural':'s' if unread_mess>1 else ''}) interface.addList(friends_list, 'friends', style=['multi']) interface.changeLayout('pairs') interface.addLabel(_("Subject")) @@ -240,10 +244,6 @@ interface.addText(_('The message has been sent to every recipients')) self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":interface.toXml()}) - - - - def sendMessage(self, id, data, profile): """Called to send a message to a friend @param data: dict with the following keys: @@ -263,4 +263,18 @@ print "send message \o/ :) :) :)" info(_("sending message to %(friends)s with subject [%(subject)s]" % {'friends':friends, 'subject':subject})) self.__sendMessage(None, subject, message, self.data[profile], friends, id, profile) - + + def __showUnreadMessages2(self, html, id, profile): + """Called when the inbox page has been received""" + #FIXME: that's really too fragile, only works if the unread messages are in the first page, and it would be too resources consuming for the website to DL each time all pages. In addition, the show attribute doesn't work as expected. + soup = BeautifulSoup(html) + for tag in soup.findAll(lambda tag: tag.name=='strong' and tag.a and tag.a['href'].startswith('messages.html?message_status=inbox')): + link = "http://www.couchsurfing.org/"+str(tag.a['href']) + webbrowser.open(link, new=2) #TODO: the web browser need to already have CS cookies (i.e. already be opened & logged on CS, or be permanently loggued), a warning to the user should be sent + + def showUnreadMessages(self, id, data, profile): + """Called when user want to see all unread messages in the external browser""" + d = getPage("http://www.couchsurfing.org/messages.html?message_status=inbox&show=10000", agent=AGENT, cookies=self.data[profile]['cookies']) + d.addCallback(self.__showUnreadMessages2, id, profile) + d.addErrback(self.erroCB, id) +