New fancy looking widget
[ussd-widget] / ussd-widget / src / usr / lib / hildon-desktop / ussd-widget.py
index 8f20617..23fc4fd 100755 (executable)
@@ -5,73 +5,160 @@ import hildon
 import hildondesktop
 import os
 from subprocess import *
+import cairo
+
+supports_alpha = False
 
 class UssdConfigDialog(gtk.Dialog):
-    def __init__(self, queryNumber):
+    def __init__(self, config):
         gtk.Dialog.__init__(self, "USSD widget", None,
                             gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR,
                             ("Save", gtk.RESPONSE_OK))
         self.ussdNumber = hildon.Entry(gtk.HILDON_SIZE_AUTO)
-        self.ussdNumber.set_text(queryNumber)
+        self.ussdNumber.set_text(config[0])
+        self.parser = hildon.Entry(gtk.HILDON_SIZE_AUTO)
+        self.parser.set_text(config[1])
         self.vbox.add(gtk.Label("USSD number"))
         self.vbox.add(self.ussdNumber)
+        self.vbox.add(gtk.Label("Parser"))
+        self.vbox.add(self.parser)
         self.show_all()
         self.parent
 
-def get_ussd_number():
+def get_config():
     try :
         config = open(os.getenv("HOME")+"/.ussdWidget.conf","r")
         config.readline()
         config.readline()
         number = config.readline().strip()
+        config.readline()
+        parser = config.readline().strip()
         config.close()
-        return number
+        return [number, parser]
     except  IOError:
         return None
 
-def ussd_renew(button):
-    query = get_ussd_number()
-    if query :
-        p = Popen(['python', '/usr/bin/ussdquery.py', query], stdout=PIPE)
+def set_config(config):
+    fconfig = open(os.getenv("HOME")+"/.ussdWidget.conf","w")
+    fconfig.writelines(["# Parameters are taken by line number, do not move them\n", "# USSD query to be run by widget\n", config[0], "\n"])
+    fconfig.writelines(["Parser command\n", config[1], "\n"])
+    fconfig.close()
+
+def ussd_renew(widget, event):
+    config = get_config()
+    widget.processing = 1
+    widget.label.set_text("Processing")
+    widget.queue_draw()
+    gtk.main_iteration ()
+#    widget.send_expose(gtk.gdk.EXPOSE)
+    if config :
+        p = Popen(['python', '/usr/bin/ussdquery.py', config[0]], stdout=PIPE)
         reply = p.communicate()[0].strip()
         if reply == "" :
             reply = "   Error   "
+#        else :
+#            if config[1] != "":
+#                p = Popen([config[1]], stdout=PIPE)
+#                reply = p.communicate(reply)[0].strip()
     else :
         reply = " Bad config "
-    button.set_label(reply)
+    widget.processing = 0
+    widget.label.set_text(reply)
 
 def on_show_settings(widget):
-    queryNumber = get_ussd_number()
-    if queryNumber == None :
-        queryNumber = ""
+    config = get_config()
+    if config == None :
+        config = ["", ""]
 
-    dialog = UssdConfigDialog(queryNumber)
+    dialog = UssdConfigDialog(config)
     dialog.run()
 
-    config = open(os.getenv("HOME")+"/.ussdWidget.conf","w")
-    config.writelines(["# Parameters are taken by line number, do not move them\n", "# USSD query to be run by widget\n", dialog.ussdNumber.get_text(), "\n"])
-    config.close()
-
+    set_config ([dialog.ussdNumber.get_text(), dialog.parser.get_text()])
     dialog.destroy()
     
-    if queryNumber == "" :
-        widget.button.set_label("Click to update")
+    if config == ["", ""] :
+        widget.label.set_text("Click to update")
+
+def get_color(logicalcolorname):
+    settings = gtk.settings_get_default()
+    color_style = gtk.rc_get_style_by_paths(settings, 'GtkButton', 'osso-logical-colors', gtk.Button)
+    return color_style.lookup_color(logicalcolorname)
 
 class UssdWidgetPlugin(hildondesktop.HomePluginItem):
     def __init__(self):
         hildondesktop.HomePluginItem.__init__(self)
-        query = get_ussd_number()
+        
+        self.processing = 0
+
+        colormap = self.get_screen().get_rgba_colormap()
+        self.set_colormap (colormap)
+
+        query = get_config()
         if query :
-            self.button = gtk.Button("Click to update")
+            self.label = gtk.Label("Click to update")
         else :
-            self.button = gtk.Button("Configure me")
-        self.button.connect("clicked", ussd_renew)
-        self.button.show_all()
-        self.add(self.button)
+            self.label = gtk.Label("Configure me")
+
+        self.connect("button-press-event", ussd_renew)
+
+        self.label.set_padding(15, 10)
+        self.label.set_size_request(-1,-1)
+        self.set_size_request(-1,-1)
+
+        self.vbox = gtk.HBox()
+        self.vbox.add(self.label)
+        self.add(self.vbox)
 
         self.set_settings(True)
         self.connect("show-settings", on_show_settings)    
 
+        self.vbox.show_all()
+
+    def _expose(self, event):
+        cr = self.window.cairo_create()
+
+        # draw rounded rect
+        width, height = self.allocation[2], self.allocation[3]
+
+        #/* a custom shape, that could be wrapped in a function */
+        x0 = 0   #/*< parameters like cairo_rectangle */
+        y0 = 0
+
+        radius = min(15, width/2, height/2)  #/*< and an approximate curvature radius */
+
+        x1 = x0 + width
+        y1 = y0 + height
+
+        cr.move_to  (x0, y0 + radius)
+        cr.arc (x0 + radius, y0 + radius, radius, 3.14, 1.5 * 3.14)
+        cr.line_to (x1 - radius, y0)
+        cr.arc (x1 - radius, y0 + radius, radius, 1.5 * 3.14, 0.0)
+        cr.line_to (x1 , y1 - radius)
+        cr.arc (x1 - radius, y1 - radius, radius, 0.0, 0.5 * 3.14)
+        cr.line_to (x0 + radius, y1)
+        cr.arc (x0 + radius, y1 - radius, radius, 0.5 * 3.14, 3.14)
+
+        cr.close_path ()
+
+        fg_color = get_color("ActiveTextColor")
+
+        if self.processing :
+            bg_color=fg_color
+        else :
+            bg_color=gtk.gdk.color_parse('#000000')
+
+        cr.set_source_rgba (bg_color.red / 65535.0, bg_color.green/65535.0, bg_color.blue/65535.0, 0.7)
+        cr.fill_preserve ()
+
+        cr.set_source_rgba (fg_color.red / 65535.0, fg_color.green / 65535.0, fg_color.blue / 65535.0, 0.5)
+        cr.stroke ()
+
+    def do_expose_event(self, event):
+        self.chain(event)
+        self._expose (event)
+        self.vbox.do_expose_event (self, event)
+
 hd_plugin_type = UssdWidgetPlugin
 
 # The code below is just for testing purposes.
@@ -79,4 +166,6 @@ hd_plugin_type = UssdWidgetPlugin
 if __name__ == "__main__":
     import gobject
     gobject.type_register(hd_plugin_type)
-
+    obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")
+    obj.show_all()
+    gtk.main()