In playing with direct dial I found I setup the user-agent incorrectly
[gc-dialer] / src / dc_glade.py
index bac9004..14ffb02 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python2.5
+#!/usr/bin/env python
 
 """
 DialCentral - Front end for Google's GoogleVoice service.
@@ -29,6 +29,7 @@ import threading
 import base64
 import ConfigParser
 import itertools
+import shutil
 import logging
 
 import gtk
@@ -97,6 +98,7 @@ class Dialcentral(object):
                self._alarmHandler = None
                self._ledHandler = None
                self._originalCurrentLabels = []
+               self._fsContactsPath = os.path.join(constants._data_path_, "contacts")
 
                for path in self._glade_files:
                        if os.path.isfile(path):
@@ -109,10 +111,15 @@ class Dialcentral(object):
 
                self._window = self._widgetTree.get_widget("mainWindow")
                self._notebook = self._widgetTree.get_widget("notebook")
-               self._errorDisplay = gtk_toolbox.ErrorDisplay(self._widgetTree)
+               errorBox = self._widgetTree.get_widget("errorEventBox")
+               errorDescription = self._widgetTree.get_widget("errorDescription")
+               errorClose = self._widgetTree.get_widget("errorClose")
+               self._errorDisplay = gtk_toolbox.ErrorDisplay(errorBox, errorDescription, errorClose)
                self._credentialsDialog = gtk_toolbox.LoginWindow(self._widgetTree)
+               self._smsEntryWindow = None
 
                self._isFullScreen = False
+               self.__isPortrait = False
                self._app = hildonize.get_app_class()()
                self._window = hildonize.hildonize_window(self._app, self._window)
                hildonize.hildonize_text_entry(self._widgetTree.get_widget("usernameentry"))
@@ -133,19 +140,12 @@ class Dialcentral(object):
                        scrollingWidget = self._widgetTree.get_widget(scrollingWidgetName)
                        assert scrollingWidget is not None, scrollingWidgetName
                        hildonize.hildonize_scrollwindow_with_viewport(scrollingWidget)
-               for pannableTreeViewName in (
-                       "messages_view",
-               ):
-                       view = self._widgetTree.get_widget(pannableTreeViewName)
-                       assert view is not None, pannableTreeViewName
-                       hildonize.pannable_treeview_theme_workaround(scrollingWidget)
 
                for buttonName in (
                        "back",
                        "addressbookSelectButton",
                        "sendSmsButton",
                        "dialButton",
-                       "cancelSmsButton",
                        "callbackSelectButton",
                        "minutesEntryButton",
                        "clearcookies",
@@ -159,12 +159,12 @@ class Dialcentral(object):
                        self._window,
                        self._widgetTree.get_widget("dialpad_menubar"),
                )
-               if hildonize.IS_FREMANTLE_SUPPORTED:
+               if not hildonize.GTK_MENU_USED:
                        button = gtk.Button("New Login")
                        button.connect("clicked", self._on_clearcookies_clicked)
                        menu.append(button)
 
-                       button= gtk.Button("Refresh")
+                       button = gtk.Button("Refresh")
                        button.connect("clicked", self._on_menu_refresh)
                        menu.append(button)
 
@@ -175,7 +175,7 @@ class Dialcentral(object):
                if not hildonize.IS_HILDON_SUPPORTED:
                        _moduleLogger.warning("No hildonization support")
 
-               hildonize.set_application_title(self._window, "%s" % constants.__pretty_app_name__)
+               hildonize.set_application_name("%s" % constants.__pretty_app_name__)
 
                self._window.connect("destroy", self._on_close)
                self._window.set_default_size(800, 300)
@@ -282,6 +282,8 @@ class Dialcentral(object):
                        import gv_views
                        from backends import merge_backend
 
+                       with gtk_toolbox.gtk_lock():
+                               self._smsEntryWindow = gv_views.SmsEntryWindow(self._widgetTree, self._window, self._app)
                        try:
                                os.makedirs(constants._data_path_)
                        except OSError, e:
@@ -319,19 +321,21 @@ class Dialcentral(object):
                                        ),
                                })
 
-                       fsContactsPath = os.path.join(constants._data_path_, "contacts")
-                       fileBackend = file_backend.FilesystemAddressBookFactory(fsContactsPath)
+                       fileBackend = file_backend.FilesystemAddressBookFactory(self._fsContactsPath)
 
-                       self._dialpads[self.GV_BACKEND].number_selected = self._select_action
-                       self._historyViews[self.GV_BACKEND].number_selected = self._select_action
-                       self._messagesViews[self.GV_BACKEND].number_selected = self._select_action
-                       self._contactsViews[self.GV_BACKEND].number_selected = self._select_action
+                       self._smsEntryWindow.send_sms = self._on_sms_clicked
+                       self._smsEntryWindow.dial = self._on_dial_clicked
+                       self._dialpads[self.GV_BACKEND].add_contact = self._add_contact
+                       self._dialpads[self.GV_BACKEND].dial = self._on_dial_clicked
+                       self._historyViews[self.GV_BACKEND].add_contact = self._add_contact
+                       self._messagesViews[self.GV_BACKEND].add_contact = self._add_contact
+                       self._contactsViews[self.GV_BACKEND].add_contact = self._add_contact
 
                        addressBooks = [
                                self._phoneBackends[self.GV_BACKEND],
                                fileBackend,
                        ]
-                       mergedBook = merge_backend.MergedAddressBook(addressBooks, merge_backend.MergedAddressBook.advanced_lastname_sorter)
+                       mergedBook = merge_backend.MergedAddressBook(addressBooks, merge_backend.MergedAddressBook.basic_firtname_sorter)
                        self._contactsViews[self.GV_BACKEND].append(mergedBook)
                        self._contactsViews[self.GV_BACKEND].extend(addressBooks)
                        self._contactsViews[self.GV_BACKEND].open_addressbook(*self._contactsViews[self.GV_BACKEND].get_addressbooks().next()[0][0:2])
@@ -491,20 +495,15 @@ class Dialcentral(object):
 
                return loggedIn, serviceId
 
-       def _select_action(self, action, number, message):
-               self.refresh_session()
-               if action == "dial":
-                       self._on_dial_clicked(number)
-               elif action == "sms":
-                       self._on_sms_clicked(number, message)
-               else:
-                       assert False, "Unknown action: %s" % action
+       def _add_contact(self, *args, **kwds):
+               self._smsEntryWindow.add_contact(*args, **kwds)
 
        def _change_loggedin_status(self, newStatus):
                oldStatus = self._selectedBackendId
                if oldStatus == newStatus:
                        return
 
+               _moduleLogger.debug("Changing from %s to %s" % (oldStatus, newStatus))
                self._dialpads[oldStatus].disable()
                self._accountViews[oldStatus].disable()
                self._historyViews[oldStatus].disable()
@@ -521,6 +520,7 @@ class Dialcentral(object):
 
                self._accountViews[self._selectedBackendId].update()
                self._refresh_active_tab()
+               self._refresh_orientation()
 
        def load_settings(self, config):
                """
@@ -543,6 +543,18 @@ class Dialcentral(object):
 
                        if self._alarmHandler is not None:
                                self._alarmHandler.load_settings(config, "alarm")
+
+                       isFullscreen = config.getboolean(constants.__pretty_app_name__, "fullscreen")
+                       if isFullscreen:
+                               self._window.fullscreen()
+
+                       isPortrait = config.getboolean(constants.__pretty_app_name__, "portrait")
+                       if isPortrait ^ self.__isPortrait:
+                               if isPortrait:
+                                       orientation = gtk.ORIENTATION_VERTICAL
+                               else:
+                                       orientation = gtk.ORIENTATION_HORIZONTAL
+                               self.set_orientation(orientation)
                except ConfigParser.NoOptionError, e:
                        _moduleLogger.exception(
                                "Settings file %s is missing section %s" % (
@@ -583,27 +595,6 @@ class Dialcentral(object):
                                        ),
                                )
 
-               try:
-                       previousOrientation = config.getint(constants.__pretty_app_name__, "orientation")
-                       if previousOrientation == gtk.ORIENTATION_HORIZONTAL:
-                               hildonize.window_to_landscape(self._window)
-                       elif previousOrientation == gtk.ORIENTATION_VERTICAL:
-                               hildonize.window_to_portrait(self._window)
-               except ConfigParser.NoOptionError, e:
-                       _moduleLogger.exception(
-                               "Settings file %s is missing section %s" % (
-                                       constants._user_settings_,
-                                       e.section,
-                               ),
-                       )
-               except ConfigParser.NoSectionError, e:
-                       _moduleLogger.exception(
-                               "Settings file %s is missing section %s" % (
-                                       constants._user_settings_,
-                                       e.section,
-                               ),
-                       )
-
        def save_settings(self, config):
                """
                @note Thread Agnostic
@@ -617,7 +608,8 @@ class Dialcentral(object):
 
                config.add_section(constants.__pretty_app_name__)
                config.set(constants.__pretty_app_name__, "active", str(backend))
-               config.set(constants.__pretty_app_name__, "orientation", str(int(gtk_toolbox.get_screen_orientation())))
+               config.set(constants.__pretty_app_name__, "portrait", str(self.__isPortrait))
+               config.set(constants.__pretty_app_name__, "fullscreen", str(self._isFullScreen))
                for i, value in enumerate(self._credentials):
                        blob = base64.b64encode(value)
                        config.set(constants.__pretty_app_name__, "bin_blob_%i" % i, blob)
@@ -658,6 +650,36 @@ class Dialcentral(object):
                        if self._ledHandler is not None:
                                self._ledHandler.off()
 
+       def set_orientation(self, orientation):
+               if orientation == gtk.ORIENTATION_VERTICAL:
+                       hildonize.window_to_portrait(self._window)
+                       self._notebook.set_property("tab-pos", gtk.POS_BOTTOM)
+                       self.__isPortrait = True
+               elif orientation == gtk.ORIENTATION_HORIZONTAL:
+                       hildonize.window_to_landscape(self._window)
+                       self._notebook.set_property("tab-pos", gtk.POS_LEFT)
+                       self.__isPortrait = False
+               else:
+                       raise NotImplementedError(orientation)
+
+       def get_orientation(self):
+               return gtk.ORIENTATION_VERTICAL if self.__isPortrait else gtk.ORIENTATION_HORIZONTAL
+
+       def _toggle_rotate(self):
+               if self.__isPortrait:
+                       self.set_orientation(gtk.ORIENTATION_HORIZONTAL)
+               else:
+                       self.set_orientation(gtk.ORIENTATION_VERTICAL)
+
+       def _refresh_orientation(self):
+               """
+               Mostly meant to be used when switching backends
+               """
+               if self.__isPortrait:
+                       self.set_orientation(gtk.ORIENTATION_VERTICAL)
+               else:
+                       self.set_orientation(gtk.ORIENTATION_HORIZONTAL)
+
        @gtk_toolbox.log_exception(_moduleLogger)
        def _on_close(self, *args, **kwds):
                try:
@@ -742,13 +764,23 @@ class Dialcentral(object):
                                        self._window.unfullscreen()
                                else:
                                        self._window.fullscreen()
-                       elif event.keyval == ord("l") and event.get_state() & gtk.gdk.CONTROL_MASK:
+                       elif event.keyval == gtk.keysyms.l and event.get_state() & gtk.gdk.CONTROL_MASK:
                                with open(constants._user_logpath_, "r") as f:
                                        logLines = f.xreadlines()
                                        log = "".join(logLines)
                                        self._clipboard.set_text(str(log))
-                       elif event.keyval == ord("r") and event.get_state() & gtk.gdk.CONTROL_MASK:
+                       elif (
+                               event.keyval in (gtk.keysyms.w, gtk.keysyms.q) and
+                               event.get_state() & gtk.gdk.CONTROL_MASK
+                       ):
+                               self._window.destroy()
+                       elif event.keyval == gtk.keysyms.o and event.get_state() & gtk.gdk.CONTROL_MASK:
+                               self._toggle_rotate()
+                               return True
+                       elif event.keyval == gtk.keysyms.r and event.get_state() & gtk.gdk.CONTROL_MASK:
                                self._refresh_active_tab()
+                       elif event.keyval == gtk.keysyms.i and event.get_state() & gtk.gdk.CONTROL_MASK:
+                               self._import_contacts()
                except Exception, e:
                        self._errorDisplay.push_exception()
 
@@ -813,10 +845,11 @@ class Dialcentral(object):
                        self._errorDisplay.push_exception()
                return False
 
-       def _on_sms_clicked(self, number, message):
+       def _on_sms_clicked(self, numbers, message):
                try:
-                       assert number, "No number specified"
+                       assert numbers, "No number specified"
                        assert message, "Empty message"
+                       self.refresh_session()
                        try:
                                loggedIn = self._phoneBackends[self._selectedBackendId].is_authed()
                        except Exception, e:
@@ -832,9 +865,9 @@ class Dialcentral(object):
 
                        dialed = False
                        try:
-                               self._phoneBackends[self._selectedBackendId].send_sms(number, message)
-                               hildonize.show_information_banner(self._window, "Sending to %s" % number)
-                               _moduleLogger.info("Sending SMS to %s" % number)
+                               self._phoneBackends[self._selectedBackendId].send_sms(numbers, message)
+                               hildonize.show_information_banner(self._window, "Sending to %s" % ", ".join(numbers))
+                               _moduleLogger.info("Sending SMS to %r" % numbers)
                                dialed = True
                        except Exception, e:
                                self._errorDisplay.push_exception()
@@ -847,6 +880,7 @@ class Dialcentral(object):
        def _on_dial_clicked(self, number):
                try:
                        assert number, "No number to call"
+                       self.refresh_session()
                        try:
                                loggedIn = self._phoneBackends[self._selectedBackendId].is_authed()
                        except Exception, e:
@@ -875,6 +909,24 @@ class Dialcentral(object):
                except Exception, e:
                        self._errorDisplay.push_exception()
 
+       def _import_contacts(self):
+               csvFilter = gtk.FileFilter()
+               csvFilter.set_name("Contacts")
+               csvFilter.add_pattern("*.csv")
+               importFileChooser = gtk.FileChooserDialog(
+                       title="Contacts",
+                       parent=self._window,
+               )
+               importFileChooser.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
+               importFileChooser.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
+
+               importFileChooser.set_property("filter", csvFilter)
+               userResponse = importFileChooser.run()
+               importFileChooser.hide()
+               if userResponse == gtk.RESPONSE_OK:
+                       filename = importFileChooser.get_filename()
+                       shutil.copy2(filename, self._fsContactsPath)
+
        def _on_menu_refresh(self, *args):
                try:
                        self._refresh_active_tab()
@@ -916,12 +968,8 @@ 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 hildonize.IS_HILDON_SUPPORTED:
-               gtk.set_application_name(constants.__pretty_app_name__)
        handle = Dialcentral()
        if not PROFILE_STARTUP:
                gtk.main()