home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / deskbar / BrowserMatch.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  8.4 KB  |  262 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import re
  5. import cgi
  6. import urllib
  7. import os
  8. from gettext import gettext as _
  9. import gnomevfs
  10. import gconf
  11. import gtk
  12. import gobject
  13. import os.path as os
  14. import deskbar
  15. import deskbar.Match as deskbar
  16. import deskbar.Utils as deskbar
  17.  
  18. def is_preferred_browser(test):
  19.     http_handler = gconf.client_get_default().get_string('/desktop/gnome/url-handlers/http/command').strip().lower()
  20.     if not gconf.client_get_default().get_bool('/desktop/gnome/url-handlers/http/enabled'):
  21.         return False
  22.     
  23.     if http_handler.find(test) != -1:
  24.         return True
  25.     
  26.     http_handler = http_handler.split(' ')[0]
  27.     paths = _[1]
  28.     for directory in paths:
  29.         while os.path.islink(program_path):
  30.             program_path = os.path.join(directory, os.readlink(program_path))
  31.             directory = os.path.dirname(program_path)
  32.             continue
  33.             None if http_handler.startswith('/') else []
  34.         if program_path.find(test) != -1:
  35.             return True
  36.             continue
  37.     
  38.     return False
  39.  
  40.  
  41. class BrowserMatch(deskbar.Match.Match):
  42.     
  43.     def __init__(self, backend, name = None, url = None, is_history = False, **args):
  44.         deskbar.Match.Match.__init__(self, backend, name = cgi.escape(name), **args)
  45.         self._priority = 10
  46.         self.url = url
  47.         self.is_history = is_history
  48.  
  49.     
  50.     def action(self, text = None):
  51.         gnomevfs.url_show(self.url)
  52.  
  53.     
  54.     def get_verb(self):
  55.         if self.is_history:
  56.             return _('Open History Item %s') % '<b>%(name)s</b>'
  57.         else:
  58.             return _('Open Bookmark %s') % '<b>%(name)s</b>'
  59.  
  60.     
  61.     def get_hash(self, text = None):
  62.         return self.url
  63.  
  64.     
  65.     def get_category(self):
  66.         return 'web'
  67.  
  68.  
  69.  
  70. class BrowserSmartMatch(deskbar.Match.Match):
  71.     
  72.     def __init__(self, backend, name = None, url = None, prefix_to_strip = None, bookmark = None, serialized_bookmark = None, **args):
  73.         deskbar.Match.Match.__init__(self, backend, name = cgi.escape(name), **args)
  74.         self._priority = 0
  75.         self.url = url
  76.         if bookmark != None:
  77.             self._bookmark = bookmark
  78.             self.serialized_bookmark = bookmark.serialize()
  79.         else:
  80.             self._bookmark = BrowserMatch(backend, **serialized_bookmark)
  81.             self.serialized_bookmark = serialized_bookmark
  82.         self.prefix_to_strip = prefix_to_strip
  83.         if self.prefix_to_strip != None and not self.prefix_to_strip.endswith(' '):
  84.             self.prefix_to_strip += ' '
  85.         
  86.  
  87.     
  88.     def get_bookmark(self):
  89.         return self._bookmark
  90.  
  91.     
  92.     def get_hash(self, text = None):
  93.         return self.url
  94.  
  95.     
  96.     def get_category(self):
  97.         return 'websearch'
  98.  
  99.     
  100.     def get_name(self, text = None):
  101.         m = deskbar.Match.Match.get_name(self, text)
  102.         if self.prefix_to_strip != None and text.startswith(self.prefix_to_strip):
  103.             m['text'] = text[len(self.prefix_to_strip):]
  104.         
  105.         return m
  106.  
  107.     
  108.     def action(self, text = ''):
  109.         if self.prefix_to_strip != None and text.startswith(self.prefix_to_strip):
  110.             text = text[len(self.prefix_to_strip):]
  111.         
  112.         real_url = re.sub('%s', urllib.quote_plus(text), self.url)
  113.         gnomevfs.url_show(real_url)
  114.  
  115.     
  116.     def get_verb(self):
  117.         return _('Search <b>%(name)s</b> for <i>%(text)s</i>')
  118.  
  119.  
  120.  
  121. def get_url_host(url):
  122.     
  123.     try:
  124.         clean = url[url.find(':') + 1:]
  125.         if clean.startswith('//www.'):
  126.             clean = '//' + clean[6:]
  127.         
  128.         return urllib.splithost(clean)[0]
  129.     except Exception:
  130.         msg = None
  131.         print 'Error:get_url_host(%s):%s' % (url, msg)
  132.         return url
  133.  
  134.  
  135.  
  136. def on_entry_key_press(query, shortcut, shortcuts_to_smart_bookmarks_map):
  137.     if shortcut > 255:
  138.         return None
  139.     else:
  140.         key = chr(shortcut)
  141.         
  142.         try:
  143.             bookmark = shortcuts_to_smart_bookmarks_map[key]
  144.             return bookmark
  145.         except KeyError:
  146.             return None
  147.  
  148.  
  149.  
  150. def _sync_shortcuts_map_from_list_store(list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map):
  151.     shortcuts_to_smart_bookmarks_map.clear()
  152.     for row in list_store:
  153.         shortcut = row[0]
  154.         smart_bookmark = row[1]
  155.         if shortcut != None and len(shortcut) > 0:
  156.             shortcuts_to_smart_bookmarks_map[shortcut] = smart_bookmark
  157.             continue
  158.     
  159.  
  160.  
  161. def _sync_list_store_from_shortcuts_map(list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map):
  162.     m = { }
  163.     for sc in shortcuts_to_smart_bookmarks_map.keys():
  164.         m[shortcuts_to_smart_bookmarks_map[sc]] = sc
  165.     
  166.     if smart_bookmarks == None:
  167.         return None
  168.     
  169.     for b in smart_bookmarks:
  170.         
  171.         try:
  172.             list_store.append([
  173.                 m[b],
  174.                 b])
  175.         continue
  176.         except KeyError:
  177.             list_store.append([
  178.                 None,
  179.                 b])
  180.             continue
  181.         
  182.  
  183.     
  184.  
  185.  
  186. def load_shortcuts(smart_bookmarks, shortcuts_to_smart_bookmarks_map):
  187.     shortcuts_to_smart_bookmarks_map.clear()
  188.     url_to_shortcuts = { }
  189.     
  190.     try:
  191.         for line in file(os.path.join(deskbar.USER_DESKBAR_DIR, 'search-bookmarks-shortcuts.txt')):
  192.             line = line.strip()
  193.             if len(line) > 0:
  194.                 (url, shortcut) = line.split()
  195.                 url_to_shortcuts[url] = shortcut
  196.                 continue
  197.     except IOError:
  198.         pass
  199.  
  200.     for b in smart_bookmarks:
  201.         
  202.         try:
  203.             sc = url_to_shortcuts[b.url]
  204.             shortcuts_to_smart_bookmarks_map[sc] = b
  205.         continue
  206.         except KeyError:
  207.             continue
  208.         
  209.  
  210.     
  211.  
  212.  
  213. def _save_shortcuts(shortcuts_to_smart_bookmarks_map):
  214.     f = open(os.path.join(deskbar.USER_DESKBAR_DIR, 'search-bookmarks-shortcuts.txt'), 'w')
  215.     for shortcut in shortcuts_to_smart_bookmarks_map.keys():
  216.         bookmark = shortcuts_to_smart_bookmarks_map[shortcut]
  217.         f.write(bookmark.url)
  218.         f.write('\t')
  219.         f.write(shortcut)
  220.         f.write('\n')
  221.     
  222.     f.close()
  223.  
  224.  
  225. def _on_shortcut_edited(cell, path, new_text, .6):
  226.     (list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map) = .6
  227.     new_text = new_text.strip()
  228.     if len(new_text) == 0:
  229.         new_text = None
  230.     
  231.     list_store[path][0] = new_text
  232.     _sync_shortcuts_map_from_list_store(list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map)
  233.     _save_shortcuts(shortcuts_to_smart_bookmarks_map)
  234.  
  235.  
  236. def on_customize_search_shortcuts(smart_bookmarks, shortcuts_to_smart_bookmarks_map):
  237.     list_store = gtk.ListStore(str, gobject.TYPE_PYOBJECT)
  238.     _sync_list_store_from_shortcuts_map(list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map)
  239.     glade = gtk.glade.XML(os.path.join(deskbar.SHARED_DATA_DIR, 'smart-bookmarks.glade'))
  240.     view = glade.get_widget('bookmarks-view')
  241.     view.set_model(list_store)
  242.     crt_shortcut = gtk.CellRendererText()
  243.     crt_shortcut.set_property('editable', True)
  244.     crt_shortcut.connect('edited', _on_shortcut_edited, (list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map))
  245.     tvc_shortcut = gtk.TreeViewColumn(_('Shortcut'), crt_shortcut, text = 0)
  246.     view.append_column(tvc_shortcut)
  247.     
  248.     def bookmark_to_bookmark_name(tree_view_column, cell_renderer, model, iter):
  249.         bookmark = model.get_value(iter, 1)
  250.         cell_renderer.set_property('text', bookmark.name)
  251.  
  252.     crt_name = gtk.CellRendererText()
  253.     tvc_name = gtk.TreeViewColumn(_('Bookmark Name'), crt_name)
  254.     tvc_name.set_cell_data_func(crt_name, bookmark_to_bookmark_name)
  255.     view.append_column(tvc_name)
  256.     dialog = glade.get_widget('smart-bookmarks')
  257.     dialog.set_icon_name('deskbar-applet')
  258.     dialog.show_all()
  259.     dialog.run()
  260.     dialog.destroy()
  261.  
  262.