Fixing up some packaging issues
[gonvert] / src / gonvert_glade.py
index 6dbb51d..8034d58 100755 (executable)
@@ -16,7 +16,6 @@ OR display everything in engineering notation
 
 import os
 import pickle
 
 import os
 import pickle
-import gettext
 import logging
 
 import pango
 import logging
 
 import pango
@@ -29,14 +28,22 @@ import constants
 import hildonize
 import unit_data
 
 import hildonize
 import unit_data
 
+try:
+       import gettext
+except ImportError:
+       _ = lambda x: x
+       gettext = None
+else:
+       _ = gettext.gettext
+
 
 _moduleLogger = logging.getLogger("gonvert_glade")
 PROFILE_STARTUP = False
 FORCE_HILDON_LIKE = False
 
 
 _moduleLogger = logging.getLogger("gonvert_glade")
 PROFILE_STARTUP = False
 FORCE_HILDON_LIKE = False
 
-gettext.bindtextdomain('gonvert', '/usr/share/locale')
-gettext.textdomain('gonvert')
-_ = gettext.gettext
+if gettext is not None:
+       gettext.bindtextdomain('gonvert', '/usr/share/locale')
+       gettext.textdomain('gonvert')
 
 
 def change_menu_label(widgets, labelname, newtext):
 
 
 def change_menu_label(widgets, labelname, newtext):
@@ -50,6 +57,7 @@ class Gonvert(object):
                os.path.join(os.path.dirname(__file__), "gonvert.glade"),
                os.path.join(os.path.dirname(__file__), "../data/gonvert.glade"),
                os.path.join(os.path.dirname(__file__), "../lib/gonvert.glade"),
                os.path.join(os.path.dirname(__file__), "gonvert.glade"),
                os.path.join(os.path.dirname(__file__), "../data/gonvert.glade"),
                os.path.join(os.path.dirname(__file__), "../lib/gonvert.glade"),
+               '/usr/share/gonvert/gonvert.glade',
                '/usr/lib/gonvert/gonvert.glade',
        ]
 
                '/usr/lib/gonvert/gonvert.glade',
        ]
 
@@ -86,12 +94,6 @@ class Gonvert(object):
                self._mainWindow = widgets.get_widget('mainWindow')
                self._app = hildonize.get_app_class()()
                self._mainWindow = hildonize.hildonize_window(self._app, self._mainWindow)
                self._mainWindow = widgets.get_widget('mainWindow')
                self._app = hildonize.get_app_class()()
                self._mainWindow = hildonize.hildonize_window(self._app, self._mainWindow)
-               for scrollingWidgetName in (
-                       "unitsViewScrolledWindow",
-               ):
-                       scrollingWidget = widgets.get_widget(scrollingWidgetName)
-                       assert scrollingWidget is not None, scrollingWidgetName
-                       hildonize.hildonize_scrollwindow_with_viewport(scrollingWidget)
 
                change_menu_label(widgets, 'fileMenuItem', _('File'))
                change_menu_label(widgets, 'exitMenuItem', _('Exit'))
 
                change_menu_label(widgets, 'fileMenuItem', _('File'))
                change_menu_label(widgets, 'exitMenuItem', _('Exit'))
@@ -106,43 +108,31 @@ class Gonvert(object):
 
                self._categorySelectionButton = widgets.get_widget("categorySelectionButton")
                self._categoryView = widgets.get_widget('categoryView')
 
                self._categorySelectionButton = widgets.get_widget("categorySelectionButton")
                self._categoryView = widgets.get_widget('categoryView')
-               if hildonize.IS_HILDON_SUPPORTED or FORCE_HILDON_LIKE:
-                       self._categoryView.get_parent().hide()
-               else:
-                       self._categorySelectionButton.hide()
 
                self._unitsView = widgets.get_widget('unitsView')
                self._unitsView.set_property('rules_hint', 1)
                self._unitsView_selection = self._unitsView.get_selection()
 
                self._unitsView = widgets.get_widget('unitsView')
                self._unitsView.set_property('rules_hint', 1)
                self._unitsView_selection = self._unitsView.get_selection()
-               if hildonize.IS_HILDON_SUPPORTED or FORCE_HILDON_LIKE:
-                       self._unitsView.set_headers_visible(False)
 
                self._unitName = widgets.get_widget('unitName')
                self._unitValue = widgets.get_widget('unitValue')
                self._previousUnitName = widgets.get_widget('previousUnitName')
                self._previousUnitValue = widgets.get_widget('previousUnitValue')
 
                self._unitName = widgets.get_widget('unitName')
                self._unitValue = widgets.get_widget('unitValue')
                self._previousUnitName = widgets.get_widget('previousUnitName')
                self._previousUnitValue = widgets.get_widget('previousUnitValue')
-               if hildonize.IS_HILDON_SUPPORTED or FORCE_HILDON_LIKE:
-                       self._previousUnitName.get_parent().hide()
 
                self._unitSymbol = widgets.get_widget('unitSymbol')
                self._previousUnitSymbol = widgets.get_widget('previousUnitSymbol')
 
                self._unitDescription = widgets.get_widget('unitDescription')
 
                self._unitSymbol = widgets.get_widget('unitSymbol')
                self._previousUnitSymbol = widgets.get_widget('previousUnitSymbol')
 
                self._unitDescription = widgets.get_widget('unitDescription')
-               if hildonize.IS_HILDON_SUPPORTED or FORCE_HILDON_LIKE:
-                       self._unitDescription.get_parent().get_parent().hide()
 
                self._searchLayout = widgets.get_widget('searchLayout')
                self._searchLayout.hide()
                self._findEntry = widgets.get_widget('findEntry')
                self._findLabel = widgets.get_widget('findLabel')
                self._findButton = widgets.get_widget('findButton')
 
                self._searchLayout = widgets.get_widget('searchLayout')
                self._searchLayout.hide()
                self._findEntry = widgets.get_widget('findEntry')
                self._findLabel = widgets.get_widget('findLabel')
                self._findButton = widgets.get_widget('findButton')
-               ToolTips = gtk.Tooltips()
-               ToolTips.set_tip(self._findButton, _(u'Find unit (F6)'))
 
                #insert a self._categoryColumnumn into the units list even though the heading will not be seen
                renderer = gtk.CellRendererText()
                renderer.set_property("ellipsize", pango.ELLIPSIZE_END)
 
                #insert a self._categoryColumnumn into the units list even though the heading will not be seen
                renderer = gtk.CellRendererText()
                renderer.set_property("ellipsize", pango.ELLIPSIZE_END)
-               renderer.set_property("width-chars", len("grams per cubic cm"))
+               renderer.set_property("width-chars", len("grams per cubic cm plus some"))
                hildonize.set_cell_thumb_selectable(renderer)
                self._unitNameColumn = gtk.TreeViewColumn(_('Name'), renderer)
                self._unitNameColumn.set_property('resizable', 1)
                hildonize.set_cell_thumb_selectable(renderer)
                self._unitNameColumn = gtk.TreeViewColumn(_('Name'), renderer)
                self._unitNameColumn.set_property('resizable', 1)
@@ -152,8 +142,17 @@ class Gonvert(object):
                self._unitsView.append_column(self._unitNameColumn)
 
                renderer = gtk.CellRendererText()
                self._unitsView.append_column(self._unitNameColumn)
 
                renderer = gtk.CellRendererText()
+               hildonize.set_cell_thumb_selectable(renderer)
+               self._unitValueColumn = gtk.TreeViewColumn(_('Value'), renderer)
+               self._unitValueColumn.set_property('resizable', 1)
+               self._unitValueColumn.add_attribute(renderer, 'text', self.UNITS_VALUE_IDX)
+               self._unitValueColumn.set_clickable(True)
+               self._unitValueColumn.connect("clicked", self._on_click_unit_column)
+               self._unitsView.append_column(self._unitValueColumn)
+
+               renderer = gtk.CellRendererText()
                renderer.set_property("ellipsize", pango.ELLIPSIZE_END)
                renderer.set_property("ellipsize", pango.ELLIPSIZE_END)
-               renderer.set_property("width-chars", len("G ohm"))
+               renderer.set_property("width-chars", len("G ohm plus some"))
                hildonize.set_cell_thumb_selectable(renderer)
                self._unitSymbolColumn = gtk.TreeViewColumn(_('Units'), renderer)
                self._unitSymbolColumn.set_property('resizable', 1)
                hildonize.set_cell_thumb_selectable(renderer)
                self._unitSymbolColumn = gtk.TreeViewColumn(_('Units'), renderer)
                self._unitSymbolColumn.set_property('resizable', 1)
@@ -162,15 +161,6 @@ class Gonvert(object):
                self._unitSymbolColumn.connect("clicked", self._on_click_unit_column)
                self._unitsView.append_column(self._unitSymbolColumn)
 
                self._unitSymbolColumn.connect("clicked", self._on_click_unit_column)
                self._unitsView.append_column(self._unitSymbolColumn)
 
-               renderer = gtk.CellRendererText()
-               hildonize.set_cell_thumb_selectable(renderer)
-               self._unitValueColumn = gtk.TreeViewColumn(_('Value'), renderer)
-               self._unitValueColumn.set_property('resizable', 1)
-               self._unitValueColumn.add_attribute(renderer, 'text', self.UNITS_VALUE_IDX)
-               self._unitValueColumn.set_clickable(True)
-               self._unitValueColumn.connect("clicked", self._on_click_unit_column)
-               self._unitsView.append_column(self._unitValueColumn)
-
                self._unitModel = gtk.ListStore(
                        gobject.TYPE_STRING, # UNITS_NAME_IDX
                        gobject.TYPE_STRING, # UNITS_VALUE_IDX
                self._unitModel = gtk.ListStore(
                        gobject.TYPE_STRING, # UNITS_NAME_IDX
                        gobject.TYPE_STRING, # UNITS_VALUE_IDX
@@ -200,25 +190,38 @@ class Gonvert(object):
                        self._categoryModel.append(row)
 
                #--------- connections to GUI ----------------
                        self._categoryModel.append(row)
 
                #--------- connections to GUI ----------------
-               dic = {
-                       "on_exit_menu_activate": self._on_user_exit,
-                       "on_main_window_destroy": self._on_user_exit,
-                       "on_categoryView_select_row": self._on_click_category,
-                       "on_unitValue_changed": self._on_unit_value_changed,
-                       "on_previousUnitValue_changed": self._on_previous_unit_value_changed,
-                       "on_findButton_clicked": self._on_find_activate,
-                       "on_findEntry_activated": self._on_find_activate,
-                       "on_findEntry_changed": self._on_findEntry_changed,
-                       "on_aboutMenuItem_activate": self._on_about_clicked,
-                       "on_clearSelectionMenuItem_activate": self._on_user_clear_selections,
-                       "on_unitsView_cursor_changed": self._on_click_unit,
-                       "on_shortlistcheck_toggled": self._on_shortlist_changed,
-                       "on_toggleShortList_activate": self._on_edit_shortlist,
-               }
-               widgets.signal_autoconnect(dic)
+               self._mainWindow.connect("delete-event", self._on_user_exit)
                self._mainWindow.connect("key-press-event", self._on_key_press)
                self._mainWindow.connect("window-state-event", self._on_window_state_change)
                self._categorySelectionButton.connect("clicked", self._on_category_selector_clicked)
                self._mainWindow.connect("key-press-event", self._on_key_press)
                self._mainWindow.connect("window-state-event", self._on_window_state_change)
                self._categorySelectionButton.connect("clicked", self._on_category_selector_clicked)
+               self._categoryView.connect("cursor-changed", self._on_click_category)
+               self._findButton.connect("clicked", self._on_find_activate)
+               self._findEntry.connect("activate", self._on_find_activate)
+               self._findEntry.connect("changed", self._on_findEntry_changed)
+               self._previousUnitValue.connect("changed", self._on_previous_unit_value_changed)
+               self._shortlistcheck.connect("toggled", self._on_shortlist_changed)
+               self._unitValue.connect("changed", self._on_unit_value_changed)
+               self._unitsView.connect("cursor-changed", self._on_click_unit)
+               if hildonize.GTK_MENU_USED:
+                       widgets.get_widget("aboutMenuItem").connect("activate", self._on_about_clicked)
+                       widgets.get_widget("clearSelectionMenuItem").connect("activate", self._on_user_clear_selections)
+                       widgets.get_widget("editShortListMenuItem").connect("activate", self._on_edit_shortlist)
+                       widgets.get_widget("exitMenuItem").connect("activate", self._on_user_exit)
+
+               for scrollingWidgetName in (
+                       "unitsViewScrolledWindow",
+               ):
+                       scrollingWidget = widgets.get_widget(scrollingWidgetName)
+                       assert scrollingWidget is not None, scrollingWidgetName
+                       hildonize.hildonize_scrollwindow_with_viewport(scrollingWidget)
+
+               if hildonize.IS_HILDON_SUPPORTED or FORCE_HILDON_LIKE:
+                       self._categoryView.get_parent().hide()
+                       self._unitsView.set_headers_visible(False)
+                       self._previousUnitName.get_parent().hide()
+                       self._unitDescription.get_parent().get_parent().hide()
+               else:
+                       self._categorySelectionButton.hide()
 
                replacementButtons = []
                menu = hildonize.hildonize_menu(
 
                replacementButtons = []
                menu = hildonize.hildonize_menu(
@@ -226,6 +229,7 @@ class Gonvert(object):
                        widgets.get_widget("mainMenuBar"),
                        replacementButtons
                )
                        widgets.get_widget("mainMenuBar"),
                        replacementButtons
                )
+
                if not hildonize.IS_HILDON_SUPPORTED:
                        _moduleLogger.info("No hildonization support")
 
                if not hildonize.IS_HILDON_SUPPORTED:
                        _moduleLogger.info("No hildonization support")
 
@@ -239,16 +243,18 @@ class Gonvert(object):
                        _moduleLogger.warn("Error: Could not find gonvert icon: %s" % iconPath)
 
                self._load_settings()
                        _moduleLogger.warn("Error: Could not find gonvert icon: %s" % iconPath)
 
                self._load_settings()
+               self._mainWindow.show()
 
        def _load_settings(self):
                #Restore window size from previously saved settings if it exists and is valid.
                windowDatPath = "/".join((constants._data_path_, "window.dat"))
                if os.path.exists(windowDatPath):
 
        def _load_settings(self):
                #Restore window size from previously saved settings if it exists and is valid.
                windowDatPath = "/".join((constants._data_path_, "window.dat"))
                if os.path.exists(windowDatPath):
-                       #Retrieving previous window settings from ~/.gonvert/window.dat
                        saved_window = pickle.load(open(windowDatPath, "r"))
                        saved_window = pickle.load(open(windowDatPath, "r"))
-                       #If the 'size' has been stored, then extract size from saved_window.
-                       if 'size' in saved_window:
+                       try:
                                a, b = saved_window['size']
                                a, b = saved_window['size']
+                       except KeyError:
+                               pass
+                       else:
                                self._mainWindow.resize(a, b)
 
                #Restore selections from previously saved settings if it exists and is valid.
                                self._mainWindow.resize(a, b)
 
                #Restore selections from previously saved settings if it exists and is valid.
@@ -256,16 +262,17 @@ class Gonvert(object):
                selectedCategoryName = unit_data.UNIT_CATEGORIES[0]
                selectionsDatPath = "/".join((constants._data_path_, "selections.dat"))
                if os.path.exists(selectionsDatPath):
                selectedCategoryName = unit_data.UNIT_CATEGORIES[0]
                selectionsDatPath = "/".join((constants._data_path_, "selections.dat"))
                if os.path.exists(selectionsDatPath):
-                       #Retrieving previous selections from ~/.gonvert/selections.dat
                        selections = pickle.load(open(selectionsDatPath, 'r'))
                        selections = pickle.load(open(selectionsDatPath, 'r'))
-                       #Restoring previous selections.
-                       #If the 'selected_unts' has been stored, then extract self._defaultUnitForCategory from selections.
-                       if 'selected_units' in selections:
+                       try:
                                self._defaultUnitForCategory = selections['selected_units']
                                self._defaultUnitForCategory = selections['selected_units']
-                       #Make sure that the 'self._selectedCategoryName' has been stored.
-                       if 'selected_category' in selections:
-                               #Match an available category to the previously selected category.
+                       except KeyError:
+                               pass
+
+                       try:
                                selectedCategoryName = selections['selected_category']
                                selectedCategoryName = selections['selected_category']
+                       except KeyError:
+                               pass
+                       else:
                                try:
                                        categoryIndex = unit_data.UNIT_CATEGORIES.index(selectedCategoryName)
                                except ValueError:
                                try:
                                        categoryIndex = unit_data.UNIT_CATEGORIES.index(selectedCategoryName)
                                except ValueError:
@@ -493,13 +500,13 @@ class Gonvert(object):
                try:
                        raise NotImplementedError("%s" % self._shortlistcheck.get_active())
                except Exception:
                try:
                        raise NotImplementedError("%s" % self._shortlistcheck.get_active())
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_shortlist_changed")
 
        def _on_edit_shortlist(self, *args):
                try:
                        raise NotImplementedError("%s" % self._toggleShortList.get_active())
                except Exception:
 
        def _on_edit_shortlist(self, *args):
                try:
                        raise NotImplementedError("%s" % self._toggleShortList.get_active())
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_edit_shortlist")
 
        def _on_user_clear_selections(self, *args):
                try:
 
        def _on_user_clear_selections(self, *args):
                try:
@@ -507,7 +514,7 @@ class Gonvert(object):
                        os.remove(selectionsDatPath)
                        self._defaultUnitForCategory = {}
                except Exception:
                        os.remove(selectionsDatPath)
                        self._defaultUnitForCategory = {}
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_user_clear_selections")
 
        def _on_key_press(self, widget, event, *args):
                """
 
        def _on_key_press(self, widget, event, *args):
                """
@@ -530,7 +537,7 @@ class Gonvert(object):
                        elif event.keyval == gtk.keysyms.n and event.get_state() & gtk.gdk.CONTROL_MASK:
                                self._find_next()
                except Exception, e:
                        elif event.keyval == gtk.keysyms.n and event.get_state() & gtk.gdk.CONTROL_MASK:
                                self._find_next()
                except Exception, e:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_key_press")
 
        def _on_window_state_change(self, widget, event, *args):
                """
 
        def _on_window_state_change(self, widget, event, *args):
                """
@@ -542,7 +549,7 @@ class Gonvert(object):
                        else:
                                self._isFullScreen = False
                except Exception, e:
                        else:
                                self._isFullScreen = False
                except Exception, e:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_window_state_change")
 
        def _on_findEntry_changed(self, *args):
                """
 
        def _on_findEntry_changed(self, *args):
                """
@@ -551,14 +558,14 @@ class Gonvert(object):
                try:
                        self._clear_find()
                except Exception:
                try:
                        self._clear_find()
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_findEntry_changed")
 
        def _on_find_activate(self, *args):
                try:
                        self._find_next()
                        self._findButton.grab_focus()
                except Exception:
 
        def _on_find_activate(self, *args):
                try:
                        self._find_next()
                        self._findButton.grab_focus()
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_find_activate")
 
        def _on_click_unit_column(self, col):
                """
 
        def _on_click_unit_column(self, col):
                """
@@ -586,7 +593,7 @@ class Gonvert(object):
                        else:
                                assert False, "Unknown column: %s" % (col.get_title(), )
                except Exception:
                        else:
                                assert False, "Unknown column: %s" % (col.get_title(), )
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_click_unit_column")
 
        def _on_category_selector_clicked(self, *args):
                try:
 
        def _on_category_selector_clicked(self, *args):
                try:
@@ -603,7 +610,7 @@ class Gonvert(object):
                        self._categoryView.set_cursor(newIndex, self._categoryColumn, False)
                        self._categoryView.grab_focus()
                except Exception:
                        self._categoryView.set_cursor(newIndex, self._categoryColumn, False)
                        self._categoryView.grab_focus()
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_category_selector_clicked")
 
        def _on_click_category(self, *args):
                try:
 
        def _on_click_category(self, *args):
                try:
@@ -614,7 +621,7 @@ class Gonvert(object):
                        selectedCategory = self._categoryModel.get_value(iter, 0)
                        self._switch_category(selectedCategory)
                except Exception:
                        selectedCategory = self._categoryModel.get_value(iter, 0)
                        self._switch_category(selectedCategory)
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_click_category")
 
        def _on_click_unit(self, *args):
                try:
 
        def _on_click_unit(self, *args):
                try:
@@ -662,7 +669,7 @@ class Gonvert(object):
                        self._unitValue.grab_focus()
                        self._unitValue.select_region(0, -1)
                except Exception:
                        self._unitValue.grab_focus()
                        self._unitValue.select_region(0, -1)
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_click_unit")
 
        def _on_unit_value_changed(self, *args):
                try:
 
        def _on_unit_value_changed(self, *args):
                try:
@@ -686,7 +693,7 @@ class Gonvert(object):
                                func, arg = self._unitDataInCategory[self._previousUnitName.get_text()][0]
                                self._previousUnitValue.set_text(str(func.from_base(base, arg, )))
                except Exception:
                                func, arg = self._unitDataInCategory[self._previousUnitName.get_text()][0]
                                self._previousUnitValue.set_text(str(func.from_base(base, arg, )))
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_unit_value_changed")
 
        def _on_previous_unit_value_changed(self, *args):
                try:
 
        def _on_previous_unit_value_changed(self, *args):
                try:
@@ -709,7 +716,7 @@ class Gonvert(object):
                        func, arg = self._unitDataInCategory[self._unitName.get_text()][0]
                        self._unitValue.set_text(str(func.from_base(base, arg, )))
                except Exception:
                        func, arg = self._unitDataInCategory[self._unitName.get_text()][0]
                        self._unitValue.set_text(str(func.from_base(base, arg, )))
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_previous_unit_value_changed")
 
        def _on_about_clicked(self, a):
                dlg = gtk.AboutDialog()
 
        def _on_about_clicked(self, a):
                dlg = gtk.AboutDialog()
@@ -726,7 +733,7 @@ class Gonvert(object):
                try:
                        self._save_settings()
                except Exception:
                try:
                        self._save_settings()
                except Exception:
-                       _moduleLogger.exception("")
+                       _moduleLogger.exception("_on_user_exit")
                finally:
                        gtk.main_quit()
 
                finally:
                        gtk.main_quit()