src/extras/examples/GUI_example_2.py
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 # Simple GUI example 2
       
     2 # Copyright (c) 2005 Nokia Corporation
       
     3 #
       
     4 # Licensed under the Apache License, Version 2.0 (the "License");
       
     5 # you may not use this file except in compliance with the License.
       
     6 # You may obtain a copy of the License at
       
     7 #
       
     8 #     http://www.apache.org/licenses/LICENSE-2.0
       
     9 #
       
    10 # Unless required by applicable law or agreed to in writing, software
       
    11 # distributed under the License is distributed on an "AS IS" BASIS,
       
    12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13 # See the License for the specific language governing permissions and
       
    14 # limitations under the License.
       
    15 
       
    16 
       
    17 import socket
       
    18 import urllib
       
    19 
       
    20 import e32
       
    21 import appuifw
       
    22 
       
    23 choices =[(u"Los Angeles Intl Airport", "KLAX"),
       
    24           (u"Dallas/Fort Forth", "KDFW"),
       
    25           (u"New York/John F. Kennedy", "KJFK")]
       
    26 choices_labels = [x[0] for x in choices]
       
    27         
       
    28 weather_url_base = "http://weather.gov/data/current_obs/"
       
    29 tempfile = "c:\\weather.xml"
       
    30 
       
    31 def find_value(text, tag):
       
    32     "Find the value between <tag> and </tag> in text. Always returns a string"
       
    33     begin_tag = "<" + tag + ">"
       
    34     begin = text.find(begin_tag)
       
    35     end = text.find("</" + tag + ">")
       
    36     if begin == -1 or end == -1:
       
    37         return ""
       
    38     begin += len(begin_tag)
       
    39     return text[begin:end]
       
    40 
       
    41 def handle_selection():
       
    42     index = lb.current()
       
    43     code = choices[index][1]
       
    44     weather_url = weather_url_base + code + ".xml"
       
    45     lb.set_list([u"Please wait..."])
       
    46     appuifw.note(u"Fetching "+ weather_url, 'info')
       
    47     try:
       
    48         urllib.urlretrieve(weather_url, tempfile)
       
    49         f = open(tempfile, 'r')
       
    50         weatherinfo = f.read()
       
    51         f.close()
       
    52         weather = find_value(weatherinfo, "weather")
       
    53         temperature_string = find_value(weatherinfo, "temperature_string")
       
    54         appuifw.popup_menu([(u"Weather", unicode(weather)), 
       
    55                             (u"Temperature", unicode(temperature_string))], 
       
    56                             unicode(code))
       
    57     except IOError:
       
    58         appuifw.note(u"Connection error to server", 'error')
       
    59     except:
       
    60         appuifw.note(u"Could not fetch information", 'error')
       
    61     lb.set_list(choices_labels)
       
    62 
       
    63 def handle_add():
       
    64     pass
       
    65 
       
    66 def handle_delete():
       
    67     pass
       
    68 
       
    69 def exit_key_handler():
       
    70     app_lock.signal()
       
    71 
       
    72 lb = appuifw.Listbox(choices_labels, handle_selection)
       
    73 
       
    74 old_title = appuifw.app.title
       
    75 appuifw.app.title = u"Weather report"
       
    76 appuifw.app.body = lb
       
    77 appuifw.app.menu = [(u"Add new item", handle_add),
       
    78                     (u"Delete item", handle_delete)]
       
    79 appuifw.app.exit_key_handler = exit_key_handler
       
    80 
       
    81 app_lock = e32.Ao_lock()
       
    82 app_lock.wait()
       
    83 
       
    84 appuifw.app.title = old_title