12 from wimpworks import WimpWorks
13 from hermes import Hermes
15 class HermesGUI(WimpWorks):
16 """Provides the GUI for Hermes, allowing the syncing of Facebook and
17 Twitter friends' information with the Evolution contacts' database.
19 Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
20 Released under the Artistic Licence."""
23 # -----------------------------------------------------------------------
25 WimpWorks.__init__(self, 'Hermes', version = '0.2.0', dbus_name = 'org.maemo.hermes')
26 self.set_background('background.png')
28 layout = wimpworks.HildonMainScreenLayout(offset = 0.8, container = self)
29 layout.add_button('Retrieve', "Get contacts' missing info")
30 layout.add_button('Refresh', "Update contacts' info")
32 self.add_menu_action("Accounts")
36 # -----------------------------------------------------------------------
37 def do_retrieve(self, widget):
38 self.sync(widget, False)
41 # -----------------------------------------------------------------------
42 def do_refresh(self, widget):
43 self.sync(widget, True)
46 # -----------------------------------------------------------------------
47 def do_accounts(self, widget = None):
48 dialog = gtk.Dialog('Accounts', self.main_window)
49 dialog.add_button('Save', gtk.RESPONSE_OK)
51 #pa = hildon.PannableArea()
56 #pa.set_size_request(600, 380)
58 use_facebook = self.new_checkbox('Use Facebook', content)
59 use_facebook.set_active(self.get_use_facebook())
61 indent = self.new_indent(content)
62 self.link_control(use_facebook, gtk.Label('Note: authentication via web page'), indent)
64 fb_empty = self.link_control(use_facebook, self.new_checkbox('Create birthday-only contacts'), indent)
65 fb_empty.set_active(self.get_create_empty())
67 use_twitter = self.new_checkbox('Use Twitter', content)
68 use_twitter.set_active(self.get_use_twitter())
70 indent = self.new_indent(content)
71 tw_user = self.link_control(use_twitter, self.new_input('Twitter username'), indent)
72 tw_user.set_text(self.get_twitter_credentials()[0])
74 tw_pass = self.link_control(use_twitter, self.new_input('Twitter password', password = True), indent)
75 tw_pass.set_text(self.get_twitter_credentials()[1])
80 if result == gtk.RESPONSE_OK:
81 self.set_use_facebook(use_facebook.get_active())
82 self.set_create_empty(fb_empty.get_active())
83 self.set_use_twitter(use_twitter.get_active(), tw_user.get_text(), tw_pass.get_text())
88 # -----------------------------------------------------------------------
89 def sync(self, widget, force, main = True):
90 if main and not self.get_use_facebook() and not self.get_use_twitter():
91 saved = self.do_accounts()
92 if saved == gtk.RESPONSE_DELETE_EVENT:
96 self.main_window.set_property('sensitive', False)
97 thread.start_new_thread(self.sync, (widget, force, False))
101 twitter = (self.get_use_twitter() and self.get_twitter_credentials()) or None,
102 facebook = self.get_use_facebook(),
103 empty = self.get_create_empty())
105 fb2c.sync_contacts(resync = force)
106 gobject.idle_add(self.open_summary, fb2c)
108 except urllib2.HTTPError, e:
109 traceback.print_exc()
111 gobject.idle_add(self.report_error, 'Authentication problem. Check credentials.', True)
113 gobject.idle_add(self.report_error, 'Network connection error. Check connectivity.')
115 except urllib2.URLError, e:
116 traceback.print_exc()
117 gobject.idle_add(self.report_error, 'Network connection error. Check connectivity.')
120 traceback.print_exc()
121 gobject.idle_add(self.report_error, 'Something went wrong: ' + e.message)
124 # -----------------------------------------------------------------------
125 def open_summary(self, fb2c):
126 gobject.idle_add(self.main_window.set_property, 'sensitive', True)
128 dialog = gtk.Dialog('Summary', self.main_window)
129 dialog.add_button('Done', gtk.RESPONSE_OK)
131 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
132 title = 'Updated %d contacts' % (len(fb2c.updated)))
133 button.connect('clicked', self.show_contacts, fb2c, fb2c.updated)
134 button.set_property('sensitive', len(fb2c.updated) > 0)
135 dialog.vbox.add(button)
137 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
138 title = 'Matched %d contacts' % (len(fb2c.matched)))
139 button.connect('clicked', self.show_contacts, fb2c, fb2c.matched)
140 button.set_property('sensitive', len(fb2c.matched) > 0)
141 dialog.vbox.add(button)
143 button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
144 title = '%d contacts unmatched' % (len(fb2c.unmatched)))
145 button.connect('clicked', self.show_contacts, fb2c, fb2c.unmatched)
146 button.set_property('sensitive', len(fb2c.unmatched) > 0)
147 dialog.vbox.add(button)
154 # -----------------------------------------------------------------------
155 def show_contacts(self, widget, fb2c, contacts):
156 view = contactview.ContactView(contacts)
158 dialog = gtk.Dialog('Contacts', self.main_window)
159 view.connect('contact-activated', self.map_contact, fb2c)
160 dialog.vbox.add(view)
167 # -----------------------------------------------------------------------
168 def map_contact(self, widget, contact, fb2c):
169 view = mapcontact.MapContact(fb2c.friends, contact)
171 dialog = gtk.Dialog(contact.get_name(), self.main_window)
172 dialog.add_button('Update', gtk.RESPONSE_OK)
173 dialog.vbox.add(view)
176 result = dialog.run()
178 if result == gtk.RESPONSE_OK:
179 friend = view.get_selected_friend()
181 if 'contact' in friend and friend['contact'] == contact:
182 hildon.hildon_banner_show_information(self.main_window, '', "Removing existing mappings is not yet supported")
183 elif view.contact_mapped:
184 hildon.hildon_banner_show_information(self.main_window, '', "Changing existing mappings is not yet supported")
186 if fb2c.update_contact(contact, friend, False):
187 fb2c.addresses.commit_contact(contact)
190 # -----------------------------------------------------------------------
191 def need_auth(self, main = False):
193 hildon.hildon_banner_show_information(self.main_window, '', "Need to authenticate with Facebook")
195 gobject.idle_add(self.need_auth, True)
198 # -----------------------------------------------------------------------
199 def block_for_auth(self, main = False, lock = None):
201 note = gtk.Dialog('Facebook authorisation', self.main_window)
202 note.add_button("Validate", gtk.RESPONSE_OK)
203 note.vbox.add(gtk.Label("\nPress 'Validate' once Facebook has\nbeen authenticated in web browser.\n"))
212 lock = thread.allocate_lock()
214 gobject.idle_add(self.block_for_auth, True, lock)
219 # -----------------------------------------------------------------------
220 def progress(self, i, j, main = False):
223 self.progressbar = gtk.ProgressBar()
224 self.progressnote = gtk.Dialog("Fetching friends' info", self.main_window)
225 self.progressnote.vbox.add(self.progressbar)
226 hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 1)
228 self.progressnote.show_all()
232 self.progressnote.set_title("Updating contacts")
233 hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 0)
235 self.progressbar.set_fraction(float(i) / float(j))
238 self.progressnote.destroy()
242 gobject.idle_add(self.progress, i, j, True)
245 # -----------------------------------------------------------------------
246 def report_error(self, e, prefs = False):
247 if self.progressnote:
248 self.main_window.set_property('sensitive', True)
249 self.progressnote.destroy()
251 hildon.hildon_banner_show_information(self.main_window, '', e)
256 def get_use_facebook(self):
257 return self.gconf.get_bool("/apps/maemo/hermes/use_facebook")
260 def set_use_facebook(self, value):
261 self.gconf.set_bool("/apps/maemo/hermes/use_facebook", value)
263 def get_create_empty(self):
264 return self.gconf.get_bool("/apps/maemo/hermes/create_empty")
267 def set_create_empty(self, value):
268 self.gconf.set_bool("/apps/maemo/hermes/create_empty", value)
271 def get_use_twitter(self):
272 return self.gconf.get_bool("/apps/maemo/hermes/use_twitter")
275 def set_use_twitter(self, value, user, password):
276 self.gconf.set_bool("/apps/maemo/hermes/use_twitter", value)
277 self.gconf.set_string("/apps/maemo/hermes/twitter_user", user)
278 self.gconf.set_string("/apps/maemo/hermes/twitter_pwd", password)
281 def get_twitter_credentials(self):
282 return (self.gconf.get_string("/apps/maemo/hermes/twitter_user") or '',
283 self.gconf.get_string("/apps/maemo/hermes/twitter_pwd") or '')
286 # -------------------------------------------------------------------------
287 if __name__ == '__main__':