- def __init__( self, widget ) :
- self.widget = widget
- # number, parser, chain, interval, regexp, width, execute_at_start, retry pattern, font, name, language, show_message_box, message_box_parser, additional arguments, regexp group, use SMS listener, SMS number, SMS regexp, SMS timeout
- self.default_config = ["", "", "", 0, "", 0, True, [], pango.FontDescription("Nokia Sans 18"), _("Click to update"), 15, False, "", "", 1, False, "", "", 60]
- self.config = self.default_config
- self.timeout_version = 0
- self.retry_version = 0
- self.retry_state = 0
- self.sms_counter = 0
- self.sms_reply = ""
-
- def save_config( self ) :
- configname = os.getenv("HOME")+"/.ussdWidget.conf"
- # Aquire lock
- lockf = open(configname+".lock", 'a')
- fcntl.flock(lockf,fcntl.LOCK_EX)
-
- oldconfig=""
- try:
- fconfig = open(configname,"r")
- #Read configuration of other instances
- my_section = False
- for line in fconfig :
- if line[0] == '%':
- my_section = line[1:].strip() == self.id
- if not my_section:
- oldconfig += line
- fconfig.close()
- except:
- print _("Couldn't read previous config")
-
- fconfig = open(configname,"w")
- fconfig.seek(0)
- 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 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"])
- fconfig.writelines(["#Widget width\n", "width="+str(self.config[5]), "\n"])
- fconfig.writelines(["#Execute query at start\n", "query_at_start="+str(self.config[6]), "\n"])
- fconfig.writelines(["#Retry pattern\n"])
- fconfig.write("retry=")
- first = True
- for i in self.config[7]:
- if not first:
- fconfig.write ("-")
- fconfig.write(str(i))
- first = False
- fconfig.write("\n")
- 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], "\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.writelines(["#Additional ussdquery.py arguments\n", "args="+self.config[13], "\n"])
- fconfig.writelines(["#Regexp matching group\n", "reggroup="+str(self.config[14]), "\n"])
- fconfig.writelines(["#Use SMS listener\n", "listen_sms="+str(self.config[15]), "\n"])
- fconfig.writelines(["#Number,from which SMS should come\n", "sms_number="+self.config[16], "\n"])
- fconfig.writelines(["#SMS RegExp pattern\n", "sms_regexp="+self.config[17], "\n"])
- fconfig.writelines(["#SMS timeout\n", "sms_timeout="+str(self.config[18]), "\n"])
- fconfig.close()
-
- fcntl.flock(lockf,fcntl.LOCK_UN)
- lockf.close()
-
- def get_config(self):
- return self.config
-
- def read_config( self, id ):
- try :
- self.id = id
- config = open(os.getenv("HOME")+"/.ussdWidget.conf","r")
-
- error = False
- i = 0
- my_section = False
- for line in config :
- i += 1
- if line[0] == '#':
- continue
- if line[0] == '%':
- my_section = line[1:].strip() == id
- continue
-
- if not my_section:
- # This is config for another instace
- continue
-
- line=line.split('=', 1)
-
- if len(line) != 2 :
- error = True
- print _("Error reading config on line %(line)d. = or # expected.")%{"line":i}
- continue
- if line[0] == "number" :
- 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" :
- try:
- self.config[3] = int(line[1].strip())
- except:
- error = True
- print _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
- continue
- elif line[0] == "regexp" :
- self.config[4] = line[1].strip()
- elif line[0] == "width" :
- try:
- self.config[5] = int(line[1].strip())
- except:
- error = True
- print _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
- continue
- elif line[0] == "query_at_start" :
- if line[1].strip() == "True" :
- self.config[6] = True
- else :
- self.config[6] = False
- elif line[0] == "retry" :
- line[1] = line[1].strip()
- if line[1] != "":
- line[1] = line[1].split("-")
- i = 0
- while i < len(line[1]) :
- try:
- line[1][i] = int(line[1][i])
- except:
- error = True
- print _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
- i += 1
- self.config[7] = line[1]
- else:
- self.config[7] = []
- continue
- elif line[0] == "font" :
- try:
- self.config[8] = pango.FontDescription(line[1].strip())
- except:
- error = True
- print _("Error reading config on line %(line)d. Pango font description expected.")%{"line":i}
- continue
- elif line[0] == "bg_color" :
- try:
- self.widget.set_bg_color(gtk.gdk.color_parse(line[1].strip()))
- except:
- error = True
- print _("Error reading config on line %(line)d. Expected color definition.")%{"line":i}
- elif line[0] == "text_color" :
- try:
- self.widget.set_text_color(gtk.gdk.color_parse(line[1].strip()))
- except:
- error = True
- print _("Error reading config on line %(line)d. Expected color definition.")%{"line":i}
- elif line[0] == "name" :
- self.config[9] = line[1].strip()
- elif line[0] == "show_box" :
- if line[1].strip() == "True" :
- self.config[11] = True
- else :
- self.config[11] = False
- elif line[0] == "language" :
- try:
- if int(line[1].strip()) >=0 and int(line[1].strip()) < len(ussd_languages):
- self.config[10] = int(line[1].strip())
- else:
- error = True
- print _("Error reading config on line %(line)d. Unknown language code.")%{"line":i}
- except:
- error = True
- print _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
- elif line[0] == "args" :
- self.config[13] = line[1].strip()
- elif line[0] == "reggroup" :
- try:
- self.config[14] = int(line[1].strip())
- except:
- error = True
- print _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
- continue
- elif line[0] == "listen_sms" :
- if line[1].strip() == "True" :
- self.config[15] = True
- else :
- self.config[15] = False
- elif line[0] == "sms_number" :
- self.config[16] = line[1].strip()
- elif line[0] == "sms_regexp" :
- self.config[17] = line[1].strip()
- elif line[0] == "sms_timeout" :
- try:
- self.config[18] = int(line[1].strip())
- except:
- error = True
- print _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
- continue
- else :
- error = True
- print _("Error reading config on line %(line)d. Unexpected variable: ")%{"line":i}+line[0]
- continue
-
- config.close()
-
- if error :
- self.widget.error = 1
- self.widget.set_text (_("Config error"), 5000)
-
- return self.config
- except IOError:
- self.widget.error = 1
- self.widget.set_text (_("Config error"), 0)
- print _("IO error while reading config")
-
- return self.default_config
-
- def on_show_settings( self, widget ) :
- dialog = UssdConfigDialog(self.config, self.widget.get_bg_color(), self.widget.get_text_color(), self.id)
-
- while True:
- if dialog.run() != gtk.RESPONSE_OK :
- dialog.destroy()
- return
-
- test = check_regexp(dialog.regexp.get_text())
- if test :
- dialog.on_error_regexp(test)
- continue
-
- # Check, that we have ussd number
- if not check_number(dialog.ussdNumber.get_text()):
- dialog.on_error_ussd_number()
- continue
-
- if not check_number(dialog.sms_number.get_text()):
- dialog.on_error_sms_number()
- continue
-
- # Parse retry pattern
- retry = dialog.retryEdit.get_text().strip()
- if retry != "" :
- retry = retry.split("-")
- i = 0
- while i < len(retry) :
- try:
- retry[i] = int(retry[i])
- except:
- dialog.on_error_retry_pattern()
- break
- i += 1
-
- if i < len(retry):
- continue
- else :
- retry = []
-
- break
-
- self.config = [
- dialog.ussdNumber.get_text(),
- dialog.parser.get_text(),
- dialog.chain.get_text(),
- dialog.update_interval.get_value(),
- dialog.regexp.get_text(),
- dialog.widthEdit.get_value(),
- dialog.query_at_start.get_active(),
- retry,
- dialog.font,
- dialog.wname.get_text(),
- dialog.language.get_active(),
- dialog.show_box.get_active(),
- dialog.b_parser.get_text(),
- dialog.args.get_text(),
- dialog.reggroup.get_value(),
- dialog.sms_listener.get_active(),
- dialog.sms_number.get_text(),
- dialog.sms_regexp.get_text(),
- dialog.sms_timeout.get_value()
- ]
-
- widget.set_bg_color(dialog.bg_color)
- widget.set_text_color(dialog.text_color)
-
- self.save_config()
-
- widget.set_width(self.config[5])
- self.reset_timed_renew()
- self.widget.label.modify_font(self.config[8])
-
- dialog.destroy()
-
- # Before running this function widget wasn't configured
- if self.config == self.default_config:
- self.widget.set_text(_("Click to update"))
- return
-
- def handle_sms(self, pdumsg, msgcenter, message, sendernumber):
- # Timeout was recieved first
- if self.sms_ready:
- return
-
- if self.config[16] == "" or self.config[16] == sendernumber:
- pdu = gsmdecode.decode_pdu (pdumsg)
- if pdu != None :
- self.sms_reply += pdu['user_data']
- if not pdu['part']:
- if self.config[17] == "" or re.search( self.config[17], message, re.MULTILINE | re.UNICODE ):
- self.sms_ready = True
- self.sms_signal.remove()
- self.process_reply()
-
- def callback_ussd_data( self, source, condition ):
- if condition == gobject.IO_IN or condition == gobject.IO_PRI :
- data = source.read( )
- if len( data ) > 0 :
- self.cb_reply += data
- return True
- else :
- self.cb_ready = 1
- return False
-
- elif condition == gobject.IO_HUP or condition == gobject.IO_ERR :
- self.cb_ready = 1
- self.ussd_ready = True
- self.process_reply()
- return False
-
- print (_("serious problems in program logic"))
- # This will force widget to show error message
- self.cb_reply = ""
- self.cb_ready = 1
- self.process_reply()
- return False
-
-
- def call_external_script( self, ussd_code, language ):
- self.cb_ready = 0
- self.cb_reply = "";
- p = subprocess.Popen(['/usr/bin/ussdquery.py', ussd_code, "-l", ussd_languages[language]] + smart_split_string(self.config[13],"%","&"), stdout=subprocess.PIPE)
- gobject.io_add_watch( p.stdout, gobject.IO_IN | gobject.IO_PRI | gobject.IO_HUP | gobject.IO_ERR , self.callback_ussd_data )
-
- def ussd_renew(self, widget, event):
- if self.widget.processing == 0:
- if self.config :
- widget.processing = 1
- widget.set_text(_("Processing"), 0)
-
- self.ussd_ready = False
- self.sms_ready = False
- self.sms_reply = ""
-
- if self.config[15]:
- self.sms_counter += 1
- self.retry_timer = gobject.timeout_add (1000*self.config[18], self.sms_timeout, self.sms_counter)
-
- self.bus = dbus.SystemBus()
- self.sms_signal = self.bus.add_signal_receiver(self.handle_sms, path='/com/nokia/phone/SMS', dbus_interface='Phone.SMS', signal_name='IncomingSegment')
-
- self.call_external_script( self.config[0], self.config[10] )
- else :
- widget.processing = 0
- widget.error = 1
- widget.set_text(_("No config"), 0)
-
- def process_reply( self ):
- if not self.ussd_ready or not self.sms_ready and self.config[15]:
- return
-
- reply = self.cb_reply.strip()
- sms_reply = self.sms_reply.strip()
-
- if reply == "" or self.config[15] and sms_reply == "" :
- self.widget.error = 1
- self.widget.set_text (_("Error"), 5000)
- if self.retry_state == len(self.config[7]):
- self.retry_version += 1
- self.retry_state = 0
- else :
- self.retry_timer = gobject.timeout_add (1000*self.config[7][self.retry_state], self.retry_renew, self.retry_version)
- self.retry_state += 1
- else :
- self.widget.error = 0
- # Apply regexp
- reresult1 = reresult2 = None
- if self.config[4] != "":
- reresult1 = re.search( self.config[4], reply, re.MULTILINE | re.UNICODE )
- if self.config[17] != "":
- reresult2 = re.search( self.config[17], sms_reply, re.MULTILINE | re.UNICODE )
- w_reply = b_reply = reply
- if self.widget.error == 0:
- # Pass to box parser
- if self.config[12] != "" and self.config[11]:
- try:
- p = subprocess.Popen(smart_split_string(self.config[12], reply, sms_reply, reresult1, reresult2), stdout=subprocess.PIPE)
- b_reply = p.communicate()[0].strip()
- except Exception, e:
- print _("Couldn't exec banner parser:")+str(e)
- self.widget.error = 1
- else:
- if self.config[4] != "":
- try :
- b_reply = reresult1.group( self.config[14] )
- except Exception, e:
- self.widget.error = 1
- b_reply = _("Group not found: \n") + reply
-
- # Pass to widget parser
- if self.config[1] != "":
- try:
- p = subprocess.Popen(smart_split_string(self.config[1], reply, sms_reply, reresult1, reresult2), stdout=subprocess.PIPE)
- w_reply = p.communicate()[0].strip()
- except Exception, e:
- print _("Couldn't exec widget parser:")+str(e)
- self.widget.error = 1
- else:
- if self.config[4] != "":
- try :
- w_reply = reresult1.group( self.config[14] )
- except Exception, e:
- self.widget.error = 1
- w_reply = _("Group not found: \n") + reply
- # Pass to chain
- if self.config[2] != "":
- try:
- p = subprocess.Popen(smart_split_string(self.config[2], reply, sms_reply, reresult1, reresult2))
- 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 sms_timeout(self, version):
- if version == self.sms_counter :
- self.sms_reply = ""
- self.sms_ready = True
- self.sms_signal.remove()
- self.process_reply()
- return False
-
- def timed_renew(self, version):
- if version < self.timeout_version :
- return False
- self.ussd_renew(self.widget, None)
- return True
-
- def retry_renew(self,version):
- if self.widget.error == 0 or self.widget.processing == 1 or version < self.retry_version :
- return False
- self.ussd_renew(self.widget, None)
- return False
-
- def reset_timed_renew (self) :
- self.timeout_version += 1
- if self.config[3] != 0 :
- self.timer = gobject.timeout_add (60000*self.config[3], self.timed_renew, self.timeout_version)
+ def __init__( self, widget ) :
+ self.widget = widget
+ # number, parser, chain, interval, regexp, width, execute_at_start,
+ # retry pattern, font, name, language, show_message_box,
+ # message_box_parser, additional arguments, regexp group,
+ # use SMS listener, SMS number, SMS regexp, SMS timeout
+ self.default_config = ["", "", "", 0, "", 0, True, [],\
+ pango.FontDescription("Nokia Sans 18"), _("Click to update"),\
+ 15, False, "", "", 1, False, "", "", 60]
+ self.config = self.default_config
+ self.timeout_version = 0
+ self.retry_version = 0
+ self.retry_state = 0
+ self.sms_counter = 0
+ self.sms_reply = ""
+
+ def save_config( self ) :
+ configname = os.getenv("HOME")+"/.ussdWidget.conf"
+ # Aquire lock
+ lockf = open(configname+".lock", 'a')
+ fcntl.flock(lockf,fcntl.LOCK_EX)
+
+ oldconfig=""
+ try:
+ fconfig = open(configname,"r")
+ #Read configuration of other instances
+ my_section = False
+ for line in fconfig :
+ if line[0] == '%':
+ my_section = line[1:].strip() == self.id
+ if not my_section:
+ oldconfig += line
+ fconfig.close()
+ except:
+ print >> log, _("Couldn't read previous config")
+
+ fconfig = open(configname,"w")
+ fconfig.seek(0)
+ 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 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"])
+ fconfig.writelines(["#Widget width\n", "width="+str(self.config[5]), "\n"])
+ fconfig.writelines(["#Execute query at start\n", "query_at_start="+str(self.config[6]), "\n"])
+ fconfig.writelines(["#Retry pattern\n"])
+ fconfig.write("retry=")
+ first = True
+ for i in self.config[7]:
+ if not first:
+ fconfig.write ("-")
+ fconfig.write(str(i))
+ first = False
+ fconfig.write("\n")
+ 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], "\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.writelines(["#Additional ussdquery.py arguments\n", "args="+self.config[13], "\n"])
+ fconfig.writelines(["#Regexp matching group\n", "reggroup="+str(self.config[14]), "\n"])
+ fconfig.writelines(["#Use SMS listener\n", "listen_sms="+str(self.config[15]), "\n"])
+ fconfig.writelines(["#Number,from which SMS should come\n", "sms_number="+self.config[16], "\n"])
+ fconfig.writelines(["#SMS RegExp pattern\n", "sms_regexp="+self.config[17], "\n"])
+ fconfig.writelines(["#SMS timeout\n", "sms_timeout="+str(self.config[18]), "\n"])
+ fconfig.close()
+
+ fcntl.flock(lockf,fcntl.LOCK_UN)
+ lockf.close()
+
+ def get_config(self):
+ return self.config
+
+ def read_config( self, id ):
+ try :
+ self.id = id
+ config = open(os.getenv("HOME")+"/.ussdWidget.conf","r")
+
+ error = False
+ i = 0
+ my_section = False
+ for line in config :
+ i += 1
+ if line[0] == '#':
+ continue
+ if line[0] == '%':
+ my_section = line[1:].strip() == id
+ continue
+
+ if not my_section:
+ # This is config for another instace
+ continue
+
+ line=line.split('=', 1)
+
+ if len(line) != 2 :
+ error = True
+ print >> log, _("Error reading config on line %(line)d. = or # expected.")%{"line":i}
+ continue
+ if line[0] == "number" :
+ 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" :
+ try:
+ self.config[3] = int(line[1].strip())
+ except:
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
+ continue
+ elif line[0] == "regexp" :
+ self.config[4] = line[1].strip()
+ elif line[0] == "width" :
+ try:
+ self.config[5] = int(line[1].strip())
+ except:
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
+ continue
+ elif line[0] == "query_at_start" :
+ if line[1].strip() == "True" :
+ self.config[6] = True
+ else :
+ self.config[6] = False
+ elif line[0] == "retry" :
+ line[1] = line[1].strip()
+ if line[1] != "":
+ line[1] = line[1].split("-")
+ i = 0
+ while i < len(line[1]) :
+ try:
+ line[1][i] = int(line[1][i])
+ except:
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
+ i += 1
+ self.config[7] = line[1]
+ else:
+ self.config[7] = []
+ continue
+ elif line[0] == "font" :
+ try:
+ self.config[8] = pango.FontDescription(line[1].strip())
+ except:
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Pango font description expected.")%{"line":i}
+ continue
+ elif line[0] == "bg_color" :
+ try:
+ self.widget.set_bg_color(gtk.gdk.color_parse(line[1].strip()))
+ except:
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Expected color definition.")%{"line":i}
+ elif line[0] == "text_color" :
+ try:
+ self.widget.set_text_color(gtk.gdk.color_parse(line[1].strip()))
+ except:
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Expected color definition.")%{"line":i}
+ elif line[0] == "name" :
+ self.config[9] = line[1].strip()
+ elif line[0] == "show_box" :
+ if line[1].strip() == "True" :
+ self.config[11] = True
+ else :
+ self.config[11] = False
+ elif line[0] == "language" :
+ try:
+ if int(line[1].strip()) >=0 and int(line[1].strip()) < len(ussd_languages):
+ self.config[10] = int(line[1].strip())
+ else:
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Unknown language code.")%{"line":i}
+ except:
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
+ elif line[0] == "args" :
+ self.config[13] = line[1].strip()
+ elif line[0] == "reggroup" :
+ try:
+ self.config[14] = int(line[1].strip())
+ except:
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
+ continue
+ elif line[0] == "listen_sms" :
+ if line[1].strip() == "True" :
+ self.config[15] = True
+ else :
+ self.config[15] = False
+ elif line[0] == "sms_number" :
+ self.config[16] = line[1].strip()
+ elif line[0] == "sms_regexp" :
+ self.config[17] = line[1].strip()
+ elif line[0] == "sms_timeout" :
+ try:
+ self.config[18] = int(line[1].strip())
+ except:
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Integer expected.")%{"line":i}
+ continue
+ else :
+ error = True
+ print >> log, _("Error reading config on line %(line)d. Unexpected variable: ")%{"line":i}+line[0]
+ continue
+
+ config.close()
+
+ if error :
+ self.widget.error = 1
+ self.widget.set_text (_("Config error"), 5000)
+
+ return self.config
+ except IOError:
+ self.widget.error = 1
+ self.widget.set_text (_("Config error"), 0)
+ print >> log, _("IO error while reading config")
+
+ return self.default_config
+
+ def on_show_settings( self, widget ) :
+ dialog = UssdConfigDialog(self.config, self.widget.get_bg_color(), self.widget.get_text_color(), self.id)
+
+ while True:
+ if dialog.run() != gtk.RESPONSE_OK :
+ dialog.destroy()
+ return
+
+ test = check_regexp(dialog.regexp.get_text())
+ if test :
+ dialog.on_error_regexp(test)
+ continue
+
+ # Check, that we have ussd number
+ if not check_number(dialog.ussdNumber.get_text()):
+ dialog.on_error_ussd_number()
+ continue
+
+ if not check_number(dialog.sms_number.get_text()):
+ dialog.on_error_sms_number()
+ continue
+
+ # Parse retry pattern
+ retry = dialog.retryEdit.get_text().strip()
+ if retry != "" :
+ retry = retry.split("-")
+ i = 0
+ while i < len(retry) :
+ try:
+ retry[i] = int(retry[i])
+ except:
+ dialog.on_error_retry_pattern()
+ break
+ i += 1
+
+ if i < len(retry):
+ continue
+ else :
+ retry = []
+
+ break
+
+ self.config = [
+ dialog.ussdNumber.get_text(),
+ dialog.parser.get_text(),
+ dialog.chain.get_text(),
+ dialog.update_interval.get_value(),
+ dialog.regexp.get_text(),
+ dialog.widthEdit.get_value(),
+ dialog.query_at_start.get_active(),
+ retry,
+ dialog.font,
+ dialog.wname.get_text(),
+ dialog.language.get_active(),
+ dialog.show_box.get_active(),
+ dialog.b_parser.get_text(),
+ dialog.args.get_text(),
+ dialog.reggroup.get_value(),
+ dialog.sms_listener.get_active(),
+ dialog.sms_number.get_text(),
+ dialog.sms_regexp.get_text(),
+ dialog.sms_timeout.get_value()
+ ]
+
+ widget.set_bg_color(dialog.bg_color)
+ widget.set_text_color(dialog.text_color)
+
+ self.save_config()
+
+ widget.set_width(self.config[5])
+ self.reset_timed_renew()
+ self.widget.label.modify_font(self.config[8])
+
+ dialog.destroy()
+
+ # Before running this function widget wasn't configured
+ if self.config == self.default_config:
+ self.widget.set_text(_("Click to update"))
+ return
+
+ def handle_sms(self, pdumsg, msgcenter, message, sendernumber):
+ # Timeout was recieved first
+ if self.sms_ready:
+ return
+
+ if self.config[16] == "" or self.config[16] == sendernumber:
+ pdu = gsmdecode.decode_pdu (pdumsg)
+ if pdu != None :
+ self.sms_reply += pdu['user_data']
+ if not pdu['part']:
+ if self.config[17] == "" or re.search( self.config[17], message, re.MULTILINE | re.UNICODE ):
+ self.sms_ready = True
+ self.sms_signal.remove()
+ self.process_reply()
+
+ def callback_ussd_data(self, source, condition, process):
+ if condition == gobject.IO_IN or condition == gobject.IO_PRI:
+ data = source.read()
+ self.cb_reply += data
+ return True
+
+ if condition == gobject.IO_ERR:
+ print >> log, "Communication error occured"
+ # This will force widget to show error message
+ self.cb_reply = ""
+
+ if condition == gobject.IO_HUP:
+ # Pipe is broken, so ussd-query.py is already terminating
+ # and we wouldn't wait fot it too long
+ retcode = process.wait()
+ if retcode != 0:
+ self.cb_reply = ""
+
+ self.ussd_ready = True
+ self.process_reply()
+ return False
+
+ def callback_ussd_error(self, source, condition):
+ if condition == gobject.IO_IN or condition == gobject.IO_PRI:
+ self.error_message += source.read()
+ else:
+ if self.error_message != "":
+ print >> log, self.error_message
+
+ def call_external_script(self, ussd_code, language):
+ self.cb_reply = "";
+ process = subprocess.Popen(
+ ['/usr/bin/ussdquery.py', ussd_code, "-l", ussd_languages[language]] +\
+ smart_split_string(self.config[13],"%","&"),
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ process.stdin.close()
+ gobject.io_add_watch(
+ p.stdout, gobject.IO_IN | gobject.IO_PRI | gobject.IO_HUP | gobject.IO_ERR,
+ self.callback_ussd_data,
+ process)
+ self.error_message = "";
+ gobject.io_add_watch(
+ p.stderr, gobject.IO_IN | gobject.IO_PRI | gobject.IO_HUP | gobject.IO_ERR,
+ self.callback_ussd_error)
+
+ def ussd_renew(self, widget, event):
+ if self.widget.processing == 0:
+ if self.config :
+ widget.processing = 1
+ widget.set_text(_("Processing"), 0)
+
+ self.ussd_ready = False
+ self.sms_ready = False
+ self.sms_reply = ""
+
+ if self.config[15]:
+ self.sms_counter += 1
+ self.retry_timer = gobject.timeout_add (1000*self.config[18], self.sms_timeout, self.sms_counter)
+
+ self.bus = dbus.SystemBus()
+ self.sms_signal = self.bus.add_signal_receiver(self.handle_sms, path='/com/nokia/phone/SMS', dbus_interface='Phone.SMS', signal_name='IncomingSegment')
+
+ self.call_external_script( self.config[0], self.config[10] )
+ else :
+ widget.processing = 0
+ widget.error = 1
+ widget.set_text(_("No config"), 0)
+
+ def process_reply( self ):
+ if not self.ussd_ready or not self.sms_ready and self.config[15]:
+ return
+
+ reply = self.cb_reply.strip()
+ sms_reply = self.sms_reply.strip()
+
+ if reply == "" or self.config[15] and sms_reply == "" :
+ self.widget.error = 1
+ self.widget.set_text (_("Error"), 5000)
+ if self.retry_state == len(self.config[7]):
+ self.retry_version += 1
+ self.retry_state = 0
+ else :
+ self.retry_timer = gobject.timeout_add (1000*self.config[7][self.retry_state], self.retry_renew, self.retry_version)
+ self.retry_state += 1
+ else :
+ self.widget.error = 0
+ # Apply regexp
+ reresult1 = reresult2 = None
+ if self.config[4] != "":
+ reresult1 = re.search( self.config[4], reply, re.MULTILINE | re.UNICODE )
+ if self.config[17] != "":
+ reresult2 = re.search( self.config[17], sms_reply, re.MULTILINE | re.UNICODE )
+ w_reply = b_reply = reply
+ if self.widget.error == 0:
+ # Pass to box parser
+ if self.config[12] != "" and self.config[11]:
+ try:
+ p = subprocess.Popen(smart_split_string(self.config[12], reply, sms_reply, reresult1, reresult2), stdout=subprocess.PIPE)
+ b_reply = p.communicate()[0].strip()
+ except Exception, e:
+ print >> log, _("Couldn't exec banner parser:")+str(e)
+ self.widget.error = 1
+ else:
+ if self.config[4] != "":
+ try :
+ b_reply = reresult1.group( self.config[14] )
+ except Exception, e:
+ self.widget.error = 1
+ b_reply = _("Group not found: \n") + reply
+
+ # Pass to widget parser
+ if self.config[1] != "":
+ try:
+ p = subprocess.Popen(smart_split_string(self.config[1], reply, sms_reply, reresult1, reresult2), stdout=subprocess.PIPE)
+ w_reply = p.communicate()[0].strip()
+ except Exception, e:
+ print >> log, _("Couldn't exec widget parser:")+str(e)
+ self.widget.error = 1
+ else:
+ if self.config[4] != "":
+ try :
+ w_reply = reresult1.group( self.config[14] )
+ except Exception, e:
+ self.widget.error = 1
+ w_reply = _("Group not found: \n") + reply
+ # Pass to chain
+ if self.config[2] != "":
+ try:
+ p = subprocess.Popen(smart_split_string(self.config[2], reply, sms_reply, reresult1, reresult2))
+ except Exception, e:
+ print >> log, _("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 sms_timeout(self, version):
+ if version == self.sms_counter :
+ self.sms_reply = ""
+ self.sms_ready = True
+ self.sms_signal.remove()
+ self.process_reply()
+ return False
+
+ def timed_renew(self, version):
+ if version < self.timeout_version :
+ return False
+ self.ussd_renew(self.widget, None)
+ return True
+
+ def retry_renew(self,version):
+ if self.widget.error == 0 or self.widget.processing == 1 or version < self.retry_version :
+ return False
+ self.ussd_renew(self.widget, None)
+ return False
+
+ def reset_timed_renew (self) :
+ self.timeout_version += 1
+ if self.config[3] != 0 :
+ self.timer = gobject.timeout_add (60000*self.config[3], self.timed_renew, self.timeout_version)