Add accounts' dialogue for the new providers, and make gtkui actually
[hermes] / package / src / org / maemo / hermes / gui / gtkui.py
1 import gettext
2 import gtk, gobject
3 import traceback
4 import time
5 import thread
6 import urllib2
7 import hildon
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 hermes import Hermes ### FIXME This needs to be new
14
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.
18        
19        Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
20        Released under the Artistic Licence."""
21
22
23     # -----------------------------------------------------------------------
24     def __init__(self, providers = None):
25         gettext.install('hermes','/opt/hermes/share/locale/')
26         WimpWorks.__init__(self, 'Hermes', version = '0.9.0', dbus_name = 'org.maemo.hermes')
27         self.set_background('background.png')
28         
29         layout = HildonMainScreenLayout(offset = 0.8, container = self)
30         layout.add_button('Retrieve', _("Get contacts' missing info"))
31         layout.add_button('Refresh', _("Update contacts' info"))
32         
33         self.add_menu_action("Accounts")
34         self.menu.show_all()
35         
36         self.providers = providers
37         self.progressnote = None
38
39   
40     # -----------------------------------------------------------------------
41     def do_retrieve(self, widget):
42         self.sync(widget, False)
43     
44     
45     # -----------------------------------------------------------------------
46     def do_refresh(self, widget):
47         self.sync(widget, True)
48
49
50     # -----------------------------------------------------------------------
51     def do_accounts(self, widget = None):
52         dialog = AccountsDialogue(self.main_window, self.providers)
53         dialog.show()
54    
55
56     # -----------------------------------------------------------------------
57     def sync(self, widget, force, main = True):
58         enabled = []
59         for provider in self.providers:
60             if self.gconf.get_bool('/apps/maemo/hermes/use_%s' % (provider.get_id())):
61                 enabled.append(provider)
62                 
63         if main and len(enabled) == 0:
64             saved = self.do_accounts()
65             if saved == gtk.RESPONSE_DELETE_EVENT:
66                 return
67         
68         if main:
69             self.main_window.set_property('sensitive', False)
70             thread.start_new_thread(self.sync, (widget, force, False))
71         else:
72             try:
73                 raise Exception("TODO - implement syncing")
74         
75             except urllib2.HTTPError, e:
76                 traceback.print_exc()
77                 if e.code == 401:
78                     gobject.idle_add(self.report_error, _('Authentication problem. Check credentials.'), True)
79                 else:
80                     gobject.idle_add(self.report_error, _('Network connection error. Check connectivity.'))
81         
82             except urllib2.URLError, e:
83                 traceback.print_exc()
84                 gobject.idle_add(self.report_error, _('Network connection error. Check connectivity.'))
85           
86             except Exception, e:
87                 traceback.print_exc()
88                 gobject.idle_add(self.report_error, _('Something went wrong: ') + e.message)
89     
90     
91     # -----------------------------------------------------------------------
92     def open_summary(self, fb2c):
93         gobject.idle_add(self.main_window.set_property, 'sensitive', True)
94     
95         dialog = gtk.Dialog(_('Summary'), self.main_window)
96         dialog.add_button(_('Done'), gtk.RESPONSE_OK)
97       
98         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
99                                title = _('Updated %d contacts') % (len(fb2c.updated)))
100         button.connect('clicked', self.show_contacts, fb2c, fb2c.updated)
101         button.set_property('sensitive', len(fb2c.updated) > 0)
102         dialog.vbox.add(button)
103         
104         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
105                                title = _('Matched %d contacts') % (len(fb2c.matched)))
106         button.connect('clicked', self.show_contacts, fb2c, fb2c.matched)
107         button.set_property('sensitive', len(fb2c.matched) > 0)
108         dialog.vbox.add(button)
109       
110         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
111                                title = _('%d contacts unmatched') % (len(fb2c.unmatched)))
112         button.connect('clicked', self.show_contacts, fb2c, fb2c.unmatched)
113         button.set_property('sensitive', len(fb2c.unmatched) > 0)
114         dialog.vbox.add(button)
115       
116         dialog.show_all()
117         dialog.run()
118         dialog.hide()
119     
120     
121     # -----------------------------------------------------------------------
122     def show_contacts(self, widget, fb2c, contacts):
123         view = ContactView(contacts)
124     
125         dialog = gtk.Dialog(_('Contacts'), self.main_window)
126         view.connect('contact-activated', self.map_contact, fb2c)
127         dialog.vbox.add(view)
128         dialog.show_all()
129       
130         dialog.run()
131         dialog.hide()
132       
133       
134     # -----------------------------------------------------------------------
135     def map_contact(self, widget, contact, fb2c):
136         view = MapContact(fb2c.friends, contact)
137     
138         dialog = gtk.Dialog(contact.get_name(), self.main_window)
139         dialog.add_button(_('Update'), gtk.RESPONSE_OK)
140         dialog.vbox.add(view)
141         dialog.show_all()
142       
143         result = dialog.run()
144         dialog.hide()
145         if result == gtk.RESPONSE_OK:
146             friend = view.get_selected_friend()
147             if friend:
148                 if 'contact' in friend and friend['contact'] == contact:
149                     hildon.hildon_banner_show_information(self.main_window, '', _("Removing existing mappings is not yet supported"))
150                 elif view.contact_mapped:
151                     if fb2c.update_contact(contact, friend, True):
152                         fb2c.addresses.commit_contact(contact)
153                 else:
154                     if fb2c.update_contact(contact, friend, False):
155                         fb2c.addresses.commit_contact(contact)
156                         
157                         
158     # -----------------------------------------------------------------------
159     def progress(self, i, j, main = False):
160         if main:
161             if i == 0:
162                 self.progressbar = gtk.ProgressBar()
163                 self.progressnote = gtk.Dialog(_("Fetching friends' info"), self.main_window)
164                 self.progressnote.vbox.add(self.progressbar)
165                 hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 1)
166                
167                 self.progressnote.show_all()
168               
169             elif i < j:
170                 if i == 1:
171                     self.progressnote.set_title(_("Updating contacts"))
172                     hildon.hildon_gtk_window_set_progress_indicator(self.progressnote, 0)
173                 
174                 self.progressbar.set_fraction(float(i) / float(j))
175               
176             else:
177                 self.progressnote.destroy()
178               
179             print i,j
180         else:
181             gobject.idle_add(self.progress, i, j, True)
182
183        
184     # -----------------------------------------------------------------------
185     def report_error(self, e, prefs = False):
186         if self.progressnote:
187             self.main_window.set_property('sensitive', True)
188             self.progressnote.destroy()
189     
190         hildon.hildon_banner_show_information(self.main_window, '', e)
191         if prefs:
192             self.do_accounts()