[wiican/gnome3porting: 2/3] [m] PyGTK2 to PyGI GTK 3 Step 2 Wash, rinse, repeat



commit 9fc6701cfc317b4c747f6285f522fbdb6b2313fc
Author: J. FÃlix OntaÃÃn <fontanon emergya es>
Date:   Wed Aug 15 22:56:07 2012 +0200

    [m] PyGTK2 to PyGI GTK 3 Step 2 Wash, rinse, repeat

 bin/wiican                  |   26 ++++++++++++++------------
 wiican/service/wiifinder.py |   24 ++++++++++++------------
 wiican/ui/editordlg.py      |   30 ++++++++++++++++--------------
 wiican/ui/managerdlg.py     |   35 +++++++++++++++++++----------------
 wiican/ui/pnganimation.py   |    2 +-
 wiican/utils.py             |   16 ++++++++++------
 6 files changed, 72 insertions(+), 61 deletions(-)
---
diff --git a/bin/wiican b/bin/wiican
index 63c3f9a..b3b2156 100755
--- a/bin/wiican
+++ b/bin/wiican
@@ -26,9 +26,10 @@ import os
 
 from gi.repository import Gtk
 from gi.repository import GObject
-import dbus, dbus.exceptions
+from gi.repository import GdkPixbuf
+from gi.repository import AppIndicator3
 
-import appindicator
+import dbus, dbus.exceptions
 
 import tarfile
 import tempfile
@@ -50,17 +51,18 @@ DISCONNECT_MENUITEM_POSITION = 4
 # The mapping manager single instance
 mapping_manager = MappingManager()
 
-class WiicanIndicator(appindicator.Indicator, GConfStore):
+class WiicanIndicator(GConfStore):
     defaults = {
             'show_manager_on_startup': True
     }
 
     def __init__(self):
-        appindicator.Indicator.__init__(self, "wiican", "wiican",
-            appindicator.CATEGORY_HARDWARE)
         GConfStore.__init__(self, GCONF_KEY)
 
-        self.set_status (appindicator.STATUS_ACTIVE)
+        self.indicator = AppIndicator3.Indicator.new("wiican", "wiican",
+            AppIndicator3.IndicatorCategory.HARDWARE)
+
+        self.indicator.set_status (AppIndicator3.IndicatorStatus.ACTIVE)
         self.menu_mappings = []
 
         # Load UI
@@ -75,7 +77,7 @@ class WiicanIndicator(appindicator.Indicator, GConfStore):
         self.error_menuitem = builder.get_object('error_menuitem')
         self.disconnect_menuitem = builder.get_object('disconnect_menuitem')
 
-        self.set_menu(self.main_menu)
+        self.indicator.set_menu(self.main_menu)
         self.aboutdlg.connect('response', lambda d, r: d.hide())
         self.disconnect_menuitem.connect('activate', self.__discover_cb, -1)
 
@@ -118,7 +120,7 @@ class WiicanIndicator(appindicator.Indicator, GConfStore):
         self.__cur_status = new_status
 
     def __set_no_bluetooth_st(self):
-        self.set_icon('wiican-off')
+        self.indicator.set_icon('wiican-off')
         self.error_menuitem.get_child().set_text(_('Plug a bluetooth adapter'))
         self.error_menuitem.set_visible(True)
         self.disconnect_menuitem.set_sensitive(False)
@@ -126,7 +128,7 @@ class WiicanIndicator(appindicator.Indicator, GConfStore):
             mapping_menuitem.set_sensitive(False)
 
     def __set_no_uinput_st(self):
-        self.set_icon('wiican-off')
+        self.indicator.set_icon('wiican-off')
         self.error_menuitem.get_child().set_text(_('Please load uinput module first'))
         self.error_menuitem.set_visible(True)
         self.disconnect_menuitem.set_sensitive(False)
@@ -136,7 +138,7 @@ class WiicanIndicator(appindicator.Indicator, GConfStore):
     def __idle_st(self):
         self.disconnect_menuitem.set_sensitive(False)
         self.error_menuitem.set_visible(False)
-        self.set_icon('wiican-on')
+        self.indicator.set_icon('wiican-on')
         for mapping_menuitem in self.menu_mappings:
             mapping_menuitem.set_sensitive(True)
 
@@ -145,7 +147,7 @@ class WiicanIndicator(appindicator.Indicator, GConfStore):
             if not self.__cur_status & WC_WIIMOTE_DISCOVERING:
                 return False
             else:
-                self.set_icon(self.__animation.next())
+                self.indicator.set_icon(self.__animation.next())
                 return True
 
         self.disconnect_menuitem.set_sensitive(True)
@@ -171,7 +173,7 @@ class WiicanIndicator(appindicator.Indicator, GConfStore):
             icon = GdkPixbuf.Pixbuf.new_from_file_at_size(mapping.get_icon(), 16, 16)
             menuitem = Gtk.ImageMenuItem(mapping.get_name())
             menuitem.set_tooltip_text(mapping.get_comment())
-            menuitem.set_image(Gtk.image_new_from_pixbuf(icon))
+            menuitem.set_image(Gtk.Image.new_from_pixbuf(icon))
             menuitem.connect('activate', self.__discover_cb, mapping)
             if self.__cur_status & WC_WIIMOTE_DISCOVERING: menuitem.set_sensitive(False)
             menuitem.show()
diff --git a/wiican/service/wiifinder.py b/wiican/service/wiifinder.py
index 52a79a8..c4f6937 100644
--- a/wiican/service/wiifinder.py
+++ b/wiican/service/wiifinder.py
@@ -27,7 +27,7 @@ WIIMOTE_DEVICE_NAME = '"Nintendo Wiimote"'
 
 class WiimoteDevice(object):
     """A simple object representing a Wiimote device."""
-    
+
     def __init__(self, device):
         """Create a new input Wiimote device
             
@@ -39,7 +39,7 @@ class WiimoteDevice(object):
     @property
     def nice_label(self):
         return self.device.get_sysfs_attr('name')
-    
+
     @property
     def path(self):
         """Get the sysfs_path for this device
@@ -54,20 +54,20 @@ class WiimoteFinder(GObject.GObject):
     An object that will find and monitor Wiimote devices on your 
     machine and emit signals when are connected / disconnected
     """
-    
+
     __gsignals__ = {
         'connected': (GObject.SignalFlags.RUN_LAST, None, 
             (GObject.TYPE_PYOBJECT,)),
         'disconnected': (GObject.SignalFlags.RUN_LAST, None, 
             (GObject.TYPE_PYOBJECT,)),
     }
-    
+
     def __init__(self):
         """
         Create a new WiimoteFinder and attach to the udev system to 
         listen for events.
         """
-        self.__gobject_init__()
+        GObject.GObject.__init__(self)
 
         self.client = gudev.Client(['input'])
         self.wiimotes = {}
@@ -86,10 +86,10 @@ class WiimoteFinder(GObject.GObject):
             "add": self.device_added,
             "remove": self.device_removed,
         }.get(action, lambda x,y: None)(device)
-    
+
     def device_added(self, device):
         """Called when a device has been added to the system"""
-        
+
         if device.get_property('NAME') == WIIMOTE_DEVICE_NAME:
             path = device.get_sysfs_path()
             self.wiimotes[path] = WiimoteDevice(device)
@@ -97,7 +97,7 @@ class WiimoteFinder(GObject.GObject):
 
     def device_removed(self, device):
         """Called when a device has been removed from the system"""
-        
+
         if device.get_property('NAME') == WIIMOTE_DEVICE_NAME:
             path = device.get_sysfs_path()
             self.emit('disconnected', self.wiimotes[path])
@@ -107,16 +107,16 @@ GObject.type_register(WiimoteFinder)
 
 if __name__ == "__main__":
     from gi.repository import GObject
-    
+
     def found(finder, device):
         print device.path + ": " + device.nice_label
-    
+
     def lost(finder, device):
         print device.path + ": " + device.nice_label
-    
+
     finder = WiimoteFinder()
     finder.connect('connected', found)
     finder.connect('disconnected', lost)
-    
+
     loop = GObject.MainLoop()
     loop.run()
diff --git a/wiican/ui/editordlg.py b/wiican/ui/editordlg.py
index e824c0a..c1a8125 100644
--- a/wiican/ui/editordlg.py
+++ b/wiican/ui/editordlg.py
@@ -20,11 +20,13 @@
 #
 ###
 
-from gi.repository import Gtk
-import webbrowser
+from gi.repository import Gtk, Gdk
+from gi.repository import GdkPixbuf
 from gi.repository import Pango
 from gi.repository import GtkSource
+
 import tempfile
+import webbrowser
 
 import dbus, dbus.exceptions
 from dbus.mainloop.glib import DBusGMainLoop
@@ -62,9 +64,9 @@ class IconChooserDialog(Gtk.FileChooserDialog):
             file_chooser.set_preview_widget_active(have_preview)
             return
 
-        GObject.GObject.__init__(self, title, parent,
-                                Gtk.FileChooserAction.OPEN,
-                                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
+        Gtk.FileChooserDialog.__init__(self, title, parent,
+                Gtk.FileChooserAction.OPEN,
+                        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                 Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
 
         self.set_default_response(Gtk.ResponseType.OK)
@@ -115,7 +117,7 @@ class MappingEditorDialog(object):
         lm = GtkSource.LanguageManager()
         self.mapping_buffer = GtkSource.Buffer()
         self.mapping_buffer.set_data('languages-manager', lm)
-        view = GtkSource.View(self.mapping_buffer)
+        view = GtkSource.View(buffer=self.mapping_buffer)
         view.set_show_line_numbers(True)
         view.set_auto_indent(True)
 
@@ -169,28 +171,28 @@ class MappingEditorDialog(object):
                     self.execute_btn.set_active(True)
                     self.execute_btn.handler_unblock(self.sig_id)
                     self.execute_btn.set_tooltip_text(_('A mapping is running'))
-                    self.execute_btn.set_image(Gtk.image_new_from_pixbuf(wiican_disc3_icon))
+                    self.execute_btn.set_image(Gtk.Image.new_from_pixbuf(wiican_disc3_icon))
                     
             elif new_status == (WC_UINPUT_PRESENT | WC_BLUEZ_PRESENT):
                 self.execute_btn.set_sensitive(True)
                 self.execute_btn.set_tooltip_text(_('Execute this mapping'))
-                self.execute_btn.set_image(Gtk.image_new_from_pixbuf(wiican_on_icon))
+                self.execute_btn.set_image(Gtk.Image.new_from_pixbuf(wiican_on_icon))
                 if self.execute_btn.get_active():
                     self.execute_btn.handler_block(self.sig_id)
                     self.execute_btn.set_active(False)
                     self.execute_btn.handler_unblock(self.sig_id)
             else:
                 self.execute_btn.set_sensitive(False)
-                self.execute_btn.set_image(Gtk.image_new_from_pixbuf(wiican_off_icon))
+                self.execute_btn.set_image(Gtk.Image.new_from_pixbuf(wiican_off_icon))
                 self.execute_btn.set_tooltip_text(_('Ensure a bluetooth ' \
                     +'adapter its available and uinput module its loaded'))
 
         self.wiican_iface.connect_to_signal('StatusChanged', wiican_status_changed, 
             dbus_interface='org.gnome.Wiican')
-            
+
         status = self.wiican_iface.GetStatus()
         wiican_status_changed(status)
-       
+
         self.notificator = Notificator('wiican')
 
     def changed_cb(self, widget, data=None):
@@ -198,7 +200,7 @@ class MappingEditorDialog(object):
         self.mapping_buffer.remove_all_tags(start, end)
 
         start, end = self.mapping_buffer.get_bounds()
-        self.validator.validate(self.mapping_buffer.get_text(start, end), 
+        self.validator.validate(self.mapping_buffer.get_text(start, end, True), 
             halt_on_errors=False)
 
         for error in self.validator.validation_errors:
@@ -225,7 +227,7 @@ class MappingEditorDialog(object):
 
     def get_mapping(self):
         start, end = self.mapping_buffer.get_bounds()
-        mapping_code = self.mapping_buffer.get_text(start, end)
+        mapping_code = self.mapping_buffer.get_text(start, end, True)
         #FIXME: If mapping doesn't ends with \n wminput prompts segfault
         if not mapping_code.endswith('\n'): mapping_code += '\n'
 
@@ -244,7 +246,7 @@ class MappingEditorDialog(object):
     def execute_btn_clicked_cb(self, widget):
         if self.execute_btn.get_active():
             start, end = self.mapping_buffer.get_bounds()
-            mapping_code = self.mapping_buffer.get_text(start, end)
+            mapping_code = self.mapping_buffer.get_text(start, end, True)
             #FIXME: If mapping doesn't ends with \n wminput prompts segfault
             if not mapping_code.endswith('\n'): mapping_code += '\n'
 
diff --git a/wiican/ui/managerdlg.py b/wiican/ui/managerdlg.py
index 1828159..47aaba0 100644
--- a/wiican/ui/managerdlg.py
+++ b/wiican/ui/managerdlg.py
@@ -22,6 +22,8 @@
 
 import os.path
 from gi.repository import Gtk
+from gi.repository import Gdk
+from gi.repository import GdkPixbuf
 
 from wiican.defs import *
 from wiican.mapping import Mapping, MappingManager, MappingManagerError
@@ -120,7 +122,7 @@ class MappingManagerDialog(object):
             mapping_name = '<b>%s</b>\n<i>%s</i>' % (mapping.get_name(), 
                     mapping.get_comment())
             self.mapping_store.append([icon, None, mapping_name,
-                mapping.get_comment(), True, None, mapping_id])
+                mapping.get_comment(), True, mapping_id])
 
         mapping_editor_dlg.destroy()
 
@@ -145,7 +147,7 @@ class MappingManagerDialog(object):
                     mapping_name = '<b>%s</b>\n<i>%s</i>' % (mapping.get_name(), 
                             mapping.get_comment())
                     self.mapping_store.append([icon, None, mapping_name,
-                        mapping.get_comment(), True, None, mapping_id])
+                        mapping.get_comment(), True, mapping_id])
                 else:
                     mapping_manager[mapping_id] = new_mapping
                     model[selected][ICON_COL] = GdkPixbuf.Pixbuf.new_from_file_at_size(new_mapping.get_icon(), 
@@ -161,16 +163,17 @@ class MappingManagerDialog(object):
 
     def mapping_list_cursor_changed_cb(self, user_data):
         selection = self.mapping_list.get_selection()
-        model, selected = selection.get_selected()
+        if selection: 
+            model, selected = selection.get_selected()
 
-        if selected is not None:
-            mapping_id = model[selected][MAPPING_ID_COL]
-            is_def_mapping = mapping_manager.is_default_mapping(mapping_id)
+            if selected is not None:
+                mapping_id = model[selected][MAPPING_ID_COL]
+                is_def_mapping = mapping_manager.is_default_mapping(mapping_id)
 
-            # Hack? Prevent triggering autostart_toggle_action_toggled_cb
-            self.autostart_ttb.handler_block(self.autostart_toggle_signal_id)
-            self.autostart_ttb.set_active(is_def_mapping)
-            self.autostart_ttb.handler_unblock(self.autostart_toggle_signal_id)
+                # Hack? Prevent triggering autostart_toggle_action_toggled_cb
+                self.autostart_ttb.handler_block(self.autostart_toggle_signal_id)
+                self.autostart_ttb.set_active(is_def_mapping)
+                self.autostart_ttb.handler_unblock(self.autostart_toggle_signal_id)
 
     def delete_action_activate_cb(self, widget):
         selection = self.mapping_list.get_selection()
@@ -308,11 +311,11 @@ class MappingManagerDialog(object):
                 mapping_manager.set_default_mapping(mapping_id)
 
     def up_btn_clicked_cb(self, widget):
-        # From PyGTK FAQ Entry 13.51
+        # Based in PyGTK FAQ Entry 13.51
         # http://faq.pyGtk.org/index.py?req=show&file=faq13.051.htp
         def iter_prev(iter, model):
             path = model.get_path(iter)
-            position = path[-1]
+            position = path.get_indices()[-1]
             if position == 0:
                 return None
             prev_path = list(path)[:-1]
@@ -323,8 +326,8 @@ class MappingManagerDialog(object):
         selection = self.mapping_list.get_selection()
         model, selected = selection.get_selected()
         if selected is not None:
-            selected_row = model.get_path(selected)[0]
-            if selected_row > 0:
+            selected_row = model.get_path(selected)
+            if selected_row.get_indices()[0] > 0:
                 prev = iter_prev(selected, model)
                 model.swap(prev, selected)
                 mapping_manager.swap_mapping_order(model[prev][MAPPING_ID_COL], 
@@ -334,8 +337,8 @@ class MappingManagerDialog(object):
         selection = self.mapping_list.get_selection()
         model, selected = selection.get_selected()
         if selected is not None:
-            selected_row = model.get_path(selected)[0]
-            if selected_row < len(model)-1:
+            selected_row = model.get_path(selected)
+            if selected_row.get_indices()[0] < len(model)-1:
                 next = model.iter_next(selected)
                 model.swap(selected, next)
                 mapping_manager.swap_mapping_order(model[selected][MAPPING_ID_COL], 
diff --git a/wiican/ui/pnganimation.py b/wiican/ui/pnganimation.py
index c6abc51..a45cb0f 100644
--- a/wiican/ui/pnganimation.py
+++ b/wiican/ui/pnganimation.py
@@ -20,7 +20,7 @@
 #
 ###
 
-import Gtk.gdk
+from gi.repository import GdkPixbuf
 
 #TODO: Replace this with GdkPixbuf.PixbufAnimation
 class PngAnimation:
diff --git a/wiican/utils.py b/wiican/utils.py
index 6a50f77..dbd3148 100644
--- a/wiican/utils.py
+++ b/wiican/utils.py
@@ -65,8 +65,9 @@ class GConfStore(object):
         casts = {GConf.ValueType.BOOL:   GConf.Value.get_bool,
             GConf.ValueType.INT:    GConf.Value.get_int,
             GConf.ValueType.FLOAT:  GConf.Value.get_float,
-            GConf.ValueType.STRING: GConf.Value.get_string,
-            GConf.ValueType.LIST:   GConf.Value.get_list}
+            GConf.ValueType.STRING: GConf.Value.get_string}
+        # This will be disabled until get_list method is implemented
+        # GConf.ValueType.LIST:   GConf.Value.get_list}
 
         if only_defaults:
             #FIXME: Why appears this message in stderr?
@@ -93,10 +94,13 @@ class GConfStore(object):
         casts = {types.BooleanType: GConf.Client.set_bool,
             types.IntType:     GConf.Client.set_int,
             types.FloatType:   GConf.Client.set_float,
-            types.StringType:  GConf.Client.set_string,
-            types.ListType:    GConf.Client.set_list,
-            types.TupleType:   GConf.Client.set_list,
-            set:               GConf.Client.set_list}
+            types.StringType:  GConf.Client.set_string}
+            # This will be disabled until set_list method is implemented
+        """
+        types.ListType:    GConf.Client.set_list,
+        types.TupleType:   GConf.Client.set_list,
+        set:               GConf.Client.set_list}
+        """
 
         #TODO: To clear the gconf dir before save, is it convenient?
         for name, value in self.options.items():



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