~thomas-deruyter-3/qreator/qreator

« back to all changes in this revision

Viewing changes to qreator/qrcodes/QRCodeWifiGtk.py

  • Committer: David Planella
  • Date: 2012-05-30 08:33:06 UTC
  • Revision ID: david.planella@ubuntu.com-20120530083306-2naqmakzg56jh5mx
Decoupled QR code types from the QR window and from a particular GUI toolkit (right now only GTK is supported)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
### BEGIN LICENSE
 
3
# Copyright (C) 2012 David Planella <david.planella@ubuntu.com>
 
4
# This program is free software: you can redistribute it and/or modify it
 
5
# under the terms of the GNU General Public License version 3, as published
 
6
# by the Free Software Foundation.
 
7
#
 
8
# This program is distributed in the hope that it will be useful, but
 
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
# PURPOSE.  See the GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License along
 
14
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
### END LICENSE
 
16
 
 
17
from qreator_lib.i18n import _
 
18
from qreator_lib.helpers import get_data_file
 
19
from gi.repository import Gtk, NetworkManager, NMClient
 
20
 
 
21
 
 
22
class QRCodeWifiGtk(object):
 
23
    def __init__(self, qr_code_update_func):
 
24
        self.qr_code_update_func = qr_code_update_func
 
25
        self.builder = Gtk.Builder()
 
26
 
 
27
        self.builder.add_from_file(
 
28
            get_data_file('ui', '%s.ui' % ('QrCodeWifi',)))
 
29
        self.grid = self.builder.get_object('qr_code_wifi')
 
30
        self.builder.connect_signals(self)
 
31
        
 
32
        self.comboboxtextSecurity = self.builder.get_object(
 
33
            'comboboxtextSecurity')
 
34
        self.entrySSID = self.builder.get_object('entrySSID')
 
35
        self.entryPassword = self.builder.get_object('entryPassword')
 
36
        self.comboboxtextSSID = self.builder.get_object('comboboxtextSSID')
 
37
        
 
38
        # Initialize combo boxes (Glade seems not to do it for us)
 
39
        self.comboboxtextSecurity.set_active(0)
 
40
 
 
41
        # TRANSLATORS: 'expand' refers to expanding the drop-down menu
 
42
        # that shows autodetected wireless network identifiers
 
43
        self.entrySSID.set_placeholder_text(
 
44
            _('[Network identifier - expand for autodetection]'))
 
45
        self.entryPassword.set_placeholder_text(_('[Network password]'))
 
46
 
 
47
    def update_ssids(self):
 
48
        ssids = get_ssids()
 
49
        for ssid in ssids:
 
50
            # TODO: clear combobox
 
51
            self.comboboxtextSSID.append_text(ssid)
 
52
 
 
53
    def update_wifi_qr_code(self, security=None, ssid=None, password=None):
 
54
        if not security:
 
55
            security = self.comboboxtextSecurity.get_active_text()
 
56
        if not ssid:
 
57
            ssid = self.entrySSID.get_text()
 
58
        if not password:
 
59
            password = self.entryPassword.get_text()
 
60
 
 
61
        if not security or ssid == '' or password == '':
 
62
            return
 
63
 
 
64
        wifi_qr_code = 'WIFI:T:{0};S:{1};P:{2};;'.format(security, ssid,
 
65
                                                         password)
 
66
        self.qr_code_update_func(wifi_qr_code)
 
67
 
 
68
    def on_comboboxtextSecurity_changed(self, widget, data=None):
 
69
        self.update_wifi_qr_code(security=widget.get_active_text())
 
70
 
 
71
    #def on_entrySSID_icon_press(self, widget, icon, mouse_button):
 
72
    #    self._clear_text_entry(widget, icon)
 
73
 
 
74
    def on_comboboxtextSSID_changed(self, widget, data=None):
 
75
        #self._check_style(self.ui.entrySSID)
 
76
        self.update_wifi_qr_code(ssid=widget.get_active_text())
 
77
 
 
78
    #def on_entryPassword_icon_press(self, widget, icon, mouse_button):
 
79
    #    self._clear_text_entry(widget, icon)
 
80
 
 
81
    def on_entryPassword_changed(self, widget, data=None):
 
82
        #self._check_style(widget)
 
83
        self.update_wifi_qr_code(password=widget.get_text())
 
84
 
 
85
    def on_checkbutton1_clicked(self, widget):
 
86
        if widget.get_active():
 
87
            self.entryPassword.set_visibility(True)
 
88
        else:
 
89
            self.entryPassword.set_visibility(False)
 
90
 
 
91
    def on_activated(self):
 
92
        self.update_ssids()
 
93
 
 
94
 
 
95
def get_ssids():
 
96
    nmc = NMClient.Client.new()
 
97
    devs = nmc.get_devices()
 
98
 
 
99
    ssids = []
 
100
    for dev in devs:
 
101
        if dev.get_device_type() == NetworkManager.DeviceType.WIFI:
 
102
            for ap in dev.get_access_points():
 
103
                ssids.append(ap.get_ssid())
 
104
 
 
105
    return ssids