a02907911657bfe5ccfe714d9c1e9d6ad223a928
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-wi.cpp
1 /*
2  * dhcpcd-qt
3  * Copyright 2014 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <QObject>
28 #include <QInputDialog>
29 #include <QLineEdit>
30 #include <QMenu>
31 #include <QMessageBox>
32 #include <QSocketNotifier>
33 #include <QTimer>
34 #include <QWidgetAction>
35
36 #include <cerrno>
37
38 #include "config.h"
39 #include "dhcpcd-wi.h"
40 #include "dhcpcd-qt.h"
41 #include "dhcpcd-ifmenu.h"
42 #include "dhcpcd-ssidmenu.h"
43
44 DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
45 {
46
47         this->dhcpcdQt = parent;
48         this->wpa = wpa;
49         menu = NULL;
50         scans = NULL;
51
52         int fd = dhcpcd_wpa_get_fd(wpa);
53         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
54         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
55         retryOpenTimer = NULL;
56 }
57
58 DhcpcdWi::~DhcpcdWi()
59 {
60
61         dhcpcd_wi_scans_free(scans);
62         if (notifier != NULL)
63                 delete notifier;
64 }
65
66 DHCPCD_WPA *DhcpcdWi::getWpa()
67 {
68
69         return wpa;
70 }
71
72 DHCPCD_WI_SCAN *DhcpcdWi::getScans()
73 {
74
75         return scans;
76 }
77
78 void DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
79 {
80
81         if (menu) {
82                 QList<DhcpcdSsidMenu*> lst;
83                 DHCPCD_WI_SCAN *scan;
84
85                 lst = menu->findChildren<DhcpcdSsidMenu*>();
86                 for (scan = scans; scan; scan = scan->next) {
87                         foreach(DhcpcdSsidMenu *sm, lst) {
88                                 DHCPCD_WI_SCAN *s = sm->getScan();
89                                 if (memcmp(scan->bssid, s->bssid,
90                                     sizeof(scan->bssid)) == 0)
91                                 {
92                                         sm->setScan(scan);
93                                         break;
94                                 }
95                         }
96                 }
97         }
98                 
99         dhcpcd_wi_scans_free(this->scans);
100         this->scans = scans;
101 }
102
103 void DhcpcdWi::createMenu1(QMenu *menu)
104 {
105         DHCPCD_WI_SCAN *scan;
106
107         for (scan = scans; scan; scan = scan->next) {
108                 QWidgetAction *wa = new QWidgetAction(menu);
109                 DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, this, scan);
110                 wa->setDefaultWidget(ssidMenu);
111                 menu->addAction(wa);
112                 connect(ssidMenu, SIGNAL(selected(DHCPCD_WI_SCAN *)),
113                     this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
114         }
115 }
116
117 void DhcpcdWi::createMenu(QMenu *menu)
118 {
119
120         this->menu = menu;
121         createMenu1(menu);
122 }
123
124 QMenu *DhcpcdWi::createIfMenu(QMenu *parent)
125 {
126         DHCPCD_IF *ifp;
127
128         ifp = dhcpcd_wpa_if(wpa);
129         menu = new DhcpcdIfMenu(ifp, parent);
130         menu->setIcon(QIcon::fromTheme("network-wireless"));
131         createMenu1(menu);
132         return menu;
133 }
134
135 void DhcpcdWi::wpaOpen()
136 {
137         int fd = dhcpcd_wpa_open(wpa);
138         static int last_error;
139
140         if (fd == -1) {
141                 if (errno != last_error) {
142                         last_error = errno;
143                         qCritical("%s: dhcpcd_wpa_open: %s",
144                             dhcpcd_wpa_if(wpa)->ifname,
145                             strerror(last_error));
146                 }
147                 return;
148         }
149
150         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
151         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
152         if (retryOpenTimer) {
153                 delete retryOpenTimer;
154                 retryOpenTimer = NULL;
155         }
156 }
157
158 void DhcpcdWi::dispatch()
159 {
160
161         if (dhcpcd_wpa_get_fd(wpa) == -1) {
162                 delete notifier;
163                 notifier = NULL;
164                 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
165                 if (i == NULL ||
166                     strcmp(i->reason, "DEPARTED") == 0 ||
167                     strcmp(i->reason, "STOPPED") == 0)
168                         return;
169                 qWarning("%s: %s",
170                     i->ifname,
171                     qPrintable(tr("dhcpcd WPA connection lost")));
172                 if (retryOpenTimer == NULL) {
173                         retryOpenTimer = new QTimer(this);
174                         connect(retryOpenTimer, SIGNAL(timeout()),
175                             this, SLOT(wpaOpen()));
176                         retryOpenTimer->start(DHCPCD_RETRYOPEN);
177                 }
178                 return;
179         }
180
181         dhcpcd_wpa_dispatch(wpa);
182 }
183
184 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
185 {
186         bool ok;
187         DHCPCD_WI_SCAN s;
188
189         /* Take a copy of scan incase it's destroyed by a scan update */
190         memcpy(&s, scan, sizeof(s));
191         s.next = NULL;
192
193         QString psk = QInputDialog::getText(dhcpcdQt, s.ssid,
194             tr("Pre Shared key"), QLineEdit::Normal, NULL, &ok);
195
196         if (!ok)
197                 return;
198
199         QString errt;
200
201         switch (dhcpcd_wpa_configure_psk(wpa, &s, psk.toAscii())) {
202         case DHCPCD_WPA_SUCCESS:
203                 return;
204         case DHCPCD_WPA_ERR_SET:
205                 errt = tr("Failed to set key management.");
206                 break;
207         case DHCPCD_WPA_ERR_SET_PSK:
208                 errt = tr("Failed to set password, probably too short.");
209                 break;
210         case DHCPCD_WPA_ERR_ENABLE:
211                 errt = tr("Failed to enable the network.");
212                 break;
213         case DHCPCD_WPA_ERR_ASSOC:
214                 errt = tr("Failed to start association.");
215                 break;
216         case DHCPCD_WPA_ERR_WRITE:
217                 errt = tr("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
218                 break;
219         default:
220                 errt = strerror(errno);
221                 break;
222         }
223
224         QMessageBox::critical(dhcpcdQt, tr("Error setting wireless properties"),
225             errt);
226 }