Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Hermes
Summary Lists News SCM
d1c92cc8339ee729b9399412623fd8fe5e2ee734
1 #!/usr/bin/env python
3 import gtk, gobject
4 import gnome.gconf
5 import traceback
6 import time
7 import thread
8 import os.path
9 import contactview
10 import urllib2
11 from wimpworks import WimpWorks
12 from hermes import Hermes
14 gobject.threads_init()
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.
19        
20      Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
21      Released under the Artistic Licence."""
24   # -----------------------------------------------------------------------
25   def __init__(self):
26     WimpWorks.__init__(self, 'Hermes', version = '0.1.1', 'org.maemo.hermes')
27     self.set_background('background.png')
28     
29     layout = wimporks.HildonMainScreenLayout(offset = 0.8, container = self)
30     layout.add_button('Retrieve', "Get contacts' missing info")
31     layout.add_button('Refresh', "Update contacts' info")
33     self.add_menu_action("Accounts")
34     self.menu.show_all()
36   
37   # -----------------------------------------------------------------------
38   def do_retrieve(self, widget):
39     self.sync(widget, False)
40     
41     
42   # -----------------------------------------------------------------------
43   def do_refresh(self, widget):
44     self.sync(widget, True)
47   # -----------------------------------------------------------------------
48   def do_accounts(self, widget = None):
49     dialog = gtk.Dialog('Accounts', self.window)
50     dialog.add_button('Save', gtk.RESPONSE_OK)
52     #pa = hildon.PannableArea()
53     #dialog.vbox.add(pa)
54     content = dialog.vbox 
55     #content = gtk.VBox()
56     #pa.add(content)
57     #pa.set_size_request(600, 380)
59     use_facebook = self.new_checkbox('Use Facebook', content)
60     use_facebook.set_active(self.get_use_facebook())
62     indent = self.new_indent(content)
63     self.link_control(use_facebook, gtk.Label('Note: authentication via web page'), indent)
64     
65     fb_empty = self.link_control(use_facebook, self.new_checkbox('Create birthday-only contacts'), indent)
66     fb_empty.set_active(self.get_create_empty())
68     use_twitter = self.new_checkbox('Use Twitter', content)
69     use_twitter.set_active(self.get_use_twitter())
71     indent = self.new_indent(content)
72     tw_user = self.link_control(use_twitter, self.new_input('Twitter username'), indent)
73     tw_user.set_text(self.get_twitter_credentials()[0])
75     tw_pass = self.link_control(indent, use_twitter, self.new_input('Twitter password', password = True), indent)
76     tw_pass.set_text(self.get_twitter_credentials()[1])
78     dialog.show_all()
79     result = dialog.run()
80     dialog.hide()
81     if result == gtk.RESPONSE_OK:
82       self.set_use_facebook(use_facebook.get_active())
83       self.set_create_empty(fb_empty.get_active())
84       self.set_use_twitter(use_twitter.get_active(), tw_user.get_text(), tw_pass.get_text())
86     return result
87    
89   # -----------------------------------------------------------------------
90   def sync(self, widget, force, main = True):
91     if main and not self.get_use_facebook() and not self.get_use_twitter():
92       saved = self.do_accounts()
93       if saved == gtk.RESPONSE_DELETE_EVENT:
94         return
96     if main:
97       self.window.set_property('sensitive', False)
98       thread.start_new_thread(self.doSync, (widget, force, False))
99     else:
100       try:
101         fb2c = Hermes(self,
102                       twitter = (self.get_use_twitter() and self.get_twitter_credentials()) or None,
103                       facebook = self.get_use_facebook(),
104                       empty = self.get_create_empty())
105         fb2c.load_friends()
106         fb2c.sync_contacts(resync = force)
107         gobject.idle_add(self.open_summary, fb2c)
109       except urllib2.HTTPError, e:
110         traceback.print_exc()
111         if e.code == 401:
112           gobject.idle_add(self.report_error, 'Authentication problem. Check credentials.', True)
113         else:
114           gobject.idle_add(self.report_error, 'Network connection error. Check connectivity.')
116       except urllib2.URLError, e:
117         traceback.print_exc()
118         gobject.idle_add(self.report_error, 'Network connection error. Check connectivity.')
119       
120       except Exception, e:
121         traceback.print_exc()
122         gobject.idle_add(self.report_error, 'Something went wrong: ' + e.message)
125   # -----------------------------------------------------------------------
126   def open_summary(self, fb2c):
127     gobject.idle_add(self.window.set_property, 'sensitive', True)
129     dialog = gtk.Dialog('Summary', self.window)
130     dialog.add_button('Done', gtk.RESPONSE_OK)
131     
132     button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
133                            title = 'Updated %d contacts' % (len(fb2c.updated)))
134     button.connect('clicked', self.show_contacts, fb2c.updated)
135     button.set_property('sensitive', len(fb2c.updated) > 0)
136     dialog.vbox.add(button)
137     
138     button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
139                            title = 'Matched %d contacts' % (len(fb2c.matched)))
140     button.connect('clicked', self.show_contacts, fb2c.matched)
141     button.set_property('sensitive', len(fb2c.matched) > 0)
142     dialog.vbox.add(button)
143     
144     button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
145                            title = '%d contacts unmatched' % (len(fb2c.unmatched)))
146     button.connect('clicked', self.show_contacts, fb2c.unmatched)
147     button.set_property('sensitive', len(fb2c.unmatched) > 0)
148     dialog.vbox.add(button)
149     
150     dialog.show_all()
151     dialog.run()
152     dialog.hide()
153   
155   # -----------------------------------------------------------------------
156   def show_contacts(self, widget, contacts):
157     view = contactview.ContactView(contacts)
159     dialog = gtk.Dialog('Contacts', self.window)
160     #view.connect('contact-activated', self.map_contact)
161     dialog.vbox.add(view)
162     dialog.show_all()
163     
164     dialog.run()
165     dialog.hide()
166     
167     
168   # -----------------------------------------------------------------------
169   def map_contact(self, widget, contact):
170     print widget, contact
172     
173   # -----------------------------------------------------------------------
174   def need_auth(self, main = False):
175     if main:
176       hildon.hildon_banner_show_information(self.window, '', "Need to authenticate with Facebook")
177     else:
178       gobject.idle_add(self.need_auth, True)
179     
181   # -----------------------------------------------------------------------
182   def block_for_auth(self, main = False, lock = None):
183     if main:
184       note = gtk.Dialog('Facebook authorisation', self.window)
185       note.add_button("Validate", gtk.RESPONSE_OK)
186       note.vbox.add(gtk.Label("\nPress 'Validate' once Facebook has\nbeen authenticated in web browser.\n"))
188       note.show_all()
189       result = note.run()
190       note.hide()
191       lock.release()
192       
193     else:
194       time.sleep(2)
195       lock = thread.allocate_lock()
196       lock.acquire()
197       gobject.idle_add(self.block_for_auth, True, lock)
198       lock.acquire()
199       lock.release()
202   # -----------------------------------------------------------------------
203   def progress(self, i, j, main = False):
204     if main:
205       if i == 0:
206         self.progressbar = gtk.ProgressBar()
207         self.progressnote = gtk.Dialog("Fetching friends' info", self.window)
208         self.progressnote.vbox.add(self.progressbar)
209         hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 1)
210         
211         self.progressnote.show_all()
212         
213       elif i < j:
214         if i == 1:
215           self.progressnote.set_title("Updating contacts")
216           hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 0)
217           
218         self.progressbar.set_fraction(float(i) / float(j))
219         
220       else:
221         self.progressnote.destroy()
222         
223       print i,j
224     else:
225       gobject.idle_add(self.progress, i, j, True)
228   # -----------------------------------------------------------------------
229   def report_error(self, e, prefs = False):
230     if self.progressnote:
231       self.window.set_property('sensitive', True)
232       self.progressnote.destroy()
234     hildon.hildon_banner_show_information(self.window, '', e)
235     if prefs:
236       self.do_accounts()
238       
239   def get_use_facebook(self):
240     return self.gc.get_bool("/apps/maemo/hermes/use_facebook")
243   def set_use_facebook(self, value):
244     self.gc.set_bool("/apps/maemo/hermes/use_facebook", value)
246   def get_create_empty(self):
247     return self.gc.get_bool("/apps/maemo/hermes/create_empty")
250   def set_create_empty(self, value):
251     self.gc.set_bool("/apps/maemo/hermes/create_empty", value)
254   def get_use_twitter(self):
255     return self.gc.get_bool("/apps/maemo/hermes/use_twitter")
258   def set_use_twitter(self, value, user, password):
259     self.gc.set_bool("/apps/maemo/hermes/use_twitter", value)
260     self.gc.set_string("/apps/maemo/hermes/twitter_user", user)
261     self.gc.set_string("/apps/maemo/hermes/twitter_pwd", password)
262     
263   
264   def get_twitter_credentials(self):
265     return (self.gc.get_string("/apps/maemo/hermes/twitter_user") or '',
266             self.gc.get_string("/apps/maemo/hermes/twitter_pwd") or '')
269 # -------------------------------------------------------------------------
270 if __name__ == '__main__':
271   gui = HermesGUI()
272   gui.run()

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment