13 from wimpworks import WimpWorks
14 from 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 # -----------------------------------------------------------------------
26 gettext.install('hermes','/opt/hermes/share/locale/')
27 WimpWorks.__init__(self, 'Hermes', version = '0.2.0', dbus_name = 'org.maemo.hermes')
28 self.set_background('background.png')
30 layout = wimpworks.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")
38 # -----------------------------------------------------------------------
39 def do_retrieve(self, widget):
40 self.sync(widget, False)
43 # -----------------------------------------------------------------------
44 def do_refresh(self, widget):
45 self.sync(widget, True)
48 # -----------------------------------------------------------------------
49 def do_accounts(self, widget = None):
50 dialog = gtk.Dialog(_('Accounts'), self.main_window)
51 dialog.add_button(_('Save'), gtk.RESPONSE_OK)
53 #pa = hildon.PannableArea()
58 #pa.set_size_request(600, 380)
60 use_facebook = self.new_checkbox(_('Use Facebook'), content)
61 use_facebook.set_active(self.get_use_facebook())
63 indent = self.new_indent(content)
64 self.link_control(use_facebook, gtk.Label(_('Note: authentication via web page')), indent)
66 fb_empty = self.link_control(use_facebook, self.new_checkbox(_('Create birthday-only contacts')), indent)
67 fb_empty.set_active(self.get_create_empty())
69 use_twitter = self.new_checkbox(_('Use Twitter'), content)
70 use_twitter.set_active(self.get_use_twitter())
72 indent = self.new_indent(content)
73 tw_user = self.link_control(use_twitter, self.new_input(_('Twitter username')), indent)
74 tw_user.set_text(self.get_twitter_credentials()[0])
76 tw_pass = self.link_control(use_twitter, self.new_input(_('Twitter password'), password = True), indent)
77 tw_pass.set_text(self.get_twitter_credentials()[1])
82 if result == gtk.RESPONSE_OK:
83 self.set_use_facebook(use_facebook.get_active())
84 self.set_create_empty(fb_empty.get_active())
85 self.set_use_twitter(use_twitter.get_active(), tw_user.get_text(), tw_pass.get_text())
90 # -----------------------------------------------------------------------
91 def sync(self, widget, force, main = True):
92 if main and not self.get_use_facebook() and not self.get_use_twitter():
93 saved = self.do_accounts()
94 if saved == gtk.RESPONSE_DELETE_EVENT:
98 self.main_window.set_property('sensitive', False)
99 thread.start_new_thread(self.sync, (widget, force, False))
103 twitter = (self.get_use_twitter() and self.get_twitter_credentials()) or None,
104 facebook = self.get_use_facebook(),
105 empty = self.get_create_empty())
107 fb2c.sync_contacts(resync = force)
108 gobject.idle_add(self.open_summary, fb2c)
110 except urllib2.HTTPError, e:
111 traceback.print_exc()
113 gobject.idle_add(self.report_error, _('Authentication problem. Check credentials.'), True)
115 gobject.idle_add(self.report_error, _('Network connection error. Check connectivity.'))
117 except urllib2.URLError, e:
118 traceback.print_exc()
119 gobject.idle_add(self.report_error, _('Network connection error. Check connectivity.'))
122 traceback.print_exc()
123 gobject.idle_add(self.report_error, _('Something went wrong: ') + e.message)
126 # -----------------------------------------------------------------------
127 def open_summary(self, fb2c):
128 gobject.idle_add(self.main_window.set_property, 'sensitive', True)
130 dialog = gtk.Dialog(_('Summary'), self.main_window)
131 dialog.add_button(_('Done'), gtk.RESPONSE_OK)
133 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
134 title = _('Updated %d contacts') % (len(fb2c.updated)))
135 button.connect('clicked', self.show_contacts, fb2c, fb2c.updated)
136 button.set_property('sensitive', len(fb2c.updated) > 0)
137 dialog.vbox.add(button)
139 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
140 title = _('Matched %d contacts') % (len(fb2c.matched)))
141 button.connect('clicked', self.show_contacts, fb2c, fb2c.matched)
142 button.set_property('sensitive', len(fb2c.matched) > 0)
143 dialog.vbox.add(button)
145 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
146 title = _('%d contacts unmatched') % (len(fb2c.unmatched)))
147 button.connect('clicked', self.show_contacts, fb2c, fb2c.unmatched)
148 button.set_property('sensitive', len(fb2c.unmatched) > 0)
149 dialog.vbox.add(button)
156 # -----------------------------------------------------------------------
157 def show_contacts(self, widget, fb2c, contacts):
158 view = contactview.ContactView(contacts)
160 dialog = gtk.Dialog(_('Contacts'), self.main_window)
161 view.connect('contact-activated', self.map_contact, fb2c)
162 dialog.vbox.add(view)
169 # -----------------------------------------------------------------------
170 def map_contact(self, widget, contact, fb2c):
171 view = mapcontact.MapContact(fb2c.friends, contact)
173 dialog = gtk.Dialog(contact.get_name(), self.main_window)
174 dialog.add_button(_('Update'), gtk.RESPONSE_OK)
175 dialog.vbox.add(view)
178 result = dialog.run()
180 if result == gtk.RESPONSE_OK:
181 friend = view.get_selected_friend()
183 if 'contact' in friend and friend['contact'] == contact:
184 hildon.hildon_banner_show_information(self.main_window, '', _("Removing existing mappings is not yet supported"))
185 elif view.contact_mapped:
186 if fb2c.update_contact(contact, friend, True):
187 fb2c.addresses.commit_contact(contact)
189 if fb2c.update_contact(contact, friend, False):
190 fb2c.addresses.commit_contact(contact)
193 # -----------------------------------------------------------------------
194 def need_auth(self, main = False):
196 hildon.hildon_banner_show_information(self.main_window, '', _("Need to authenticate with Facebook"))
198 gobject.idle_add(self.need_auth, True)
201 # -----------------------------------------------------------------------
202 def block_for_auth(self, main = False, lock = None):
204 note = gtk.Dialog(_('Facebook authorisation'), self.main_window)
205 note.add_button(_("Validate"), gtk.RESPONSE_OK)
206 note.vbox.add(gtk.Label(_("\nPress 'Validate' once Facebook has\nbeen authenticated in web browser.\n")))
215 lock = thread.allocate_lock()
217 gobject.idle_add(self.block_for_auth, True, lock)
222 # -----------------------------------------------------------------------
223 def progress(self, i, j, main = False):
226 self.progressbar = gtk.ProgressBar()
227 self.progressnote = gtk.Dialog(_("Fetching friends' info"), self.main_window)
228 self.progressnote.vbox.add(self.progressbar)
229 hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 1)
231 self.progressnote.show_all()
235 self.progressnote.set_title(_("Updating contacts"))
236 hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 0)
238 self.progressbar.set_fraction(float(i) / float(j))
241 self.progressnote.destroy()
245 gobject.idle_add(self.progress, i, j, True)
248 # -----------------------------------------------------------------------
249 def report_error(self, e, prefs = False):
250 if self.progressnote:
251 self.main_window.set_property('sensitive', True)
252 self.progressnote.destroy()
254 hildon.hildon_banner_show_information(self.main_window, '', e)
259 def get_use_facebook(self):
260 return self.gconf.get_bool("/apps/maemo/hermes/use_facebook")
263 def set_use_facebook(self, value):
264 self.gconf.set_bool("/apps/maemo/hermes/use_facebook", value)
266 def get_create_empty(self):
267 return self.gconf.get_bool("/apps/maemo/hermes/create_empty")
270 def set_create_empty(self, value):
271 self.gconf.set_bool("/apps/maemo/hermes/create_empty", value)
274 def get_use_twitter(self):
275 return self.gconf.get_bool("/apps/maemo/hermes/use_twitter")
278 def set_use_twitter(self, value, user, password):
279 self.gconf.set_bool("/apps/maemo/hermes/use_twitter", value)
280 self.gconf.set_string("/apps/maemo/hermes/twitter_user", user)
281 self.gconf.set_string("/apps/maemo/hermes/twitter_pwd", password)
284 def get_twitter_credentials(self):
285 return (self.gconf.get_string("/apps/maemo/hermes/twitter_user") or '',
286 self.gconf.get_string("/apps/maemo/hermes/twitter_pwd") or '')
289 # -------------------------------------------------------------------------
290 if __name__ == '__main__':