Port to Seed to support (only) the WebKit version of Epiphany master v1.1.1
authorIain Nicol <iainn@src.gnome.org>
Thu, 31 Dec 2009 00:30:54 +0000 (00:30 +0000)
committerIain Nicol <iainn@src.gnome.org>
Sat, 13 Aug 2011 10:53:36 +0000 (11:53 +0100)
This required a port from GnomeVFS to the more modern GIO.

Additionally, add an Edit | Bookmark Shortcuts menu entry to Epiphany.
Previously there was no way to configure this extension from within
Epiphany.  (This should have been a separate commit but, *ahem*, I
didn't use version control at the time.  Years later, splitting this
commit is not worth the effort.)

README.bookmark-shortcuts [new file with mode: 0644]
about-bookmark-shortcuts.txt [deleted file]
bookmark-shortcuts.ephy-extension [new file with mode: 0644]
bookmark-shortcuts.js [new file with mode: 0644]
bookmark-shortcuts.py [deleted file]
bookmark-shortcuts.xml [deleted file]

diff --git a/README.bookmark-shortcuts b/README.bookmark-shortcuts
new file mode 100644 (file)
index 0000000..a7ec3ce
--- /dev/null
@@ -0,0 +1,25 @@
+This Epiphany extension uses the Deskbar's shortcuts for the browser's
+``smart bookmarks'', or searches, so that, if you have the Wikipedia
+search bookmarked, and you have configured Deskbar properly, you can
+type ``wp foo'' in Epiphany's location bar to go to
+http://en.wikipedia.org/wiki/Special:Search?search=%foo
+
+To install, move the .js and .ephy-extension files into
+~/.gnome2/epiphany/extensions/ and then, from Epiphany's menu, go to
+Tools -> Extensions to enable it.
+
+You can configure the shortcuts from Epiphany's Edit -> Bookmark
+Shortcuts menu.  Alternatively, right-click on Deskbar's icon, choose
+Preferences and from the ``Web Searches (Epiphany)'' extension, click
+the ``More...''  button.  That, or just edit Deskbar's list of search
+shortcuts (it's a text file):
+~/.gnome2/deskbar-applet/search-bookmarks-shortcuts.txt
+
+Note that this is an Epiphany extension, not a Deskbar extension.
+Deskbar comes out-of-the-box with this behavior.
+
+This extension was originally written by Nigel Tao.  It was ported to be
+a ``Seed'' Javascript extension by Iain Nicol, to get it to work with
+versions of Epiphany greater than 2.28.  However, to avoid crashes when
+using this extension you should use Epiphany 2.29.3 or later, and Seed
+2.29.5 or later.
\ No newline at end of file
diff --git a/about-bookmark-shortcuts.txt b/about-bookmark-shortcuts.txt
deleted file mode 100644 (file)
index 9458e1e..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-This Epiphany extension uses the Deskbar's shortcuts for the browser's "smart
-bookmarks", or searches, so that, if you have the Wikipedia search bookmarked,
-and you have configured Deskbar properly, you can type "wp foo" in Epiphany's
-location bar to go to http://en.wikipedia.org/wiki/Special:Search?search=%foo
-
-To configure the Deskbar's shortcuts, right-click on its icon, choose
-Preferences and from the "Web Searches" extension, click the "More..." button.
-Alternatively, just edit Deskbar's list of search shortcuts (it's a text file):
-~/.gnome2/deskbar-applet/search-bookmarks-shortcuts.txt
-
-To install, move the .py and .xml files into ~/.gnome2/epiphany/extensions/
-and then, from Epiphany's menu, go to Tools -> Extensions to enable it.
-
-Note that this is an Epiphany extension, not a Deskbar extension.  Deskbar
-comes out-of-the-box with this behavior.
-
-This has been tested with Deskbar 2.14 and Epiphany 2.14.
-Nigel Tao, 2006-10-17.
diff --git a/bookmark-shortcuts.ephy-extension b/bookmark-shortcuts.ephy-extension
new file mode 100644 (file)
index 0000000..46a231b
--- /dev/null
@@ -0,0 +1,10 @@
+[Epiphany Extension]
+Name=Bookmark shortcuts
+Description=Perform bookmark searches by typing a shortcut in the address bar
+Authors=Iain Nicol\nNigel Tao
+Version=1.1.1
+URL=http://live.gnome.org/Epiphany/ThirdPartyExtensions
+
+[Loader]
+Type=seed
+Module=bookmark-shortcuts
diff --git a/bookmark-shortcuts.js b/bookmark-shortcuts.js
new file mode 100644 (file)
index 0000000..df82809
--- /dev/null
@@ -0,0 +1,221 @@
+// bookmark shortcuts v1.1.1
+// shortcuts for smart bookmark searches, shared with Deskbar Applet
+
+// Original extension for the Mozilla-based Epiphany:
+// (c) 2006-10-17 by Nigel Tao <nigeltao@gnome.org>, GPL licensed.
+// Port to Seed and GIO, and add an entry to the Edit menu:
+// Copyright © 2009 Iain Nicol <iainn@src.gnome.org>
+
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+Gio = imports.gi.Gio;
+Gdk = imports.gi.Gdk;
+
+var shortcut_to_bookmark_map = {};
+var shortcuts_file_name = GLib.get_user_config_dir() +
+    "/deskbar-applet/search-bookmarks-shortcuts.txt";
+var shortcuts_file_monitor;
+
+var get_map_from_file = function (file) {
+    var map = {};
+
+    try {
+        var data_in_stream = new Gio.DataInputStream.c_new(file.read());
+
+        while ((line = data_in_stream.read_line(null)) !== null) {
+            line = line.trim();
+
+            var sep_location = line.search(/ |\t/);
+            if (sep_location > 0) {
+                var url = line.substring(0, sep_location);
+                var shortcut = line.substring(sep_location + 1).trim();
+
+                map[shortcut] = url;
+            }
+        }
+    } catch (e) {
+        // Maybe the file doesn't exist, etc.  Ignore.
+        printerr(e);
+    }
+
+    return map;
+};
+
+var shortcuts_changed_cb = function (file_monitor, file, other_file,
+                                     event_type, user_data) {
+    if (event_type === Gio.FileMonitorEvent.CHANGES_DONE_HINT) {
+        shortcut_to_bookmark_map = get_map_from_file(file);
+    }
+};
+
+var edit_bookmark_shortcuts_cb = function (action, window) {
+    // Comment out the code to spawn programs on the correct screen
+    // because as of the end of 2009, missing annotations in GDK will
+    // cause a crash.
+    //
+    var screen = null; // window.get_screen();
+    var deskbar_extn = Gio.file_new_for_path(
+        "/usr/lib/deskbar-applet/modules-2.20-compatible/epiphany.py");
+
+    if (deskbar_extn.query_exists()) {
+        // launch Deskbar's editor
+        var python_commands =
+            "import sys\n" +
+            "sys.path.insert(0," +
+            "    '/usr/lib/deskbar-applet/modules-2.20-compatible')\n" +
+            "import epiphany\n" +
+            "e = epiphany.EpiphanySearchHandler()\n" +
+            "e.initialize()\n" +
+            "e.show_config(None)\n";
+
+        // Gdk.spawn_on_screen(screen, null, ["python", "-c", python_commands],
+        //                     null, GLib.SpawnFlags.SEARCH_PATH, null, null,
+        //                     null);
+        GLib.spawn_async(null, ["python", "-c", python_commands],
+                        null, GLib.SpawnFlags.SEARCH_PATH, null, null,
+                        null);
+    } else {
+        var dialog = new Gtk.MessageDialog({
+            text: "The file format is a series of lines, each of the form:\n" +
+                "\n" +
+                "url shortcut\n" +
+                "\n" +
+                "Each “url” should contain “%s” in the position the search " +
+                "terms should go."});
+        dialog.add_button(Gtk.STOCK_CANCEL,
+                          Gtk.ResponseType.CANCEL);
+        dialog.add_button("Open Editor", Gtk.ResponseType.OK).grab_focus();
+
+        var response = dialog.run();
+        dialog.destroy();
+
+        if (response !== Gtk.ResponseType.OK) {
+            return;
+        }
+
+        var shortcuts_file = Gio.file_new_for_path(shortcuts_file_name);
+        try {
+            shortcuts_file.create();
+        } catch (e) {
+        }
+        Gtk.show_uri(screen, shortcuts_file.get_uri(), Gdk.CURRENT_TIME);
+    }
+};
+
+var resolve_address_cb = function (bookmarks, address, content) {
+    address = address.trim();
+    var n = address.indexOf(" ");
+    var shortcut;
+    var args;
+
+    if (n === -1) {
+        shortcut = address;
+        args = "";
+    } else {
+        shortcut = address.substring(0, n);
+        args = address.substring(n+1);
+    }
+
+    bookmark = shortcut_to_bookmark_map[shortcut];
+    if (bookmark === undefined) {
+        return null;
+    } else {
+        return bookmark.replace("%s", args);
+    }
+};
+
+var initialize_extension = function () {
+    bookmarks = Epiphany.EphyShell.get_default().get_bookmarks();
+    bookmarks._bookmark_shortcuts_signal = bookmarks.signal.
+        resolve_address.connect(resolve_address_cb);
+
+    var file = Gio.file_new_for_path(shortcuts_file_name);
+    try {
+        shortcuts_file_monitor = file.monitor();
+        shortcuts_file_monitor.signal.changed.connect(shortcuts_changed_cb);
+        shortcut_to_bookmark_map = get_map_from_file(file);
+    } catch (e) {
+    }
+};
+
+var finalize_extension = function () {
+    bookmarks = Epiphany.EphyShell.get_default().get_bookmarks();
+    bookmarks.signal.disconnect(bookmarks._bookmark_shortcuts_signal);
+    delete bookmarks._bookmark_shortcuts_signal;
+    shortcuts_file_monitor = null;
+};
+
+var attached_window_count = 0;
+
+var attach_window = function (window) {
+    // No race condition below because this extension is always called
+    // in the one GUI thread.  Hopefully.
+    if (attached_window_count === 0) {
+        initialize_extension();
+    }
+    attached_window_count++;
+
+    // Add Edit -> Bookmark Shortcuts to this window's menu.
+
+    // Object to hold onto the Edit menu entry, etc., for the window's
+    // lifetime.
+    window._bookmark_shortcuts = {};
+    var wbs = window._bookmark_shortcuts;
+
+    wbs.action = new Gtk.Action({
+        name: "EditBookmarkShortcuts",
+        label: "_Bookmark Shortcuts",
+        tooltip: "Configure shortcuts for your bookmark searches",
+        stock_id: null});
+    wbs.sig_id = wbs.action.signal.activate.connect(edit_bookmark_shortcuts_cb,
+                                                    window);
+    wbs.action_group = new Gtk.ActionGroup({
+        name: "EditBookmarkShortcutsActionGroup"});
+    wbs.action_group.add_action(wbs.action);
+
+    // Unfortunately the following line can trigger:
+    //     Gtk-CRITICAL **: gtk_container_foreach: assertion
+    //     `GTK_IS_CONTAINER (container)' failed
+    //  when we close the window.  This isn't our fault, though.
+    wbs.manager = window.get_ui_manager();
+
+    wbs.manager.insert_action_group(wbs.action_group, 0);
+    wbs.merge_id = wbs.manager.new_merge_id();
+    wbs.manager.add_ui(wbs.merge_id, "/menubar/EditMenu/EditPrefsMenu",
+                       "EditBookmarkShortcutsMenu", "EditBookmarkShortcuts",
+                       Gtk.UIManagerItemType.MENUITEM, true);
+};
+
+var detach_window = function (window) {
+    // Remove our Edit menu entry.
+    // Is this necessary?  Well, it doesn't hurt /too/ much, at least.
+    var wbs = window._bookmark_shortcuts;
+
+    wbs.manager.remove_ui(wbs.merge_id);
+    wbs.manager.remove_action_group(wbs.action_group);
+    wbs.action_group.remove_action(wbs.action);
+    wbs.action.signal.disconnect(wbs.sig_id);
+    delete window._bookmark_shortcuts;
+
+    // Cleanup the rest of the extension if necessary.
+    attached_window_count--;
+    if (attached_window_count === 0) {
+        finalize_extension();
+    }
+};
+
+extension = {
+    attach_window: attach_window,
+    detach_window: detach_window
+};
diff --git a/bookmark-shortcuts.py b/bookmark-shortcuts.py
deleted file mode 100644 (file)
index bff0b52..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-#!/usr/bin/env python
-# (c) 2006-10-17 by Nigel Tao <nigeltao@gnome.org>, GPL licensed.
-#
-# Shortcuts for Epiphany's searches, shared with the deskbar applet.
-
-
-import epiphany, gnomevfs, os
-
-
-def load_map():
-       global sc_bm_map, shortcuts_file
-       sc_bm_map.clear()
-       try:
-               for line in file(shortcuts_file):
-                       line = line.strip()
-                       if len(line) > 0:
-                               try:
-                                       url, shortcut = line.split()
-                                       sc_bm_map[shortcut] = url
-                               except ValueError:
-                                       # Ignore badly formatted lines.
-                                       pass
-       except IOError:
-               # The file probably does not exist.  Ignore the error.
-               pass
-
-
-def on_vfs_monitor(monitored_uri, altered_uri, event_type):
-       load_map()
-
-
-def resolve_address_cb(bookmarks, address, smart_bookmark_content):
-       global sc_bm_map
-       address = address.strip()
-       n = address.find(' ')
-       if n == -1:
-               shortcut, args = address, ''
-       else:
-               shortcut, args = address[:n], address[n+1:]
-       try:
-               return sc_bm_map[shortcut].replace('%s', args)
-       except KeyError:
-               return None
-
-
-def extension_initialize():
-       global gnomevfs_id, shortcuts_file
-       bookmarks = epiphany.ephy_shell_get_default().get_bookmarks()
-       # bmsc stands for BookMark ShortCut.
-       bookmarks._bmsc_signal = bookmarks.connect('resolve-address',
-                                                  resolve_address_cb)
-       load_map()
-       gnomevfs_id = gnomevfs.monitor_add('file://' + shortcuts_file,
-                                          gnomevfs.MONITOR_FILE,
-                                          on_vfs_monitor)
-
-
-def extension_finalize():
-       global gnomevfs_id
-       bookmarks = epiphany.ephy_shell_get_default().get_bookmarks()
-       signal = bookmarks._bmsc_signal
-       del bookmarks._bmsc_signal
-       bookmarks.disconnect(signal)
-       if gnomevfs_id != None:
-               gnomevfs.monitor_cancel(gnomevfs_id)
-               gnomevfs_id = None
-
-
-def attach_window(window):
-       # I don't think that there is a race condition with this a_w_c variable,
-       # since this code all happens in the GUI thread...
-       global attached_window_count
-       if attached_window_count == 0:
-               extension_initialize()
-       attached_window_count = attached_window_count + 1
-
-
-def detach_window(window):
-       global attached_window_count
-       attached_window_count = attached_window_count - 1
-       if attached_window_count == 0:
-               extension_finalize()
-
-
-shortcuts_file = os.path.expanduser(
-                 "~/.gnome2/deskbar-applet/search-bookmarks-shortcuts.txt")
-attached_window_count = 0
-gnomevfs_id = None
-sc_bm_map = {}  # map from ShortCuts to (smart) BookMarks
diff --git a/bookmark-shortcuts.xml b/bookmark-shortcuts.xml
deleted file mode 100644 (file)
index 4510718..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<extension>
-       <name>Bookmark shortcuts</name>
-       <description>Bookmark shortcuts, just like the deskbar applet</description>
-       <loader type="python">
-               <attribute name="module">bookmark-shortcuts</attribute>
-       </loader>
-</extension>