23fc4fd68075ae8cbd538f107010dfc511f03d9b
[ussd-widget] / ussd-widget / src / usr / lib / hildon-desktop / ussd-widget.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 import gtk
4 import hildon
5 import hildondesktop
6 import os
7 from subprocess import *
8 import cairo
9
10 supports_alpha = False
11
12 class UssdConfigDialog(gtk.Dialog):
13     def __init__(self, config):
14         gtk.Dialog.__init__(self, "USSD widget", None,
15                             gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR,
16                             ("Save", gtk.RESPONSE_OK))
17         self.ussdNumber = hildon.Entry(gtk.HILDON_SIZE_AUTO)
18         self.ussdNumber.set_text(config[0])
19         self.parser = hildon.Entry(gtk.HILDON_SIZE_AUTO)
20         self.parser.set_text(config[1])
21         self.vbox.add(gtk.Label("USSD number"))
22         self.vbox.add(self.ussdNumber)
23         self.vbox.add(gtk.Label("Parser"))
24         self.vbox.add(self.parser)
25         self.show_all()
26         self.parent
27
28 def get_config():
29     try :
30         config = open(os.getenv("HOME")+"/.ussdWidget.conf","r")
31         config.readline()
32         config.readline()
33         number = config.readline().strip()
34         config.readline()
35         parser = config.readline().strip()
36         config.close()
37         return [number, parser]
38     except  IOError:
39         return None
40
41 def set_config(config):
42     fconfig = open(os.getenv("HOME")+"/.ussdWidget.conf","w")
43     fconfig.writelines(["# Parameters are taken by line number, do not move them\n", "# USSD query to be run by widget\n", config[0], "\n"])
44     fconfig.writelines(["Parser command\n", config[1], "\n"])
45     fconfig.close()
46
47 def ussd_renew(widget, event):
48     config = get_config()
49     widget.processing = 1
50     widget.label.set_text("Processing")
51     widget.queue_draw()
52     gtk.main_iteration ()
53 #    widget.send_expose(gtk.gdk.EXPOSE)
54     if config :
55         p = Popen(['python', '/usr/bin/ussdquery.py', config[0]], stdout=PIPE)
56         reply = p.communicate()[0].strip()
57         if reply == "" :
58             reply = "   Error   "
59 #        else :
60 #            if config[1] != "":
61 #                p = Popen([config[1]], stdout=PIPE)
62 #                reply = p.communicate(reply)[0].strip()
63     else :
64         reply = " Bad config "
65     widget.processing = 0
66     widget.label.set_text(reply)
67
68 def on_show_settings(widget):
69     config = get_config()
70     if config == None :
71         config = ["", ""]
72
73     dialog = UssdConfigDialog(config)
74     dialog.run()
75
76     set_config ([dialog.ussdNumber.get_text(), dialog.parser.get_text()])
77  
78     dialog.destroy()
79     
80     if config == ["", ""] :
81         widget.label.set_text("Click to update")
82
83 def get_color(logicalcolorname):
84     settings = gtk.settings_get_default()
85     color_style = gtk.rc_get_style_by_paths(settings, 'GtkButton', 'osso-logical-colors', gtk.Button)
86     return color_style.lookup_color(logicalcolorname)
87
88 class UssdWidgetPlugin(hildondesktop.HomePluginItem):
89     def __init__(self):
90         hildondesktop.HomePluginItem.__init__(self)
91         
92         self.processing = 0
93
94         colormap = self.get_screen().get_rgba_colormap()
95         self.set_colormap (colormap)
96
97         query = get_config()
98         if query :
99             self.label = gtk.Label("Click to update")
100         else :
101             self.label = gtk.Label("Configure me")
102
103         self.connect("button-press-event", ussd_renew)
104
105         self.label.set_padding(15, 10)
106         self.label.set_size_request(-1,-1)
107         self.set_size_request(-1,-1)
108
109         self.vbox = gtk.HBox()
110         self.vbox.add(self.label)
111         self.add(self.vbox)
112
113         self.set_settings(True)
114         self.connect("show-settings", on_show_settings)    
115
116         self.vbox.show_all()
117
118     def _expose(self, event):
119         cr = self.window.cairo_create()
120
121         # draw rounded rect
122         width, height = self.allocation[2], self.allocation[3]
123
124         #/* a custom shape, that could be wrapped in a function */
125         x0 = 0   #/*< parameters like cairo_rectangle */
126         y0 = 0
127
128         radius = min(15, width/2, height/2)  #/*< and an approximate curvature radius */
129
130         x1 = x0 + width
131         y1 = y0 + height
132
133         cr.move_to  (x0, y0 + radius)
134         cr.arc (x0 + radius, y0 + radius, radius, 3.14, 1.5 * 3.14)
135         cr.line_to (x1 - radius, y0)
136         cr.arc (x1 - radius, y0 + radius, radius, 1.5 * 3.14, 0.0)
137         cr.line_to (x1 , y1 - radius)
138         cr.arc (x1 - radius, y1 - radius, radius, 0.0, 0.5 * 3.14)
139         cr.line_to (x0 + radius, y1)
140         cr.arc (x0 + radius, y1 - radius, radius, 0.5 * 3.14, 3.14)
141
142         cr.close_path ()
143
144         fg_color = get_color("ActiveTextColor")
145
146         if self.processing :
147             bg_color=fg_color
148         else :
149             bg_color=gtk.gdk.color_parse('#000000')
150
151         cr.set_source_rgba (bg_color.red / 65535.0, bg_color.green/65535.0, bg_color.blue/65535.0, 0.7)
152         cr.fill_preserve ()
153
154         cr.set_source_rgba (fg_color.red / 65535.0, fg_color.green / 65535.0, fg_color.blue / 65535.0, 0.5)
155         cr.stroke ()
156
157     def do_expose_event(self, event):
158         self.chain(event)
159         self._expose (event)
160         self.vbox.do_expose_event (self, event)
161
162 hd_plugin_type = UssdWidgetPlugin
163
164 # The code below is just for testing purposes.
165 # It allows to run the widget as a standalone process.
166 if __name__ == "__main__":
167     import gobject
168     gobject.type_register(hd_plugin_type)
169     obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")
170     obj.show_all()
171     gtk.main()