~ubuntu-branches/ubuntu/quantal/wicd/quantal

« back to all changes in this revision

Viewing changes to wicd/prefs.py

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2009-06-22 17:59:27 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090622175927-iax3alden0bmj6zg
Tags: 1.6.1-3
* debian/config, debian/templates updated:
  - only show users to add to netdev, skip those who are already
    members (Closes: #534138)
  - gracefully handle upgrades from previous broken versions, where
    debconf set a value of ${default} for wicd/users
    (Closes: #532112)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
""" prefs -- Wicd Preferences Dialog.
 
4
 
 
5
Displays the main settings dialog window for wicd and
 
6
handles recieving/sendings the settings from/to the daemon.
 
7
 
 
8
"""
 
9
 
 
10
#
 
11
#   Copyright (C) 2008-2009 Adam Blackburn
 
12
#   Copyright (C) 2008-2009 Dan O'Reilly
 
13
#
 
14
#   This program is free software; you can redistribute it and/or modify
 
15
#   it under the terms of the GNU General Public License Version 2 as
 
16
#   published by the Free Software Foundation.
 
17
#
 
18
#   This program is distributed in the hope that it will be useful,
 
19
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
#   GNU General Public License for more details.
 
22
#
 
23
#   You should have received a copy of the GNU General Public License
 
24
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
25
#
 
26
 
 
27
import gtk
 
28
import gobject
 
29
#import pango
 
30
import os
 
31
import gtk.glade
 
32
 
 
33
from wicd import misc
 
34
from wicd import wpath
 
35
from wicd import dbusmanager
 
36
from wicd.misc import checkboxTextboxToggle, noneToBlankString
 
37
 
 
38
daemon = None
 
39
wireless = None
 
40
wired = None
 
41
 
 
42
from translations import language
 
43
 
 
44
USER_SETTINGS_DIR = os.path.expanduser('~/.wicd/')
 
45
 
 
46
def setup_dbus():
 
47
    global daemon, wireless, wired
 
48
    daemon = dbusmanager.get_interface('daemon')
 
49
    wireless = dbusmanager.get_interface('wireless')
 
50
    wired = dbusmanager.get_interface('wired')
 
51
 
 
52
class PreferencesDialog(object):
 
53
    """ Class for handling the wicd preferences dialog window. """
 
54
    def __init__(self, parent, wTree):
 
55
        setup_dbus()
 
56
        self.parent = parent
 
57
        self.wTree = wTree
 
58
        self.prep_settings_diag()
 
59
        self.load_preferences_diag()
 
60
        
 
61
    def _setup_external_app_radios(self, radio_list, get_method, set_method):
 
62
        """ Generic function for setting up external app radios. """
 
63
        # Disable radios for apps that aren't installed.
 
64
        for app in radio_list[1:]:
 
65
            app.set_sensitive(daemon.GetAppAvailable(app.get_label()))
 
66
        selected_app = get_method()
 
67
        # Make sure the app we want to select is actually available.
 
68
        if radio_list[selected_app].get_property("sensitive"):
 
69
            radio_list[selected_app].set_active(True)
 
70
        else:
 
71
            # If it isn't, default to Automatic.
 
72
            set_method(misc.AUTO)
 
73
            radio_list[misc.AUTO].set_active(True)
 
74
            
 
75
    def load_preferences_diag(self):
 
76
        """ Loads data into the preferences Dialog. """
 
77
        
 
78
        self.wiredcheckbox.set_active(daemon.GetAlwaysShowWiredInterface())
 
79
        self.reconnectcheckbox.set_active(daemon.GetAutoReconnect())
 
80
        self.debugmodecheckbox.set_active(daemon.GetDebugMode())
 
81
        self.displaytypecheckbox.set_active(daemon.GetSignalDisplayType())
 
82
        self.preferwiredcheckbox.set_active(daemon.GetPreferWiredNetwork())
 
83
        
 
84
        dhcp_list = [self.dhcpautoradio, self.dhclientradio, self.dhcpcdradio, 
 
85
                     self.pumpradio, self.udhcpcradio]
 
86
        self._setup_external_app_radios(dhcp_list, daemon.GetDHCPClient,
 
87
                                        daemon.SetDHCPClient)
 
88
        
 
89
        wired_link_list = [self.linkautoradio, self.ethtoolradio,
 
90
                           self.miitoolradio]
 
91
        self._setup_external_app_radios(wired_link_list,
 
92
                                        daemon.GetLinkDetectionTool,
 
93
                                        daemon.SetLinkDetectionTool)
 
94
 
 
95
        flush_list = [self.flushautoradio, self.ipflushradio,
 
96
                      self.routeflushradio]
 
97
        self._setup_external_app_radios(flush_list, daemon.GetFlushTool,
 
98
                                        daemon.SetFlushTool)
 
99
        
 
100
        sudo_list = [self.sudoautoradio, self.gksudoradio, self.kdesuradio,
 
101
                     self.ktsussradio]
 
102
        self._setup_external_app_radios(sudo_list, daemon.GetSudoApp,
 
103
                                        daemon.SetAudoApp)
 
104
        
 
105
        auto_conn_meth = daemon.GetWiredAutoConnectMethod()
 
106
        if auto_conn_meth == 1:
 
107
            self.usedefaultradiobutton.set_active(True)
 
108
        elif auto_conn_meth == 2:
 
109
            self.showlistradiobutton.set_active(True)
 
110
        elif auto_conn_meth == 3:
 
111
            self.lastusedradiobutton.set_active(True)
 
112
        
 
113
        self.entryWirelessInterface.set_text(daemon.GetWirelessInterface())
 
114
        self.entryWiredInterface.set_text(daemon.GetWiredInterface())
 
115
 
 
116
        def_driver = daemon.GetWPADriver()
 
117
        try:
 
118
            self.wpadrivercombo.set_active(self.wpadrivers.index(def_driver))
 
119
        except ValueError:
 
120
            self.wpadrivercombo.set_active(0)
 
121
 
 
122
        self.useGlobalDNSCheckbox.connect("toggled", checkboxTextboxToggle,
 
123
                                          (self.dns1Entry, self.dns2Entry,
 
124
                                           self.dns3Entry, self.dnsDomEntry, 
 
125
                                           self.searchDomEntry))
 
126
 
 
127
        dns_addresses = daemon.GetGlobalDNSAddresses()
 
128
        self.useGlobalDNSCheckbox.set_active(daemon.GetUseGlobalDNS())
 
129
        self.dns1Entry.set_text(noneToBlankString(dns_addresses[0]))
 
130
        self.dns2Entry.set_text(noneToBlankString(dns_addresses[1]))
 
131
        self.dns3Entry.set_text(noneToBlankString(dns_addresses[2]))
 
132
        self.dnsDomEntry.set_text(noneToBlankString(dns_addresses[3]))
 
133
        self.searchDomEntry.set_text(noneToBlankString(dns_addresses[4]))
 
134
 
 
135
        if not daemon.GetUseGlobalDNS():
 
136
            self.searchDomEntry.set_sensitive(False)
 
137
            self.dnsDomEntry.set_sensitive(False)
 
138
            self.dns1Entry.set_sensitive(False)
 
139
            self.dns2Entry.set_sensitive(False)
 
140
            self.dns3Entry.set_sensitive(False)
 
141
            
 
142
        cur_backend = daemon.GetSavedBackend()
 
143
        try:
 
144
            self.backendcombo.set_active(self.backends.index(cur_backend))
 
145
        except ValueError:
 
146
            self.backendcombo.set_active(0)
 
147
 
 
148
        self.notificationscheckbox.set_active(
 
149
                os.path.exists(
 
150
                    os.path.join(USER_SETTINGS_DIR, 'USE_NOTIFICATIONS')
 
151
                ))
 
152
 
 
153
        # if pynotify isn't installed disable the option
 
154
        try:
 
155
            import pynotify
 
156
        except ImportError:
 
157
            self.notificationscheckbox.set_active(False)
 
158
            self.notificationscheckbox.set_sensitive(False)
 
159
 
 
160
        # if notifications were disabled with the configure flag
 
161
        if wpath.no_use_notifications:
 
162
            self.notificationscheckbox.set_active(False)
 
163
            self.notificationscheckbox.hide()
 
164
            self.wTree.get_widget('label2').hide()
 
165
        
 
166
        self.wTree.get_widget("notebook2").set_current_page(0)
 
167
        
 
168
    def run(self):
 
169
        """ Runs the preferences dialog window. """
 
170
        return self.dialog.run()
 
171
    
 
172
    def hide(self):
 
173
        """ Hides the preferences dialog window. """
 
174
        self.dialog.hide()
 
175
        
 
176
    def destroy(self):
 
177
        self.dialog.destroy()
 
178
        
 
179
    def show_all(self):
 
180
        """ Shows the preferences dialog window. """
 
181
        self.dialog.show()
 
182
    
 
183
    def save_results(self):
 
184
        """ Pushes the selected settings to the daemon. """
 
185
        daemon.SetUseGlobalDNS(self.useGlobalDNSCheckbox.get_active())
 
186
        daemon.SetGlobalDNS(self.dns1Entry.get_text(), self.dns2Entry.get_text(),
 
187
                            self.dns3Entry.get_text(), self.dnsDomEntry.get_text(),
 
188
                            self.searchDomEntry.get_text())
 
189
        daemon.SetWirelessInterface(self.entryWirelessInterface.get_text())
 
190
        daemon.SetWiredInterface(self.entryWiredInterface.get_text())
 
191
        daemon.SetWPADriver(self.wpadrivers[self.wpadrivercombo.get_active()])
 
192
        daemon.SetAlwaysShowWiredInterface(self.wiredcheckbox.get_active())
 
193
        daemon.SetAutoReconnect(self.reconnectcheckbox.get_active())
 
194
        daemon.SetDebugMode(self.debugmodecheckbox.get_active())
 
195
        daemon.SetSignalDisplayType(int(self.displaytypecheckbox.get_active()))
 
196
        daemon.SetPreferWiredNetwork(bool(self.preferwiredcheckbox.get_active()))
 
197
        if self.showlistradiobutton.get_active():
 
198
            daemon.SetWiredAutoConnectMethod(2)
 
199
        elif self.lastusedradiobutton.get_active():
 
200
            daemon.SetWiredAutoConnectMethod(3)
 
201
        else:
 
202
            daemon.SetWiredAutoConnectMethod(1)
 
203
 
 
204
        daemon.SetBackend(self.backends[self.backendcombo.get_active()])
 
205
            
 
206
        # External Programs Tab
 
207
        if self.dhcpautoradio.get_active():
 
208
            dhcp_client = misc.AUTO
 
209
        elif self.dhclientradio.get_active():
 
210
            dhcp_client = misc.DHCLIENT
 
211
        elif self.dhcpcdradio.get_active():
 
212
            dhcp_client = misc.DHCPCD
 
213
        elif self.pumpradio.get_active():
 
214
            dhcp_client = misc.PUMP
 
215
        else:
 
216
            dhcp_client = misc.UDHCPC
 
217
        daemon.SetDHCPClient(dhcp_client)
 
218
        
 
219
        if self.linkautoradio.get_active():
 
220
            link_tool = misc.AUTO
 
221
        elif self.ethtoolradio.get_active():
 
222
            link_tool = misc.ETHTOOL
 
223
        else:
 
224
            link_tool = misc.MIITOOL
 
225
        daemon.SetLinkDetectionTool(link_tool)
 
226
        
 
227
        if self.flushautoradio.get_active():
 
228
            flush_tool = misc.AUTO
 
229
        elif self.ipflushradio.get_active():
 
230
            flush_tool = misc.IP
 
231
        else:
 
232
            flush_tool = misc.ROUTE
 
233
        daemon.SetFlushTool(flush_tool)
 
234
        
 
235
        if self.sudoautoradio.get_active():
 
236
            sudo_tool = misc.AUTO
 
237
        elif self.gksudoradio.get_active():
 
238
            sudo_tool = misc.GKSUDO
 
239
        elif self.kdesuradio.get_active():
 
240
            sudo_tool = misc.KDESU
 
241
        else:
 
242
            sudo_tool = misc.KTSUSS
 
243
        daemon.SetSudoApp(sudo_tool)
 
244
 
 
245
        [width, height] = self.dialog.get_size()
 
246
        daemon.WriteWindowSize(width, height, "pref")
 
247
        
 
248
        not_path = os.path.join(USER_SETTINGS_DIR, 'USE_NOTIFICATIONS')
 
249
        if self.notificationscheckbox.get_active():
 
250
            if not os.path.exists(not_path):
 
251
                open(not_path, 'w')
 
252
        else:
 
253
            if os.path.exists(not_path):
 
254
                os.remove(not_path)
 
255
        # if this GUI was started by a tray icon,
 
256
        # instantly change the notifications there
 
257
        if self.parent.tray:
 
258
            self.parent.tray.icon_info.use_notify = \
 
259
                                self.notificationscheckbox.get_active()
 
260
 
 
261
    def set_label(self, glade_str, label):
 
262
        """ Sets the label for the given widget in wicd.glade. """
 
263
        self.wTree.get_widget(glade_str).set_label(label)
 
264
        
 
265
    def prep_settings_diag(self):
 
266
        """ Set up anything that doesn't have to be persisted later. """
 
267
        def build_combobox(lbl):
 
268
            """ Sets up a ComboBox using the given widget name. """
 
269
            liststore = gtk.ListStore(gobject.TYPE_STRING)
 
270
            combobox = self.wTree.get_widget(lbl)
 
271
            combobox.clear()
 
272
            combobox.set_model(liststore)
 
273
            cell = gtk.CellRendererText()
 
274
            combobox.pack_start(cell, True)
 
275
            combobox.add_attribute(cell, 'text', 0)
 
276
            return combobox
 
277
        
 
278
        def setup_label(name, lbl=""):
 
279
            """ Sets up a label for the given widget name. """
 
280
            widget = self.wTree.get_widget(name)
 
281
            # if lbl:
 
282
            #     widget.set_label(language[lbl])
 
283
            if widget is None:
 
284
                raise ValueError('widget %s does not exist' % name)
 
285
            return widget
 
286
        
 
287
        # External Programs tab
 
288
        # self.wTree.get_widget("gen_settings_label").set_label(language["gen_settings"])
 
289
        # self.wTree.get_widget("ext_prog_label").set_label(language["ext_programs"])
 
290
        # self.wTree.get_widget("dhcp_client_label").set_label(language["dhcp_client"])
 
291
        # self.wTree.get_widget("wired_detect_label").set_label(language["wired_detect"])
 
292
        # self.wTree.get_widget("route_flush_label").set_label(language["route_flush"])
 
293
        # self.wTree.get_widget("pref_backend_label").set_label(language["backend"] + ":")
 
294
        
 
295
        # entryWiredAutoMethod = self.wTree.get_widget("pref_wired_auto_label")
 
296
        # entryWiredAutoMethod.set_label('Wired Autoconnect Setting:')
 
297
        # entryWiredAutoMethod.set_alignment(0, 0)
 
298
        # atrlist = pango.AttrList()
 
299
        # atrlist.insert(pango.AttrWeight(pango.WEIGHT_BOLD, 0, 50))
 
300
        # entryWiredAutoMethod.set_attributes(atrlist)
 
301
        
 
302
        # self.set_label("pref_dns1_label", "%s %s" % (language['dns'], language['1']))
 
303
        # self.set_label("pref_dns2_label", "%s %s" % (language['dns'], language['2']))
 
304
        # self.set_label("pref_dns3_label", "%s %s" % (language['dns'], language['3']))
 
305
        # self.set_label("pref_search_dom_label", "%s:" % language['search_domain'])
 
306
        # self.set_label("pref_wifi_label", "%s:" % language['wireless_interface'])
 
307
        # self.set_label("pref_wired_label", "%s:" % language['wired_interface'])
 
308
        # self.set_label("pref_driver_label", "%s:" % language['wpa_supplicant_driver'])
 
309
        
 
310
        self.dialog = self.wTree.get_widget("pref_dialog")
 
311
        self.dialog.set_title(language['preferences'])
 
312
        if os.path.exists(os.path.join(wpath.images, "wicd.png")):
 
313
            self.dialog.set_icon_from_file(os.path.join(wpath.images, "wicd.png"))
 
314
        size = daemon.ReadWindowSize("pref")
 
315
        width = size[0]
 
316
        height = size[1]
 
317
        if width > -1 and height > -1:
 
318
            self.dialog.resize(int(width), int(height))
 
319
        else:
 
320
            width = int(gtk.gdk.screen_width() / 2.4)
 
321
            if width > 450:
 
322
                width = 450
 
323
            self.dialog.resize(width, int(gtk.gdk.screen_height() / 2))
 
324
            
 
325
        self.wiredcheckbox = setup_label("pref_always_check",
 
326
                                         'wired_always_on')
 
327
        self.preferwiredcheckbox = setup_label("pref_prefer_wired_check",
 
328
                                               "prefer_wired")
 
329
 
 
330
        self.reconnectcheckbox = setup_label("pref_auto_check",
 
331
                                             'auto_reconnect')
 
332
        self.debugmodecheckbox = setup_label("pref_debug_check",
 
333
                                             'use_debug_mode')
 
334
        self.displaytypecheckbox = setup_label("pref_dbm_check",
 
335
                                               'display_type_dialog')
 
336
        self.usedefaultradiobutton = setup_label("pref_use_def_radio",
 
337
                                                 'use_default_profile')
 
338
        self.showlistradiobutton = setup_label("pref_prompt_radio",
 
339
                                               'show_wired_list')
 
340
        self.lastusedradiobutton = setup_label("pref_use_last_radio",
 
341
                                               'use_last_used_profile')
 
342
 
 
343
            
 
344
        self.notificationscheckbox = setup_label("pref_use_libnotify",
 
345
                                             'display_notifications')
 
346
 
 
347
        # DHCP Clients
 
348
        self.dhcpautoradio = setup_label("dhcp_auto_radio", "wicd_auto_config")
 
349
        self.dhclientradio = self.wTree.get_widget("dhclient_radio")
 
350
        self.pumpradio = self.wTree.get_widget("pump_radio")
 
351
        self.dhcpcdradio = self.wTree.get_widget("dhcpcd_radio")
 
352
        self.udhcpcradio = self.wTree.get_widget("udhcpc_radio")
 
353
        
 
354
        # Wired Link Detection Apps
 
355
        self.linkautoradio = setup_label("link_auto_radio", 'wicd_auto_config')
 
356
        self.linkautoradio = setup_label("link_auto_radio")
 
357
        self.ethtoolradio = setup_label("ethtool_radio")
 
358
        self.miitoolradio = setup_label("miitool_radio")
 
359
        
 
360
        # Route Flushing Apps
 
361
        self.flushautoradio = setup_label("flush_auto_radio",
 
362
                                          'wicd_auto_config')
 
363
        self.ipflushradio = setup_label("ip_flush_radio")
 
364
        self.routeflushradio = setup_label("route_flush_radio")
 
365
        
 
366
        # Graphical Sudo Apps
 
367
        self.sudoautoradio = setup_label("sudo_auto_radio", "wicd_auto_config")
 
368
        self.gksudoradio = setup_label("gksudo_radio")
 
369
        self.kdesuradio = setup_label("kdesu_radio")
 
370
        self.ktsussradio = setup_label("ktsuss_radio")
 
371
 
 
372
        # Replacement for the combo box hack
 
373
        self.wpadrivercombo = build_combobox("pref_wpa_combobox")
 
374
        self.wpadrivers = ["wext", "hostap", "madwifi", "atmel",
 
375
                           "ndiswrapper", "ipw"]
 
376
        self.wpadrivers = wireless.GetWpaSupplicantDrivers(self.wpadrivers)
 
377
        self.wpadrivers.append("ralink_legacy")
 
378
        
 
379
        for x in self.wpadrivers:
 
380
            self.wpadrivercombo.append_text(x)
 
381
 
 
382
        self.entryWirelessInterface = self.wTree.get_widget("pref_wifi_entry")
 
383
        self.entryWiredInterface = self.wTree.get_widget("pref_wired_entry")
 
384
        
 
385
        # Set up global DNS stuff
 
386
        self.useGlobalDNSCheckbox = setup_label("pref_global_check",
 
387
                                                'use_global_dns')
 
388
        self.searchDomEntry = self.wTree.get_widget("pref_search_dom_entry")
 
389
        self.dnsDomEntry = self.wTree.get_widget("pref_dns_dom_entry")
 
390
        self.dns1Entry = self.wTree.get_widget("pref_dns1_entry")
 
391
        self.dns2Entry = self.wTree.get_widget("pref_dns2_entry")
 
392
        self.dns3Entry = self.wTree.get_widget("pref_dns3_entry")
 
393
        
 
394
        self.backendcombo = build_combobox("pref_backend_combobox")
 
395
        self.backendcombo.connect("changed", self.be_combo_changed)
 
396
        # Load backend combobox
 
397
        self.backends = daemon.GetBackendList()
 
398
        self.be_descriptions = daemon.GetBackendDescriptionDict()
 
399
        
 
400
        for x in self.backends:
 
401
            if x:
 
402
                self.backendcombo.append_text(x)
 
403
            
 
404
    def be_combo_changed(self, combo):
 
405
        """ Update the description label for the given backend. """
 
406
        self.backendcombo.set_tooltip_text(
 
407
            self.be_descriptions[combo.get_active_text()]
 
408
        )