Adding some todos and bugs
[gc-dialer] / src / dc_glade.py
index 245d6c6..e9e30c6 100755 (executable)
@@ -18,12 +18,16 @@ You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
+@todo Try gogol's bigger tree view items
+@todo Try gogol's wrapping for messages
+@bug Does special notifiers even work?
+@bug Messaging UI is bad(?)
+@bug Not logging back in on network reconnect
+
 @todo Figure out how to integrate with the Maemo contacts app
-@todo Look into an actor system
 @bug Session timeouts are bad, possible solutions:
        @li For every X minutes, if logged in, attempt login
        @li Restructure so code handling login/dial/sms is beneath everything else and login attempts are made if those fail
-@todo Can't text from dialpad (so can't do any arbitrary number texts)
 @todo Add logging support to make debugging issues for people a lot easier
 """
 
@@ -39,7 +43,6 @@ import ConfigParser
 import itertools
 import warnings
 
-import gobject
 import gtk
 import gtk.glade
 
@@ -71,9 +74,9 @@ def display_error_message(msg):
 class Dialcentral(object):
 
        _glade_files = [
-               '/usr/lib/dialcentral/dialcentral.glade',
                os.path.join(os.path.dirname(__file__), "dialcentral.glade"),
                os.path.join(os.path.dirname(__file__), "../lib/dialcentral.glade"),
+               '/usr/lib/dialcentral/dialcentral.glade',
        ]
 
        KEYPAD_TAB = 0
@@ -87,9 +90,6 @@ class Dialcentral(object):
        GV_BACKEND = 2
        BACKENDS = (NULL_BACKEND, GC_BACKEND, GV_BACKEND)
 
-       _data_path = os.path.join(os.path.expanduser("~"), ".dialcentral")
-       _user_settings = "%s/settings.ini" % _data_path
-
        def __init__(self):
                self._initDone = False
                self._connection = None
@@ -105,6 +105,8 @@ class Dialcentral(object):
                self._messagesViews = None
                self._recentViews = None
                self._contactsViews = None
+               self._alarmHandler = None
+               self._ledHandler = None
                self._originalCurrentLabels = []
 
                for path in self._glade_files:
@@ -136,9 +138,16 @@ class Dialcentral(object):
                                self._widgetTree.get_widget("callbackcombo").get_child().set_property('hildon-input-mode', (1 << 4))
                        except TypeError, e:
                                warnings.warn(e.message)
-                       hildon.hildon_helper_set_thumb_scrollbar(self._widgetTree.get_widget('recent_scrolledwindow'), True)
-                       hildon.hildon_helper_set_thumb_scrollbar(self._widgetTree.get_widget('message_scrolledwindow'), True)
-                       hildon.hildon_helper_set_thumb_scrollbar(self._widgetTree.get_widget('contacts_scrolledwindow'), True)
+                       for scrollingWidget in (
+                               'recent_scrolledwindow',
+                               'message_scrolledwindow',
+                               'contacts_scrolledwindow',
+                               "phoneSelectionMessage_scrolledwindow",
+                               "phonetypes_scrolledwindow",
+                               "smsMessage_scrolledwindow",
+                               "smsMessage_scrolledEntry",
+                       ):
+                               hildon.hildon_helper_set_thumb_scrollbar(self._widgetTree.get_widget(scrollingWidget), True)
 
                        gtkMenu = self._widgetTree.get_widget("dialpad_menubar")
                        menu = gtk.Menu()
@@ -212,6 +221,19 @@ class Dialcentral(object):
                        else:
                                pass # warnings.warn("No OSSO", UserWarning, 2)
 
+                       try:
+                               import alarm_handler
+                               self._alarmHandler = alarm_handler.AlarmHandler()
+                       except ImportError:
+                               alarm_handler = None
+                       except Exception:
+                               with gtk_toolbox.gtk_lock():
+                                       self._errorDisplay.push_exception()
+                               alarm_handler = None
+                       if hildon is not None:
+                               import led_handler
+                               self._ledHandler = led_handler.LedHandler()
+
                        # Setup maemo specifics
                        try:
                                import conic
@@ -229,16 +251,15 @@ class Dialcentral(object):
                        import gv_backend
                        import gc_backend
                        import file_backend
-                       import evo_backend
                        import gc_views
 
                        try:
-                               os.makedirs(self._data_path)
+                               os.makedirs(constants._data_path_)
                        except OSError, e:
                                if e.errno != 17:
                                        raise
-                       gcCookiePath = os.path.join(self._data_path, "gc_cookies.txt")
-                       gvCookiePath = os.path.join(self._data_path, "gv_cookies.txt")
+                       gcCookiePath = os.path.join(constants._data_path_, "gc_cookies.txt")
+                       gvCookiePath = os.path.join(constants._data_path_, "gv_cookies.txt")
                        self._defaultBackendId = self._guess_preferred_backend((
                                (self.GC_BACKEND, gcCookiePath),
                                (self.GV_BACKEND, gvCookiePath),
@@ -257,12 +278,14 @@ class Dialcentral(object):
                                })
                                self._accountViews.update({
                                        self.GC_BACKEND: gc_views.AccountInfo(
-                                               self._widgetTree, self._phoneBackends[self.GC_BACKEND], self._errorDisplay
+                                               self._widgetTree, self._phoneBackends[self.GC_BACKEND], None, self._errorDisplay
                                        ),
                                        self.GV_BACKEND: gc_views.AccountInfo(
-                                               self._widgetTree, self._phoneBackends[self.GV_BACKEND], self._errorDisplay
+                                               self._widgetTree, self._phoneBackends[self.GV_BACKEND], self._alarmHandler, self._errorDisplay
                                        ),
                                })
+                               self._accountViews[self.GC_BACKEND].save_everything = lambda *args: None
+                               self._accountViews[self.GV_BACKEND].save_everything = self._save_settings
                                self._recentViews.update({
                                        self.GC_BACKEND: gc_views.RecentCallsView(
                                                self._widgetTree, self._phoneBackends[self.GC_BACKEND], self._errorDisplay
@@ -286,8 +309,7 @@ class Dialcentral(object):
                                        ),
                                })
 
-                       evoBackend = evo_backend.EvolutionAddressBook()
-                       fsContactsPath = os.path.join(self._data_path, "contacts")
+                       fsContactsPath = os.path.join(constants._data_path_, "contacts")
                        fileBackend = file_backend.FilesystemAddressBookFactory(fsContactsPath)
                        for backendId in (self.GV_BACKEND, self.GC_BACKEND):
                                self._dialpads[backendId].number_selected = self._select_action
@@ -297,7 +319,6 @@ class Dialcentral(object):
 
                                addressBooks = [
                                        self._phoneBackends[backendId],
-                                       evoBackend,
                                        fileBackend,
                                ]
                                mergedBook = gc_views.MergedAddressBook(addressBooks, gc_views.MergedAddressBook.advanced_lastname_sorter)
@@ -329,14 +350,14 @@ class Dialcentral(object):
                        self._initDone = True
 
                        config = ConfigParser.SafeConfigParser()
-                       config.read(self._user_settings)
+                       config.read(constants._user_settings_)
                        with gtk_toolbox.gtk_lock():
                                self.load_settings(config)
 
                        self._spawn_attempt_login(2)
                except Exception, e:
                        with gtk_toolbox.gtk_lock():
-                               self._errorDisplay.push_exception(e)
+                               self._errorDisplay.push_exception()
 
        def attempt_login(self, numOfAttempts = 10, force = False):
                """
@@ -365,7 +386,7 @@ class Dialcentral(object):
                                self._change_loggedin_status(serviceId)
                except StandardError, e:
                        with gtk_toolbox.gtk_lock():
-                               self._errorDisplay.push_exception(e)
+                               self._errorDisplay.push_exception()
 
        def _spawn_attempt_login(self, *args):
                self._loginSink.send(args)
@@ -493,10 +514,21 @@ class Dialcentral(object):
                                for blob in blobs
                        )
                        self._credentials = tuple(creds)
+
+                       if self._alarmHandler is not None:
+                               self._alarmHandler.load_settings(config, "alarm")
+               except ConfigParser.NoOptionError, e:
+                       warnings.warn(
+                               "Settings file %s is missing section %s" % (
+                                       constants._user_settings_,
+                                       e.section,
+                               ),
+                               stacklevel=2
+                       )
                except ConfigParser.NoSectionError, e:
                        warnings.warn(
                                "Settings file %s is missing section %s" % (
-                                       self._user_settings,
+                                       constants._user_settings_,
                                        e.section,
                                ),
                                stacklevel=2
@@ -512,10 +544,18 @@ class Dialcentral(object):
                        sectionName = "%s - %s" % (backendId, view.name())
                        try:
                                view.load_settings(config, sectionName)
+                       except ConfigParser.NoOptionError, e:
+                               warnings.warn(
+                                       "Settings file %s is missing section %s" % (
+                                               constants._user_settings_,
+                                               e.section,
+                                       ),
+                                       stacklevel=2
+                               )
                        except ConfigParser.NoSectionError, e:
                                warnings.warn(
                                        "Settings file %s is missing section %s" % (
-                                               self._user_settings,
+                                               constants._user_settings_,
                                                e.section,
                                        ),
                                        stacklevel=2
@@ -530,6 +570,10 @@ class Dialcentral(object):
                for i, value in enumerate(self._credentials):
                        blob = base64.b64encode(value)
                        config.set(constants.__pretty_app_name__, "bin_blob_%i" % i, blob)
+               config.add_section("alarm")
+               if self._alarmHandler is not None:
+                       self._alarmHandler.save_settings(config, "alarm")
+
                for backendId, view in itertools.chain(
                        self._dialpads.iteritems(),
                        self._accountViews.iteritems(),
@@ -555,7 +599,7 @@ class Dialcentral(object):
                """
                config = ConfigParser.SafeConfigParser()
                self.save_settings(config)
-               with open(self._user_settings, "wb") as configFile:
+               with open(constants._user_settings_, "wb") as configFile:
                        config.write(configFile)
 
        def _refresh_active_tab(self):
@@ -567,6 +611,10 @@ class Dialcentral(object):
                elif pageIndex == self.MESSAGES_TAB:
                        self._messagesViews[self._selectedBackendId].update(force=True)
 
+               if pageIndex in (self.RECENT_TAB, self.MESSAGES_TAB):
+                       if self._ledHandler is not None:
+                               self._ledHandler.off()
+
        def _on_close(self, *args, **kwds):
                try:
                        if self._osso is not None:
@@ -643,15 +691,23 @@ class Dialcentral(object):
 
        def _on_notebook_switch_page(self, notebook, page, pageIndex):
                self._reset_tab_refresh()
+
+               didRecentUpdate = False
+               didMessagesUpdate = False
+
                if pageIndex == self.RECENT_TAB:
-                       self._recentViews[self._selectedBackendId].update()
+                       didRecentUpdate = self._recentViews[self._selectedBackendId].update()
                elif pageIndex == self.MESSAGES_TAB:
-                       self._messagesViews[self._selectedBackendId].update()
+                       didMessagesUpdate = self._messagesViews[self._selectedBackendId].update()
                elif pageIndex == self.CONTACTS_TAB:
                        self._contactsViews[self._selectedBackendId].update()
                elif pageIndex == self.ACCOUNT_TAB:
                        self._accountViews[self._selectedBackendId].update()
 
+               if didRecentUpdate or didMessagesUpdate:
+                       if self._ledHandler is not None:
+                               self._ledHandler.off()
+
        def _set_tab_refresh(self, *args):
                pageIndex = self._notebook.get_current_page()
                child = self._notebook.get_nth_page(pageIndex)
@@ -676,7 +732,7 @@ class Dialcentral(object):
                        loggedIn = self._phoneBackends[self._selectedBackendId].is_authed()
                except StandardError, e:
                        loggedIn = False
-                       self._errorDisplay.push_exception(e)
+                       self._errorDisplay.push_exception()
                        return
 
                if not loggedIn:
@@ -690,9 +746,12 @@ class Dialcentral(object):
                        self._phoneBackends[self._selectedBackendId].send_sms(number, message)
                        dialed = True
                except StandardError, e:
-                       self._errorDisplay.push_exception(e)
+                       self._errorDisplay.push_exception()
                except ValueError, e:
-                       self._errorDisplay.push_exception(e)
+                       self._errorDisplay.push_exception()
+
+               if dialed:
+                       self._dialpads[self._selectedBackendId].clear()
 
        def _on_dial_clicked(self, number):
                assert number, "No number to call"
@@ -700,7 +759,7 @@ class Dialcentral(object):
                        loggedIn = self._phoneBackends[self._selectedBackendId].is_authed()
                except StandardError, e:
                        loggedIn = False
-                       self._errorDisplay.push_exception(e)
+                       self._errorDisplay.push_exception()
                        return
 
                if not loggedIn:
@@ -715,9 +774,9 @@ class Dialcentral(object):
                        self._phoneBackends[self._selectedBackendId].dial(number)
                        dialed = True
                except StandardError, e:
-                       self._errorDisplay.push_exception(e)
+                       self._errorDisplay.push_exception()
                except ValueError, e:
-                       self._errorDisplay.push_exception(e)
+                       self._errorDisplay.push_exception()
 
                if dialed:
                        self._dialpads[self._selectedBackendId].clear()
@@ -753,7 +812,10 @@ def run_doctest():
 
 
 def run_dialpad():
+       _lock_file = os.path.join(constants._data_path_, ".lock")
+       #with gtk_toolbox.flock(_lock_file, 0):
        gtk.gdk.threads_init()
+
        if hildon is not None:
                gtk.set_application_name(constants.__pretty_app_name__)
        handle = Dialcentral()