8 from org.bleb.wimpworks import WimpWorks
9 from org.maemo.hermes.gui.contactview import ContactView
10 from org.maemo.hermes.gui.mapcontact import MapContact
11 from org.maemo.hermes.gui.accountsdialogue import AccountsDialogue
12 from org.bleb.wimpworks import HildonMainScreenLayout
13 from org.maemo.hermes.engine.syncjob import SyncJob
14 from org.maemo.hermes.engine.hermes import Hermes
16 class HermesGUI(WimpWorks):
17 """Provides the GUI for Hermes, allowing the syncing of Facebook and
18 Twitter friends' information with the Evolution contacts' database.
20 Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
21 Released under the Artistic Licence."""
24 # -----------------------------------------------------------------------
25 def __init__(self, providers = None):
26 gettext.install('hermes','/opt/hermes/share/locale/')
27 WimpWorks.__init__(self, 'Hermes', version = '0.9.0', dbus_name = 'org.maemo.hermes')
28 self.set_background('background.png')
30 layout = HildonMainScreenLayout(offset = 0.8, container = self)
31 layout.add_button('Retrieve', _("Get contacts' missing info"))
32 layout.add_button('Refresh', _("Update contacts' info"))
34 self.add_menu_action("Accounts")
37 self.providers = providers
38 self.progressnote = None
41 # -----------------------------------------------------------------------
42 def do_retrieve(self, widget):
43 self.sync(widget, False)
46 # -----------------------------------------------------------------------
47 def do_refresh(self, widget):
48 self.sync(widget, True)
51 # -----------------------------------------------------------------------
52 def do_accounts(self, widget = None):
53 dialog = AccountsDialogue(self.main_window, self.providers)
57 # -----------------------------------------------------------------------
58 def sync(self, widget, force, main = True):
60 for provider in self.providers:
61 if self.gconf.get_bool('/apps/maemo/hermes/use_%s' % (provider.get_id())):
62 enabled.append(provider)
64 if main and len(enabled) == 0:
65 saved = self.do_accounts()
66 if saved == gtk.RESPONSE_DELETE_EVENT:
69 print "doing sync", main
72 self.main_window.set_property('sensitive', False)
73 thread.start_new_thread(self.sync, (widget, force, False))
76 self.progress('', 0, 0)
78 for provider in enabled:
79 services.append(provider.service(self))
81 hermes = Hermes(services, self.progress)
83 gobject.idle_add(self.open_summary, hermes)
85 except urllib2.HTTPError, e:
88 gobject.idle_add(self.report_error, _('Authentication problem. Check credentials.'), True)
90 gobject.idle_add(self.report_error, _('Network connection error. Check connectivity.'))
92 except urllib2.URLError, e:
94 gobject.idle_add(self.report_error, _('Network connection error. Check connectivity.'))
98 gobject.idle_add(self.report_error, _('Something went wrong: ') + e.message)
101 # -----------------------------------------------------------------------
102 def open_summary(self, fb2c):
103 gobject.idle_add(self.main_window.set_property, 'sensitive', True)
105 dialog = gtk.Dialog(_('Summary'), self.main_window)
106 dialog.add_button(_('Done'), gtk.RESPONSE_OK)
108 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
109 title = _('Updated %d contacts') % (len(fb2c.updated)))
110 button.connect('clicked', self.show_contacts, fb2c, fb2c.updated)
111 button.set_property('sensitive', len(fb2c.updated) > 0)
112 dialog.vbox.add(button)
114 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
115 title = _('Matched %d contacts') % (len(fb2c.matched)))
116 button.connect('clicked', self.show_contacts, fb2c, fb2c.matched)
117 button.set_property('sensitive', len(fb2c.matched) > 0)
118 dialog.vbox.add(button)
120 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
121 title = _('%d contacts unmatched') % (len(fb2c.unmatched)))
122 button.connect('clicked', self.show_contacts, fb2c, fb2c.unmatched)
123 button.set_property('sensitive', len(fb2c.unmatched) > 0)
124 dialog.vbox.add(button)
131 # -----------------------------------------------------------------------
132 def show_contacts(self, widget, fb2c, contacts):
133 view = ContactView(contacts)
135 dialog = gtk.Dialog(_('Contacts'), self.main_window)
136 view.connect('contact-activated', self.map_contact, fb2c)
137 dialog.vbox.add(view)
144 # -----------------------------------------------------------------------
145 def map_contact(self, widget, contact, fb2c):
146 view = MapContact(fb2c.friends, contact)
148 dialog = gtk.Dialog(contact.get_name(), self.main_window)
149 dialog.add_button(_('Update'), gtk.RESPONSE_OK)
150 dialog.vbox.add(view)
153 result = dialog.run()
155 if result == gtk.RESPONSE_OK:
156 friend = view.get_selected_friend()
158 if 'contact' in friend and friend['contact'] == contact:
159 hildon.hildon_banner_show_information(self.main_window, '', _("Removing existing mappings is not yet supported"))
160 elif view.contact_mapped:
161 if fb2c.update_contact(contact, friend, True):
162 fb2c.addresses.commit_contact(contact)
164 if fb2c.update_contact(contact, friend, False):
165 fb2c.addresses.commit_contact(contact)
168 # -----------------------------------------------------------------------
169 def need_auth(self, main = False):
170 """Part of the GUI callback API."""
173 hildon.hildon_banner_show_information(self.main_window, '', _("Need to authenticate via browser"))
175 gobject.idle_add(self.need_auth, True)
178 # -----------------------------------------------------------------------
179 def block_for_auth(self, prompt = False, main = False, lock = None):
180 """Part of the GUI callback API."""
183 note = gtk.Dialog(_('Service authorisation'), self.main_window)
184 note.add_button(_("Validate"), gtk.RESPONSE_OK)
186 input = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
187 input.set_property('is-focus', False)
188 note.set_title(_("Verification code from web browser"))
191 note.vbox.add(gtk.Label(_("\nPress 'Validate' once account has\nbeen authorised in web browser.\n")))
197 if prompt and result == gtk.RESPONSE_OK:
198 print input.get_text()
199 return input.get_text()
205 lock = thread.allocate_lock()
207 gobject.idle_add(self.block_for_auth, prompt, True, lock)
212 # -----------------------------------------------------------------------
213 def progress(self, msg, i, j, main = False):
214 """Part of the GUI callback API."""
218 self.progressbar = gtk.ProgressBar()
219 self.progressnote = gtk.Dialog(_("Initialising connections"), self.main_window)
220 self.progressnote.vbox.add(self.progressbar)
221 hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 1)
223 self.progressnote.show_all()
226 self.progressnote.set_title(_(msg))
227 hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 0)
229 self.progressbar.set_fraction(float(i) / float(j))
232 self.progressnote.destroy()
236 gobject.idle_add(self.progress, msg, i, j, True)
239 # -----------------------------------------------------------------------
240 def report_error(self, e, prefs = False):
241 if self.progressnote:
242 self.main_window.set_property('sensitive', True)
243 self.progressnote.destroy()
245 hildon.hildon_banner_show_information(self.main_window, '', e)