~dpm/qreator/snap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2012 David Planella <david.planella@ubuntu.com>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

from qreator_lib.i18n import _
from qreator_lib.helpers import get_data_file
from gi.repository import Gtk, NetworkManager, NMClient


class QRCodeWifiGtk(object):
    def __init__(self, qr_code_update_func):
        self.qr_code_update_func = qr_code_update_func
        self.builder = Gtk.Builder()

        self.builder.add_from_file(
            get_data_file('ui', '%s.ui' % ('QrCodeWifi',)))
        self.grid = self.builder.get_object('qr_code_wifi')
        self.builder.connect_signals(self)
        
        self.comboboxtextSecurity = self.builder.get_object(
            'comboboxtextSecurity')
        self.entrySSID = self.builder.get_object('entrySSID')
        self.entryPassword = self.builder.get_object('entryPassword')
        self.comboboxtextSSID = self.builder.get_object('comboboxtextSSID')
        
        # Initialize combo boxes (Glade seems not to do it for us)
        self.comboboxtextSecurity.set_active(0)

        # TRANSLATORS: 'expand' refers to expanding the drop-down menu
        # that shows autodetected wireless network identifiers
        self.entrySSID.set_placeholder_text(
            _('[Network identifier - expand for autodetection]'))
        self.entryPassword.set_placeholder_text(_('[Network password]'))

    def update_ssids(self):
        ssids = get_ssids()
        for ssid in ssids:
            # TODO: clear combobox
            self.comboboxtextSSID.append_text(ssid)

    def update_wifi_qr_code(self, security=None, ssid=None, password=None):
        if not security:
            security = self.comboboxtextSecurity.get_active_text()
        if not ssid:
            ssid = self.entrySSID.get_text()
        if not password:
            password = self.entryPassword.get_text()

        if not security or ssid == '' or password == '':
            return

        wifi_qr_code = 'WIFI:T:{0};S:{1};P:{2};;'.format(security, ssid,
                                                         password)
        self.qr_code_update_func(wifi_qr_code)

    def on_comboboxtextSecurity_changed(self, widget, data=None):
        self.update_wifi_qr_code(security=widget.get_active_text())

    #def on_entrySSID_icon_press(self, widget, icon, mouse_button):
    #    self._clear_text_entry(widget, icon)

    def on_comboboxtextSSID_changed(self, widget, data=None):
        #self._check_style(self.ui.entrySSID)
        self.update_wifi_qr_code(ssid=widget.get_active_text())

    #def on_entryPassword_icon_press(self, widget, icon, mouse_button):
    #    self._clear_text_entry(widget, icon)

    def on_entryPassword_changed(self, widget, data=None):
        #self._check_style(widget)
        self.update_wifi_qr_code(password=widget.get_text())

    def on_checkbutton1_clicked(self, widget):
        if widget.get_active():
            self.entryPassword.set_visibility(True)
        else:
            self.entryPassword.set_visibility(False)

    def on_activated(self):
        self.update_ssids()


def get_ssids():
    nmc = NMClient.Client.new()
    devs = nmc.get_devices()

    ssids = []
    for dev in devs:
        if dev.get_device_type() == NetworkManager.DeviceType.WIFI:
            for ap in dev.get_access_points():
                ssids.append(ap.get_ssid())

    return ssids