Cleaning up the makefile
[gonvert] / gonvert / gonvert_qt.py
index aa3d7e5..3cc50b6 100755 (executable)
@@ -8,7 +8,11 @@ from __future__ import with_statement
 
 import os
 import math
-import simplejson
+try:
+       import json as simplejson
+except ImportError:
+       print "json not available, falling back to simplejson"
+       import simplejson
 import logging
 import logging.handlers
 
@@ -19,6 +23,8 @@ QtGui = qt_compat.import_module("QtGui")
 import constants
 from util import qui_utils
 from util import misc as misc_utils
+from util import linux as linux_utils
+
 import unit_data
 
 
@@ -225,8 +231,11 @@ class Gonvert(object):
                        return self._hiddenUnits[categoryName]
 
        def load_settings(self):
+               settingsPath = linux_utils.get_resource_path(
+                       "config", constants.__app_name__, "settings.json"
+               )
                try:
-                       with open(constants._user_settings_, "r") as settingsFile:
+                       with open(settingsPath, "r") as settingsFile:
                                settings = simplejson.load(settingsFile)
                except IOError, e:
                        _moduleLogger.info("No settings")
@@ -291,7 +300,11 @@ class Gonvert(object):
                        "useQuick": self._condensedAction.isChecked(),
                        "sortBy": sortBy,
                }
-               with open(constants._user_settings_, "w") as settingsFile:
+
+               settingsPath = linux_utils.get_resource_path(
+                       "config", constants.__app_name__, "settings.json"
+               )
+               with open(settingsPath, "w") as settingsFile:
                        simplejson.dump(settings, settingsFile)
 
        @property
@@ -398,7 +411,10 @@ class Gonvert(object):
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_log(self, checked = False):
-               with open(constants._user_logpath_, "r") as f:
+               logPath = linux_utils.get_resource_path(
+                       "cache", self._constants.__app_name__, "%s.log" % self._constants.__app_name__
+               )
+               with open(logPath, "r") as f:
                        logLines = f.xreadlines()
                        log = "".join(logLines)
                        self._clipboard.setText(log)
@@ -567,11 +583,12 @@ class QuickConvert(object):
        def select_category(self, categoryName):
                self._select_category(categoryName)
 
+               self._categoryView.clearSelection()
                i = unit_data.UNIT_CATEGORIES.index(categoryName)
                rootIndex = self._categoryView.rootIndex()
                currentIndex = self._categoryView.model().index(i, 0, rootIndex)
                self._categoryView.scrollTo(currentIndex)
-               self._categoryView.setItemSelected(self._categoryView.topLevelItem(i), True)
+               self._categoryView.topLevelItem(i).setSelected(True)
 
                return self
 
@@ -582,20 +599,22 @@ class QuickConvert(object):
        def select_input(self, name):
                self._select_input(name)
 
+               self._inputView.clearSelection()
                i = self._unitNames.index(name)
                rootIndex = self._inputView.rootIndex()
                currentIndex = self._inputView.model().index(i, 0, rootIndex)
                self._inputView.scrollTo(currentIndex)
-               self._inputView.setItemSelected(self._inputView.topLevelItem(i), True)
+               self._inputView.topLevelItem(i).setSelected(True)
 
        def select_output(self, name):
                self._select_output(name)
 
+               self._outputView.clearSelection()
                i = self._unitNames.index(name)
                rootIndex = self._outputView.rootIndex()
                currentIndex = self._outputView.model().index(i, 0, rootIndex)
                self._outputView.scrollTo(currentIndex)
-               self._outputView.setItemSelected(self._outputView.topLevelItem(i), True)
+               self._outputView.topLevelItem(i).setSelected(True)
 
        def _select_category(self, categoryName):
                self._inputUnitName = ""
@@ -801,8 +820,8 @@ class QuickConvert(object):
                        str(item.text(0))
                        for item in self._categoryView.selectedItems()
                ]
-               assert len(selectedNames) == 1
-               self._select_category(selectedNames[0])
+               if selectedNames:
+                       self._select_category(selectedNames[0])
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_input_selection_changed(self, selected, deselected):
@@ -940,11 +959,12 @@ class CategoryWindow(object):
        def select_category(self, categoryName):
                self._select_category(categoryName)
 
+               self._categories.clearSelection()
                i = unit_data.UNIT_CATEGORIES.index(categoryName)
                rootIndex = self._categories.rootIndex()
                currentIndex = self._categories.model().index(i, 0, rootIndex)
                self._categories.scrollTo(currentIndex)
-               self._categories.setItemSelected(self._categories.topLevelItem(i), True)
+               self._categories.topLevelItem(i).setSelected(True)
                return self._unitWindow
 
        def set_fullscreen(self, isFullscreen):
@@ -1547,14 +1567,25 @@ class UnitWindow(object):
 
 def run_gonvert():
        try:
-               os.makedirs(constants._data_path_)
+               os.makedirs(linux_utils.get_resource_path("config", constants.__app_name__))
+       except OSError, e:
+               if e.errno != 17:
+                       raise
+       try:
+               os.makedirs(linux_utils.get_resource_path("cache", constants.__app_name__))
+       except OSError, e:
+               if e.errno != 17:
+                       raise
+       try:
+               os.makedirs(linux_utils.get_resource_path("data", constants.__app_name__))
        except OSError, e:
                if e.errno != 17:
                        raise
 
+       logPath = linux_utils.get_resource_path("cache", constants.__app_name__, "%s.log" % constants.__app_name__)
        logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
        logging.basicConfig(level=logging.DEBUG, format=logFormat)
-       rotating = logging.handlers.RotatingFileHandler(constants._user_logpath_, maxBytes=512*1024, backupCount=1)
+       rotating = logging.handlers.RotatingFileHandler(logPath, maxBytes=512*1024, backupCount=1)
        rotating.setFormatter(logging.Formatter(logFormat))
        root = logging.getLogger()
        root.addHandler(rotating)