[gedit] Fix various snippets pygi bugs



commit 2dbe5e048766a94b2ba42346b5d197edc92cd03d
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date:   Fri Mar 25 11:55:24 2011 +0100

    Fix various snippets pygi bugs
    
    This fixes the manager to be launched from the menu. Also various functions
    of the manager were not working (accelerators, saving libraries).
    Accelerators in general were broken and are now fixed.

 plugins/snippets/snippets/Makefile.am          |    3 +
 plugins/snippets/snippets/__init__.py          |   73 +---------------------
 plugins/snippets/snippets/appactivatable.py    |   81 ++++++++++++++++++++++++
 plugins/snippets/snippets/library.py           |   16 +++--
 plugins/snippets/snippets/manager.py           |   30 ++++++---
 plugins/snippets/snippets/shareddata.py        |   50 +++++++++++++++
 plugins/snippets/snippets/singleton.py         |   27 ++++++++
 plugins/snippets/snippets/snippets.ui          |   60 +++++++++++++++++-
 plugins/snippets/snippets/windowactivatable.py |   21 +++++--
 9 files changed, 268 insertions(+), 93 deletions(-)
---
diff --git a/plugins/snippets/snippets/Makefile.am b/plugins/snippets/snippets/Makefile.am
index ae46ee9..9466996 100644
--- a/plugins/snippets/snippets/Makefile.am
+++ b/plugins/snippets/snippets/Makefile.am
@@ -3,6 +3,7 @@ plugindir = $(GEDIT_PLUGINS_LIBS_DIR)/snippets
 
 plugin_PYTHON = \
 	__init__.py \
+	appactivatable.py \
 	windowactivatable.py \
 	document.py \
 	library.py \
@@ -11,6 +12,8 @@ plugin_PYTHON = \
 	placeholder.py \
 	manager.py \
 	helper.py \
+	singleton.py \
+	shareddata.py \
 	substitutionparser.py \
 	importer.py \
 	exporter.py \
diff --git a/plugins/snippets/snippets/__init__.py b/plugins/snippets/snippets/__init__.py
index c20bf41..2e27417 100644
--- a/plugins/snippets/snippets/__init__.py
+++ b/plugins/snippets/snippets/__init__.py
@@ -15,78 +15,7 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-import sys
-import os
-import shutil
-
-import cairo
-import glib
-
-from gi.repository import Gtk, Gdk, Gedit, PeasGtk, GObject
-import platform
-
-from library import Library
-from manager import Manager
-from snippet import Snippet
-
+from appactivatable import AppActivatable
 from windowactivatable import WindowActivatable
 
-class AppActivatable(GObject.Object, Gedit.AppActivatable, PeasGtk.Configurable):
-        __gtype_name__ = "GeditSnippetsAppActivatable"
-
-        app = GObject.property(type=Gedit.App)
-
-        def __init__(self):
-                GObject.Object.__init__(self)
-
-                self.dlg = None
-
-        def do_activate(self):
-                # Initialize snippets library
-                library = Library()
-                library.set_accelerator_callback(self.accelerator_activated)
-
-                if platform.platform() == 'Windows':
-                        snippetsdir = os.path.expanduser('~/gedit/snippets')
-                else:
-                        userdir = os.getenv('GNOME22_USER_DIR')
-                        if userdir:
-                                snippetsdir = os.path.join(userdir, 'gedit/snippets')
-                        else:
-                                snippetsdir = os.path.join(glib.get_user_config_dir(), 'gedit/snippets')
-
-                library.set_dirs(snippetsdir, self.system_dirs())
-
-        def system_dirs(self):
-                if platform.platform() != 'Windows':
-                        if 'XDG_DATA_DIRS' in os.environ:
-                                datadirs = os.environ['XDG_DATA_DIRS']
-                        else:
-                                datadirs = '/usr/local/share' + os.pathsep + '/usr/share'
-
-                        dirs = []
-
-                        for d in datadirs.split(os.pathsep):
-                                d = os.path.join(d, 'gedit', 'plugins', 'snippets')
-
-                                if os.path.isdir(d):
-                                        dirs.append(d)
-
-                dirs.append(self.plugin_info.get_data_dir())
-                return dirs
-
-        def do_create_configure_widget(self):
-                builder = Gtk.Builder()
-                builder.add_from_file(os.path.join(self.plugin_info.get_data_dir(), 'ui', 'snippets.ui'))
-
-                return builder.get_object('snippets_manager')
-
-        def accelerator_activated(self, group, obj, keyval, mod):
-                ret = False
-
-#                if hasattr(obj, '_snippets_plugin_data'):
-#                        ret = obj._snippets_plugin_data.accelerator_activated(keyval, mod)
-
-                return ret
-
 # ex:ts=8:et:
diff --git a/plugins/snippets/snippets/appactivatable.py b/plugins/snippets/snippets/appactivatable.py
new file mode 100644
index 0000000..533d704
--- /dev/null
+++ b/plugins/snippets/snippets/appactivatable.py
@@ -0,0 +1,81 @@
+#    Gedit snippets plugin
+#    Copyright (C) 2005-2006  Jesse van den Kieboom <jesse icecrew nl>
+#
+#    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, write to the Free Software
+#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+import sys
+import os
+import shutil
+
+import glib
+
+from gi.repository import Gedit, GObject, Gtk
+import platform
+
+from library import Library
+from manager import Manager
+
+class AppActivatable(GObject.Object, Gedit.AppActivatable):
+        __gtype_name__ = "GeditSnippetsAppActivatable"
+
+        app = GObject.property(type=Gedit.App)
+
+        def __init__(self):
+                GObject.Object.__init__(self)
+
+        def do_activate(self):
+                # Initialize snippets library
+                library = Library()
+
+                if platform.platform() == 'Windows':
+                        snippetsdir = os.path.expanduser('~/gedit/snippets')
+                else:
+                        userdir = os.getenv('GNOME22_USER_DIR')
+                        if userdir:
+                                snippetsdir = os.path.join(userdir, 'gedit/snippets')
+                        else:
+                                snippetsdir = os.path.join(glib.get_user_config_dir(), 'gedit/snippets')
+
+                library.set_dirs(snippetsdir, self.system_dirs())
+
+        def system_dirs(self):
+                if platform.platform() != 'Windows':
+                        if 'XDG_DATA_DIRS' in os.environ:
+                                datadirs = os.environ['XDG_DATA_DIRS']
+                        else:
+                                datadirs = '/usr/local/share' + os.pathsep + '/usr/share'
+
+                        dirs = []
+
+                        for d in datadirs.split(os.pathsep):
+                                d = os.path.join(d, 'gedit', 'plugins', 'snippets')
+
+                                if os.path.isdir(d):
+                                        dirs.append(d)
+
+                dirs.append(self.plugin_info.get_data_dir())
+                return dirs
+
+        def accelerator_activated(self, group, obj, keyval, mod):
+                activatable = SharedData().lookup_window_activatable(obj)
+
+                ret = False
+
+                if activatable:
+                        ret = activatable.accelerator_activated(keyval, mod)
+
+                return ret
+
+# vi:ex:ts=8:et
diff --git a/plugins/snippets/snippets/library.py b/plugins/snippets/snippets/library.py
index e477719..fe48a6a 100644
--- a/plugins/snippets/snippets/library.py
+++ b/plugins/snippets/snippets/library.py
@@ -641,7 +641,7 @@ class Singleton(object):
 
 class Library(Singleton):
         def __init_once__(self):
-                self._accelerator_activated_cb = None
+                self._accelerator_activated_cb = []
                 self.loaded = False
                 self.check_buffer = Gtk.TextBuffer()
 
@@ -656,14 +656,20 @@ class Library(Singleton):
 
                 self.loaded = False
 
-        def set_accelerator_callback(self, cb):
-                self._accelerator_activated_cb = cb
+        def add_accelerator_callback(self, cb):
+                self._accelerator_activated_cb.append(cb)
+
+        def remove_accelerator_callback(self, cb):
+                self._accelerator_activated_cb.remove(cb)
 
         def accelerator_activated(self, group, obj, keyval, mod):
                 ret = False
 
-                if self._accelerator_activated_cb:
-                        ret = self._accelerator_activated_cb(group, obj, keyval, mod)
+                for cb in self._accelerator_activated_cb:
+                        ret = cb(group, obj, keyval, mod)
+
+                        if ret:
+                                break
 
                 return ret
 
diff --git a/plugins/snippets/snippets/manager.py b/plugins/snippets/snippets/manager.py
index fe0dece..70ee740 100644
--- a/plugins/snippets/snippets/manager.py
+++ b/plugins/snippets/snippets/manager.py
@@ -29,7 +29,7 @@ from exporter import *
 from document import Document
 from languagemanager import get_language_manager
 
-class Manager(Gtk.VBox, Gtk.Buildable):
+class Manager(Gtk.Dialog, Gtk.Buildable):
         NAME_COLUMN = 0
         SORT_COLUMN = 1
         LANG_COLUMN = 2
@@ -48,12 +48,9 @@ class Manager(Gtk.VBox, Gtk.Buildable):
                 self.snippet = None
                 self._temp_export = None
                 self.snippets_doc = None
-                self.manager = None
 
                 self.key_press_id = 0
 
-                self.set_size_request(600, 400)
-
         def get_language_snippets(self, path, name = None):
                 library = Library()
 
@@ -579,6 +576,11 @@ class Manager(Gtk.VBox, Gtk.Buildable):
 
         # Callbacks
         def do_destroy(self):
+                Gtk.Dialog.do_destroy(self)
+
+                if not self.model:
+                        return
+
                 # Remove temporary drag export
                 if self._temp_export:
                       shutil.rmtree(os.path.dirname(self._temp_export))
@@ -587,11 +589,17 @@ class Manager(Gtk.VBox, Gtk.Buildable):
                 if self.snippets_doc:
                         self.snippets_doc.stop()
 
-                self.manager = None
                 self.unref_languages()
                 self.snippet = None
                 self.model = None
 
+        def do_response(self, resp):
+                if resp == Gtk.ResponseType.HELP:
+                        Gedit.App.get_default().show_help(self, 'gedit', 'gedit-plugins-snippets')
+                        return
+
+                self.destroy()
+
         def on_cell_editing_started(self, renderer, editable, path):
                 piter = self.model.get_iter(path)
 
@@ -945,11 +953,12 @@ class Manager(Gtk.VBox, Gtk.Buildable):
                 parent = self.model.iter_parent(piter)
 
                 Library().remove_snippet(node)
+                idx = path.get_indices()
 
                 if self.model.remove(piter):
                         return piter
-                elif path[-1] != 0:
-                        self.select_iter(self.model.get_iter((path[0], path[1] - 1)))
+                elif idx[-1] != 0:
+                        self.select_iter(self.model.get_iter((idx[0], idx[1] - 1)))
                 else:
                         dummy = self.add_new_snippet_node(parent)
                         self.tree_view.expand_row(self.model.get_path(parent), False)
@@ -974,7 +983,8 @@ class Manager(Gtk.VBox, Gtk.Buildable):
                 # Create tree row references
                 references = []
                 for path in paths:
-                        references.append(Gtk.TreeRowReference(self.model, path))
+                        # FIXME: this should be fixed in pygobject or something
+                        references.append(Gtk.TreeRowReference.new(self.model, path))
 
                 # Remove/revert snippets
                 select = None
@@ -1016,10 +1026,10 @@ class Manager(Gtk.VBox, Gtk.Buildable):
 
                         self.snippet_changed()
                         return True
-                elif Library().valid_accelerator(event.keyval, event.get_state()[1]):
+                elif Library().valid_accelerator(event.keyval, event.get_state()):
                         # New accelerator
                         self.set_accelerator(event.keyval, \
-                                        event.get_state()[1] & Gtk.accelerator_get_default_mod_mask())
+                                        event.get_state() & Gtk.accelerator_get_default_mod_mask())
                         entry.set_text(self.snippet.accelerator_display())
                         self.snippet_changed()
                         self.tree_view.grab_focus()
diff --git a/plugins/snippets/snippets/shareddata.py b/plugins/snippets/snippets/shareddata.py
new file mode 100644
index 0000000..f1d8f0d
--- /dev/null
+++ b/plugins/snippets/snippets/shareddata.py
@@ -0,0 +1,50 @@
+#    Gedit snippets plugin
+#    Copyright (C) 2011  Jesse van den Kieboom <jessevdk 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, write to the Free Software
+#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+from singleton import Singleton
+import os
+
+from gi.repository import Gtk
+
+class SharedData(object):
+    __metaclass__ = Singleton
+
+    def __init__(self):
+        self.dlg = None
+        self.dlg_default_size = None
+
+    def manager_destroyed(self, dlg):
+        self.dlg_default_size = [dlg.get_allocation().width, dlg.get_allocation().height]
+        self.dlg = None
+
+    def show_manager(self, window, datadir):
+        if not self.dlg:
+            builder = Gtk.Builder()
+            builder.add_from_file(os.path.join(datadir, 'ui', 'snippets.ui'))
+
+            self.dlg = builder.get_object('snippets_manager')
+            self.dlg.connect('destroy', self.manager_destroyed)
+
+            if self.dlg_default_size:
+                self.dlg.set_default_size(self.dlg_default_size[0], self.dlg_default_size[1])
+
+        if window:
+            self.dlg.set_transient_for(window)
+
+        self.dlg.present()
+
+# vi:ex:ts=4:et
diff --git a/plugins/snippets/snippets/singleton.py b/plugins/snippets/snippets/singleton.py
new file mode 100644
index 0000000..9bc346d
--- /dev/null
+++ b/plugins/snippets/snippets/singleton.py
@@ -0,0 +1,27 @@
+#    Gedit snippets plugin
+#    Copyright (C) 2011  Jesse van den Kieboom <jessevdk 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, write to the Free Software
+#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+class Singleton(type):
+    def __init__(cls, name, bases, dict):
+        super(Singleton, cls).__init__(name, bases, dict)
+        cls.instance = None
+
+    def __call__(cls, *args, **kw):
+        if cls.instance is None:
+            cls.instance = super(Singleton, cls).__call__(*args, **kw)
+
+        return cls.instance
diff --git a/plugins/snippets/snippets/snippets.ui b/plugins/snippets/snippets/snippets.ui
index c3bcf55..5f954f3 100644
--- a/plugins/snippets/snippets/snippets.ui
+++ b/plugins/snippets/snippets/snippets.ui
@@ -35,10 +35,62 @@
   <object class="GeditDocument" id="source_buffer">
     <property name="highlight-matching-brackets">True</property>
   </object>
-      <object class="GeditSnippetsManager" id="snippets_manager">
+  <object class="GeditSnippetsManager" id="snippets_manager">
+    <property name="title" translatable="yes">Snippets Manager</property>
+    <property name="type">GTK_WINDOW_TOPLEVEL</property>
+    <property name="window_position">GTK_WIN_POS_NONE</property>
+    <property name="visible">True</property>
+    <property name="modal">False</property>
+    <property name="default_width">750</property>
+    <property name="default_height">500</property>
+    <property name="resizable">True</property>
+    <property name="destroy_with_parent">True</property>
+    <property name="decorated">True</property>
+    <property name="skip_taskbar_hint">True</property>
+    <property name="skip_pager_hint">False</property>
+    <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
+    <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
+    <property name="focus_on_map">True</property>
+    <property name="urgency_hint">False</property>
+    <child internal-child="vbox">
+      <object class="GtkVBox" id="dialog-vbox1">
         <property name="visible">True</property>
         <property name="homogeneous">False</property>
         <property name="spacing">0</property>
+        <child internal-child="action_area">
+          <object class="GtkHButtonBox" id="dialog-action_area1">
+            <property name="visible">True</property>
+            <property name="layout_style">GTK_BUTTONBOX_END</property>
+            <child>
+              <object class="GtkButton" id="closebutton1">
+                <property name="visible">True</property>
+                <property name="can_default">True</property>
+                <property name="can_focus">True</property>
+                <property name="label">gtk-close</property>
+                <property name="use_stock">True</property>
+                <property name="relief">GTK_RELIEF_NORMAL</property>
+                <property name="focus_on_click">True</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkButton" id="button1">
+                <property name="visible">True</property>
+                <property name="can_default">True</property>
+                <property name="can_focus">True</property>
+                <property name="label">gtk-help</property>
+                <property name="use_stock">True</property>
+                <property name="relief">GTK_RELIEF_NORMAL</property>
+                <property name="focus_on_click">True</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="padding">0</property>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="pack_type">GTK_PACK_END</property>
+          </packing>
+        </child>
         <child>
           <object class="GtkHPaned" id="hpaned_paned">
             <property name="border_width">6</property>
@@ -585,4 +637,10 @@
           </packing>
         </child>
       </object>
+    </child>
+    <action-widgets>
+      <action-widget response="-7">closebutton1</action-widget>
+      <action-widget response="-11">button1</action-widget>
+    </action-widgets>
+  </object>
 </interface>
diff --git a/plugins/snippets/snippets/windowactivatable.py b/plugins/snippets/snippets/windowactivatable.py
index 5ff8f9a..9b50ed0 100644
--- a/plugins/snippets/snippets/windowactivatable.py
+++ b/plugins/snippets/snippets/windowactivatable.py
@@ -20,10 +20,10 @@ import os
 import gettext
 
 from gi.repository import Gtk, Gdk, Gedit, GObject
-import gobject
 
 from document import Document
 from library import Library
+from shareddata import SharedData
 
 class Activate(Gedit.Message):
         view = GObject.property(type=Gedit.View)
@@ -45,6 +45,9 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
                 self.insert_menu()
                 self.register_messages()
 
+                library = Library()
+                library.add_accelerator_callback(self.accelerator_activated)
+
                 self.accel_group = Library().get_accel_group(None)
 
                 if self.accel_group:
@@ -74,6 +77,9 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
                                 view._snippet_controller.stop()
                                 view._snippet_controller = None
 
+                library = Library()
+                library.remove_accelerator_callback(self.accelerator_activated)
+
         def do_update_state(self):
                 view = self.window.get_active_view()
 
@@ -201,11 +207,16 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
 
                 self.do_update_state()
 
+        def create_configure_dialog(self):
+                SharedData().show_manager(self.window, self.plugin_info.get_data_dir())
+
         def on_action_snippets_activate(self, action, data):
-                #self.plugin.create_configure_dialog()
-                pass
+                self.create_configure_dialog()
 
-        def accelerator_activated(self, keyval, mod):
-                return self.current_controller.accelerator_activate(keyval, mod)
+        def accelerator_activated(self, group, obj, keyval, mod):
+                if obj == self.window:
+                        return self.current_controller.accelerator_activate(keyval, mod)
+                else:
+                        return False
 
 # ex:ts=8:et:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]