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.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  11.8 KB  |  356 lines

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