New release
[ussd-widget] / ussd-widget / src / usr / lib / hildon-desktop / ussd-widget.py
index 4854980..70f4512 100755 (executable)
@@ -26,13 +26,13 @@ ussd_languages = ["German", "English", "Italian", "French", "Spanish", "Dutch",
 
 # TODO Cutt off too long messages and show them in separate dialog
 # how TODO widget vertical minimum size policy
-# TODO interface for queryng from scripts. For example query from one widget can change color of another widget
+# TODO interface for queryng from scripts. For example query from one widget can change color of another widget 
 
 class USSD_Controller:
        def __init__( self, widget ) :
                self.widget = widget
-               # number, parser, chain, interval, regexp, width, execute_at_start, retry pattern, font, [name, always show], language 
-               self.default_config = ["", "", "", 0, "", 0, True, [], pango.FontDescription("Nokia Sans 18"), [_("Click to update"), False], 15]
+               # number, parser, chain, interval, regexp, width, execute_at_start, retry pattern, font, name, language, show_message_box, message_box_parser 
+               self.default_config = ["", "", "", 0, "", 0, True, [], pango.FontDescription("Nokia Sans 18"), _("Click to update"), 15, False, ""]
                self.config = self.default_config
                self.timeout_version = 0
                self.retry_version = 0
@@ -63,7 +63,8 @@ class USSD_Controller:
                fconfig.write(oldconfig)
                fconfig.write("%"+self.id+"\n");
                fconfig.writelines(["# USSD query to be run by widget\n", "number="+self.config[0], "\n"])
-               fconfig.writelines(["#Parser command\n", "parser="+self.config[1], "\n"])
+               fconfig.writelines(["#Parser command for widget\n", "parser="+self.config[1], "\n"])
+               fconfig.writelines(["#Parser command for banner\n", "parser_box="+self.config[12], "\n"])
                fconfig.writelines(["#Chain command\n", "chain="+self.config[2], "\n"])
                fconfig.writelines(["#Update interval in minutes\n", "interval="+str(self.config[3]), "\n"])
                fconfig.writelines(["#RegExp pattern\n", "regexp="+self.config[4], "\n"])
@@ -81,8 +82,8 @@ class USSD_Controller:
                fconfig.writelines(["#Font description\n", "font="+self.config[8].to_string(), "\n"])
                fconfig.writelines(["#Font color\n", "text_color="+self.widget.get_text_color().to_string(), "\n"])
                fconfig.writelines(["#Background color\n", "bg_color="+self.widget.get_bg_color().to_string(), "\n"])
-               fconfig.writelines(["#Widget name\n", "name="+self.config[9][0], "\n"])
-               fconfig.writelines(["#Always show widget name\n", "show_name="+str(self.config[9][1]), "\n"])
+               fconfig.writelines(["#Widget name\n", "name="+self.config[9], "\n"])
+               fconfig.writelines(["#Show banner\n", "show_box="+str(self.config[11]), "\n"])
                fconfig.writelines(["#USSD reply language\n", "language="+str(self.config[10]), "\n"])
                fconfig.close()
 
@@ -122,6 +123,8 @@ class USSD_Controller:
                                        self.config[0] = line[1].strip()
                                elif line[0] == "parser" :
                                        self.config[1] = line[1].strip()
+                               elif line[0] == "parser_box" :
+                                       self.config[12] = line[1].strip()
                                elif line[0] == "chain" :
                                        self.config[2] = line[1].strip()
                                elif line[0] == "interval" :
@@ -181,12 +184,12 @@ class USSD_Controller:
                                                error = True
                                                print _("Error reading config on line %(line)d. Expected color definition.")%{"line":i}
                                elif line[0] == "name" :
-                                       self.config[9][0] = line[1].strip()
-                               elif line[0] == "show_name" :
+                                       self.config[9] = line[1].strip()
+                               elif line[0] == "show_box" :
                                        if line[1].strip() == "True" :
-                                               self.config[9][1] = True
+                                               self.config[11] = True
                                        else :
-                                               self.config[9][1] = False
+                                               self.config[11] = False
                                elif line[0] == "language" :
                                        try:
                                                if int(line[1].strip()) >=0 and int(line[1].strip()) < len(ussd_languages):
@@ -264,8 +267,10 @@ class USSD_Controller:
                        dialog.query_at_start.get_active(),
                        retry,
                        dialog.font,
-                       [dialog.wname.get_text(), dialog.show_name.get_active()],
-                       dialog.language.get_active()
+                       dialog.wname.get_text(),
+                       dialog.language.get_active(),
+                       dialog.show_box.get_active(),
+                       dialog.b_parser.get_text()
                ]
 
                widget.set_bg_color(dialog.bg_color)
@@ -338,23 +343,43 @@ class USSD_Controller:
                                self.retry_state += 1
                else :
                        self.widget.error = 0
-                       # Pass to parser
-                       if self.config[1] != "":
-                               p = Popen(smart_split_string(self.config[1], reply), stdout=PIPE)
-                               reply = p.communicate(reply+"\n")[0].strip()
-                       # Pass to chain
-                       if self.config[2] != "":
-                               p = Popen(smart_split_string(self.config[2], reply))
                        # Apply regexp
                        if self.config[4] != "":
                                try :
-                                       r = re.match( self.config[4], reply ).group( 1 )
+                                       reply = re.match( self.config[4], reply ).group( 1 )
                                except Exception, e:
-                                       r = _("Regexp Error: ") + str( e ) + "\n" + reply
-
-                               if r :
-                                       reply = r
-                       self.widget.set_text(reply)
+                                       self.widget.error = 1
+                                       reply = _("Regexp Error: ") + str( e ) + "\n" + reply
+                       w_reply = b_reply = reply
+                       if self.widget.error == 0:
+                               # Pass to box parser
+                               if self.config[12] != "" and self.config[11]:
+                                       try:
+                                               p = Popen(smart_split_string(self.config[12], reply), stdout=PIPE)
+                                               b_reply = p.communicate()[0].strip()
+                                       except Exception, e:
+                                               print _("Couldn't exec banner parser:")+str(e)
+                                               self.widget.error = 1
+                               # Pass to widget parser
+                               if self.config[1] != "":
+                                       try:
+                                               p = Popen(smart_split_string(self.config[1], reply), stdout=PIPE)
+                                               w_reply = p.communicate()[0].strip()
+                                       except Exception, e:
+                                               print _("Couldn't exec widget parser:")+str(e)
+                                               self.widget.error = 1
+                               # Pass to chain
+                               if self.config[2] != "":
+                                       try:
+                                               p = Popen(smart_split_string(self.config[2], reply))
+                                       except Exception, e:
+                                               print _("Couldn't exec chain:")+str(e)
+                                               self.widget.error = 1
+                       if self.config[11]:
+                               banner = hildon.hildon_banner_show_information (self.widget, "", b_reply)
+                               banner.set_timeout (5000)
+                               b_reply
+                       self.widget.set_text(w_reply)
                self.widget.processing = 0
 
        def timed_renew(self, version):
@@ -400,6 +425,9 @@ class UssdConfigDialog(gtk.Dialog):
                self.ussdNumber.set_text(config[0])
                self.parser = hildon.Entry(gtk.HILDON_SIZE_AUTO)
                self.parser.set_text(config[1])
+               self.b_parser = hildon.Entry(gtk.HILDON_SIZE_AUTO)
+               self.b_parser.set_text(config[12])
+
                self.chain = hildon.Entry(gtk.HILDON_SIZE_AUTO)
                self.chain.set_text(config[2])
                self.update_interval = hildon.NumberEditor(0, 9999)
@@ -411,7 +439,6 @@ class UssdConfigDialog(gtk.Dialog):
                self.retryEdit = hildon.Entry(gtk.HILDON_SIZE_AUTO)
 
 
-               #selector = hildon.hildon_touch_selector_new_text()
                selector = hildon.TouchSelector(text=True)
                for i in ussd_languages:
                        selector.append_text(i)
@@ -422,9 +449,9 @@ class UssdConfigDialog(gtk.Dialog):
                self.language.set_size_request(-1, -1)
 
                self.wname = hildon.Entry(gtk.HILDON_SIZE_AUTO)
-               self.wname.set_text(config[9][0])
-               self.show_name = gtk.CheckButton(_("Always show name"))
-               self.show_name.set_active(config[9][1])
+               self.wname.set_text(config[9])
+               self.show_box = gtk.CheckButton(_("Enable banner. Parser:"))
+               self.show_box.set_active(config[11])
 
                text = ""
                for i in config[7]:
@@ -446,6 +473,9 @@ class UssdConfigDialog(gtk.Dialog):
                
                phelp = gtk.Button("?")
                phelp.connect("clicked", self.on_show_phelp)
+               
+               bphelp = gtk.Button("?")
+               bphelp.connect("clicked", self.on_show_bphelp)
 
                chelp = gtk.Button("?")
                chelp.connect("clicked", self.on_show_chelp)
@@ -475,14 +505,16 @@ class UssdConfigDialog(gtk.Dialog):
                vbox.add(self.query_at_start)
 
                nameBox = gtk.HBox()
-               self.show_name.set_size_request(100, -1)
-               self.wname.set_size_request(100, -1)
-               nameBox.add(self.show_name)
+               nameLabel = gtk.Label(_("Name"))
+               nameLabel.set_alignment(0,0)
+               nameLabel.set_size_request(100, -1)
+               self.wname.set_size_request(200, -1)
+               nameBox.add(nameLabel)
                nameBox.add(self.wname)
                vbox.add(nameBox)
 
                parserBox = gtk.HBox()
-               parserLabel = gtk.Label(_("Parser"))
+               parserLabel = gtk.Label(_("Parser for widget"))
                parserLabel.set_alignment(0,0)
                parserLabel.set_size_request(200, -1)
                phelp.set_size_request(10, -1)
@@ -490,6 +522,16 @@ class UssdConfigDialog(gtk.Dialog):
                parserBox.add(phelp)
                vbox.add(parserBox)
                vbox.add(self.parser)
+               
+               b_parserBox = gtk.HBox()
+#              parserLabel = gtk.Label(_("Parser"))
+#              parserLabel.set_alignment(0,0)
+               self.show_box.set_size_request(200, -1)
+               bphelp.set_size_request(10, -1)
+               b_parserBox.add(self.show_box)
+               b_parserBox.add(bphelp)
+               vbox.add(b_parserBox)
+               vbox.add(self.b_parser)
 
                chainBox = gtk.HBox()
                chainLabel = gtk.Label(_("Chain"))
@@ -556,10 +598,15 @@ class UssdConfigDialog(gtk.Dialog):
 
        #============ Dialog helper functions =============
        def on_show_phelp(self, widget):
-               dialog = pHelpDialog(_("Format help"), _("Reply would be passed to specified utility, output of utility would be shown to you.\n       Format:\n% would be replaced by reply\n\\ invalidates special meaming of following symbol\n\" and ' work as usual\nspace delimits command line parameters of utility"))
+               dialog = pHelpDialog(_("Format help"), _("Reply would be passed to specified utility, output of utility would be shown to you on widget.\n       Format:\n% would be replaced by reply\n\\ invalidates special meaming of following symbol\n\" and ' work as usual\nspace delimits command line parameters of utility\n      Hint: use echo \"Your string %\" to prepend your string to reply."))
                dialog.run()
                dialog.destroy()
 
+       def on_show_bphelp(self, widget):
+               dialog = pHelpDialog(_("Format help"), _("Reply would be passed to specified utility, output of utility would be shown to you on banner.\n       Format:\n% would be replaced by reply\n\\ invalidates special meaming of following symbol\n\" and ' work as usual\nspace delimits command line parameters of utility\n      Hint: use echo \"Your string %\" to prepend your string to reply."))
+               dialog.run()
+               dialog.destroy()
+       
        def on_show_chelp(self, widget):
                dialog = pHelpDialog(_("Format help"), _("Reply would be passed to specified utility after parser utility. May be used for logging, statistics etc.\n       Format:\n% would be replaced by reply\n\\ invalidates special meaning of following symbol\n\" and ' work as usual\nspace delimits command line parameters of utility"))
                dialog.run()
@@ -706,7 +753,7 @@ class UssdWidgetPlugin(hildondesktop.HomePluginItem):
        def do_show(self):
                config = self.controller.read_config(self.get_applet_id())
                self.set_width(config[5])
-               self.set_text("")               
+               self.set_text(config[9])                
                if config[6]:
                        self.controller.ussd_renew(self, None)
 
@@ -731,8 +778,6 @@ class UssdWidgetPlugin(hildondesktop.HomePluginItem):
                                self.text = text
                
                config = self.controller.get_config()
-               if config[9][1] or text == "":
-                       text = config[9][0]+text
                self.label.set_text(text)
 
        def get_text(self):