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:
70 self.main_window.set_property('sensitive', False)
71 thread.start_new_thread(self.sync, (widget, force, False))
75 for provider in enabled:
76 services.append(provider.service(self))
78 hermes = Hermes(services, self.progress)
80 gobject.idle_add(self.open_summary, hermes)
82 except urllib2.HTTPError, e:
85 gobject.idle_add(self.report_error, _('Authentication problem. Check credentials.'), True)
87 gobject.idle_add(self.report_error, _('Network connection error. Check connectivity.'))
89 except urllib2.URLError, e:
91 gobject.idle_add(self.report_error, _('Network connection error. Check connectivity.'))
95 gobject.idle_add(self.report_error, _('Something went wrong: ') + e.message)
98 # -----------------------------------------------------------------------
99 def open_summary(self, fb2c):
100 gobject.idle_add(self.main_window.set_property, 'sensitive', True)
102 dialog = gtk.Dialog(_('Summary'), self.main_window)
103 dialog.add_button(_('Done'), gtk.RESPONSE_OK)
105 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
106 title = _('Updated %d contacts') % (len(fb2c.updated)))
107 button.connect('clicked', self.show_contacts, fb2c, fb2c.updated)
108 button.set_property('sensitive', len(fb2c.updated) > 0)
109 dialog.vbox.add(button)
111 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
112 title = _('Matched %d contacts') % (len(fb2c.matched)))
113 button.connect('clicked', self.show_contacts, fb2c, fb2c.matched)
114 button.set_property('sensitive', len(fb2c.matched) > 0)
115 dialog.vbox.add(button)
117 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
118 title = _('%d contacts unmatched') % (len(fb2c.unmatched)))
119 button.connect('clicked', self.show_contacts, fb2c, fb2c.unmatched)
120 button.set_property('sensitive', len(fb2c.unmatched) > 0)
121 dialog.vbox.add(button)
128 # -----------------------------------------------------------------------
129 def show_contacts(self, widget, fb2c, contacts):
130 view = ContactView(contacts)
132 dialog = gtk.Dialog(_('Contacts'), self.main_window)
133 view.connect('contact-activated', self.map_contact, fb2c)
134 dialog.vbox.add(view)
141 # -----------------------------------------------------------------------
142 def map_contact(self, widget, contact, fb2c):
143 view = MapContact(fb2c.friends, contact)
145 dialog = gtk.Dialog(contact.get_name(), self.main_window)
146 dialog.add_button(_('Update'), gtk.RESPONSE_OK)
147 dialog.vbox.add(view)
150 result = dialog.run()
152 if result == gtk.RESPONSE_OK:
153 friend = view.get_selected_friend()
155 if 'contact' in friend and friend['contact'] == contact:
156 hildon.hildon_banner_show_information(self.main_window, '', _("Removing existing mappings is not yet supported"))
157 elif view.contact_mapped:
158 if fb2c.update_contact(contact, friend, True):
159 fb2c.addresses.commit_contact(contact)
161 if fb2c.update_contact(contact, friend, False):
162 fb2c.addresses.commit_contact(contact)
165 # -----------------------------------------------------------------------
166 def need_auth(self, main = False):
167 """Part of the GUI callback API."""
170 hildon.hildon_banner_show_information(self.main_window, '', _("Need to authenticate via browser"))
172 gobject.idle_add(self.need_auth, True)
175 # -----------------------------------------------------------------------
176 def block_for_auth(self, prompt = False, main = False, lock = None):
177 """Part of the GUI callback API."""
180 note = gtk.Dialog(_('Service authorisation'), self.main_window)
181 note.add_button(_("Validate"), gtk.RESPONSE_OK)
183 input = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
184 input.set_property('is-focus', False)
185 note.set_title(_("Verification code from web browser"))
188 note.vbox.add(gtk.Label(_("\nPress 'Validate' once account has\nbeen authorised in web browser.\n")))
194 if prompt and result == gtk.RESPONSE_OK:
195 print input.get_text()
196 return input.get_text()
202 lock = thread.allocate_lock()
204 gobject.idle_add(self.block_for_auth, prompt, True, lock)
209 # -----------------------------------------------------------------------
210 def progress(self, i, j, main = False):
211 """Part of the GUI callback API."""
215 self.progressbar = gtk.ProgressBar()
216 self.progressnote = gtk.Dialog(_("Fetching friends' info"), self.main_window)
217 self.progressnote.vbox.add(self.progressbar)
218 hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 1)
220 self.progressnote.show_all()
224 self.progressnote.set_title(_("Updating contacts"))
225 hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 0)
227 self.progressbar.set_fraction(float(i) / float(j))
230 self.progressnote.destroy()
234 gobject.idle_add(self.progress, i, j, True)
237 # -----------------------------------------------------------------------
238 def report_error(self, e, prefs = False):
239 if self.progressnote:
240 self.main_window.set_property('sensitive', True)
241 self.progressnote.destroy()
243 hildon.hildon_banner_show_information(self.main_window, '', e)