home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / deskbar-applet / handlers / epiphany.py < prev    next >
Encoding:
Python Source  |  2006-08-29  |  9.1 KB  |  318 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.         self.watcher.remove(self.watched_file)
  79.         del self.watcher
  80.         
  81. class EpiphanyBookmarksHandler(EpiphanyHandler):
  82.     def __init__(self):
  83.         EpiphanyHandler.__init__(self, EPHY_BOOKMARKS_FILE, lambda: self._parse_bookmarks(True))
  84.                 
  85.     def initialize(self):
  86.         EpiphanyHandler.initialize(self)
  87.         self._parse_bookmarks()
  88.         
  89.     def _parse_bookmarks(self, force=False):
  90.         global favicon_cache, bookmarks, smart_bookmarks
  91.         if force or bookmarks == None:
  92.             parser = EpiphanyBookmarksParser(self, favicon_cache)
  93.             bookmarks = parser.get_indexer()
  94.             smart_bookmarks = parser.get_smart_bookmarks()
  95.             load_shortcuts(smart_bookmarks, shortcuts_to_smart_bookmarks_map)
  96.         
  97.     def query(self, query):
  98.         global bookmarks
  99.         return bookmarks.look_up(query)[:deskbar.DEFAULT_RESULTS_PER_HANDLER]
  100.  
  101. class EpiphanySearchHandler(EpiphanyBookmarksHandler):
  102.     def __init__(self):
  103.         EpiphanyBookmarksHandler.__init__(self)
  104.     
  105.     def on_key_press(self, query, shortcut):
  106.         return on_entry_key_press(query, shortcut, shortcuts_to_smart_bookmarks_map)
  107.     
  108.     def query(self, query):
  109.         # if one of the smart bookmarks' shortcuts matches as a prefix,
  110.         # then only return that bookmark
  111.         x = query.find(" ")
  112.         if x != -1:
  113.             prefix = query[:x]
  114.             try:
  115.                 b = shortcuts_to_smart_bookmarks_map[prefix]
  116.                 text = query[x+1:]
  117.                 return [BrowserSmartMatch(b.get_handler(), b.name, b.url, prefix, b, icon=b.icon)]
  118.             except KeyError:
  119.                 # Probably from the b = ... line.  Getting here
  120.                 # means that there is no such shortcut.
  121.                 pass
  122.         
  123.         return smart_bookmarks
  124.         
  125. class EpiphanyHistoryHandler(EpiphanyHandler):
  126.     def __init__(self):
  127.         EpiphanyHandler.__init__(self, EPHY_HISTORY_FILE, self._parse_history, "epiphany-history.png")
  128.         self._history = None
  129.         
  130.     def initialize(self):
  131.         EpiphanyHandler.initialize(self)
  132.         self._parse_history()
  133.         
  134.     def _parse_history(self):
  135.         global favicon_cache
  136.         self._history = EpiphanyHistoryParser(self, favicon_cache).get_indexer()
  137.             
  138.     def query(self, query):
  139.         return self._history.look_up(query)[:deskbar.DEFAULT_RESULTS_PER_HANDLER]
  140.         
  141. class EpiphanyBookmarksParser(xml.sax.ContentHandler):
  142.     def __init__(self, handler, cache):
  143.         xml.sax.ContentHandler.__init__(self)
  144.         
  145.         self.handler = handler
  146.         
  147.         self.chars = ""
  148.         self.title = None
  149.         self.href = None
  150.         self.smarthref = None
  151.         
  152.         self._indexer = deskbar.Indexer.Indexer()
  153.         self._smart_bookmarks = []
  154.         self._cache = cache;
  155.         
  156.         self._index_bookmarks()
  157.     
  158.     def get_indexer(self):
  159.         """
  160.         Returns a completed indexer with the contents of bookmark file
  161.         """
  162.         return self._indexer
  163.     
  164.     def get_smart_bookmarks(self):
  165.         """
  166.         Return a list of EpiphanySmartMatch instances representing smart bookmarks
  167.         """
  168.         return self._smart_bookmarks
  169.         
  170.     def _index_bookmarks(self):
  171.         if exists(EPHY_BOOKMARKS_FILE):
  172.             parser = xml.sax.make_parser()
  173.             parser.setContentHandler(self)
  174.             parser.parse(EPHY_BOOKMARKS_FILE)
  175.     
  176.     def characters(self, chars):
  177.         self.chars = self.chars + chars
  178.         
  179.     def startElement(self, name, attrs):
  180.         self.chars = ""
  181.         if name == "item":
  182.             self.title = None
  183.             self.href = None
  184.             self.smarthref = None
  185.  
  186.     def endElement(self, name):
  187.         if name == "title":
  188.             self.title = self.chars.encode('utf8')
  189.         elif name == "link":
  190.             self.href = self.chars.encode('utf8')
  191.         elif name == "ephy:smartlink":
  192.             self.smarthref = self.chars.encode('utf8')
  193.         elif name == "item":
  194.             if self.href.startswith("javascript:"):
  195.                 return
  196.             
  197.             icon = None
  198.             host = get_url_host(self.href)
  199.             if host in self._cache:
  200.                 icon = self._cache[host]
  201.  
  202.             bookmark = BrowserMatch(self.handler, self.title, self.href, icon=icon)
  203.             if self.smarthref != None:
  204.                 bookmark = BrowserSmartMatch(self.handler, self.title, self.smarthref, icon=icon, bookmark=bookmark)
  205.                 self._smart_bookmarks.append(bookmark)
  206.             else:
  207.                 self._indexer.add("%s %s" % (self.title, self.href), bookmark)
  208.  
  209. class EpiphanyFaviconCacheParser(xml.sax.ContentHandler):
  210.     def __init__(self):
  211.         xml.sax.ContentHandler.__init__(self)
  212.         self.ephy_dir = expanduser("~/.gnome2/epiphany")
  213.         self.filename = join(self.ephy_dir, "ephy-favicon-cache.xml")
  214.         
  215.         self.cache = None
  216.         
  217.         self.chars = ""
  218.         self.url = None
  219.         self.name = None
  220.     
  221.     def get_cache(self):
  222.         """
  223.         Returns a dictionary of (host, favicon path) entries where
  224.           host is the hostname, like google.com (without www)
  225.           favicon path is the on-disk path to the favicon image file.
  226.         """
  227.         if self.cache != None:
  228.             return self.cache
  229.         
  230.         self.cache = {}
  231.         if exists(self.filename):
  232.             parser = xml.sax.make_parser()
  233.             parser.setContentHandler(self)
  234.             parser.parse(self.filename)
  235.             
  236.         return self.cache
  237.     
  238.     def characters(self, chars):
  239.         self.chars = self.chars + chars
  240.         
  241.     def startElement(self, name, attrs):
  242.         self.chars = ""
  243.         if name == "property" and attrs['id'] == "2":
  244.             self.url = None
  245.         if name == "property" and attrs['id'] == "3":
  246.             self.name = None
  247.  
  248.     def endElement(self, name):
  249.         if name == "property":
  250.             if self.url == None:
  251.                 self.url = self.chars
  252.             elif self.name == None:
  253.                 self.name = self.chars
  254.         elif name == "node":
  255.             # Splithost requires //xxxx[:port]/xxxx, so we remove "http:"
  256.             host = get_url_host(self.url)
  257.             self.cache[host] = join(self.ephy_dir, "favicon_cache", self.name.encode('utf8'))
  258.  
  259.  
  260.  
  261. class EpiphanyHistoryParser(xml.sax.ContentHandler):
  262.     def __init__(self, handler, cache):
  263.         xml.sax.ContentHandler.__init__(self)
  264.  
  265.         self.handler = handler;
  266.         self._cache = cache;
  267.         
  268.         self.url = None
  269.         self.title = None
  270.         self.icon = None
  271.         self._id = None;
  272.     
  273.         self._indexer = deskbar.Indexer.Indexer()
  274.  
  275.         self._index_history();
  276.  
  277.     def get_indexer(self):
  278.         """
  279.         Returns a completed indexer with the contents of the history file
  280.         """
  281.         return self._indexer;
  282.  
  283.     def _index_history(self):
  284.         if exists(EPHY_HISTORY_FILE):
  285.             parser = xml.sax.make_parser()
  286.             parser.setContentHandler(self)
  287.             parser.parse(EPHY_HISTORY_FILE)
  288.  
  289.     
  290.     def characters(self, chars):
  291.         self.chars = self.chars + chars
  292.         
  293.     def startElement(self, name, attrs):
  294.         self.chars = ""
  295.         if name == "property":
  296.             self._id = attrs['id']
  297.  
  298.         if name == "node":
  299.             self.title = None
  300.             self.url = None
  301.             self.icon = None
  302.  
  303.     def endElement(self, name):
  304.         if name == "property":
  305.             if self._id == "2":
  306.                 self.title = self.chars.encode('utf8')
  307.             elif self._id == "3":
  308.                 self.url = self.chars.encode('utf8')
  309.             elif self._id == "9":
  310.                 self.icon = self.chars.encode('utf8')
  311.         elif name == "node":
  312.             icon = None
  313.             if self.icon in self._cache:
  314.                 icon = self._cache[self.icon]
  315.  
  316.             item = BrowserMatch(self.handler, self.title, self.url, True, icon=icon)
  317.             self._indexer.add("%s %s" % (self.title, self.url), item)
  318.