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"),
53 _data_path = os.path.join(os.path.expanduser("~"), ".dialcentral")
56 self._gcBackend = None
57 self._clipboard = gtk.clipboard_get()
59 self._deviceIsOnline = True
61 self._accountView = None
62 self._recentView = None
63 self._contactsView = None
65 for path in Dialcentral._glade_files:
66 if os.path.isfile(path):
67 self._widgetTree = gtk.glade.XML(path)
70 self.display_error_message("Cannot find dialcentral.glade")
74 self._window = self._widgetTree.get_widget("Dialpad")
75 self._notebook = self._widgetTree.get_widget("notebook")
79 self._isFullScreen = False
80 if hildon is not None and self._window is gtk.Window:
81 warnings.warn("Hildon installed but glade file not updated to work with hildon", UserWarning, 2)
83 elif hildon is not None:
84 self._app = hildon.Program()
85 self._window = hildon.Window()
86 self._widgetTree.get_widget("vbox1").reparent(self._window)
87 self._app.add_window(self._window)
88 self._widgetTree.get_widget("usernameentry").set_property('hildon-input-mode', 7)
89 self._widgetTree.get_widget("passwordentry").set_property('hildon-input-mode', 7|(1 << 29))
91 gtkMenu = self._widgetTree.get_widget("dialpad_menubar")
93 for child in gtkMenu.get_children():
95 self._window.set_menu(menu)
98 self._window.connect("key-press-event", self._on_key_press)
99 self._window.connect("window-state-event", self._on_window_state_change)
101 warnings.warn("No Hildon", UserWarning, 2)
103 if hildon is not None:
104 self._window.set_title("Keypad")
106 self._window.set_title("%s - Keypad" % self.__pretty_app_name__)
109 "on_loginbutton_clicked": self._on_loginbutton_clicked,
110 "on_loginclose_clicked": self._on_loginclose_clicked,
111 "on_dialpad_quit": self._on_close,
113 self._widgetTree.signal_autoconnect(callbackMapping)
116 self._window.connect("destroy", gtk.main_quit)
117 self._window.show_all()
119 backgroundSetup = threading.Thread(target=self._idle_setup)
120 backgroundSetup.setDaemon(True)
121 backgroundSetup.start()
123 def _idle_setup(self):
125 If something can be done after the UI loads, push it here so it's not blocking the UI
133 self._osso = osso.Context(Dialcentral.__app_name__, Dialcentral.__version__, False)
134 device = osso.DeviceState(self._osso)
135 device.set_device_state_callback(self._on_device_state_change, 0)
137 warnings.warn("No OSSO", UserWarning, 2)
143 self._connection = None
144 if conic is not None:
145 self._connection = conic.Connection()
146 self._connection.connect("connection-event", self._on_connection_change, Dialcentral.__app_magic__)
147 self._connection.request_connection(conic.CONNECT_FLAG_NONE)
149 warnings.warn("No Internet Connectivity API ", UserWarning, 2)
154 # import gmail_backend
155 # import maemo_backend
158 cookieFile = os.path.join(self._data_path, "cookies.txt")
160 os.makedirs(os.path.dirname(cookieFile))
164 self._gcBackend = gc_backend.GCDialer(cookieFile)
165 gtk.gdk.threads_enter()
167 self._dialpad = gc_views.Dialpad(self._widgetTree)
168 self._dialpad.set_number("")
169 self._accountView = gc_views.AccountInfo(self._widgetTree, self._gcBackend)
170 self._recentView = gc_views.RecentCallsView(self._widgetTree, self._gcBackend)
171 self._contactsView = gc_views.ContactsView(self._widgetTree, self._gcBackend)
173 gtk.gdk.threads_leave()
175 self._dialpad.dial = self._on_dial_clicked
176 self._recentView.number_selected = self._on_number_selected
177 self._contactsView.number_selected = self._on_number_selected
179 #This is where the blocking can start
180 if self._gcBackend.is_authed():
181 gtk.gdk.threads_enter()
183 self._accountView.update()
185 gtk.gdk.threads_leave()
187 self.attempt_login(2)
189 fsContactsPath = os.path.join(self._data_path, "contacts")
192 evo_backend.EvolutionAddressBook(),
193 file_backend.FilesystemAddressBookFactory(fsContactsPath),
195 mergedBook = gc_views.MergedAddressBook(addressBooks, gc_views.MergedAddressBook.advanced_lastname_sorter)
196 self._contactsView.append(mergedBook)
197 self._contactsView.extend(addressBooks)
198 self._contactsView.open_addressbook(*self._contactsView.get_addressbooks().next()[0][0:2])
199 gtk.gdk.threads_enter()
201 self._contactsView._init_books_combo()
203 gtk.gdk.threads_leave()
206 "on_paste": self._on_paste,
207 "on_clearcookies_clicked": self._on_clearcookies_clicked,
208 "on_notebook_switch_page": self._on_notebook_switch_page,
209 "on_about_activate": self._on_about_activate,
211 self._widgetTree.signal_autoconnect(callbackMapping)
215 def attempt_login(self, numOfAttempts = 1):
217 @todo Handle user notification better like attempting to login and failed login
219 @note Not meant to be called directly, but run as a seperate thread.
221 assert 0 < numOfAttempts, "That was pointless having 0 or less login attempts"
223 if not self._deviceIsOnline:
224 warnings.warn("Attempted to login while device was offline", UserWarning, 2)
227 if self._gcBackend.is_authed():
230 for x in xrange(numOfAttempts):
231 gtk.gdk.threads_enter()
233 dialog = self._widgetTree.get_widget("login_dialog")
234 dialog.set_transient_for(self._window)
235 dialog.set_default_response(gtk.RESPONSE_CLOSE)
238 username = self._widgetTree.get_widget("usernameentry").get_text()
239 password = self._widgetTree.get_widget("passwordentry").get_text()
240 self._widgetTree.get_widget("passwordentry").set_text("")
243 gtk.gdk.threads_leave()
244 loggedIn = self._gcBackend.login(username, password)
246 gtk.gdk.threads_enter()
248 if self._gcBackend.get_callback_number() is None:
249 self._gcBackend.set_sane_callback()
250 self._accountView.update()
252 gtk.gdk.threads_leave()
257 def display_error_message(self, msg):
258 error_dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, msg)
260 def close(dialog, response, editor):
261 editor.about_dialog = None
263 error_dialog.connect("response", close, self)
267 def _on_close(*args, **kwds):
270 def _on_device_state_change(self, shutdown, save_unsaved_data, memory_low, system_inactivity, message, userData):
272 For shutdown or save_unsaved_data, our only state is cookies and I think the cookie manager handles that for us.
273 For system_inactivity, we have no background tasks to pause
275 @note Hildon specific
278 self._gcBackend.clear_caches()
279 self._contactsView.clear_caches()
282 def _on_connection_change(self, connection, event, magicIdentifier):
284 @note Hildon specific
288 status = event.get_status()
289 error = event.get_error()
290 iap_id = event.get_iap_id()
291 bearer = event.get_bearer_type()
293 if status == conic.STATUS_CONNECTED:
294 self._window.set_sensitive(True)
295 self._deviceIsOnline = True
296 backgroundLogin = threading.Thread(target=self.attempt_login, args=[2])
297 backgroundLogin.setDaemon(True)
298 backgroundLogin.start()
299 elif status == conic.STATUS_DISCONNECTED:
300 self._window.set_sensitive(False)
301 self._deviceIsOnline = False
303 def _on_window_state_change(self, widget, event, *args):
305 @note Hildon specific
307 if event.new_window_state & gtk.gdk.WINDOW_STATE_FULLSCREEN:
308 self._isFullScreen = True
310 self._isFullScreen = False
312 def _on_key_press(self, widget, event, *args):
314 @note Hildon specific
316 if event.keyval == gtk.keysyms.F6:
317 if self._isFullScreen:
318 self._window.unfullscreen()
320 self._window.fullscreen()
322 def _on_loginbutton_clicked(self, *args):
323 self._widgetTree.get_widget("login_dialog").response(gtk.RESPONSE_OK)
325 def _on_loginclose_clicked(self, *args):
328 def _on_clearcookies_clicked(self, *args):
329 self._gcBackend.logout()
330 self._accountView.clear()
331 self._recentView.clear()
332 self._contactsView.clear()
334 # re-run the inital grandcentral setup
335 backgroundLogin = threading.Thread(target=self.attempt_login, args=[2])
336 backgroundLogin.setDaemon(True)
337 backgroundLogin.start()
339 def _on_notebook_switch_page(self, notebook, page, page_num):
341 self._contactsView.update()
343 self._recentView.update()
345 tabTitle = self._notebook.get_tab_label(self._notebook.get_nth_page(page_num)).get_text()
346 if hildon is not None:
347 self._window.set_title(tabTitle)
349 self._window.set_title("%s - %s" % (self.__pretty_app_name__, tabTitle))
351 def _on_number_selected(self, number):
352 self._dialpad.set_number(number)
353 self._notebook.set_current_page(0)
355 def _on_dial_clicked(self, number):
357 @todo Potential blocking on web access, maybe we should defer parts of this or put up a dialog?
359 loggedIn = self._gcBackend.is_authed()
362 #loggedIn = self.attempt_login(2)
364 if not loggedIn or not self._gcBackend.is_authed() or self._gcBackend.get_callback_number() == "":
365 self.display_error_message("Backend link with grandcentral is not working, please try again")
366 warnings.warn("Backend Status: Logged in? %s, Authenticated? %s, Callback=%s" % (loggedIn, self._gcBackend.is_authed(), self._gcBackend.get_callback_number()), UserWarning, 2)
371 self._gcBackend.dial(number)
373 except RuntimeError, e:
374 self.display_error_message(e.message)
375 except ValueError, e:
376 self.display_error_message(e.message)
379 self._dialpad.clear()
380 self._recentView.clear()
382 def _on_paste(self, *args):
383 contents = self._clipboard.wait_for_text()
384 self._dialpad.set_number(contents)
386 def _on_about_activate(self, *args):
387 dlg = gtk.AboutDialog()
388 dlg.set_name(self.__pretty_app_name__)
389 dlg.set_version(self.__version__)
390 dlg.set_copyright("Copyright 2008 - LGPL")
391 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")
392 dlg.set_website("http://gc-dialer.garage.maemo.org/")
393 dlg.set_authors(["<z2n@merctech.com>", "Eric Warnke <ericew@gmail.com>", "Ed Page <edpage@byu.net>"])
401 failureCount, testCount = doctest.testmod()
403 print "Tests Successful"
410 gtk.gdk.threads_init()
411 if hildon is not None:
412 gtk.set_application_name(Dialcentral.__pretty_app_name__)
413 handle = Dialcentral()
417 class DummyOptions(object):
423 if __name__ == "__main__":
424 if len(sys.argv) > 1:
430 if optparse is not None:
431 parser = optparse.OptionParser()
432 parser.add_option("-t", "--test", action="store_true", dest="test", help="Run tests")
433 (commandOptions, commandArgs) = parser.parse_args()
435 commandOptions = DummyOptions()
438 if commandOptions.test: