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))
80 hermes = Hermes(services, self.progress)
82 gobject.idle_add(self.open_summary, hermes)
84 except urllib2.HTTPError, e:
87 gobject.idle_add(self.report_error, _('Authentication problem. Check credentials.'), True)
89 gobject.idle_add(self.report_error, _('Network connection error. Check connectivity.'))
91 except urllib2.URLError, e:
93 gobject.idle_add(self.report_error, _('Network connection error. Check connectivity.'))
97 gobject.idle_add(self.report_error, _('Something went wrong: ') + e.message)
100 # -----------------------------------------------------------------------
101 def open_summary(self, fb2c):
102 gobject.idle_add(self.main_window.set_property, 'sensitive', True)
104 dialog = gtk.Dialog(_('Summary'), self.main_window)
105 dialog.add_button(_('Done'), gtk.RESPONSE_OK)
107 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
108 title = _('Updated %d contacts') % (len(fb2c.updated)))
109 button.connect('clicked', self.show_contacts, fb2c, fb2c.updated)
110 button.set_property('sensitive', len(fb2c.updated) > 0)
111 dialog.vbox.add(button)
113 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
114 title = _('Matched %d contacts') % (len(fb2c.matched)))
115 button.connect('clicked', self.show_contacts, fb2c, fb2c.matched)
116 button.set_property('sensitive', len(fb2c.matched) > 0)
117 dialog.vbox.add(button)
119 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
120 title = _('%d contacts unmatched') % (len(fb2c.unmatched)))
121 button.connect('clicked', self.show_contacts, fb2c, fb2c.unmatched)
122 button.set_property('sensitive', len(fb2c.unmatched) > 0)
123 dialog.vbox.add(button)
130 # -----------------------------------------------------------------------
131 def show_contacts(self, widget, fb2c, contacts):
132 view = ContactView(contacts)
134 dialog = gtk.Dialog(_('Contacts'), self.main_window)
135 view.connect('contact-activated', self.map_contact, fb2c)
136 dialog.vbox.add(view)
143 # -----------------------------------------------------------------------
144 def map_contact(self, widget, contact, fb2c):
145 view = MapContact(fb2c.friends, contact)
147 dialog = gtk.Dialog(contact.get_name(), self.main_window)
148 dialog.add_button(_('Update'), gtk.RESPONSE_OK)
149 dialog.vbox.add(view)
152 result = dialog.run()
154 if result == gtk.RESPONSE_OK:
155 friend = view.get_selected_friend()
157 if 'contact' in friend and friend['contact'] == contact:
158 hildon.hildon_banner_show_information(self.main_window, '', _("Removing existing mappings is not yet supported"))
159 elif view.contact_mapped:
160 if fb2c.update_contact(contact, friend, True):
161 fb2c.addresses.commit_contact(contact)
163 if fb2c.update_contact(contact, friend, False):
164 fb2c.addresses.commit_contact(contact)
167 # -----------------------------------------------------------------------
168 def need_auth(self, main = False):
169 """Part of the GUI callback API."""
172 hildon.hildon_banner_show_information(self.main_window, '', _("Need to authenticate via browser"))
174 gobject.idle_add(self.need_auth, True)
177 # -----------------------------------------------------------------------
178 def block_for_auth(self, prompt = False, main = False, lock = None):
179 """Part of the GUI callback API."""
182 note = gtk.Dialog(_('Service authorisation'), self.main_window)
183 note.add_button(_("Validate"), gtk.RESPONSE_OK)
185 input = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
186 input.set_property('is-focus', False)
187 note.set_title(_("Verification code from web browser"))
190 note.vbox.add(gtk.Label(_("\nPress 'Validate' once account has\nbeen authorised in web browser.\n")))
196 if prompt and result == gtk.RESPONSE_OK:
197 print input.get_text()
198 return input.get_text()
204 lock = thread.allocate_lock()
206 gobject.idle_add(self.block_for_auth, prompt, True, lock)
211 # -----------------------------------------------------------------------
212 def progress(self, i, j, main = False):
213 """Part of the GUI callback API."""
217 self.progressbar = gtk.ProgressBar()
218 self.progressnote = gtk.Dialog(_("Fetching friends' info"), self.main_window)
219 self.progressnote.vbox.add(self.progressbar)
220 hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 1)
222 self.progressnote.show_all()
226 self.progressnote.set_title(_("Updating contacts"))
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, 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)