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.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  13.2 KB  |  367 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  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.         if hasattr(self, 'watcher'):
  76.             self.watcher.remove(self.watched_file)
  77.             del self.watcher
  78.         
  79.  
  80.  
  81.  
  82. class EpiphanyBookmarksHandler(EpiphanyHandler):
  83.     
  84.     def __init__(self):
  85.         EpiphanyHandler.__init__(self, (EPHY_BOOKMARKS_FILE,), (lambda : self._parse_bookmarks(True)))
  86.  
  87.     
  88.     def initialize(self):
  89.         EpiphanyHandler.initialize(self)
  90.         self._parse_bookmarks()
  91.  
  92.     
  93.     def _parse_bookmarks(self, force = False):
  94.         global bookmarks, smart_bookmarks
  95.         if force or bookmarks == None:
  96.             parser = EpiphanyBookmarksParser(self, favicon_cache)
  97.             bookmarks = parser.get_indexer()
  98.             smart_bookmarks = parser.get_smart_bookmarks()
  99.             load_shortcuts(smart_bookmarks, shortcuts_to_smart_bookmarks_map)
  100.         
  101.  
  102.     
  103.     def query(self, query):
  104.         return bookmarks.look_up(query)[:deskbar.DEFAULT_RESULTS_PER_HANDLER]
  105.  
  106.  
  107.  
  108. class EpiphanySearchHandler(EpiphanyBookmarksHandler):
  109.     
  110.     def __init__(self):
  111.         EpiphanyBookmarksHandler.__init__(self)
  112.  
  113.     
  114.     def on_key_press(self, query, shortcut):
  115.         return on_entry_key_press(query, shortcut, shortcuts_to_smart_bookmarks_map)
  116.  
  117.     
  118.     def query(self, query):
  119.         x = query.find(' ')
  120.         if x != -1:
  121.             prefix = query[:x]
  122.             
  123.             try:
  124.                 b = shortcuts_to_smart_bookmarks_map[prefix]
  125.                 text = query[x + 1:]
  126.                 return [
  127.                     BrowserSmartMatch(b.get_handler(), b.name, b.url, prefix, b, icon = b.icon)]
  128.             except KeyError:
  129.                 pass
  130.             except:
  131.                 None<EXCEPTION MATCH>KeyError
  132.             
  133.  
  134.         None<EXCEPTION MATCH>KeyError
  135.         return smart_bookmarks
  136.  
  137.  
  138.  
  139. class EpiphanyHistoryHandler(EpiphanyHandler):
  140.     
  141.     def __init__(self):
  142.         EpiphanyHandler.__init__(self, EPHY_HISTORY_FILE, self._parse_history, 'epiphany-history.png')
  143.         self._history = None
  144.  
  145.     
  146.     def initialize(self):
  147.         EpiphanyHandler.initialize(self)
  148.         self._parse_history()
  149.  
  150.     
  151.     def _parse_history(self):
  152.         self._history = EpiphanyHistoryParser(self, favicon_cache).get_indexer()
  153.  
  154.     
  155.     def query(self, query):
  156.         return self._history.look_up(query)[:deskbar.DEFAULT_RESULTS_PER_HANDLER]
  157.  
  158.  
  159.  
  160. class EpiphanyBookmarksParser(xml.sax.ContentHandler):
  161.     
  162.     def __init__(self, handler, cache):
  163.         xml.sax.ContentHandler.__init__(self)
  164.         self.handler = handler
  165.         self.chars = ''
  166.         self.title = None
  167.         self.href = None
  168.         self.smarthref = None
  169.         self._indexer = deskbar.Indexer.Indexer()
  170.         self._smart_bookmarks = []
  171.         self._cache = cache
  172.         self._index_bookmarks()
  173.  
  174.     
  175.     def get_indexer(self):
  176.         '''
  177. \t\tReturns a completed indexer with the contents of bookmark file
  178. \t\t'''
  179.         return self._indexer
  180.  
  181.     
  182.     def get_smart_bookmarks(self):
  183.         '''
  184. \t\tReturn a list of EpiphanySmartMatch instances representing smart bookmarks
  185. \t\t'''
  186.         return self._smart_bookmarks
  187.  
  188.     
  189.     def _index_bookmarks(self):
  190.         if exists(EPHY_BOOKMARKS_FILE):
  191.             parser = xml.sax.make_parser()
  192.             parser.setContentHandler(self)
  193.             parser.parse(EPHY_BOOKMARKS_FILE)
  194.         
  195.  
  196.     
  197.     def characters(self, chars):
  198.         self.chars = self.chars + chars
  199.  
  200.     
  201.     def startElement(self, name, attrs):
  202.         self.chars = ''
  203.         if name == 'item':
  204.             self.title = None
  205.             self.href = None
  206.             self.smarthref = None
  207.         
  208.  
  209.     
  210.     def endElement(self, name):
  211.         if name == 'title':
  212.             self.title = self.chars.encode('utf8')
  213.         elif name == 'link':
  214.             self.href = self.chars.encode('utf8')
  215.         elif name == 'ephy:smartlink':
  216.             self.smarthref = self.chars.encode('utf8')
  217.         elif name == 'item':
  218.             if self.href.startswith('javascript:'):
  219.                 return None
  220.             
  221.             icon = None
  222.             host = get_url_host(self.href)
  223.             if host in self._cache:
  224.                 icon = self._cache[host]
  225.             
  226.             bookmark = BrowserMatch(self.handler, self.title, self.href, icon = icon)
  227.             if self.smarthref != None:
  228.                 bookmark = BrowserSmartMatch(self.handler, self.title, self.smarthref, icon = icon, bookmark = bookmark)
  229.                 self._smart_bookmarks.append(bookmark)
  230.             else:
  231.                 self._indexer.add('%s %s' % (self.title, self.href), bookmark)
  232.         
  233.  
  234.  
  235.  
  236. class EpiphanyFaviconCacheParser(xml.sax.ContentHandler):
  237.     
  238.     def __init__(self):
  239.         xml.sax.ContentHandler.__init__(self)
  240.         self.ephy_dir = expanduser('~/.gnome2/epiphany')
  241.         self.filename = join(self.ephy_dir, 'ephy-favicon-cache.xml')
  242.         self.cache = None
  243.         self.chars = ''
  244.         self.url = None
  245.         self.name = None
  246.  
  247.     
  248.     def get_cache(self):
  249.         '''
  250. \t\tReturns a dictionary of (host, favicon path) entries where
  251. \t\t  host is the hostname, like google.com (without www)
  252. \t\t  favicon path is the on-disk path to the favicon image file.
  253. \t\t'''
  254.         if self.cache != None:
  255.             return self.cache
  256.         
  257.         self.cache = { }
  258.         if exists(self.filename):
  259.             parser = xml.sax.make_parser()
  260.             parser.setContentHandler(self)
  261.             parser.parse(self.filename)
  262.         
  263.         return self.cache
  264.  
  265.     
  266.     def characters(self, chars):
  267.         self.chars = self.chars + chars
  268.  
  269.     
  270.     def startElement(self, name, attrs):
  271.         self.chars = ''
  272.         if name == 'property' and attrs['id'] == '2':
  273.             self.url = None
  274.         
  275.         if name == 'property' and attrs['id'] == '3':
  276.             self.name = None
  277.         
  278.  
  279.     
  280.     def endElement(self, name):
  281.         if name == 'property':
  282.             if self.url == None:
  283.                 self.url = self.chars
  284.             elif self.name == None:
  285.                 self.name = self.chars
  286.             
  287.         elif name == 'node':
  288.             host = get_url_host(self.url)
  289.             self.cache[host] = join(self.ephy_dir, 'favicon_cache', self.name.encode('utf8'))
  290.         
  291.  
  292.  
  293.  
  294. class EpiphanyHistoryParser(xml.sax.ContentHandler):
  295.     
  296.     def __init__(self, handler, cache):
  297.         xml.sax.ContentHandler.__init__(self)
  298.         self.handler = handler
  299.         self._cache = cache
  300.         self.url = None
  301.         self.title = None
  302.         self.icon = None
  303.         self._id = None
  304.         self._indexer = deskbar.Indexer.Indexer()
  305.         self._index_history()
  306.  
  307.     
  308.     def get_indexer(self):
  309.         '''
  310. \t\tReturns a completed indexer with the contents of the history file
  311. \t\t'''
  312.         return self._indexer
  313.  
  314.     
  315.     def _index_history(self):
  316.         if exists(EPHY_HISTORY_FILE):
  317.             parser = xml.sax.make_parser()
  318.             parser.setContentHandler(self)
  319.             
  320.             try:
  321.                 parser.parse(EPHY_HISTORY_FILE)
  322.             except Exception:
  323.                 e = None
  324.                 print "Couldn't parse epiphany history file:", e
  325.             except:
  326.                 None<EXCEPTION MATCH>Exception
  327.             
  328.  
  329.         None<EXCEPTION MATCH>Exception
  330.  
  331.     
  332.     def characters(self, chars):
  333.         self.chars = self.chars + chars
  334.  
  335.     
  336.     def startElement(self, name, attrs):
  337.         self.chars = ''
  338.         if name == 'property':
  339.             self._id = attrs['id']
  340.         
  341.         if name == 'node':
  342.             self.title = None
  343.             self.url = None
  344.             self.icon = None
  345.         
  346.  
  347.     
  348.     def endElement(self, name):
  349.         if name == 'property':
  350.             if self._id == '2':
  351.                 self.title = self.chars.encode('utf8')
  352.             elif self._id == '3':
  353.                 self.url = self.chars.encode('utf8')
  354.             elif self._id == '9':
  355.                 self.icon = self.chars.encode('utf8')
  356.             
  357.         elif name == 'node':
  358.             icon = None
  359.             if self.icon in self._cache:
  360.                 icon = self._cache[self.icon]
  361.             
  362.             item = BrowserMatch(self.handler, self.title, self.url, True, icon = icon)
  363.             self._indexer.add('%s %s' % (self.title, self.url), item)
  364.         
  365.  
  366.  
  367.