|
1 # Simple GUI example 2 |
|
2 |
|
3 # Copyright (c) 2005 Nokia Corporation |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
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, |
|
54 "temperature_string") |
|
55 appuifw.popup_menu([(u"Weather", unicode(weather)), |
|
56 (u"Temperature", |
|
57 unicode(temperature_string))], |
|
58 unicode(code)) |
|
59 except IOError: |
|
60 appuifw.note(u"Connection error to server", 'error') |
|
61 except: |
|
62 appuifw.note(u"Could not fetch information", 'error') |
|
63 lb.set_list(choices_labels) |
|
64 |
|
65 def handle_add(): |
|
66 pass |
|
67 |
|
68 def handle_delete(): |
|
69 pass |
|
70 |
|
71 def exit_key_handler(): |
|
72 app_lock.signal() |
|
73 |
|
74 lb = appuifw.Listbox(choices_labels, handle_selection) |
|
75 |
|
76 old_title = appuifw.app.title |
|
77 appuifw.app.title = u"Weather report" |
|
78 appuifw.app.body = lb |
|
79 appuifw.app.menu = [(u"Add new item", handle_add), |
|
80 (u"Delete item", handle_delete)] |
|
81 appuifw.app.exit_key_handler = exit_key_handler |
|
82 |
|
83 app_lock = e32.Ao_lock() |
|
84 app_lock.wait() |
|
85 |
|
86 appuifw.app.title = old_title |
|
87 |