~elementary-os/elementaryos/os-patch-ubiquity-xenial

« back to all changes in this revision

Viewing changes to ubiquity/frontend/gtk_components/nmwidgets.py

  • Committer: RabbitBot
  • Date: 2017-11-16 04:33:43 UTC
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: rabbitbot@elementary.io-20171116043343-px1hdee3iabw6qfk
updated to version 2.21.63.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import string
2
 
 
3
1
from dbus.mainloop.glib import DBusGMainLoop
4
2
DBusGMainLoop(set_as_default=True)
5
3
 
6
4
from gi.repository import Gtk, GObject, GLib
 
5
from gi.repository import NM, NMA
7
6
 
8
 
from ubiquity.nm import QueuedCaller, NetworkStore, NetworkManager
 
7
from ubiquity.nm import decode_ssid, QueuedCaller, NetworkStore, NetworkManager
9
8
 
10
9
 
11
10
class GLibQueuedCaller(QueuedCaller):
92
91
class NetworkManagerTreeView(Gtk.TreeView):
93
92
    __gtype_name__ = 'NetworkManagerTreeView'
94
93
 
95
 
    def __init__(self, password_entry=None, state_changed=None):
 
94
    def __init__(self, state_changed=None):
96
95
        Gtk.TreeView.__init__(self)
97
 
        self.password_entry = password_entry
98
96
        self.configure_icons()
99
97
        model = GtkNetworkStore()
100
98
        model.set_sort_column_id(0, Gtk.SortType.ASCENDING)
102
100
        self.wifi_model = NetworkManager(model,
103
101
                                         GLibQueuedCaller,
104
102
                                         state_changed)
 
103
 
 
104
        self.nm_client = None
 
105
        self.nm_connection = None
 
106
 
105
107
        self.set_model(model)
106
108
 
107
109
        ssid_column = Gtk.TreeViewColumn('')
157
159
                self.expand_row(path, False)
158
160
            i = model.iter_next(i)
159
161
 
 
162
    def row_activated(self, unused, path, column):
 
163
        self.connect_to_selection()
 
164
 
160
165
    def get_state(self):
161
166
        return self.wifi_model.get_state()
162
167
 
163
168
    def disconnect_from_ap(self):
164
 
        self.wifi_model.disconnect_from_ap()
 
169
        if self.nm_connection:
 
170
            if not self.nm_client:
 
171
                self.nm_client = NM.Client.new()
165
172
 
166
 
    def row_activated(self, unused, path, column):
167
 
        passphrase = None
168
 
        if self.password_entry:
169
 
            passphrase = self.password_entry.get_text()
170
 
        self.connect_to_selection(passphrase)
 
173
            self.nm_client.deactivate_connection(self.nm_connection)
 
174
            self.nm_connection = None
 
175
        else:
 
176
            self.wifi_model.disconnect_from_ap()
171
177
 
172
178
    def configure_icons(self):
173
179
        it = Gtk.IconTheme()
220
226
        else:
221
227
            cell.set_property('text', ssid)
222
228
 
223
 
    def get_passphrase(self, ssid):
224
 
        try:
225
 
            cached = self.wifi_model.passphrases_cache[ssid]
226
 
        except KeyError:
227
 
            return ''
228
 
        return cached
229
 
 
230
229
    def is_row_an_ap(self):
231
230
        model, iterator = self.get_selection().get_selected()
232
231
        if iterator is None:
244
243
        else:
245
244
            return False
246
245
 
247
 
    def connect_to_selection(self, passphrase):
 
246
    def find_ap(self, device, ssid):
 
247
        for ap in device.get_access_points():
 
248
            ap_ssid = ap.get_ssid()
 
249
            if ap_ssid and decode_ssid(ap_ssid.get_data()) == ssid:
 
250
                return ap
 
251
        return None
 
252
 
 
253
    def connect_cb(self, client, result, user_data):
 
254
        self.nm_connection = client.add_and_activate_connection_finish(result)
 
255
 
 
256
    def connect_dialog_cb(self, dialog, response):
 
257
        if response == Gtk.ResponseType.OK:
 
258
            connection, device, ap = dialog.get_connection()
 
259
 
 
260
            if not self.nm_client:
 
261
                self.nm_client = NM.Client.new()
 
262
 
 
263
            self.nm_client.add_and_activate_connection_async(
 
264
                connection, device, None, None, self.connect_cb, None
 
265
            )
 
266
        dialog.hide()
 
267
 
 
268
    def connect_to_selection(self):
248
269
        model, iterator = self.get_selection().get_selected()
249
 
        ssid = model[iterator][0]
 
270
        if iterator is None:
 
271
            return
250
272
        parent = model.iter_parent(iterator)
251
273
        if parent:
252
 
            self.wifi_model.connect_to_ap(model[parent][0], ssid, passphrase)
 
274
            try:
 
275
                devid = model[parent][0]
 
276
                ssid = model[iterator][0]
 
277
                if model[iterator][1]:
 
278
                    if not self.nm_client:
 
279
                        self.nm_client = NM.Client.new()
 
280
 
 
281
                    device = self.nm_client.get_device_by_path(devid)
 
282
                    ap = self.find_ap(device, ssid)
 
283
 
 
284
                    connection = NM.SimpleConnection()
 
285
                    connection.add_setting(NM.SettingConnection(
 
286
                        uuid=NM.utils_uuid_generate()
 
287
                    ))
 
288
                    connection.add_setting(NM.SettingWireless(
 
289
                        ssid=ap.get_property("ssid")
 
290
                    ))
 
291
 
 
292
                    dialog = NMA.WifiDialog.new(
 
293
                        self.nm_client,
 
294
                        connection,
 
295
                        device,
 
296
                        ap,
 
297
                        False
 
298
                    )
 
299
                    dialog.connect("response", self.connect_dialog_cb)
 
300
                    dialog.run()
 
301
                else:
 
302
                    self.wifi_model.connect_to_ap(devid, ssid)
 
303
            except Exception as e:
 
304
                dialog = Gtk.MessageDialog(
 
305
                    None, Gtk.DialogFlags.MODAL,
 
306
                    Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE,
 
307
                    "Failed to connect to wireless network"
 
308
                )
 
309
                dialog.format_secondary_text("{}".format(e))
 
310
                dialog.run()
 
311
                dialog.hide()
253
312
 
254
313
GObject.type_register(NetworkManagerTreeView)
255
314
 
261
320
            GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE,
262
321
            (GObject.TYPE_UINT,)),
263
322
        'selection_changed': (
264
 
            GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE, ()),
265
 
        'pw_validated': (
266
 
            GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE,
267
 
            (GObject.TYPE_BOOLEAN,))}
 
323
            GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE, ())}
268
324
 
269
325
    def __init__(self):
270
326
        Gtk.Box.__init__(self)
271
327
        self.set_orientation(Gtk.Orientation.VERTICAL)
272
328
        self.set_spacing(12)
273
 
        self.password_entry = Gtk.Entry()
274
 
        self.view = NetworkManagerTreeView(self.password_entry,
275
 
                                           self.state_changed)
 
329
        self.view = NetworkManagerTreeView(self.state_changed)
276
330
        scrolled_window = Gtk.ScrolledWindow()
277
331
        scrolled_window.set_policy(
278
332
            Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
279
333
        scrolled_window.set_shadow_type(Gtk.ShadowType.IN)
280
334
        scrolled_window.add(self.view)
281
335
        self.pack_start(scrolled_window, True, True, 0)
282
 
        self.hbox = Gtk.Box(spacing=6)
283
 
        self.pack_start(self.hbox, False, True, 0)
284
 
        self.password_label = Gtk.Label(label='Password:')
285
 
        self.password_entry.set_visibility(False)
286
 
        self.password_entry.connect('activate', self.connect_to_ap)
287
 
        self.password_entry.connect('changed', self.password_entry_changed)
288
 
        self.display_password = Gtk.CheckButton(label='Display password')
289
 
        self.display_password.connect('toggled', self.display_password_toggled)
290
 
        self.hbox.pack_start(self.password_label, False, True, 0)
291
 
        self.hbox.pack_start(self.password_entry, True, True, 0)
292
 
        self.hbox.pack_start(self.display_password, False, True, 0)
293
 
        self.hbox.set_sensitive(False)
294
336
        self.selection = self.view.get_selection()
295
337
        self.selection.connect('changed', self.changed)
296
338
        self.show_all()
297
339
 
298
 
    def translate(self, password_label_text, display_password_text):
299
 
        self.password_label.set_label(password_label_text)
300
 
        self.display_password.set_label(display_password_text)
301
 
 
302
340
    def get_state(self):
303
341
        return self.view.get_state()
304
342
 
314
352
    def state_changed(self, state):
315
353
        self.emit('connection', state)
316
354
 
317
 
    def password_is_valid(self):
318
 
        passphrase = self.password_entry.get_text()
319
 
        if len(passphrase) >= 8 and len(passphrase) < 64:
320
 
            return True
321
 
        if len(passphrase) == 64:
322
 
            for c in passphrase:
323
 
                if c not in string.hexdigits:
324
 
                    return False
325
 
            return True
326
 
        else:
327
 
            return False
328
 
 
329
355
    def connect_to_ap(self, *args):
330
 
        if self.password_is_valid():
331
 
            passphrase = self.password_entry.get_text()
332
 
            self.view.connect_to_selection(passphrase)
 
356
        self.view.connect_to_selection()
333
357
 
334
358
    def disconnect_from_ap(self):
335
359
        self.view.disconnect_from_ap()
336
360
 
337
 
    def password_entry_changed(self, *args):
338
 
        self.emit('pw_validated', self.password_is_valid())
339
 
 
340
 
    def display_password_toggled(self, *args):
341
 
        self.password_entry.set_visibility(self.display_password.get_active())
342
 
 
343
361
    def changed(self, selection):
344
362
        iterator = selection.get_selected()[1]
345
363
        if not iterator:
346
364
            return
347
 
        row = selection.get_tree_view().get_model()[iterator]
348
 
        secure = row[1]
349
 
        ssid = row[0]
350
 
        if secure:
351
 
            self.hbox.set_sensitive(True)
352
 
            passphrase = self.view.get_passphrase(ssid)
353
 
            self.password_entry.set_text(passphrase)
354
 
            self.emit('pw_validated', False)
355
 
        else:
356
 
            self.hbox.set_sensitive(False)
357
 
            self.password_entry.set_text('')
358
 
            self.emit('pw_validated', True)
359
365
        self.emit('selection_changed')
360
366
 
361
367
GObject.type_register(NetworkManagerWidget)