changeset 108:e24e080e6b16

CS plugin: unread messages can now be openned in external web browser
author Goffi <goffi@goffi.org>
date Mon, 28 Jun 2010 17:19:35 +0800
parents 5ae370c71803
children 18b0cf49a6f1
files plugins/plugin_misc_cs.py
diffstat 1 files changed, 19 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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)
+