Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Hermes
Summary Lists News SCM
1 #!/usr/bin/env python
3 import gtk, gobject
4 import gnome.gconf
5 import hildon, osso
6 import traceback
7 import time
8 import thread
9 import contactview
10 from hermes import Hermes
12 gobject.threads_init()
14 class HermesGUI:
15   """Provides the GUI for Hermes, allowing the syncing of Facebook and
16      Twitter friends' information with the Evolution contacts' database.
17        
18      Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
19      Released under the Artistic Licence."""
22   # -----------------------------------------------------------------------
23   def __init__(self):
24     """Constructor. Initialises the gconf connection."""
25     self.gc = gnome.gconf.client_get_default()
28   # -----------------------------------------------------------------------
29   def doSync(self, widget, force, main = True):
30     if main and not self.get_use_facebook() and not self.get_use_twitter():
31       saved = self.open_prefs()
32       if saved == gtk.RESPONSE_DELETE_EVENT:
33         return
35     if main:
36       self.window.set_property('sensitive', False)
37       thread.start_new_thread(self.doSync, (widget, force, False))
38     else:
39       try:
40         fb2c = Hermes(self,
41                       twitter = (self.get_use_twitter() and self.get_twitter_credentials()) or None,
42                       facebook = self.get_use_facebook())
43         fb2c.load_friends()
44         fb2c.sync_contacts(resync = force)
45         gobject.idle_add(self.open_summary, fb2c)
46       except Exception, e:
47         traceback.print_exc()
48         gobject.idle_add(self.report_error, 'Something went wrong: ' + e.message)
51   # -----------------------------------------------------------------------
52   def open_summary(self, fb2c):
53     gobject.idle_add(self.window.set_property, 'sensitive', True)
55     dialog = gtk.Dialog('Summary', self.window)
56     dialog.add_button('Done', gtk.RESPONSE_OK)
57     
58     button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
59                            title = 'Updated %d contacts' % (len(fb2c.updated)))
60     button.connect('clicked', self.show_contacts, fb2c.updated)
61     button.set_property('sensitive', len(fb2c.updated) > 0)
62     dialog.vbox.add(button)
63     
64     button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
65                            title = 'Matched %d contacts' % (len(fb2c.matched)))
66     button.connect('clicked', self.show_contacts, fb2c.matched)
67     button.set_property('sensitive', len(fb2c.matched) > 0)
68     dialog.vbox.add(button)
69     
70     button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
71                            title = '%d contacts unmatched' % (len(fb2c.unmatched)))
72     button.connect('clicked', self.show_contacts, fb2c.unmatched)
73     button.set_property('sensitive', len(fb2c.unmatched) > 0)
74     dialog.vbox.add(button)
75     
76     dialog.show_all()
77     dialog.run()
78     dialog.hide()
79   
81   # -----------------------------------------------------------------------
82   def show_contacts(self, widget, contacts):
83     view = contactview.ContactView(contacts)
85     dialog = gtk.Dialog('Contacts', self.window)
86     dialog.add_button('Back', gtk.RESPONSE_OK)
87     #view.connect('contact-activated', self.map_contact)
88     dialog.vbox.add(view)
89     dialog.show_all()
90     
91     dialog.run()
92     dialog.hide()
93     
94     
95   # -----------------------------------------------------------------------
96   def map_contact(self, widget, contact):
97     print widget, contact
100   # -----------------------------------------------------------------------
101   def open_prefs(self, widget = None):
102     dialog = gtk.Dialog('Accounts', self.window)
103     dialog.add_button('Save', gtk.RESPONSE_OK)
105     use_facebook = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
106     use_facebook.set_label('Use Facebook')
107     use_facebook.set_active(self.get_use_facebook())
108     dialog.vbox.add(use_facebook)
110     use_twitter = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
111     use_twitter.set_label('Use Twitter')
112     use_twitter.set_active(self.get_use_twitter())
113     dialog.vbox.add(use_twitter)
115     tw_indent = gtk.HBox()
116     tw_user = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
117     tw_user.set_property('hildon-input-mode', gtk.HILDON_GTK_INPUT_MODE_FULL)
118     tw_user.set_placeholder("Username")
119     tw_user.set_property('is-focus', False)
120     tw_user.set_text(self.get_twitter_credentials()[0])
121     self.sync_edit(use_twitter, tw_user)
122     use_twitter.connect('toggled', self.sync_edit, tw_user)
123     tw_indent.pack_start(tw_user, padding=48)
124     dialog.vbox.add(tw_indent)
126     tw_indent = gtk.HBox()
127     tw_pass = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
128     tw_pass.set_placeholder("Password")
129     tw_pass.set_property('hildon-input-mode', gtk.HILDON_GTK_INPUT_MODE_FULL |
130                                               gtk.HILDON_GTK_INPUT_MODE_INVISIBLE)
131     tw_pass.set_text(self.get_twitter_credentials()[1])
132     self.sync_edit(use_twitter, tw_pass)
133     use_twitter.connect('toggled', self.sync_edit, tw_pass)
134     tw_indent.pack_start(tw_pass, padding=48)
135     dialog.vbox.add(tw_indent)
137     dialog.show_all()
138     result = dialog.run()
139     dialog.hide()
140     if result == gtk.RESPONSE_OK:
141       self.set_use_facebook(use_facebook.get_active())
142       self.set_use_twitter(use_twitter.get_active(), tw_user.get_text(), tw_pass.get_text())
144     return result
145     
146     
147   # -----------------------------------------------------------------------
148   def sync_edit(self, use_twitter, edit):
149     edit.set_property('sensitive', use_twitter.get_active())
152   # -----------------------------------------------------------------------
153   def need_auth(self, main = False):
154     if main:
155       hildon.hildon_banner_show_information(self.window, '', "Need to authenticate with Facebook")
156     else:
157       gobject.idle_add(self.need_auth, True)
158     
160   # -----------------------------------------------------------------------
161   def block_for_auth(self, main = False, lock = None):
162     if main:
163       note = gtk.Dialog('Facebook authorisation', self.window)
164       note.add_button("Validate", gtk.RESPONSE_OK)
165       note.vbox.add(gtk.Label("\nPress 'Validate' once Facebook has\nbeen authenticated in web browser.\n"))
167       note.show_all()
168       result = note.run()
169       note.hide()
170       lock.release()
171       
172     else:
173       time.sleep(2)
174       lock = thread.allocate_lock()
175       lock.acquire()
176       gobject.idle_add(self.block_for_auth, True, lock)
177       lock.acquire()
178       lock.release()
181   # -----------------------------------------------------------------------
182   def progress(self, i, j, main = False):
183     if main:
184       if i == 0:
185         self.progressbar = gtk.ProgressBar()
186         self.progressnote = gtk.Dialog("Fetching friends' info", self.window)
187         self.progressnote.vbox.add(self.progressbar)
188         hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 1)
189         
190         self.progressnote.show_all()
191         
192       elif i < j:
193         if i == 1:
194           self.progressnote.set_title("Updating contacts")
195           hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 0)
196           
197         self.progressbar.set_fraction(float(i) / float(j))
198         
199       else:
200         self.progressnote.destroy()
201         
202       print i,j
203     else:
204       gobject.idle_add(self.progress, i, j, True)
207   # -----------------------------------------------------------------------
208   def report_error(self, e):
209     hildon.hildon_banner_show_information(self.window, '', e)
212   # -----------------------------------------------------------------------
213   def main(self):
214     # -- Window and app...
215     #
216     self.app = hildon.Program()
217     self.window = hildon.Window()
218     gtk.set_application_name('Hermes')
219     self.osso_context = osso.Context('org.maemo.hermes', '0.0.3', False)
220     self.app.add_window(self.window)
222     self.window.connect("delete-event", gtk.main_quit)
224     # -- Main window buttons...
225     #
226     box = gtk.HButtonBox()
227     self.window.add(box)
228     
229     box.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
230     button = hildon.Button(gtk.HILDON_SIZE_THUMB_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
231                            title = 'Retrieve', value = "Get contacts' missing info")
232     button.set_property('width-request', 250)
233     button.connect('clicked', self.doSync, False)
234     box.add(button)
236     button = hildon.Button(gtk.HILDON_SIZE_THUMB_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
237                            title = 'Update', value = "Update contacts' info")
238     button.set_property('width-request', 250)
239     button.connect('clicked', self.doSync, True)
240     box.add(button)
241     
242     # -- Application menu...
243     #
244     menu = hildon.AppMenu()
245     button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
246     button.set_label("Accounts")
247     button.connect("clicked", self.open_prefs)
248     menu.append(button)
249     menu.show_all()
250     self.window.set_app_menu(menu)
251   
252     self.window.show_all()
253     gtk.main()
256   def get_use_facebook(self):
257     return self.gc.get_bool("/apps/maemo/hermes/use_facebook")
260   def set_use_facebook(self, value):
261     self.gc.set_bool("/apps/maemo/hermes/use_facebook", value)
264   def get_use_twitter(self):
265     return self.gc.get_bool("/apps/maemo/hermes/use_twitter")
268   def set_use_twitter(self, value, user, password):
269     self.gc.set_bool("/apps/maemo/hermes/use_twitter", value)
270     self.gc.set_string("/apps/maemo/hermes/twitter_user", user)
271     self.gc.set_string("/apps/maemo/hermes/twitter_pwd", password)
272     
273   
274   def get_twitter_credentials(self):
275     return (self.gc.get_string("/apps/maemo/hermes/twitter_user") or '',
276             self.gc.get_string("/apps/maemo/hermes/twitter_pwd") or '')
279 # -------------------------------------------------------------------------
280 if __name__ == '__main__':
281   HermesGUI().main()

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment