home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / pycentral / deskbar-applet / site-packages / deskbar / BrowserMatch.py < prev    next >
Encoding:
Python Source  |  2006-08-29  |  7.3 KB  |  223 lines

  1. import re,cgi, urllib, os
  2. from gettext import gettext as _
  3. import gnomevfs, gconf, gtk, gobject, os.path
  4. import deskbar, deskbar.Match, deskbar.Utils
  5.  
  6. def is_preferred_browser(test):
  7.     # We will import only if the user's preferred browser is mozilla
  8.     http_handler = gconf.client_get_default().get_string("/desktop/gnome/url-handlers/http/command").strip().lower()
  9.     if not gconf.client_get_default().get_bool("/desktop/gnome/url-handlers/http/enabled"):
  10.         return False
  11.     
  12.     if http_handler.find(test) != -1:
  13.         return True
  14.     
  15.     http_handler = http_handler.split(" ")[0]
  16.  
  17.     paths = [path for path in os.getenv("PATH").split(os.path.pathsep) if path.strip() != "" and os.path.exists(path) and os.path.isdir(path)]
  18.     for directory in paths:
  19.         if http_handler.startswith("/"):
  20.             program_path = http_handler
  21.         else:
  22.             program_path = os.path.join(directory, http_handler)
  23.         
  24.         while os.path.islink(program_path):
  25.             program_path = os.path.join(directory, os.readlink(program_path))
  26.             directory = os.path.dirname(program_path)
  27.         
  28.         if program_path.find(test) != -1:
  29.             return True
  30.     
  31.     return False
  32.         
  33. class BrowserMatch(deskbar.Match.Match):
  34.     def __init__(self, backend, name=None, url=None, is_history=False, **args):
  35.         deskbar.Match.Match.__init__(self, backend, name=cgi.escape(name), **args)
  36.         self._priority = 10
  37.         
  38.         self.url = url
  39.         self.is_history = is_history
  40.         
  41.     def action(self, text=None):
  42.         gnomevfs.url_show(self.url)
  43.         
  44.     def get_verb(self):
  45.         if self.is_history:
  46.             return _("Open History Item %s") % "<b>%(name)s</b>"
  47.         else:
  48.             return _("Open Bookmark %s") % "<b>%(name)s</b>"
  49.     
  50.     def get_hash(self, text=None):
  51.         return self.url
  52.     
  53.     def get_category(self):
  54.         return "web"
  55.         
  56. class BrowserSmartMatch(deskbar.Match.Match):
  57.     def __init__(self, backend, name=None, url=None, prefix_to_strip=None, bookmark=None, serialized_bookmark=None, **args):
  58.         deskbar.Match.Match.__init__(self, backend, name=cgi.escape(name), **args)
  59.         self._priority = 0
  60.         
  61.         self.url = url
  62.         if bookmark != None:
  63.             self._bookmark = bookmark
  64.             self.serialized_bookmark = bookmark.serialize()
  65.         else:
  66.             self._bookmark = BrowserMatch(backend, **serialized_bookmark)
  67.             self.serialized_bookmark = serialized_bookmark
  68.         
  69.         self.prefix_to_strip = prefix_to_strip
  70.         if self.prefix_to_strip != None and not self.prefix_to_strip.endswith(" "):
  71.             self.prefix_to_strip += " "
  72.     
  73.     def get_bookmark(self):
  74.         return self._bookmark
  75.         
  76.     def get_hash(self, text=None):
  77.         return self.url
  78.     
  79.     def get_category(self):
  80.         return "websearch"
  81.         
  82.     def get_name(self, text=None):
  83.         m = deskbar.Match.Match.get_name(self, text)
  84.         if self.prefix_to_strip != None and text.startswith(self.prefix_to_strip):
  85.             m["text"] = text[len(self.prefix_to_strip):]
  86.         return m
  87.         
  88.     def action(self, text=""):
  89.         if self.prefix_to_strip != None and text.startswith(self.prefix_to_strip):
  90.             text = text[len(self.prefix_to_strip):]
  91.         
  92.         real_url = re.sub("%s", urllib.quote_plus(text), self.url)
  93.         gnomevfs.url_show(real_url)
  94.         
  95.     def get_verb(self):
  96.         #translators: First %s is the search engine name, second %s is the search term
  97.         return _("Search <b>%(name)s</b> for <i>%(text)s</i>")
  98.                 
  99. def get_url_host(url):
  100.     try:
  101.         #Remove http: needed by splithost
  102.         clean = url[url.find(":")+1:]
  103.         
  104.         #Remove the www part so we have more matches
  105.         if clean.startswith("//www."):
  106.             clean = "//"+clean[6:]
  107.             
  108.         return urllib.splithost(clean)[0]
  109.     except Exception, msg:
  110.         print 'Error:get_url_host(%s):%s' % (url, msg)
  111.         return url
  112.  
  113.  
  114. # FIXME: Begins nastyness:
  115. # Definitions from here down deal with shortcuts for smart bookmarks - both
  116. # managing the UI for customizing shortcuts, and methods for activating them
  117. # on the right triggers (e.g. Ctrl-something).
  118.  
  119. def on_entry_key_press(query, shortcut, shortcuts_to_smart_bookmarks_map):
  120.     if shortcut > 255:
  121.         return None
  122.     else:
  123.         key = chr(shortcut)
  124.         try:
  125.             bookmark = shortcuts_to_smart_bookmarks_map[key]
  126.             return bookmark
  127.         except KeyError:
  128.             # There was no shortcut defined for this keypress
  129.             return None
  130.  
  131. def _sync_shortcuts_map_from_list_store(list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map):
  132.     shortcuts_to_smart_bookmarks_map.clear()
  133.     for row in list_store:
  134.         shortcut, smart_bookmark = row[0], row[1]
  135.         if shortcut != None and len(shortcut) > 0:
  136.             shortcuts_to_smart_bookmarks_map[shortcut] = smart_bookmark
  137.  
  138. def _sync_list_store_from_shortcuts_map(list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map):
  139.     # m is the inverse map of shortcuts_to_smart_bookmarks_map
  140.     m = {}
  141.     for sc in shortcuts_to_smart_bookmarks_map.keys():
  142.         m[shortcuts_to_smart_bookmarks_map[sc]] = sc
  143.     
  144.     # FIXME: this is a major design flaw since not inited bookmarks will fail here
  145.     # FIXME: Another fixme is to resolve conflicts in keybindings, and probably we
  146.     # should provide an unified framework to bind shortcuts with register/unregiter things
  147.     if smart_bookmarks == None:
  148.         return
  149.         
  150.     for b in smart_bookmarks:
  151.         try:
  152.             list_store.append([m[b], b])
  153.         except KeyError:
  154.             # The smart bookmark b did not have a shortcut
  155.             list_store.append([None, b])
  156.  
  157. def load_shortcuts(smart_bookmarks, shortcuts_to_smart_bookmarks_map):
  158.     shortcuts_to_smart_bookmarks_map.clear()
  159.     url_to_shortcuts = {}
  160.     try:
  161.         for line in file(os.path.join(deskbar.USER_DESKBAR_DIR, "search-bookmarks-shortcuts.txt")):
  162.             line = line.strip()
  163.             if len(line) > 0:
  164.                 url, shortcut = line.split()
  165.                 url_to_shortcuts[url] = shortcut
  166.     except IOError:
  167.         # The file probably does not exist
  168.         pass
  169.     for b in smart_bookmarks:
  170.         try:
  171.             sc = url_to_shortcuts[b.url]
  172.             shortcuts_to_smart_bookmarks_map[sc] = b
  173.         except KeyError:
  174.             pass
  175.  
  176. def _save_shortcuts(shortcuts_to_smart_bookmarks_map):
  177.     f = open(os.path.join(deskbar.USER_DESKBAR_DIR, "search-bookmarks-shortcuts.txt"), "w")
  178.     for shortcut in shortcuts_to_smart_bookmarks_map.keys():
  179.         bookmark = shortcuts_to_smart_bookmarks_map[shortcut]
  180.         f.write(bookmark.url)
  181.         f.write("\t")
  182.         f.write(shortcut)
  183.         f.write("\n")
  184.     f.close()
  185.  
  186. def _on_shortcut_edited(cell, path, new_text, (list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map)):
  187.     new_text = new_text.strip()
  188.     if len(new_text) == 0:
  189.         new_text = None
  190.     list_store[path][0] = new_text
  191.     _sync_shortcuts_map_from_list_store(list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map)
  192.     _save_shortcuts(shortcuts_to_smart_bookmarks_map)
  193.  
  194. def on_customize_search_shortcuts(smart_bookmarks, shortcuts_to_smart_bookmarks_map):
  195.     list_store = gtk.ListStore(str, gobject.TYPE_PYOBJECT)
  196.     _sync_list_store_from_shortcuts_map(list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map)
  197.     
  198.     glade = gtk.glade.XML(os.path.join(deskbar.SHARED_DATA_DIR, "smart-bookmarks.glade"))
  199.     
  200.     view = glade.get_widget("bookmarks-view")
  201.     view.set_model(list_store)
  202.     
  203.     crt_shortcut = gtk.CellRendererText()
  204.     crt_shortcut.set_property("editable", True)
  205.     crt_shortcut.connect("edited", _on_shortcut_edited, (list_store, smart_bookmarks, shortcuts_to_smart_bookmarks_map))
  206.     tvc_shortcut = gtk.TreeViewColumn(_("Shortcut"), crt_shortcut, text=0)
  207.     view.append_column(tvc_shortcut)
  208.     
  209.     def bookmark_to_bookmark_name(tree_view_column, cell_renderer, model, iter):
  210.         bookmark = model.get_value(iter, 1)
  211.         cell_renderer.set_property("text", bookmark.name)
  212.  
  213.     crt_name = gtk.CellRendererText()
  214.     tvc_name = gtk.TreeViewColumn(_("Bookmark Name"), crt_name)
  215.     tvc_name.set_cell_data_func(crt_name, bookmark_to_bookmark_name)
  216.     view.append_column(tvc_name)
  217.     
  218.     dialog = glade.get_widget("smart-bookmarks")
  219.     dialog.set_icon_name("deskbar-applet")
  220.     dialog.show_all()
  221.     dialog.run()
  222.     dialog.destroy()
  223.