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

« back to all changes in this revision

Viewing changes to curses/prefs_curses.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/env python
 
2
 
 
3
"""prefs_curses.py -- Pretty, tabbable, console preferences dialog"""
 
4
 
 
5
#       Copyright (C) 2008-2009 Andrew Psaltis
 
6
 
 
7
#       This program is free software; you can redistribute it and/or modify
 
8
#       it under the terms of the GNU General Public License as published by
 
9
#       the Free Software Foundation; either version 2 of the License, or
 
10
#       (at your option) any later version.
 
11
#       
 
12
#       This program is distributed in the hope that it will be useful,
 
13
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#       GNU General Public License for more details.
 
16
#       
 
17
#       You should have received a copy of the GNU General Public License
 
18
#       along with this program; if not, write to the Free Software
 
19
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
20
#       MA 02110-1301, USA.
 
21
 
 
22
import urwid
 
23
import urwid.curses_display
 
24
 
 
25
from wicd import misc
 
26
from wicd import dbusmanager
 
27
from curses_misc import SelText,DynWrap,DynRadioButton,ComboBox,TabColumns
 
28
 
 
29
daemon = None
 
30
wireless = None
 
31
wired = None
 
32
 
 
33
from wicd.translations import language
 
34
 
 
35
class PrefsDialog(urwid.WidgetWrap):
 
36
    def __init__(self,body,pos,ui,dbus=None):
 
37
        global daemon, wireless, wired
 
38
 
 
39
        daemon = dbus['daemon']
 
40
        wireless = dbus['wireless']
 
41
        wired = dbus['wired']
 
42
 
 
43
        width,height =  ui.get_cols_rows()
 
44
        height -= 3
 
45
        #width = 80
 
46
        #height = 20
 
47
        # Stuff that goes at the top
 
48
 
 
49
        header0_t = language["gen_settings"]
 
50
        header1_t = language["ext_programs"]
 
51
        header2_t = language["advanced_settings"]
 
52
        self.header0 = urwid.AttrWrap(SelText(header0_t),'tab active','focus')
 
53
        self.header1 = urwid.AttrWrap(SelText(header1_t),'body','focus')
 
54
        self.header2 = urwid.AttrWrap(SelText(header2_t),'body','focus')
 
55
        title   = language['preferences']
 
56
 
 
57
        # Blank line
 
58
        _blank = urwid.Text('')
 
59
 
 
60
        ####
 
61
        #### Text in the widgets
 
62
        ####
 
63
 
 
64
        # General Settings
 
65
        net_cat_t           = ('header',language['network_interfaces'])
 
66
        wired_t             = ('editcp',language['wired_interface']+':   ')
 
67
        wless_t             = ('editcp',language['wireless_interface']+':')
 
68
        always_show_wired_t = language['wired_always_on']
 
69
        prefer_wired_t      = language['always_switch_to_wired']
 
70
 
 
71
        global_dns_cat_t = ('header',language['global_dns_servers'])
 
72
        global_dns_t     = ('editcp',language['use_global_dns'])
 
73
        dns_dom_t        = ('editcp','    '+language['dns_domain']+':   ')
 
74
        search_dom_t     = ('editcp','    '+language['search_domain']+':')
 
75
        dns1_t           = ('editcp','    DNS server 1: ')
 
76
        dns2_t           = ('editcp','    DNS server 2: ')
 
77
        dns3_t           = ('editcp','    DNS server 3: ')
 
78
 
 
79
 
 
80
        wired_auto_cat_t= ('header',language['wired_autoconnect_settings'])
 
81
        wired_auto_1_t = language['use_default_profile']
 
82
        wired_auto_2_t = language['show_wired_list']
 
83
        wired_auto_3_t = language['use_last_used_profile']
 
84
 
 
85
        auto_reconn_cat_t = ('header',language['automatic_reconnection'])
 
86
        auto_reconn_t = language['auto_reconnect']
 
87
 
 
88
        #### External Programs
 
89
        automatic_t = language['wicd_auto_config']
 
90
 
 
91
        dhcp_header_t = ('header',language["dhcp_client"])
 
92
        # Automatic
 
93
        dhcp1_t  = 'dhclient'
 
94
        dhcp2_t  = 'dhcpcd'
 
95
        dhcp3_t  = 'pump'
 
96
        dhcp4_t  = 'udhcpc'
 
97
 
 
98
        wired_detect_header_t = ('header',language["wired_detect"])
 
99
        wired1_t              = 'ethtool'
 
100
        wired2_t              = 'mii-tool'
 
101
 
 
102
        flush_header_t = ('header',language["route_flush"])
 
103
        flush1_t           = 'ip'
 
104
        flush2_t           = 'route'
 
105
 
 
106
        #### Advanced Settings
 
107
        #wpa_t=('editcp',language['wpa_supplicant_driver']+':')
 
108
        wpa_cat_t=('header',language['wpa_supplicant'])
 
109
        wpa_t=('editcp','Driver:')
 
110
        wpa_list = ['spam','double spam','triple spam','quadruple spam']
 
111
        wpa_warn_t = ('important',language['always_use_wext'])
 
112
        
 
113
        backend_cat_t = ('header',language['backend'])
 
114
        backend_t = language['backend']+':'
 
115
        backend_list = ['spam','double spam','triple spam','quadruple spam']
 
116
        #backend_warn_t = ('important',
 
117
        #   'Changes to the backend (probably) requires a daemon restart')
 
118
        
 
119
        debug_cat_t = ('header',language['debugging'])
 
120
        debug_mode_t = language['use_debug_mode']
 
121
 
 
122
        wless_cat_t = ('header',language['wireless_interface'])
 
123
        use_dbm_t = language['display_type_dialog']
 
124
        
 
125
 
 
126
 
 
127
        ####
 
128
        #### UI Widgets
 
129
        ####
 
130
 
 
131
        # General Settings
 
132
        self.net_cat     = urwid.Text(net_cat_t)
 
133
        self.wired_edit = urwid.AttrWrap(urwid.Edit(wired_t),'editbx','editfc')
 
134
        self.wless_edit = urwid.AttrWrap(urwid.Edit(wless_t),'editbx','editfc')
 
135
        self.prefer_wired_chkbx = urwid.CheckBox(prefer_wired_t)
 
136
        self.global_dns_cat = urwid.Text(global_dns_cat_t)
 
137
        # Default the global DNS settings to off.  They will be reenabled later
 
138
        # if so required.
 
139
        global_dns_state = False
 
140
        self.global_dns_checkb  = urwid.CheckBox(global_dns_t,global_dns_state,
 
141
                on_state_change=self.global_dns_trigger)
 
142
        self.search_dom = DynWrap(urwid.Edit(search_dom_t),global_dns_state)
 
143
        self.dns_dom    = DynWrap(urwid.Edit(dns_dom_t),global_dns_state)
 
144
        self.dns1       = DynWrap(urwid.Edit(dns1_t),global_dns_state)
 
145
        self.dns2       = DynWrap(urwid.Edit(dns2_t),global_dns_state)
 
146
        self.dns3       = DynWrap(urwid.Edit(dns3_t),global_dns_state)
 
147
 
 
148
 
 
149
        self.always_show_wired_checkb = urwid.CheckBox(always_show_wired_t)
 
150
 
 
151
        self.wired_auto_l  = []
 
152
        self.wired_auto_cat= urwid.Text(wired_auto_cat_t)
 
153
        self.wired_auto_1  = urwid.RadioButton(self.wired_auto_l,wired_auto_1_t)
 
154
        self.wired_auto_2  = urwid.RadioButton(self.wired_auto_l,wired_auto_2_t)
 
155
        self.wired_auto_3  = urwid.RadioButton(self.wired_auto_l,wired_auto_3_t)
 
156
 
 
157
        self.auto_reconn_cat    = urwid.Text(auto_reconn_cat_t)
 
158
        self.auto_reconn_checkb = urwid.CheckBox(auto_reconn_t)
 
159
        generalLB = urwid.ListBox([self.net_cat,
 
160
                                   self.wless_edit,#_blank,
 
161
                                   self.wired_edit,
 
162
                                   self.always_show_wired_checkb,
 
163
                                   self.prefer_wired_chkbx,_blank,
 
164
                                   self.global_dns_cat,
 
165
                                   self.global_dns_checkb,#_blank,
 
166
                                   self.search_dom,self.dns_dom,
 
167
                                   self.dns1,self.dns2,self.dns3,_blank,
 
168
                                   self.wired_auto_cat,
 
169
                                   self.wired_auto_1,
 
170
                                   self.wired_auto_2,
 
171
                                   self.wired_auto_3, _blank,
 
172
                                   self.auto_reconn_cat,
 
173
                                   self.auto_reconn_checkb
 
174
                                  ])
 
175
 
 
176
        #### External Programs tab
 
177
        automatic_t = language['wicd_auto_config']
 
178
 
 
179
        self.dhcp_header = urwid.Text(dhcp_header_t)
 
180
        self.dhcp_l = []
 
181
 
 
182
        # Order of these is flipped in the actual interface,
 
183
        # (2,3,1 -> dhcpcd, pump, dhclient), because dhclient often doesn't like
 
184
        # to work on several distros.
 
185
        self.dhcp0  = urwid.RadioButton(self.dhcp_l,automatic_t)
 
186
        self.dhcp1  = DynRadioButton(self.dhcp_l,dhcp1_t)
 
187
        self.dhcp2  = DynRadioButton(self.dhcp_l,dhcp2_t)
 
188
        self.dhcp3  = DynRadioButton(self.dhcp_l,dhcp3_t)
 
189
        self.dhcp4  = DynRadioButton(self.dhcp_l,dhcp4_t)
 
190
        self.dhcp_l = [self.dhcp0,self.dhcp1,self.dhcp2,self.dhcp3,self.dhcp4]
 
191
 
 
192
        self.wired_l = []
 
193
        self.wired_detect_header = urwid.Text(wired_detect_header_t)
 
194
        self.wired0         = urwid.RadioButton(self.wired_l,automatic_t)
 
195
        self.wired1         = DynRadioButton(self.wired_l,wired1_t)
 
196
        self.wired2         = DynRadioButton(self.wired_l,wired2_t)
 
197
        self.wired_l = [self.wired0,self.wired1,self.wired2]
 
198
 
 
199
        self.flush_l = []
 
200
        self.flush_header   = urwid.Text(flush_header_t)
 
201
        self.flush0         = urwid.RadioButton(self.flush_l,automatic_t)
 
202
        self.flush1         = DynRadioButton(self.flush_l,flush1_t)
 
203
        self.flush2         = DynRadioButton(self.flush_l,flush2_t)
 
204
        self.flush_l = [self.flush0,self.flush1,self.flush2]
 
205
 
 
206
        externalLB = urwid.ListBox([self.dhcp_header,
 
207
                                    self.dhcp0,self.dhcp2,self.dhcp3,self.dhcp1,self.dhcp4,
 
208
                                    _blank,
 
209
                                    self.wired_detect_header,
 
210
                                    self.wired0,self.wired1,self.wired2,
 
211
                                    _blank,
 
212
                                    self.flush_header,
 
213
                                    self.flush0,self.flush1,self.flush2
 
214
                                   ])
 
215
 
 
216
 
 
217
        #### Advanced settings
 
218
        self.wpa_cat      = urwid.Text(wpa_cat_t)
 
219
        self.wpa_cbox     = ComboBox(wpa_t)
 
220
        self.wpa_warn     = urwid.Text(wpa_warn_t)
 
221
        
 
222
        self.backend_cat  = urwid.Text(backend_cat_t)
 
223
        self.backend_cbox = ComboBox(backend_t)
 
224
        
 
225
        self.debug_cat           = urwid.Text(debug_cat_t)
 
226
        self.debug_mode_checkb   = urwid.CheckBox(debug_mode_t)
 
227
 
 
228
        self.wless_cat      = urwid.Text(wless_cat_t)
 
229
        self.use_dbm_checkb = urwid.CheckBox(use_dbm_t)
 
230
 
 
231
 
 
232
        advancedLB = urwid.ListBox([self.wpa_cat,
 
233
                                    self.wpa_cbox,self.wpa_warn,_blank,
 
234
                                    self.backend_cat,
 
235
                                    self.backend_cbox,_blank,
 
236
                                    self.debug_cat,
 
237
                                    self.debug_mode_checkb, _blank,
 
238
                                    self.wless_cat,
 
239
                                    self.use_dbm_checkb, _blank
 
240
                                    ])
 
241
 
 
242
 
 
243
        headerList = [self.header0,self.header1,self.header2]
 
244
        lbList = [generalLB,externalLB,advancedLB]
 
245
        self.tab_map = {self.header0 : generalLB,
 
246
                        self.header1 : externalLB,
 
247
                        self.header2 : advancedLB}
 
248
        #self.load_settings()
 
249
 
 
250
        self.tabs = TabColumns(headerList,lbList,language['preferences'])
 
251
        self.__super.__init__(self.tabs)
 
252
        
 
253
    def load_settings(self):
 
254
 
 
255
        ### General Settings
 
256
        # ComboBox does not like dbus.Strings as text markups.  My fault. :/
 
257
        wless_iface = unicode(daemon.GetWirelessInterface())
 
258
        wired_iface = unicode(daemon.GetWiredInterface())
 
259
        self.wless_edit.set_edit_text(wless_iface)
 
260
        self.wired_edit.set_edit_text(wired_iface)
 
261
 
 
262
        self.always_show_wired_checkb.set_state(
 
263
                daemon.GetAlwaysShowWiredInterface())
 
264
        self.prefer_wired_chkbx.set_state(daemon.GetPreferWiredNetwork())
 
265
        # DNS
 
266
        self.global_dns_checkb.set_state(daemon.GetUseGlobalDNS())
 
267
        theDNS = daemon.GetGlobalDNSAddresses()
 
268
 
 
269
        i = 0
 
270
        for w in self.dns1,self.dns2,self.dns3,self.dns_dom,self.search_dom :
 
271
            w.set_edit_text(misc.noneToBlankString(theDNS[i]))
 
272
            i+=1
 
273
 
 
274
        # Wired Automatic Connection
 
275
        self.wired_auto_l[daemon.GetWiredAutoConnectMethod()-1]
 
276
        self.auto_reconn_checkb.set_state(daemon.GetAutoReconnect())
 
277
 
 
278
        def find_avail(apps):
 
279
            for app in apps[1:]:
 
280
                app.set_sensitive(daemon.GetAppAvailable(app.get_label()))
 
281
 
 
282
        ### External Programs
 
283
        find_avail(self.dhcp_l)
 
284
        dhcp_method = daemon.GetDHCPClient()
 
285
        self.dhcp_l[dhcp_method].set_state(True)
 
286
        
 
287
        find_avail(self.wired_l)
 
288
        wired_link_method = daemon.GetLinkDetectionTool()
 
289
        self.wired_l[wired_link_method].set_state(True)
 
290
 
 
291
        find_avail(self.flush_l)
 
292
        flush_method = daemon.GetFlushTool()
 
293
        self.flush_l[flush_method].set_state(True)
 
294
 
 
295
        ### Advanced settings
 
296
        # wpa_supplicant janx
 
297
        self.wpadrivers = ["wext", "hostap", "madwifi", "atmel",
 
298
                           "ndiswrapper", "ipw"]
 
299
        self.wpadrivers = wireless.GetWpaSupplicantDrivers(self.wpadrivers)
 
300
        self.wpadrivers.append("ralink_legacy")
 
301
        # Same as above with the dbus.String
 
302
        self.thedrivers = [unicode(w) for w in self.wpadrivers]
 
303
        self.wpa_cbox.set_list(self.thedrivers)
 
304
        
 
305
        # Pick where to begin first:
 
306
        def_driver = daemon.GetWPADriver()
 
307
        try:
 
308
            self.wpa_cbox.set_focus(self.wpadrivers.index(def_driver))
 
309
        except ValueError:
 
310
            pass # It defaults to 0 anyway (I hope)
 
311
 
 
312
        self.backends = daemon.GetBackendList()
 
313
        self.thebackends= [unicode(w) for w in self.backends]
 
314
        self.backend_cbox.set_list(self.thebackends) 
 
315
        cur_backend = daemon.GetSavedBackend()
 
316
        try:
 
317
            self.backend_cbox.set_focus(self.thebackends.index(cur_backend))
 
318
        except ValueError:
 
319
            self.backend_cbox.set_focus(0)
 
320
 
 
321
        # Two last checkboxes
 
322
        self.debug_mode_checkb.set_state(daemon.GetDebugMode())
 
323
        self.use_dbm_checkb.set_state(daemon.GetSignalDisplayType())
 
324
 
 
325
    def save_settings(self):
 
326
        """ Pushes the selected settings to the daemon.
 
327
            This exact order is found in prefs.py"""
 
328
        daemon.SetUseGlobalDNS(self.global_dns_checkb.get_state())
 
329
        daemon.SetGlobalDNS(self.dns1.get_edit_text(), self.dns2.get_edit_text(),
 
330
                            self.dns3.get_edit_text(), self.dns_dom.get_edit_text(),
 
331
                            self.search_dom.get_edit_text())
 
332
        daemon.SetWirelessInterface(self.wless_edit.get_edit_text())
 
333
        daemon.SetWiredInterface(self.wired_edit.get_edit_text())
 
334
        daemon.SetWPADriver(self.wpadrivers[self.wpa_cbox.get_focus()[1]])
 
335
        daemon.SetAlwaysShowWiredInterface(self.always_show_wired_checkb.get_state())
 
336
        daemon.SetAutoReconnect(self.auto_reconn_checkb.get_state())
 
337
        daemon.SetDebugMode(self.debug_mode_checkb.get_state())
 
338
        daemon.SetSignalDisplayType(int(self.use_dbm_checkb.get_state()))
 
339
        daemon.SetPreferWiredNetwork(bool(self.prefer_wired_chkbx.get_state()))
 
340
        if self.wired_auto_2.get_state():
 
341
            daemon.SetWiredAutoConnectMethod(2)
 
342
        elif self.wired_auto_3.get_state():
 
343
            daemon.SetWiredAutoConnectMethod(3)
 
344
        else:
 
345
            daemon.SetWiredAutoConnectMethod(1)
 
346
 
 
347
        daemon.SetBackend(self.backends[self.backend_cbox.get_focus()[1]])
 
348
            
 
349
        # External Programs Tab
 
350
        if self.dhcp0.get_state():
 
351
            dhcp_client = misc.AUTO
 
352
        elif self.dhcp1.get_state():
 
353
            dhcp_client = misc.DHCLIENT
 
354
        elif self.dhcp2.get_state():
 
355
            dhcp_client = misc.DHCPCD
 
356
        elif self.dhcp3.get_state():
 
357
            dhcp_client = misc.PUMP
 
358
        else:
 
359
            dhcp_client = misc.UDHCPC
 
360
        daemon.SetDHCPClient(dhcp_client)
 
361
        
 
362
        if self.wired0.get_state():
 
363
            link_tool = misc.AUTO
 
364
        elif self.wired1.get_state():
 
365
            link_tool = misc.ETHTOOL
 
366
        else:
 
367
            link_tool = misc.MIITOOL
 
368
        daemon.SetLinkDetectionTool(link_tool)
 
369
        
 
370
        if self.flush0.get_state():
 
371
            flush_tool = misc.AUTO
 
372
        elif self.flush1.get_state():
 
373
            flush_tool = misc.IP
 
374
        else:
 
375
            flush_tool = misc.ROUTE
 
376
        daemon.SetFlushTool(flush_tool)
 
377
 
 
378
    # DNS CheckBox callback
 
379
    def global_dns_trigger(self,check_box,new_state,user_data=None):
 
380
        for w in self.dns1,self.dns2,self.dns3,self.dns_dom,self.search_dom:
 
381
            w.set_sensitive(new_state)
 
382
 
 
383
    def ready_widgets(self,ui,body):
 
384
        self.wpa_cbox.build_combobox(body,ui,4)
 
385
        self.backend_cbox.build_combobox(body,ui,8)