[gedit-plugins] Port sessionsaver to libpeas/gtk3



commit 757e3da43fbb5bf9a9ba779a68dbbab9da65cfaf
Author: Kenny Meyer <knny myer gmail com>
Date:   Thu Dec 9 20:58:00 2010 -0300

    Port sessionsaver to libpeas/gtk3

 plugins/sessionsaver/__init__.py     |  198 +++++++++++++++++-----------------
 plugins/sessionsaver/dialogs.py      |   65 ++++++-----
 plugins/sessionsaver/sessionsaver.ui |    9 +--
 plugins/sessionsaver/store.py        |   25 +++--
 4 files changed, 148 insertions(+), 149 deletions(-)
---
diff --git a/plugins/sessionsaver/__init__.py b/plugins/sessionsaver/__init__.py
index 560dc65..41f1326 100644
--- a/plugins/sessionsaver/__init__.py
+++ b/plugins/sessionsaver/__init__.py
@@ -3,6 +3,7 @@
 # This file is part of gedit Session Saver Plugin
 #
 # Copyright (C) 2006-2007 - Steve Frécinaux <code istique net>
+# Copyright (C) 2010 - Kenny Meyer <knny myer gmail com>
 #
 # gedit Session Saver Plugin is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as published by
@@ -19,149 +20,146 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor,
 # Boston, MA  02110-1301  USA
 
-import gobject
-import gtk
-import gedit
+from gi.repository import GObject, Gio, Gtk, Gedit
 import os.path
 import gettext
 from store import XMLSessionStore
 from dialogs import SaveSessionDialog, SessionManagerDialog
+from gpdefs import *
 
 try:
-    from gpdefs import *
     gettext.bindtextdomain(GETTEXT_PACKAGE, GP_LOCALEDIR)
     _ = lambda s: gettext.dgettext(GETTEXT_PACKAGE, s);
 except:
     _ = lambda s: s
 
-class SessionSaverWindowHelper(object):
+ui_str = """
+<ui>
+  <menubar name="MenuBar">
+    <menu name="FileMenu" action="File">
+      <placeholder name="FileOps_2">
+        <separator/>
+        <menu name="FileSessionMenu" action="FileSession">
+          <placeholder name="SessionPluginPlaceHolder"/>
+          <separator/>
+          <menuitem name="FileSessionSaveMenu" action="FileSessionSave"/>
+          <menuitem name="FileSessionManageMenu" action="FileSessionManage"/>
+        </menu>
+      </placeholder>
+    </menu>
+  </menubar>
+</ui>
+"""
+
+class SessionSaverPlugin(GObject.Object, Gedit.WindowActivatable):
+    __gtype_name__ = "SessionSaverPlugin"
+
+    window = GObject.property(type = Gedit.Window)
+
     ACTION_HANDLER_DATA_KEY = "SessionSaverActionHandlerData"
     SESSION_MENU_PATH = '/MenuBar/FileMenu/FileOps_2/FileSessionMenu/SessionPluginPlaceHolder'
-    SESSION_MENU_UI_STRING = """
-        <ui>
-          <menubar name="MenuBar">
-            <menu name="FileMenu" action="File">
-              <placeholder name="FileOps_2">
-                <separator/>
-                <menu name="FileSessionMenu" action="FileSession">
-                  <placeholder name="SessionPluginPlaceHolder"/>
-                  <separator/>
-                  <menuitem name="FileSessionSaveMenu" action="FileSessionSave"/>
-                  <menuitem name="FileSessionManageMenu" action="FileSessionManage"/>
-                </menu>
-              </placeholder>
-            </menu>
-          </menubar>
-        </ui>"""
-
-    def __init__(self, plugin, window):
-        self.plugin = plugin
-        self.window = window
-        manager = window.get_ui_manager()
-
-        self._menu_action_group = gtk.ActionGroup("SessionSaverPluginActions")
-        self._menu_action_group.add_actions(
-            (("FileSession", None, _("Sa_ved sessions")),
-             ("FileSessionSave", gtk.STOCK_SAVE_AS, _("_Save current session"), None, _("Save the current document list as a new session"), self.on_save_session_action),
-             ("FileSessionManage", None, _("_Manage saved sessions..."), None, _("Open the saved session manager"), self.on_manage_sessions_action)))
-        manager.insert_action_group(self._menu_action_group, -1)
-        self._menu_ui_id = manager.add_ui_from_string(self.SESSION_MENU_UI_STRING)
-
-        self._ui_id = 0
-        self._action_group = gtk.ActionGroup("SessionSaverPluginSessionActions")
-        manager.insert_action_group(self._action_group, -1)
-        self.update_session_menu()
-
-        manager.ensure_update()
-
-    def on_save_session_action(self, action):
-        SaveSessionDialog(self.window, self.plugin).run()
 
-    def on_manage_sessions_action(self, action):
-        SessionManagerDialog(self.plugin).run()
+    def __init__(self):
+        GObject.Object.__init__(self)
+        self.sessions = XMLSessionStore()
 
-    def session_menu_action(self, action, session):
-        self.plugin.load_session(session, self.window)
+    def do_activate(self):
+        self._insert_menu()
 
-    def remove_session_menu(self):
-        if self._ui_id != 0:
-            self.window.get_ui_manager().remove_ui(self._ui_id)
-            self._ui_id = 0
+    def do_deactivate(self):
+        self._remove_menu()
 
-        for action in self._action_group.list_actions():
-            handler = action.get_data(self.ACTION_HANDLER_DATA_KEY)
-            if handler is not None:
-                action.disconnect(handler)
-            self._action_group.remove_action(action)
+    def do_update_state(self):
+        pass
 
-    def update_session_menu(self):
+    def _update_session_menu(self):
         manager = self.window.get_ui_manager()
 
-        self.remove_session_menu()
-        self._ui_id = manager.new_merge_id()
+        self._menu_ui_id = manager.new_merge_id()
 
-        i = 0
-        for session in self.plugin.sessions:
+        for i, session in enumerate(self.sessions):
             action_name = 'SessionSaver%X' % i
-            action = gtk.Action(action_name, session.name, _("Recover '%s' session") % session.name, None)
+            action = Gtk.Action(action_name,
+                                session.name,
+                                _("Recover '%s' session") % session.name,
+                                "")
             handler = action.connect("activate", self.session_menu_action, session)
 
             action.set_data(self.ACTION_HANDLER_DATA_KEY, handler)
+            # Add an action to the session list items.
             self._action_group.add_action(action)
 
-            manager.add_ui(self._ui_id, self.SESSION_MENU_PATH,
-                           action_name, action_name,
-                           gtk.UI_MANAGER_MENUITEM, False)
-            i += 1
+            manager.add_ui(self._ui_id,
+                           self.SESSION_MENU_PATH,
+                           action_name,
+                           action_name,
+                           Gtk.UIManagerItemType.MENUITEM,
+                           False)
 
-    def update_ui(self):
-        pass
+    def _insert_menu(self):
+        manager = self.window.get_ui_manager()
+        self._menu_action_group = Gtk.ActionGroup("SessionSaverPluginActions")
+        self._menu_action_group.add_actions(
+            [("FileSession", None, _("Sa_ved sessions"), None, None),
+             ("FileSessionSave", Gtk.STOCK_SAVE_AS,
+              _("_Save current session"), None,
+              _("Save the current document list as a new session"),
+              self.on_save_session_action),
+             ("FileSessionManage", None, _("_Manage saved sessions..."),
+              None, _("Open the saved session manager"),
+              self.on_manage_sessions_action)
+            ], self.window)
+        manager.insert_action_group(self._menu_action_group)
+
+        self._ui_id = manager.add_ui_from_string(ui_str)
+        self._action_group = Gtk.ActionGroup(name="SessionSaverPluginSessionActions")
+        manager.insert_action_group(self._action_group)
+
+        self._update_session_menu()
 
-    def deactivate(self):
+        manager.ensure_update()
+
+    def _remove_session_menu(self):
         manager = self.window.get_ui_manager()
         manager.remove_ui(self._menu_ui_id)
-        manager.remove_action_group(self._menu_action_group)
-        self.remove_session_menu()
-        manager.remove_action_group(self._action_group)
-
 
-class SessionSaverPlugin(gedit.Plugin):
-    WINDOW_DATA_KEY = "SessionSaverWindowData"
+        for action in self._action_group.list_actions():
+            handler = action.get_data(self.ACTION_HANDLER_DATA_KEY)
+            if handler is not None:
+                action.disconnect(handler)
+            self._action_group.remove_action(action)
 
-    def __init__(self):
-        super(SessionSaverPlugin, self).__init__()
-        self.sessions = XMLSessionStore()
+        manager.remove_action_group(self._action_group)
 
-    def activate(self, window):
-        helper = SessionSaverWindowHelper(self, window)
-        window.set_data(self.WINDOW_DATA_KEY, helper)
+    def _remove_menu(self):
+        manager = self.window.get_ui_manager()
 
-    def deactivate(self, window):
-        window.get_data(self.WINDOW_DATA_KEY).deactivate()
-        window.set_data(self.WINDOW_DATA_KEY, None)
+        self._remove_session_menu()
 
-    def update_ui(self, window):
-        window.get_data(self.WINDOW_DATA_KEY).update_ui()
+        manager.remove_ui(self._ui_id)
+        manager.remove_action_group(self._menu_action_group)
 
-    def update_session_menu(self):
-        for window in gedit.app_get_default().get_windows():
-            window.get_data(self.WINDOW_DATA_KEY).update_session_menu()
+        manager.ensure_update()
 
-    def load_session(self, session, window = None):
+    def _load_session(self, session, window):
         # Note: a session has to stand on its own window.
-        app = gedit.app_get_default()
-
-        if window is None:
-            window = app.get_active_window()
-
         tab = window.get_active_tab()
         if tab is not None and \
            not (tab.get_document().is_untouched() and \
-                tab.get_state() == gedit.TAB_STATE_NORMAL):
-            window = app.create_window()
+                tab.get_state() == Gedit.TabState.STATE_NORMAL):
+            # Create a new gedit window
+            window = Gedit.App.get_default().create_window(None)
             window.show()
 
-        gedit.commands.load_uris(window, session.files, None, 0)
+        Gedit.commands_load_locations(window, session.files, None, 0, 0)
 
-# ex:ts=4:et:
+    def on_save_session_action(self, action, window):
+        SaveSessionDialog(window, self.plugin_info, self.sessions, self).run()
+
+    def on_manage_sessions_action(self, action, window):
+        SessionManagerDialog(self.plugin_info, self.sessions).run()
 
+    def session_menu_action(self, action, session):
+        self._load_session(session, self.window)
+
+# ex:ts=4:et:
diff --git a/plugins/sessionsaver/dialogs.py b/plugins/sessionsaver/dialogs.py
index cdf1df0..fb454b5 100644
--- a/plugins/sessionsaver/dialogs.py
+++ b/plugins/sessionsaver/dialogs.py
@@ -1,27 +1,26 @@
 # -*- coding: utf-8 -*-
 # Copyright (c) 2007 - Steve Frécinaux <code istique net>
+# Copyright (c) 2010 - Kenny Meyer <knny myer gmail com>
 # Licence: GPL2 or later
 
-import gobject
-import gedit
-import gtk
+from gi.repository import GObject, Gtk, Gedit
 import os.path
 import gettext
+
 from store import Session
 
 try:
     from gpdefs import *
     gettext.bindtextdomain(GETTEXT_PACKAGE, GP_LOCALEDIR)
-    gtk.glade.bindtextdomain(GETTEXT_PACKAGE, GP_LOCALEDIR)
     _ = lambda s: gettext.dgettext(GETTEXT_PACKAGE, s);
 except:
     _ = lambda s: s
 
-class SessionModel(gtk.GenericTreeModel):
+class SessionModel(Gtk.ListStore):
     OBJECT_COLUMN = 0
     NAME_COLUMN = 1
     N_COLUMNS = 2
-    column_types = (gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
+    column_types = (GObject.TYPE_PYOBJECT, GObject.TYPE_STRING)
 
     def __init__(self, store):
         super(SessionModel, self).__init__()
@@ -43,7 +42,7 @@ class SessionModel(gtk.GenericTreeModel):
         self.row_deleted(self.on_get_path(piter))
 
     def on_get_flags(self):
-        return gtk.TREE_MODEL_LIST_ONLY
+        return Gtk.TREE_MODEL_LIST_ONLY
 
     def on_get_n_columns(self):
         return self.N_COLUMNS
@@ -102,12 +101,12 @@ class Dialog(object):
         super(Dialog, self).__init__()
 
         if parent_window is None:
-            parent_window = gedit.app_get_default().get_active_window()
+            parent_window = Gedit.App.get_default().get_active_window()
         self.parent = parent_window
 
-        self.ui = gtk.Builder()
+        self.ui = Gtk.Builder()
+        self.ui.set_translation_domain(GETTEXT_PACKAGE)
         self.ui.add_from_file(os.path.join(datadir, self.UI_FILE))
-        self.ui.set_translation_domain(domain=GETTEXT_PACKAGE)
         self.dialog = self.ui.get_object(main_widget)
         self.dialog.connect('delete-event', self.on_delete_event)
 
@@ -130,41 +129,48 @@ class Dialog(object):
         self.__del__()
 
 class SaveSessionDialog(Dialog):
-    def __init__(self, window, plugin):
-        super(SaveSessionDialog, self).__init__('save-session-dialog', plugin.get_data_dir(), window)
+    def __init__(self, window, plugin, sessions, sessionsaver):
+        super(SaveSessionDialog, self).__init__('save-session-dialog',
+                                                plugin.get_data_dir(),
+                                                window)
         self.plugin = plugin
+        self.sessions = sessions
+        self.sessionsaver = sessionsaver
 
-        model = SessionModel(plugin.sessions)
+        model = SessionModel(sessions)
 
         combobox = self['session-name']
         combobox.set_model(model)
-        combobox.set_text_column(1)
+        combobox.set_entry_text_column(1)
 
         self.dialog.connect('response', self.on_response)
 
     def on_response(self, dialog, response_id):
-        if response_id == gtk.RESPONSE_OK:
-            files = [doc.get_uri()
+        if response_id == Gtk.ResponseType.OK:
+            files = [doc.get_location()
                         for doc in self.parent.get_documents()
-                        if doc.get_uri() is not None]
-            name = self['session-name'].child.get_text()
-            self.plugin.sessions.add(Session(name, files))
-            self.plugin.sessions.save()
-        self.plugin.update_session_menu()
+                        if doc.get_location() is not None]
+            name = self['session-name'].get_child().get_text()
+            self.sessions.add(Session(name, files))
+            self.sessions.save()
+            self.sessionsaver.sessions = self.sessions
+            self.sessionsaver._update_session_menu()
         self.destroy()
 
 class SessionManagerDialog(Dialog):
-    def __init__(self, plugin):
-        super(SessionManagerDialog, self).__init__('session-manager-dialog', plugin.get_data_dir())
+    def __init__(self, plugin, sessions):
+        super(SessionManagerDialog, self).__init__('session-manager-dialog',
+                                                   plugin.get_data_dir())
         self.plugin = plugin
+        self.sessions = sessions
 
-        model = SessionModel(plugin.sessions)
+        model = SessionModel(sessions)
 
         self.view = self['session-view']
         self.view.set_model(model)
 
-        renderer = gtk.CellRendererText()
-        column = gtk.TreeViewColumn(_("Session Name"), renderer, text = model.NAME_COLUMN)
+        renderer = Gtk.CellRendererText()
+        column = Gtk.TreeViewColumn(_("Session Name"), renderer, text = model.NAME_COLUMN)
         self.view.append_column(column)
 
         handlers = {
@@ -176,7 +182,7 @@ class SessionManagerDialog(Dialog):
 
     def on_delete_event(self, dialog, event):
         dialog.hide()
-        self.plugin.sessions.save()
+        self.sessions.save()
         return True
 
     def get_current_session(self):
@@ -193,12 +199,11 @@ class SessionManagerDialog(Dialog):
 
     def on_delete_button_clicked(self, button):
         session = self.get_current_session()
-        self.plugin.sessions.remove(session)
+        self.sessions.remove(session)
         self.plugin.update_session_menu()
 
     def on_close_button_clicked(self, button):
-        self.plugin.sessions.save()
+        self.sessions.save()
         self.destroy()
 
 # ex:ts=4:et:
-
diff --git a/plugins/sessionsaver/sessionsaver.ui b/plugins/sessionsaver/sessionsaver.ui
index cbad7e8..6bb72c2 100644
--- a/plugins/sessionsaver/sessionsaver.ui
+++ b/plugins/sessionsaver/sessionsaver.ui
@@ -11,7 +11,6 @@
     <property name="title" translatable="yes">Save session</property>
     <property name="resizable">False</property>
     <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
-    <property name="has_separator">False</property>
     <child internal-child="vbox">
       <object class="GtkVBox" id="dialog-vbox1">
         <property name="visible">True</property>
@@ -31,13 +30,9 @@
               </packing>
             </child>
             <child>
-              <object class="GtkComboBoxEntry" id="session-name">
+              <object class="GtkComboBox" id="session-name">
                 <property name="visible">True</property>
-                <child internal-child="entry">
-                  <object class="GtkEntry" id="comboboxentry-entry1">
-                    <property name="visible">True</property>
-                  </object>
-                </child>
+                <property name="has-entry">True</property>
               </object>
               <packing>
                 <property name="expand">False</property>
diff --git a/plugins/sessionsaver/store.py b/plugins/sessionsaver/store.py
index 2925e4d..1938015 100644
--- a/plugins/sessionsaver/store.py
+++ b/plugins/sessionsaver/store.py
@@ -3,6 +3,7 @@
 # This file is part of gedit Session Saver Plugin
 #
 # Copyright (C) 2006-2007 - Steve Frécinaux <code istique net>
+# Copyright (C) 2010 - Kenny Meyer <knny myer gmail com>
 #
 # gedit Session Saver Plugin is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as published by
@@ -21,7 +22,8 @@
 
 import os.path
 from xml.parsers import expat
-import gobject
+from gi.repository import GObject
+import glib
 
 class Session(object):
     def __init__(self, name, files = None):
@@ -35,22 +37,22 @@ class Session(object):
         return cmp(self.name.lower(), session.name.lower())
 
     def add_file(self, filename):
-        self.files.append(filename)
+        self.files.append(Gio.file_new_for_path(filename))
 
-class SessionStore(gobject.GObject):
+class SessionStore(GObject.Object):
     __gsignals__ = {
-        "session-added":    (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                             (gobject.TYPE_PYOBJECT,)),
-        "session-changed":  (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                             (gobject.TYPE_PYOBJECT,)),
-        "session-removed":  (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                            (gobject.TYPE_PYOBJECT,))
+        "session-added":    (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
+                             (GObject.TYPE_PYOBJECT,)),
+        "session-changed":  (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
+                             (GObject.TYPE_PYOBJECT,)),
+        "session-removed":  (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
+                            (GObject.TYPE_PYOBJECT,))
     }
 
     _instance = None
     def __new__(cls):
         if cls._instance is None:
-            cls._instance = gobject.GObject.__new__(cls)
+            cls._instance = GObject.Object.__new__(cls)
         return cls._instance
 
     def __init__(self):
@@ -99,7 +101,7 @@ class SessionStore(gobject.GObject):
 class XMLSessionStore(SessionStore):
     def __init__(self):
         super(XMLSessionStore, self).__init__()
-        self.filename = os.path.expanduser('~/.gnome2/gedit/saved-sessions.xml')
+        self.filename = os.path.join(glib.get_user_config_dir(), 'gedit/saved-sessions.xml')
         self.load()
 
     def _escape(self, string):
@@ -155,4 +157,3 @@ class XMLSessionStore(SessionStore):
             self._current_session = None
 
 # ex:ts=4:et:
-



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