3
1
from dbus.mainloop.glib import DBusGMainLoop
4
2
DBusGMainLoop(set_as_default=True)
6
4
from gi.repository import Gtk, GObject, GLib
5
from gi.repository import NM, NMA
8
from ubiquity.nm import QueuedCaller, NetworkStore, NetworkManager
7
from ubiquity.nm import decode_ssid, QueuedCaller, NetworkStore, NetworkManager
11
10
class GLibQueuedCaller(QueuedCaller):
92
91
class NetworkManagerTreeView(Gtk.TreeView):
93
92
__gtype_name__ = 'NetworkManagerTreeView'
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)
157
159
self.expand_row(path, False)
158
160
i = model.iter_next(i)
162
def row_activated(self, unused, path, column):
163
self.connect_to_selection()
160
165
def get_state(self):
161
166
return self.wifi_model.get_state()
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()
166
def row_activated(self, unused, path, column):
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
176
self.wifi_model.disconnect_from_ap()
172
178
def configure_icons(self):
173
179
it = Gtk.IconTheme()
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:
253
def connect_cb(self, client, result, user_data):
254
self.nm_connection = client.add_and_activate_connection_finish(result)
256
def connect_dialog_cb(self, dialog, response):
257
if response == Gtk.ResponseType.OK:
258
connection, device, ap = dialog.get_connection()
260
if not self.nm_client:
261
self.nm_client = NM.Client.new()
263
self.nm_client.add_and_activate_connection_async(
264
connection, device, None, None, self.connect_cb, None
268
def connect_to_selection(self):
248
269
model, iterator = self.get_selection().get_selected()
249
ssid = model[iterator][0]
250
272
parent = model.iter_parent(iterator)
252
self.wifi_model.connect_to_ap(model[parent][0], ssid, passphrase)
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()
281
device = self.nm_client.get_device_by_path(devid)
282
ap = self.find_ap(device, ssid)
284
connection = NM.SimpleConnection()
285
connection.add_setting(NM.SettingConnection(
286
uuid=NM.utils_uuid_generate()
288
connection.add_setting(NM.SettingWireless(
289
ssid=ap.get_property("ssid")
292
dialog = NMA.WifiDialog.new(
299
dialog.connect("response", self.connect_dialog_cb)
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"
309
dialog.format_secondary_text("{}".format(e))
254
313
GObject.type_register(NetworkManagerTreeView)
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, ()),
266
GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE,
267
(GObject.TYPE_BOOLEAN,))}
323
GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE, ())}
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,
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)
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)
302
340
def get_state(self):
303
341
return self.view.get_state()
314
352
def state_changed(self, state):
315
353
self.emit('connection', state)
317
def password_is_valid(self):
318
passphrase = self.password_entry.get_text()
319
if len(passphrase) >= 8 and len(passphrase) < 64:
321
if len(passphrase) == 64:
323
if c not in string.hexdigits:
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()
334
358
def disconnect_from_ap(self):
335
359
self.view.disconnect_from_ap()
337
def password_entry_changed(self, *args):
338
self.emit('pw_validated', self.password_is_valid())
340
def display_password_toggled(self, *args):
341
self.password_entry.set_visibility(self.display_password.get_active())
343
361
def changed(self, selection):
344
362
iterator = selection.get_selected()[1]
347
row = selection.get_tree_view().get_model()[iterator]
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)
356
self.hbox.set_sensitive(False)
357
self.password_entry.set_text('')
358
self.emit('pw_validated', True)
359
365
self.emit('selection_changed')
361
367
GObject.type_register(NetworkManagerWidget)