3 # DialCentral - Front end for Google's Grand Central service.
4 # Copyright (C) 2008 Mark Bergman bergman AT merctech DOT com
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 DialCentral: A phone dialer using GrandCentral
40 class Dialcentral(object):
42 __pretty_app_name__ = "DialCentral"
43 __app_name__ = "dialcentral"
45 __app_magic__ = 0xdeadbeef
48 '/usr/lib/dialcentral/dialcentral.glade',
49 os.path.join(os.path.dirname(__file__), "dialcentral.glade"),
50 os.path.join(os.path.dirname(__file__), "../lib/dialcentral.glade"),
54 self._gcBackend = None
55 self._clipboard = gtk.clipboard_get()
57 self._deviceIsOnline = True
59 self._accountView = None
60 self._recentView = None
61 self._contactsView = None
63 for path in Dialcentral._glade_files:
64 if os.path.isfile(path):
65 self._widgetTree = gtk.glade.XML(path)
68 self.display_error_message("Cannot find gc_dialer.glade")
72 self._window = self._widgetTree.get_widget("Dialpad")
73 self._notebook = self._widgetTree.get_widget("notebook")
77 self._isFullScreen = False
78 if hildon is not None and self._window is gtk.Window:
79 warnings.warn("Hildon installed but glade file not updated to work with hildon", UserWarning, 2)
81 elif hildon is not None:
82 self._app = hildon.Program()
83 self._window = hildon.Window()
84 self._widgetTree.get_widget("vbox1").reparent(self._window)
85 self._app.add_window(self._window)
86 self._widgetTree.get_widget("usernameentry").set_property('hildon-input-mode', 7)
87 self._widgetTree.get_widget("passwordentry").set_property('hildon-input-mode', 7|(1 << 29))
89 gtkMenu = self._widgetTree.get_widget("dialpad_menubar")
91 for child in gtkMenu.get_children():
93 self._window.set_menu(menu)
96 self._window.connect("key-press-event", self._on_key_press)
97 self._window.connect("window-state-event", self._on_window_state_change)
99 warnings.warn("No Hildon", UserWarning, 2)
101 if hildon is not None:
102 self._window.set_title("Keypad")
104 self._window.set_title("%s - Keypad" % self.__pretty_app_name__)
107 "on_loginbutton_clicked": self._on_loginbutton_clicked,
108 "on_loginclose_clicked": self._on_loginclose_clicked,
109 "on_dialpad_quit": self._on_close,
111 self._widgetTree.signal_autoconnect(callbackMapping)
114 self._window.connect("destroy", gtk.main_quit)
115 self._window.show_all()
117 backgroundSetup = threading.Thread(target=self._idle_setup)
118 backgroundSetup.setDaemon(True)
119 backgroundSetup.start()
122 def _idle_setup(self):
124 If something can be done after the UI loads, push it here so it's not blocking the UI
132 self._osso = osso.Context(Dialcentral.__app_name__, Dialcentral.__version__, False)
133 device = osso.DeviceState(self._osso)
134 device.set_device_state_callback(self._on_device_state_change, 0)
136 warnings.warn("No OSSO", UserWarning, 2)
142 self._connection = None
143 if conic is not None:
144 self._connection = conic.Connection()
145 self._connection.connect("connection-event", self._on_connection_change, Dialcentral.__app_magic__)
146 self._connection.request_connection(conic.CONNECT_FLAG_NONE)
148 warnings.warn("No Internet Connectivity API ", UserWarning, 2)
152 # import gmail_backend
153 # import maemo_backend
156 self._gcBackend = gc_backend.GCDialer()
157 gtk.gdk.threads_enter()
159 self._dialpad = views.Dialpad(self._widgetTree)
160 self._dialpad.set_number("")
161 self._accountView = views.AccountInfo(self._widgetTree, self._gcBackend)
162 self._recentView = views.RecentCallsView(self._widgetTree, self._gcBackend)
163 self._contactsView = views.ContactsView(self._widgetTree, self._gcBackend)
165 gtk.gdk.threads_leave()
167 self._dialpad.dial = self._on_dial_clicked
168 self._recentView.number_selected = self._on_number_selected
169 self._contactsView.number_selected = self._on_number_selected
171 #This is where the blocking can start
172 if self._gcBackend.is_authed():
173 gtk.gdk.threads_enter()
175 self._accountView.update()
177 gtk.gdk.threads_leave()
179 self.attempt_login(2)
183 evo_backend.EvolutionAddressBook(),
185 mergedBook = views.MergedAddressBook(addressBooks, views.MergedAddressBook.basic_lastname_sorter)
186 self._contactsView.append(mergedBook)
187 self._contactsView.extend(addressBooks)
188 self._contactsView.open_addressbook(*self._contactsView.get_addressbooks().next()[0][0:2])
189 gtk.gdk.threads_enter()
191 self._contactsView._init_books_combo()
193 gtk.gdk.threads_leave()
196 "on_paste": self._on_paste,
197 "on_clearcookies_clicked": self._on_clearcookies_clicked,
198 "on_notebook_switch_page": self._on_notebook_switch_page,
199 "on_about_activate": self._on_about_activate,
201 self._widgetTree.signal_autoconnect(callbackMapping)
205 def attempt_login(self, numOfAttempts = 1):
207 @todo Handle user notification better like attempting to login and failed login
209 @note Not meant to be called directly, but run as a seperate thread.
211 assert 0 < numOfAttempts, "That was pointless having 0 or less login attempts"
213 if not self._deviceIsOnline:
214 warnings.warn("Attempted to login while device was offline", UserWarning, 2)
217 if self._gcBackend.is_authed():
220 for x in xrange(numOfAttempts):
221 gtk.gdk.threads_enter()
223 dialog = self._widgetTree.get_widget("login_dialog")
224 dialog.set_transient_for(self._window)
225 dialog.set_default_response(0)
228 username = self._widgetTree.get_widget("usernameentry").get_text()
229 password = self._widgetTree.get_widget("passwordentry").get_text()
230 self._widgetTree.get_widget("passwordentry").set_text("")
233 gtk.gdk.threads_leave()
234 loggedIn = self._gcBackend.login(username, password)
236 gtk.gdk.threads_enter()
238 if self._gcBackend.get_callback_number() is None:
239 self._gcBackend.set_sane_callback()
240 self._accountView.update()
242 gtk.gdk.threads_leave()
247 def display_error_message(self, msg):
248 error_dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, msg)
250 def close(dialog, response, editor):
251 editor.about_dialog = None
253 error_dialog.connect("response", close, self)
257 def _on_close(*args, **kwds):
260 def _on_device_state_change(self, shutdown, save_unsaved_data, memory_low, system_inactivity, message, userData):
262 For shutdown or save_unsaved_data, our only state is cookies and I think the cookie manager handles that for us.
263 For system_inactivity, we have no background tasks to pause
265 @note Hildon specific
268 self._gcBackend.clear_caches()
269 self._contactsView.clear_caches()
272 def _on_connection_change(self, connection, event, magicIdentifier):
274 @note Hildon specific
278 status = event.get_status()
279 error = event.get_error()
280 iap_id = event.get_iap_id()
281 bearer = event.get_bearer_type()
283 if status == conic.STATUS_CONNECTED:
284 self._window.set_sensitive(True)
285 self._deviceIsOnline = True
286 backgroundLogin = threading.Thread(target=self.attempt_login, args=[2])
287 backgroundLogin.setDaemon(True)
288 backgroundLogin.start()
289 elif status == conic.STATUS_DISCONNECTED:
290 self._window.set_sensitive(False)
291 self._deviceIsOnline = False
293 def _on_window_state_change(self, widget, event, *args):
295 @note Hildon specific
297 if event.new_window_state & gtk.gdk.WINDOW_STATE_FULLSCREEN:
298 self._isFullScreen = True
300 self._isFullScreen = False
302 def _on_key_press(self, widget, event, *args):
304 @note Hildon specific
306 if event.keyval == gtk.keysyms.F6:
307 if self._isFullScreen:
308 self._window.unfullscreen()
310 self._window.fullscreen()
312 def _on_loginbutton_clicked(self, *args):
313 self._widgetTree.get_widget("login_dialog").response(gtk.RESPONSE_OK)
315 def _on_loginclose_clicked(self, *args):
319 def _on_clearcookies_clicked(self, *args):
320 self._gcBackend.logout()
321 self._accountView.clear()
322 self._recentView.clear()
323 self._contactsView.clear()
325 # re-run the inital grandcentral setup
326 backgroundLogin = threading.Thread(target=self.attempt_login, args=[2])
327 backgroundLogin.setDaemon(True)
328 backgroundLogin.start()
330 def _on_notebook_switch_page(self, notebook, page, page_num):
332 self._contactsView.update()
334 self._recentView.update()
336 tabTitle = self._notebook.get_tab_label(self._notebook.get_nth_page(page_num)).get_text()
337 if hildon is not None:
338 self._window.set_title(tabTitle)
340 self._window.set_title("%s - %s" % (self.__pretty_app_name__, tabTitle))
342 def _on_number_selected(self, number):
343 self._dialpad.set_number(number)
344 self._notebook.set_current_page(0)
346 def _on_dial_clicked(self, number):
348 @todo Potential blocking on web access, maybe we should defer parts of this or put up a dialog?
350 loggedIn = self._gcBackend.is_authed()
353 #loggedIn = self.attempt_login(2)
355 if not loggedIn or not self._gcBackend.is_authed() or self._gcBackend.get_callback_number() == "":
356 self.display_error_message("Backend link with grandcentral is not working, please try again")
357 warnings.warn("Backend Status: Logged in? %s, Authenticated? %s, Callback=%s" % (loggedIn, self._gcBackend.is_authed(), self._gcBackend.get_callback_number()), UserWarning, 2)
361 callSuccess = self._gcBackend.dial(number)
362 except ValueError, e:
363 self._gcBackend._msg = e.message
367 self.display_error_message(self._gcBackend._msg)
369 self._dialpad.clear()
371 self._recentView.clear()
373 def _on_paste(self, *args):
374 contents = self._clipboard.wait_for_text()
375 self._dialpad.set_number(contents)
377 def _on_about_activate(self, *args):
378 dlg = gtk.AboutDialog()
379 dlg.set_name(self.__pretty_app_name__)
380 dlg.set_version(self.__version__)
381 dlg.set_copyright("Copyright 2008 - LGPL")
382 dlg.set_comments("Dialer is designed to interface with your Google Grandcentral account. This application is not affiliated with Google or Grandcentral in any way")
383 dlg.set_website("http://gc-dialer.garage.maemo.org/")
384 dlg.set_authors(["<z2n@merctech.com>", "Eric Warnke <ericew@gmail.com>", "Ed Page <edpage@byu.net>"])
392 failureCount, testCount = doctest.testmod()
394 print "Tests Successful"
401 gtk.gdk.threads_init()
402 if hildon is not None:
403 gtk.set_application_name(Dialcentral.__pretty_app_name__)
404 handle = Dialcentral()
408 class DummyOptions(object):
414 if __name__ == "__main__":
415 if len(sys.argv) > 1:
421 if optparse is not None:
422 parser = optparse.OptionParser()
423 parser.add_option("-t", "--test", action="store_true", dest="test", help="Run tests")
424 (commandOptions, commandArgs) = parser.parse_args()
426 commandOptions = DummyOptions()
429 if commandOptions.test: