home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / deskbar-applet / handlers / web_address.py < prev    next >
Encoding:
Python Source  |  2006-08-29  |  2.3 KB  |  88 lines

  1. from gettext import gettext as _
  2. import re, os
  3. import gobject
  4. import gnomevfs
  5. import deskbar.Handler
  6. import deskbar.Match
  7. from deskbar.defs import VERSION
  8.  
  9. HANDLERS = {
  10.     "WebAddressHandler" : {
  11.         "name": _("Web"),
  12.         "description": _("Open web pages and send emails by typing a complete address"),
  13.         "version": VERSION,
  14.     }
  15. }
  16.  
  17. AUTH_REGEX = re.compile(r'[a-zA-Z]+://\w+(:\w+)?@([\w\-]+\.)+[\w\-]+(:\d+)?(/.*)?')
  18. HTTP_REGEX = re.compile(r'^(?P<method>[a-zA-Z]+://)?([\w\-]+\.)+[\w\-]+(:\d+)?(/.*)?$')
  19. MAIL_REGEX = re.compile(r'^([\w\-]+\.)*[\w\-]+@([\w\-]+\.)*[\w\-]+$')
  20.  
  21. class WebAddressMatch(deskbar.Match.Match):
  22.     def __init__(self, backend, name=None, has_method=True, **args):
  23.         deskbar.Match.Match.__init__(self, backend, name=name, **args)
  24.         
  25.         self.has_method = has_method
  26.         if not has_method and not self.name.startswith("http://"):
  27.             self.name = "http://" + name
  28.         
  29.     def action(self, text=None):
  30.         if self.name.startswith("http"):
  31.             gnomevfs.url_show(self.name)
  32.         else:
  33.             gobject.spawn_async(["nautilus", self.name], flags=gobject.SPAWN_SEARCH_PATH)
  34.             
  35.     def get_category(self):
  36.         return "web"
  37.     
  38.     def get_verb(self):
  39.         if not self.has_method:
  40.             return _("Open the web page %s") % "<b>%(name)s</b>"
  41.         else:
  42.             return _("Open the location %s") % "<b>%(name)s</b>"
  43.     
  44.     def get_hash(self, text=None):
  45.         return self.name
  46.  
  47. class EmailAddressMatch(deskbar.Match.Match):
  48.     def __init__(self, backend, **args):
  49.         deskbar.Match.Match.__init__(self, backend, icon="stock_mail", **args)
  50.         
  51.     def action(self, text=None):
  52.         gnomevfs.url_show("mailto:"+self.name)
  53.         
  54.     def get_category(self):
  55.         return "people"
  56.     
  57.     def get_verb(self):
  58.         return _("Send Email to %s") % "<b>%(name)s</b>"
  59.     
  60.     def get_hash(self, text=None):
  61.         return self.name
  62.         
  63. class WebAddressHandler(deskbar.Handler.Handler):
  64.     def __init__(self):
  65.         deskbar.Handler.Handler.__init__(self, "stock_internet")
  66.     
  67.     def query(self, query):
  68.         result = self.query_http(query)
  69.         result += self.query_mail(query)
  70.         return result
  71.         
  72.     def query_http(self, query):
  73.         match = AUTH_REGEX.match(query)
  74.         if match != None:
  75.             return [WebAddressMatch(self, query)]
  76.         
  77.         match = HTTP_REGEX.match(query)
  78.         if match != None:
  79.             return [WebAddressMatch(self, query, (match.group('method') != None))]
  80.     
  81.         return []
  82.         
  83.     def query_mail(self, query):
  84.         if MAIL_REGEX.match(query) != None:
  85.             return [EmailAddressMatch(self, name=query)]
  86.         else:
  87.             return []
  88.