home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / deskbar-applet / handlers / epiphany.py < prev    next >
Encoding:
Python Source  |  2007-04-09  |  9.2 KB  |  322 lines

  1. import xml.sax
  2. from os.path import join, expanduser, exists
  3. from gettext import gettext as _
  4. import gtk
  5. import deskbar, deskbar.Indexer, deskbar.Handler
  6. from deskbar.Watcher import FileWatcher
  7. from deskbar.BrowserMatch import get_url_host, is_preferred_browser, on_customize_search_shortcuts, on_entry_key_press, load_shortcuts
  8. from deskbar.BrowserMatch import BrowserSmartMatch, BrowserMatch
  9. from deskbar.defs import VERSION
  10.  
  11. def _check_requirements():
  12. #    if deskbar.UNINSTALLED_DESKBAR:
  13. #        return (deskbar.Handler.HANDLER_IS_HAPPY, None, None)
  14.         
  15.     if is_preferred_browser("epiphany"):
  16.         return (deskbar.Handler.HANDLER_IS_HAPPY, None, None)
  17.     else:
  18.         return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, "Epiphany is not your preferred browser, not using it.", None)
  19.     
  20. def _check_requirements_search():
  21.     callback = lambda dialog: on_customize_search_shortcuts(smart_bookmarks, shortcuts_to_smart_bookmarks_map)
  22.     
  23. #    if deskbar.UNINSTALLED_DESKBAR:
  24. #        return (deskbar.Handler.HANDLER_IS_CONFIGURABLE, "You can set shortcuts for your searches.", callback)
  25.         
  26.     if is_preferred_browser("epiphany"):
  27.         return (deskbar.Handler.HANDLER_IS_CONFIGURABLE, _("You can set shortcuts for your searches."), callback)
  28.     else:
  29.         return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, "Epiphany is not your preferred browser, not using it.", None)
  30.     
  31. HANDLERS = {
  32.     "EpiphanyBookmarksHandler": {
  33.         "name": _("Web Bookmarks"),
  34.         "description": _("Open your web bookmarks by name"),
  35.         "requirements": _check_requirements,
  36.         "version": VERSION,
  37.     },
  38.     "EpiphanyHistoryHandler": {
  39.         "name": _("Web History"),
  40.         "description": _("Open your web history by name"),
  41.         "requirements": _check_requirements,
  42.         "version": VERSION,
  43.     },
  44.     "EpiphanySearchHandler": {
  45.         "name": _("Web Searches"),
  46.         "description": _("Search the web via your browser's search settings"),
  47.         "requirements": _check_requirements_search,
  48.         "version": VERSION,
  49.     },
  50. }
  51.  
  52. EPHY_BOOKMARKS_FILE = expanduser("~/.gnome2/epiphany/bookmarks.rdf")
  53. EPHY_HISTORY_FILE   = expanduser("~/.gnome2/epiphany/ephy-history.xml")
  54.  
  55. favicon_cache = None
  56. bookmarks = None
  57. smart_bookmarks = None
  58. shortcuts_to_smart_bookmarks_map = {}
  59.  
  60. class EpiphanyHandler(deskbar.Handler.Handler):
  61.     def __init__(self, watched_file, callback, icon="stock_bookmark"):
  62.         deskbar.Handler.Handler.__init__(self, icon)
  63.         self.watched_file = watched_file
  64.         self.watch_callback = callback
  65.         
  66.     def initialize(self):
  67.         global favicon_cache
  68.         if favicon_cache == None:
  69.             favicon_cache = EpiphanyFaviconCacheParser().get_cache()
  70.             
  71.         if not hasattr(self, 'watcher'):
  72.             self.watcher = FileWatcher()
  73.             self.watcher.connect('changed', lambda watcher, f: self.watch_callback())
  74.             
  75.         self.watcher.add(self.watched_file)
  76.     
  77.     def stop(self):
  78.         if hasattr(self, 'watcher'):
  79.             self.watcher.remove(self.watched_file)
  80.             del self.watcher
  81.         
  82. class EpiphanyBookmarksHandler(EpiphanyHandler):
  83.     def __init__(self):
  84.         EpiphanyHandler.__init__(self, EPHY_BOOKMARKS_FILE, lambda: self._parse_bookmarks(True))
  85.                 
  86.     def initialize(self):
  87.         EpiphanyHandler.initialize(self)
  88.         self._parse_bookmarks()
  89.         
  90.     def _parse_bookmarks(self, force=False):
  91.         global favicon_cache, bookmarks, smart_bookmarks
  92.         if force or bookmarks == None:
  93.             parser = EpiphanyBookmarksParser(self, favicon_cache)
  94.             bookmarks = parser.get_indexer()
  95.             smart_bookmarks = parser.get_smart_bookmarks()
  96.             load_shortcuts(smart_bookmarks, shortcuts_to_smart_bookmarks_map)
  97.         
  98.     def query(self, query):
  99.         global bookmarks
  100.         return bookmarks.look_up(query)[:deskbar.DEFAULT_RESULTS_PER_HANDLER]
  101.  
  102. class EpiphanySearchHandler(EpiphanyBookmarksHandler):
  103.     def __init__(self):
  104.         EpiphanyBookmarksHandler.__init__(self)
  105.     
  106.     def on_key_press(self, query, shortcut):
  107.         return on_entry_key_press(query, shortcut, shortcuts_to_smart_bookmarks_map)
  108.     
  109.     def query(self, query):
  110.         # if one of the smart bookmarks' shortcuts matches as a prefix,
  111.         # then only return that bookmark
  112.         x = query.find(" ")
  113.         if x != -1:
  114.             prefix = query[:x]
  115.             try:
  116.                 b = shortcuts_to_smart_bookmarks_map[prefix]
  117.                 text = query[x+1:]
  118.                 return [BrowserSmartMatch(b.get_handler(), b.name, b.url, prefix, b, icon=b.icon)]
  119.             except KeyError:
  120.                 # Probably from the b = ... line.  Getting here
  121.                 # means that there is no such shortcut.
  122.                 pass
  123.         
  124.         return smart_bookmarks
  125.         
  126. class EpiphanyHistoryHandler(EpiphanyHandler):
  127.     def __init__(self):
  128.         EpiphanyHandler.__init__(self, EPHY_HISTORY_FILE, self._parse_history, "epiphany-history.png")
  129.         self._history = None
  130.         
  131.     def initialize(self):
  132.         EpiphanyHandler.initialize(self)
  133.         self._parse_history()
  134.         
  135.     def _parse_history(self):
  136.         global favicon_cache
  137.         self._history = EpiphanyHistoryParser(self, favicon_cache).get_indexer()
  138.             
  139.     def query(self, query):
  140.         return self._history.look_up(query)[:deskbar.DEFAULT_RESULTS_PER_HANDLER]
  141.         
  142. class EpiphanyBookmarksParser(xml.sax.ContentHandler):
  143.     def __init__(self, handler, cache):
  144.         xml.sax.ContentHandler.__init__(self)
  145.         
  146.         self.handler = handler
  147.         
  148.         self.chars = ""
  149.         self.title = None
  150.         self.href = None
  151.         self.smarthref = None
  152.         
  153.         self._indexer = deskbar.Indexer.Indexer()
  154.         self._smart_bookmarks = []
  155.         self._cache = cache;
  156.         
  157.         self._index_bookmarks()
  158.     
  159.     def get_indexer(self):
  160.         """
  161.         Returns a completed indexer with the contents of bookmark file
  162.         """
  163.         return self._indexer
  164.     
  165.     def get_smart_bookmarks(self):
  166.         """
  167.         Return a list of EpiphanySmartMatch instances representing smart bookmarks
  168.         """
  169.         return self._smart_bookmarks
  170.         
  171.     def _index_bookmarks(self):
  172.         if exists(EPHY_BOOKMARKS_FILE):
  173.             parser = xml.sax.make_parser()
  174.             parser.setContentHandler(self)
  175.             parser.parse(EPHY_BOOKMARKS_FILE)
  176.     
  177.     def characters(self, chars):
  178.         self.chars = self.chars + chars
  179.         
  180.     def startElement(self, name, attrs):
  181.         self.chars = ""
  182.         if name == "item":
  183.             self.title = None
  184.             self.href = None
  185.             self.smarthref = None
  186.  
  187.     def endElement(self, name):
  188.         if name == "title":
  189.             self.title = self.chars.encode('utf8')
  190.         elif name == "link":
  191.             self.href = self.chars.encode('utf8')
  192.         elif name == "ephy:smartlink":
  193.             self.smarthref = self.chars.encode('utf8')
  194.         elif name == "item":
  195.             if self.href.startswith("javascript:"):
  196.                 return
  197.             
  198.             icon = None
  199.             host = get_url_host(self.href)
  200.             if host in self._cache:
  201.                 icon = self._cache[host]
  202.  
  203.             bookmark = BrowserMatch(self.handler, self.title, self.href, icon=icon)
  204.             if self.smarthref != None:
  205.                 bookmark = BrowserSmartMatch(self.handler, self.title, self.smarthref, icon=icon, bookmark=bookmark)
  206.                 self._smart_bookmarks.append(bookmark)
  207.             else:
  208.                 self._indexer.add("%s %s" % (self.title, self.href), bookmark)
  209.  
  210. class EpiphanyFaviconCacheParser(xml.sax.ContentHandler):
  211.     def __init__(self):
  212.         xml.sax.ContentHandler.__init__(self)
  213.         self.ephy_dir = expanduser("~/.gnome2/epiphany")
  214.         self.filename = join(self.ephy_dir, "ephy-favicon-cache.xml")
  215.         
  216.         self.cache = None
  217.         
  218.         self.chars = ""
  219.         self.url = None
  220.         self.name = None
  221.     
  222.     def get_cache(self):
  223.         """
  224.         Returns a dictionary of (host, favicon path) entries where
  225.           host is the hostname, like google.com (without www)
  226.           favicon path is the on-disk path to the favicon image file.
  227.         """
  228.         if self.cache != None:
  229.             return self.cache
  230.         
  231.         self.cache = {}
  232.         if exists(self.filename):
  233.             parser = xml.sax.make_parser()
  234.             parser.setContentHandler(self)
  235.             parser.parse(self.filename)
  236.             
  237.         return self.cache
  238.     
  239.     def characters(self, chars):
  240.         self.chars = self.chars + chars
  241.         
  242.     def startElement(self, name, attrs):
  243.         self.chars = ""
  244.         if name == "property" and attrs['id'] == "2":
  245.             self.url = None
  246.         if name == "property" and attrs['id'] == "3":
  247.             self.name = None
  248.  
  249.     def endElement(self, name):
  250.         if name == "property":
  251.             if self.url == None:
  252.                 self.url = self.chars
  253.             elif self.name == None:
  254.                 self.name = self.chars
  255.         elif name == "node":
  256.             # Splithost requires //xxxx[:port]/xxxx, so we remove "http:"
  257.             host = get_url_host(self.url)
  258.             self.cache[host] = join(self.ephy_dir, "favicon_cache", self.name.encode('utf8'))
  259.  
  260.  
  261.  
  262. class EpiphanyHistoryParser(xml.sax.ContentHandler):
  263.     def __init__(self, handler, cache):
  264.         xml.sax.ContentHandler.__init__(self)
  265.  
  266.         self.handler = handler;
  267.         self._cache = cache;
  268.         
  269.         self.url = None
  270.         self.title = None
  271.         self.icon = None
  272.         self._id = None;
  273.     
  274.         self._indexer = deskbar.Indexer.Indexer()
  275.  
  276.         self._index_history();
  277.  
  278.     def get_indexer(self):
  279.         """
  280.         Returns a completed indexer with the contents of the history file
  281.         """
  282.         return self._indexer;
  283.  
  284.     def _index_history(self):
  285.         if exists(EPHY_HISTORY_FILE):
  286.             parser = xml.sax.make_parser()
  287.             parser.setContentHandler(self)
  288.             try:
  289.                 parser.parse(EPHY_HISTORY_FILE)
  290.             except Exception, e:
  291.                 print "Couldn't parse epiphany history file:", e
  292.  
  293.     
  294.     def characters(self, chars):
  295.         self.chars = self.chars + chars
  296.         
  297.     def startElement(self, name, attrs):
  298.         self.chars = ""
  299.         if name == "property":
  300.             self._id = attrs['id']
  301.  
  302.         if name == "node":
  303.             self.title = None
  304.             self.url = None
  305.             self.icon = None
  306.  
  307.     def endElement(self, name):
  308.         if name == "property":
  309.             if self._id == "2":
  310.                 self.title = self.chars.encode('utf8')
  311.             elif self._id == "3":
  312.                 self.url = self.chars.encode('utf8')
  313.             elif self._id == "9":
  314.                 self.icon = self.chars.encode('utf8')
  315.         elif name == "node":
  316.             icon = None
  317.             if self.icon in self._cache:
  318.                 icon = self._cache[self.icon]
  319.  
  320.             item = BrowserMatch(self.handler, self.title, self.url, True, icon=icon)
  321.             self._indexer.add("%s %s" % (self.title, self.url), item)
  322.