Change GUI to use WimpWorks
authorAndrew Flegg <andrew@bleb.org>
Fri, 30 Oct 2009 16:22:32 +0000 (16:22 +0000)
committerAndrew Flegg <andrew@bleb.org>
Fri, 30 Oct 2009 16:22:32 +0000 (16:22 +0000)
package/src/gui.py

index d1c92cc..4c0e365 100755 (executable)
@@ -2,18 +2,18 @@
 
 import gtk, gobject
 import gnome.gconf
+import hildon, osso
 import traceback
 import time
 import thread
 import os.path
 import contactview
-import urllib2
-from wimpworks import WimpWorks
+import urllib2 
 from hermes import Hermes
 
 gobject.threads_init()
 
-class HermesGUI(WimpWorks):
+class HermesGUI:
   """Provides the GUI for Hermes, allowing the syncing of Facebook and
      Twitter friends' information with the Evolution contacts' database.
        
@@ -23,73 +23,14 @@ class HermesGUI(WimpWorks):
 
   # -----------------------------------------------------------------------
   def __init__(self):
-    WimpWorks.__init__(self, 'Hermes', version = '0.1.1', 'org.maemo.hermes')
-    self.set_background('background.png')
-    
-    layout = wimporks.HildonMainScreenLayout(offset = 0.8, container = self)
-    layout.add_button('Retrieve', "Get contacts' missing info")
-    layout.add_button('Refresh', "Update contacts' info")
+    """Constructor. Initialises the gconf connection."""
+    self.gc = gnome.gconf.client_get_default()
 
-    self.add_menu_action("Accounts")
-    self.menu.show_all()
 
-  
   # -----------------------------------------------------------------------
-  def do_retrieve(self, widget):
-    self.sync(widget, False)
-    
-    
-  # -----------------------------------------------------------------------
-  def do_refresh(self, widget):
-    self.sync(widget, True)
-
-
-  # -----------------------------------------------------------------------
-  def do_accounts(self, widget = None):
-    dialog = gtk.Dialog('Accounts', self.window)
-    dialog.add_button('Save', gtk.RESPONSE_OK)
-
-    #pa = hildon.PannableArea()
-    #dialog.vbox.add(pa)
-    content = dialog.vbox 
-    #content = gtk.VBox()
-    #pa.add(content)
-    #pa.set_size_request(600, 380)
-
-    use_facebook = self.new_checkbox('Use Facebook', content)
-    use_facebook.set_active(self.get_use_facebook())
-
-    indent = self.new_indent(content)
-    self.link_control(use_facebook, gtk.Label('Note: authentication via web page'), indent)
-    
-    fb_empty = self.link_control(use_facebook, self.new_checkbox('Create birthday-only contacts'), indent)
-    fb_empty.set_active(self.get_create_empty())
-
-    use_twitter = self.new_checkbox('Use Twitter', content)
-    use_twitter.set_active(self.get_use_twitter())
-
-    indent = self.new_indent(content)
-    tw_user = self.link_control(use_twitter, self.new_input('Twitter username'), indent)
-    tw_user.set_text(self.get_twitter_credentials()[0])
-
-    tw_pass = self.link_control(indent, use_twitter, self.new_input('Twitter password', password = True), indent)
-    tw_pass.set_text(self.get_twitter_credentials()[1])
-
-    dialog.show_all()
-    result = dialog.run()
-    dialog.hide()
-    if result == gtk.RESPONSE_OK:
-      self.set_use_facebook(use_facebook.get_active())
-      self.set_create_empty(fb_empty.get_active())
-      self.set_use_twitter(use_twitter.get_active(), tw_user.get_text(), tw_pass.get_text())
-
-    return result
-   
-
-  # -----------------------------------------------------------------------
-  def sync(self, widget, force, main = True):
+  def doSync(self, widget, force, main = True):
     if main and not self.get_use_facebook() and not self.get_use_twitter():
-      saved = self.do_accounts()
+      saved = self.open_prefs()
       if saved == gtk.RESPONSE_DELETE_EVENT:
         return
 
@@ -169,7 +110,93 @@ class HermesGUI(WimpWorks):
   def map_contact(self, widget, contact):
     print widget, contact
 
+
+  # -----------------------------------------------------------------------
+  def open_prefs(self, widget = None):
+    dialog = gtk.Dialog('Accounts', self.window)
+    dialog.add_button('Save', gtk.RESPONSE_OK)
+
+    #pa = hildon.PannableArea()
+    #dialog.vbox.add(pa)
+    content = dialog.vbox 
+    #content = gtk.VBox()
+    #pa.add(content)
+    #pa.set_size_request(600, 380)
+
+    use_facebook = self.new_checkbox('Use Facebook', content)
+    use_facebook.set_active(self.get_use_facebook())
+
+    indent = self.new_indent(content)
+    fb_msg = self.add_linked(indent, use_facebook, gtk.Label('Note: authentication via web page'))
+    fb_msg.set_property('justify', gtk.JUSTIFY_LEFT)
     
+    fb_empty = self.add_linked(indent, use_facebook, self.new_checkbox('Create birthday-only contacts'))
+    fb_empty.set_active(self.get_create_empty())
+
+    use_twitter = self.new_checkbox('Use Twitter', content)
+    use_twitter.set_active(self.get_use_twitter())
+
+    indent = self.new_indent(content)
+    tw_user = self.add_linked(indent, use_twitter, self.new_input('Twitter username'))
+    tw_user.set_text(self.get_twitter_credentials()[0])
+
+    tw_pass = self.add_linked(indent, use_twitter, self.new_input('Twitter password'))
+    tw_pass.set_property('hildon-input-mode', gtk.HILDON_GTK_INPUT_MODE_FULL |
+                                              gtk.HILDON_GTK_INPUT_MODE_INVISIBLE)
+    tw_pass.set_text(self.get_twitter_credentials()[1])
+
+    dialog.show_all()
+    result = dialog.run()
+    dialog.hide()
+    if result == gtk.RESPONSE_OK:
+      self.set_use_facebook(use_facebook.get_active())
+      self.set_create_empty(fb_empty.get_active())
+      self.set_use_twitter(use_twitter.get_active(), tw_user.get_text(), tw_pass.get_text())
+
+    return result
+  # -----------------------------------------------------------------------
+  def new_checkbox(self, label, box = None):
+    checkbox = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
+    checkbox.set_label(label)
+    if box:
+      box.add(checkbox)
+    return checkbox
+
+
+  # -----------------------------------------------------------------------
+  def new_indent(self, box):
+    outer = gtk.HBox()
+    indent = gtk.VBox()
+    outer.pack_start(indent, padding=48)
+    box.add(outer)
+    return indent
+
+  # -----------------------------------------------------------------------
+  def new_input(self, text, box = None):
+    input = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
+    input.set_property('hildon-input-mode', gtk.HILDON_GTK_INPUT_MODE_FULL)
+    input.set_placeholder(text)
+    input.set_property('is-focus', False)
+    if box:
+      box.add(input)
+    return input
+
+
+  # -----------------------------------------------------------------------
+  def add_linked(self, box, ctrl, to_add):
+    box.add(to_add)
+    self.sync_edit(ctrl, to_add)
+    ctrl.connect('toggled', self.sync_edit, to_add)
+    return to_add
+  
+    
+  # -----------------------------------------------------------------------
+  def sync_edit(self, use_twitter, edit):
+    edit.set_property('sensitive', use_twitter.get_active())
+
+
   # -----------------------------------------------------------------------
   def need_auth(self, main = False):
     if main:
@@ -233,9 +260,66 @@ class HermesGUI(WimpWorks):
 
     hildon.hildon_banner_show_information(self.window, '', e)
     if prefs:
-      self.do_accounts()
+      self.open_prefs()
+
+
+  # -----------------------------------------------------------------------
+  def take_screenshot(self, event = None, data = None):
+    self.window.disconnect(self.expose_hid)
+    if not os.path.isfile("/home/user/.cache/launch/org.maemo.hermes.pvr"):
+      gobject.timeout_add(80, hildon.hildon_gtk_window_take_screenshot, self.window, True)
+
+
+  # -----------------------------------------------------------------------
+  def main(self):
+    # -- Window and app...
+    #
+    self.app = hildon.Program()
+    self.window = hildon.Window()
+    gtk.set_application_name('Hermes')
+    self.osso_context = osso.Context('org.maemo.hermes', '0.0.5', False)
+    self.app.add_window(self.window)
+
+    self.window.connect("delete-event", gtk.main_quit)
+    self.expose_hid = self.window.connect('expose-event', self.take_screenshot)
+
+    # -- Main window buttons...
+    #
+    self.background, mask = gtk.gdk.pixbuf_new_from_file('/opt/hermes/share/background.png').render_pixmap_and_mask()
+    self.window.realize()
+    self.window.window.set_back_pixmap(self.background, False)
+    alignment = gtk.Alignment(xalign=0.5, yalign=0.8, xscale=0.8)
+    box = gtk.HButtonBox()
+    alignment.add(box)
+    self.window.add(alignment)
+    
+    box.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
+    button = hildon.Button(gtk.HILDON_SIZE_THUMB_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
+                           title = 'Retrieve', value = "Get contacts' missing info")
+    button.set_property('width-request', 250)
+    button.connect('clicked', self.doSync, False)
+    box.add(button)
+
+    button = hildon.Button(gtk.HILDON_SIZE_THUMB_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
+                           title = 'Update', value = "Update contacts' info")
+    button.set_property('width-request', 250)
+    button.connect('clicked', self.doSync, True)
+    box.add(button)
+
+    # -- Application menu...
+    #
+    menu = hildon.AppMenu()
+    button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
+    button.set_label("Accounts")
+    button.connect("clicked", self.open_prefs)
+    menu.append(button)
+    menu.show_all()
+    self.window.set_app_menu(menu)
+  
+    self.window.show_all()
+    gtk.main()
+
 
-      
   def get_use_facebook(self):
     return self.gc.get_bool("/apps/maemo/hermes/use_facebook")
 
@@ -268,6 +352,5 @@ class HermesGUI(WimpWorks):
 
 # -------------------------------------------------------------------------
 if __name__ == '__main__':
-  gui = HermesGUI()
-  gui.run()
+  HermesGUI().main()