~dhavlicek/awn-extras/dhavlicek

« back to all changes in this revision

Viewing changes to src/weather/weatherprefs.py

  • Committer: Dave Havlicek
  • Date: 2009-01-30 17:54:50 UTC
  • Revision ID: dhavlicek@info.com-20090130175450-atwey8hvr872b0tg
dhavlicek bugfix branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: iso-8859-15 -*-
 
3
#
 
4
# Copyright (c) 2007, 2008:
 
5
#   Mike (mosburger) Desjardins <desjardinsmike@gmail.com>
 
6
#   Mike Rooney (launchpad.net/~michael) <mrooney@gmail.com>
 
7
#
 
8
# This is the configuration dialog for a weather applet for Avant Window Navigator.
 
9
#
 
10
# This library is free software; you can redistribute it and/or
 
11
# modify it under the terms of the GNU Lesser General Public
 
12
# License as published by the Free Software Foundation; either
 
13
# version 2 of the License, or (at your option) any later version.
 
14
#
 
15
# This library is distributed in the hope that it will be useful,
 
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
# Lesser General Public License for more details.
 
19
#
 
20
# You should have received a copy of the GNU Lesser General Public
 
21
# License along with this library; if not, write to the
 
22
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
23
# Boston, MA 02111-1307, USA.
 
24
 
 
25
import gtk, cairo
 
26
import gtk.glade
 
27
import os
 
28
from gtk import gdk
 
29
import xml.dom.minidom
 
30
import urllib
 
31
import weathericons
 
32
from helpers import debug
 
33
 
 
34
class WeatherCodeSearch(gtk.Window):
 
35
    def __init__(self, config_win):
 
36
        gtk.Window.__init__(self)
 
37
 
 
38
        self.config_win = config_win
 
39
        self.set_title(_("Search for Location Code"))        # needs i18n
 
40
        vbox = gtk.VBox(False, 2)
 
41
        self.add(vbox)
 
42
        # row 1
 
43
        hbox1 = gtk.HBox(True,0)
 
44
        label1 = gtk.Label(_("Location Name"))
 
45
        hbox1.pack_start(label1)
 
46
        self.location = gtk.Entry(20)
 
47
        self.location.connect("changed", self.text_entered, "text")
 
48
        self.location.connect("activate", self.go_clicked, "go")
 
49
        hbox1.pack_start(self.location)
 
50
        self.go = gtk.Button(_("Search"))
 
51
        self.go.connect("clicked", self.go_clicked, "go")
 
52
        self.go.set_sensitive(False)
 
53
        hbox1.pack_start(self.go)
 
54
        vbox.pack_start(hbox1,False,False,2)
 
55
        # row 1a
 
56
        hbox1a = gtk.HBox(True,0)
 
57
        label2 = gtk.Label(_("Enter the location you want to search for above.  For example: <i>Boston</i>; <i>Portland, ME</i>; <i>Paris</i>; or <i>Osaka, Japan</i>."))
 
58
        label2.set_line_wrap(True)
 
59
        label2.set_use_markup(True)
 
60
        hbox1a.pack_start(label2)
 
61
        vbox.pack_start(hbox1a,False,False,5)
 
62
        # row 2
 
63
        self.scrolled_win = gtk.ScrolledWindow()
 
64
        self.scrolled_win.set_border_width(10)
 
65
        self.scrolled_win.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
 
66
        self.list = gtk.ListStore(str,str)
 
67
        self.list.append([_("No records found."),None])
 
68
        self.treeview = gtk.TreeView(self.list)
 
69
        tvcolumn = gtk.TreeViewColumn()
 
70
        self.treeview.append_column(tvcolumn)
 
71
        cell = gtk.CellRendererText()
 
72
        tvcolumn.pack_start(cell,True)
 
73
        tvcolumn.add_attribute(cell, 'text', 0)
 
74
        self.treeview.set_sensitive(False)
 
75
        self.treeview.set_headers_visible(False)
 
76
        self.treeview.set_rules_hint(True)
 
77
        self.treeview.set_fixed_height_mode(True)
 
78
        self.treeview.connect("cursor-changed", self.selected, "selected")
 
79
        self.scrolled_win.add_with_viewport(self.treeview)
 
80
        vbox.pack_start(self.scrolled_win,True,True,5)
 
81
        # row 3
 
82
        hbox3 = gtk.HButtonBox()
 
83
        hbox3.set_layout(gtk.BUTTONBOX_END)
 
84
        self.ok = gtk.Button(stock=gtk.STOCK_OK)
 
85
        self.ok.set_sensitive(False)
 
86
        self.ok.connect("clicked", self.ok_button, "ok")
 
87
        hbox3.add(self.ok)
 
88
        self.cancel = gtk.Button(stock=gtk.STOCK_CANCEL)
 
89
        self.cancel.connect("clicked", self.cancel_button, "cancel")
 
90
        hbox3.add(self.cancel)
 
91
        vbox.pack_start(hbox3,False,False,2)
 
92
 
 
93
    def selected(self, widget, window):
 
94
        self.ok.set_sensitive(True)
 
95
 
 
96
    def text_entered(self, widget, window):
 
97
        if self.location.get_text() == "":
 
98
            self.go.set_sensitive(False)
 
99
        else:
 
100
            self.go.set_sensitive(True)
 
101
        
 
102
    def ok_button(self, widget, window):
 
103
        treeselection = self.treeview.get_selection()
 
104
        (model, iter) = treeselection.get_selected()
 
105
        self.selected_location = model.get_value(iter,0)
 
106
        self.selected_code = model.get_value(iter, 1)
 
107
        self.config_win.location = self.selected_location
 
108
        self.config_win.location_code = self.selected_code
 
109
        self.config_win.loc_label.set_label("<b>" + self.selected_location + "</b>")
 
110
        self.destroy()
 
111
 
 
112
    def cancel_button(self, widget, window):
 
113
        self.destroy()
 
114
 
 
115
    def go_clicked(self, widget, window):
 
116
        if self.location.get_text() != "":
 
117
            self.list.clear()
 
118
            self.list.append([_("Searching..."),None])
 
119
            url = 'http://xoap.weather.com/search/search?where=' + urllib.quote(self.location.get_text())
 
120
            usock = urllib.urlopen(url)
 
121
            xmldoc = xml.dom.minidom.parse(usock)
 
122
            usock.close()
 
123
            locations = xmldoc.getElementsByTagName("loc")
 
124
            empty = True
 
125
            for loc in locations:
 
126
                if empty == True:
 
127
                    self.treeview.set_sensitive(True)
 
128
                    self.list.clear()
 
129
                    empty = False
 
130
                code = loc.getAttribute('id')
 
131
                city = loc.childNodes[0].data
 
132
                self.list.append([city,code])
 
133
            if empty == True:
 
134
                self.list.clear()
 
135
                self.list.append([_("No records found."),None])
 
136
 
 
137
class WeatherConfig:
 
138
    def __init__(self, applet):
 
139
        glade_path = os.path.join(os.path.dirname(__file__),
 
140
                                  "weather-prefs.glade")
 
141
        self.wTree = gtk.glade.XML(glade_path)
 
142
 
 
143
        self.toplevel = self.wTree.get_widget("weatherDialog")
 
144
 
 
145
        self.applet = applet
 
146
        self.location = applet.settingsDict['location']
 
147
        self.location_code = applet.settingsDict['location_code']
 
148
        self.units_checkbox = self.wTree.get_widget("metricCheckbutton")
 
149
        if applet.settingsDict['metric']:
 
150
            self.units_checkbox.set_active(True)
 
151
        else:
 
152
            self.units_checkbox.set_active(False)
 
153
 
 
154
        self.click_checkbox = self.wTree.get_widget("clickCheckbutton")
 
155
        if applet.settingsDict['open_til_clicked']:
 
156
            self.click_checkbox.set_active(False)
 
157
        else: 
 
158
            self.click_checkbox.set_active(True)
 
159
 
 
160
        self.click_checkbox2 = self.wTree.get_widget("curvedCheckbutton")
 
161
        if applet.settingsDict['curved_dialog']:
 
162
            self.click_checkbox2.set_active(True)
 
163
        else: 
 
164
            self.click_checkbox2.set_active(False)
 
165
        
 
166
        self.temp_pos = self.wTree.get_widget("posCombobox")
 
167
 
 
168
        self.temp_pos.set_active(applet.settingsDict['temp_position'])
 
169
        
 
170
        # TEMP_FONTSIZE
 
171
        self.tempspin = self.wTree.get_widget("fontSpinbutton")
 
172
        font_size = applet.settingsDict['temp_fontsize']
 
173
        self.tempspin.set_value(font_size)
 
174
        
 
175
        # MAP_MAXWIDTH
 
176
        self.spin2 = self.wTree.get_widget("mapWidthSpinbutton")
 
177
        current_size = applet.settingsDict['map_maxwidth']
 
178
        self.spin2.set_value(current_size)
 
179
 
 
180
        # FREQUENCY (ICON)
 
181
        self.spin = self.wTree.get_widget("iconSpinbutton")
 
182
        current_freq = applet.settingsDict['frequency']
 
183
        self.spin.set_value(current_freq)
 
184
 
 
185
        # FREQUENCY (5DAY)
 
186
        adj = gtk.Adjustment(30, 30, 120, 5, 15, 0)
 
187
        self.spin3 = self.wTree.get_widget("freqSpinbutton")
 
188
        current_freq = applet.settingsDict['frequency_5day']
 
189
        self.spin3.set_value(current_freq)
 
190
 
 
191
        # FREQUENCY (MAP)
 
192
        self.spin4 = self.wTree.get_widget("mapSpinbutton")
 
193
        current_freq = applet.settingsDict['frequency_map']
 
194
        self.spin4.set_value(current_freq)
 
195
 
 
196
        # LOCATION
 
197
        hbox2 = gtk.HBox(True, 0)
 
198
        self.loc_label = self.wTree.get_widget("locationLabel")
 
199
        self.loc_label.set_markup("<b>" + self.location + "</b>")
 
200
        
 
201
        # change location button
 
202
        search = self.wTree.get_widget("locationButton")
 
203
        search.connect("clicked", self.search_button, "search")
 
204
 
 
205
        ok = self.wTree.get_widget("okButton")
 
206
        ok.connect("clicked", self.ok_button, applet)
 
207
        cancel = self.wTree.get_widget("cancelButton")
 
208
        cancel.connect("clicked", self.cancel_button, applet)
 
209
      
 
210
    def get_toplevel(self):
 
211
        return self.toplevel
 
212
 
 
213
    def search_button(self, widget, event):
 
214
        self.code_window = WeatherCodeSearch(self)
 
215
        self.code_window.set_size_request(400, 400)
 
216
        self.code_window.set_modal(True)
 
217
        self.code_window.set_destroy_with_parent(True)
 
218
        icon_name = weathericons.GetIcon("0")
 
219
        icon = gtk.gdk.pixbuf_new_from_file(icon_name)
 
220
        self.code_window.set_icon(icon)
 
221
 
 
222
        self.code_window.show_all()
 
223
        
 
224
    def ok_button(self, widget, parent):
 
225
        mapping = {
 
226
            'location' : self.location,
 
227
            'location_code' : self.location_code,
 
228
            'metric' : self.units_checkbox.get_active(),
 
229
            'temp_position' : self.temp_pos.get_active(),
 
230
            'temp_fontsize' : self.tempspin.get_value_as_int(),
 
231
            'open_til_clicked' : not self.click_checkbox.get_active(),
 
232
            'curved_dialog' : self.click_checkbox2.get_active(),
 
233
            'map_maxwidth' : self.spin2.get_value_as_int(),
 
234
            'frequency' : self.spin.get_value_as_int(),
 
235
            'frequency_5day' : self.spin3.get_value_as_int(),
 
236
            'frequency_map' : self.spin4.get_value_as_int(),
 
237
        }
 
238
        
 
239
        for name, value in mapping.items():
 
240
            self.applet.applet.settings[name] = value
 
241
 
 
242
        parent.onSettingsChanged() #TODO: remove this once we have listening properly working, maybe?
 
243
        self.toplevel.destroy()
 
244
 
 
245
    def cancel_button(self, widget, event):
 
246
        self.toplevel.destroy()