""" Gmail Deskbar A plugin for the Gnome deskbar applet that searches your Gmail account. Stuart Langridge, http://www.kryogenix.org/ Licenced under the GNU GPL, version 2.0 """ # THIS IS THE SUSPECT HACKY VERSION # YOU SHOULDN'T REALLY BE USING THIS # TO USE THIS, YOU HAVE TO HARDCODE YOUR GMAIL USERNAME AND PASSWORD # IF YOU'RE NOT PREPARED TO DO THAT, DON'T USE THIS HACKY VERSION # GMAIL_USERNAME = "" GMAIL_PASSWORD = "" # # END HACKY BIT from deskbar.Utils import strip_html from gettext import gettext as _ import urllib, os import gnomevfs, gtk import deskbar.Handler, deskbar import xml.dom.minidom try: import libgmail except: pass # alert in requirements GMAIL_MESSAGE_URL = 'https://mail.google.com/mail/h/?q=%(search)s&s=q&th=%(thread)s&v=c' MAX_QUERIES = 10 QUERY_DELAY = 1 USERNAME_FILE=os.path.expanduser('~/.gnome2/deskbar-applet/gmail.login') GMAIL_ICON = [ "16 16 8 1", " c None", ". c #DA3838", "+ c #F28181", "@ c #F9A7A7", "# c #FFB6B6", "$ c #FFFFFF", "% c #E95A5A", "& c #FFE2E2", " ", " ", " ", "...+@@@@@@@@+...", "....#$$$$$$#....", "..+%.#$$$$#.%+..", "..$+%.#$$#.%+$..", "..$$+%.##.%+$$..", "..$$#+%..%+#$$..", "..$&@#+%%+#@&$..", "..&@&$$++$$&@&..", "..@&$$$$$$$$&@..", "..&$$$$$$$$$$&..", "..@@@@@@@@@@@@..", " ", " " ] def monkeypatch_libgmail_login(self): "New login method for GmailAccount in old libgmails: monkeypatch" import urllib,urllib2,re data = urllib.urlencode({'continue': "https://mail.google.com/mail/", # hardcode new URL_GMAIL 'Email': self.name, 'Passwd': self._pw}) headers = {'Host': 'www.google.com', 'User-Agent': 'User-Agent: Mozilla/5.0 (compatible;)'} req = urllib2.Request(libgmail.URL_LOGIN, data=data, headers=headers) pageData = self._retrievePage(req) RE_PAGE_REDIRECT = 'CheckCookie\?continue=([^"]+)' try: redirectURL = urllib.unquote(re.search(RE_PAGE_REDIRECT, pageData).group(1)) except AttributeError: raise GmailLoginFailure pageData = self._retrievePage(redirectURL) HELP_TEXT = _("""Gmail Search requires libgmail. Download from http://libgmail.sf.net/ or install your distribution's package (python-libgmail in Ubuntu).""") def _on_more_information(): deskbar.Utils.more_information_dialog(_("Getting libgmail"),HELP_TEXT) def _check_requirements(*args): try: import libgmail return (deskbar.Handler.HANDLER_IS_HAPPY, None, None) except Exception, e: return (deskbar.Handler.HANDLER_HAS_REQUIREMENTS, "Gmail Search requires libgmail (libgmail.sf.net)", _on_more_information) HANDLERS = { "GmailHandler" : { "name": _("Gmail Search"), "description": _("Search your Gmail"), "requirements" : _check_requirements } } class GmailMatch(deskbar.Match.Match): def __init__(self, handler, name, threadid, search, **args): deskbar.Match.Match.__init__ (self, handler, name=name, **args) self.url = GMAIL_MESSAGE_URL % {"search": search, "thread": threadid} def get_verb(self): return "%(name)s" def action(self, text=None): gnomevfs.url_show(self.url) def get_category(self): return "web" def get_hash(self, text=None): return self.url class GmailHandler(deskbar.Handler.AsyncHandler): def __init__(self): deskbar.Handler.AsyncHandler.__init__(self, None) self._icon = gtk.gdk.pixbuf_new_from_xpm_data(GMAIL_ICON) self.server = None def initialize(self): self.ga = libgmail.GmailAccount(GMAIL_USERNAME,GMAIL_PASSWORD) try: v = libgmail.Version except: # libgmail had a broken login() in older versions, including # that packaged with Ubuntu Dapper and previous. If it doesn't # have a version number then it's an old version, so monkeypatch # the login function to work. libgmail.GmailAccount.login = monkeypatch_libgmail_login self.ga.login() def query(self, qstring, defres): # Just to ensure we don't bork anything qmax = min (deskbar.DEFAULT_RESULTS_PER_HANDLER, MAX_QUERIES) # Delay before we query so we *don't* make four queries # "s", "sp", "spa", "spam". self.check_query_changed (timeout=QUERY_DELAY) print 'Query Gmail for:', qstring self.check_query_changed () # The gmail search might have taken a long time # better check if we're still valid matches = [GmailMatch(self, x.subject, x.info[0], qstring) for x in self.ga.getMessagesByQuery(qstring)] self.check_query_changed () print "Returning Gmail answer for:", qstring return matches