427f268151df291fb4951bff411039848837346e
[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 <QAction>
28 #include <QObject>
29 #include <QInputDialog>
30 #include <QLineEdit>
31 #include <QMenu>
32 #include <QMessageBox>
33 #include <QSocketNotifier>
34 #include <QTimer>
35 #include <QWidgetAction>
36
37 #include <cerrno>
38
39 #include "config.h"
40 #include "dhcpcd-wi.h"
41 #include "dhcpcd-qt.h"
42 #include "dhcpcd-ifmenu.h"
43 #include "dhcpcd-ssid.h"
44 #include "dhcpcd-ssidmenu.h"
45
46 DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
47 {
48
49         this->dhcpcdQt = parent;
50         this->wpa = wpa;
51         menu = NULL;
52         scans = NULL;
53         ssid = NULL;
54
55         notifier = NULL;
56         pingTimer = NULL;
57         scanTimer = NULL;
58 }
59
60 DhcpcdWi::~DhcpcdWi()
61 {
62
63         if (menu) {
64                 dhcpcdQt->menuDeleted(menu);
65                 menu->setVisible(false);
66                 menu->deleteLater();
67                 menu = NULL;
68         }
69
70         if (notifier) {
71                 notifier->setEnabled(false);
72                 notifier->deleteLater();
73                 notifier = NULL;
74         }
75
76         if (pingTimer) {
77                 pingTimer->stop();
78                 pingTimer->deleteLater();
79                 pingTimer = NULL;
80         }
81
82         if (ssid) {
83                 ssid->reject();
84                 ssid->deleteLater();
85                 ssid = NULL;
86         }
87
88         if (scanTimer) {
89                 scanTimer->stop();
90                 scanTimer->deleteLater();
91                 scanTimer = NULL;
92         }
93
94         dhcpcd_wi_scans_free(scans);
95 }
96
97 DHCPCD_WPA *DhcpcdWi::getWpa()
98 {
99
100         return wpa;
101 }
102
103 DHCPCD_WI_SCAN *DhcpcdWi::getScans()
104 {
105
106         return scans;
107 }
108
109 bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
110 {
111         bool changed = false;
112
113         if (menu) {
114                 QList<DhcpcdSsidMenu*> lst;
115                 DHCPCD_WI_SCAN *scan;
116                 DHCPCD_IF *i;
117                 bool found, associated;
118                 QAction *before;
119
120                 i = dhcpcd_wpa_if(wpa);
121                 for (scan = scans; scan; scan = scan->next) {
122                         found = false;
123                         before = NULL;
124                         associated = dhcpcd_wi_associated(i, scan);
125
126                         lst = menu->findChildren<DhcpcdSsidMenu*>();
127                         foreach(DhcpcdSsidMenu *sm, lst) {
128                                 DHCPCD_WI_SCAN *s = sm->getScan();
129
130                                 if (strcmp(scan->ssid, s->ssid) == 0) {
131                                         /* If association changes, remove
132                                          * the entry and re-create it
133                                          * so assoicates entries appear at
134                                          * the top */
135                                         if (associated != sm->isAssociated()) {
136                                                 menu->removeAction(sm);
137                                                 break;
138                                         }
139                                         sm->setScan(scan);
140                                         found = true;
141                                         break;
142                                 }
143                                 if (!associated &&
144                                     dhcpcd_wi_scan_compare(scan, s) < 0)
145                                         before = sm;
146                         }
147
148                         if (!found) {
149                                 if (associated) {
150                                         lst = menu->findChildren<DhcpcdSsidMenu*>();
151                                         if (lst.empty())
152                                                 before = NULL;
153                                         else
154                                                 before = lst.at(0);
155                                 }
156                                 createMenuItem(menu, scan, before);
157                                 changed = true;
158                         }
159                 }
160
161                 lst = menu->findChildren<DhcpcdSsidMenu*>();
162                 foreach(DhcpcdSsidMenu *sm, lst) {
163                         DHCPCD_WI_SCAN *s = sm->getScan();
164                         for (scan = scans; scan; scan = scan->next) {
165                                 if (strcmp(scan->ssid, s->ssid) == 0)
166                                         break;
167                         }
168                         if (scan == NULL) {
169                                 menu->removeAction(sm);
170                                 changed = true;
171                         }
172                 }
173         }
174
175         dhcpcd_wi_scans_free(this->scans);
176         this->scans = scans;
177
178         return (changed && menu && menu->isVisible());
179 }
180
181 void DhcpcdWi::createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan,
182     QAction *before)
183 {
184         DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, this, scan);
185         menu->insertAction(before, ssidMenu);
186         connect(ssidMenu, SIGNAL(triggered(DHCPCD_WI_SCAN *)),
187             this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
188 }
189
190 void DhcpcdWi::createMenu1(QMenu *menu)
191 {
192         DHCPCD_IF *i;
193         DHCPCD_WI_SCAN *scan;
194         QAction *before;
195
196         connect(menu, SIGNAL(aboutToShow()), this, SLOT(menuShown()));
197         connect(menu, SIGNAL(aboutToHide()), this, SLOT(menuHidden()));
198
199         i = dhcpcd_wpa_if(wpa);
200         for (scan = scans; scan; scan = scan->next) {
201                 before = NULL;
202                 if (dhcpcd_wi_associated(i, scan)) {
203                         QList<DhcpcdSsidMenu*> lst;
204
205                         lst = menu->findChildren<DhcpcdSsidMenu*>();
206                         if (!lst.empty())
207                                 before = lst.at(0);
208                 }
209                 createMenuItem(menu, scan, before);
210         }
211 }
212
213 void DhcpcdWi::createMenu(QMenu *menu)
214 {
215
216         if (this->menu && this->menu != menu)
217                 this->menu->deleteLater();
218         this->menu = menu;
219         createMenu1(menu);
220 }
221
222 QMenu *DhcpcdWi::createIfMenu(QMenu *parent)
223 {
224         DHCPCD_IF *ifp;
225         QIcon icon;
226
227         ifp = dhcpcd_wpa_if(wpa);
228         if (this->menu)
229                 this->menu->deleteLater();
230         menu = new DhcpcdIfMenu(ifp, parent);
231         icon = DhcpcdQt::getIcon("devices", "network-wireless");
232         menu->setIcon(icon);
233         createMenu1(menu);
234         return menu;
235 }
236
237 bool DhcpcdWi::open()
238 {
239         int fd = dhcpcd_wpa_open(wpa);
240
241         if (fd == -1) {
242                 qCritical("%s: dhcpcd_wpa_open: %s",
243                     dhcpcd_wpa_if(wpa)->ifname,
244                     strerror(errno));
245                 dhcpcd_wpa_close(wpa);
246                 return false;
247         }
248
249         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
250         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
251         pingTimer = new QTimer(this);
252         connect(pingTimer, SIGNAL(timeout()), this, SLOT(ping()));
253         pingTimer->start(DHCPCD_WPA_PING);
254         scanTimer = new QTimer(this);
255         connect(scanTimer, SIGNAL(timeout()), this, SLOT(scan()));
256         scanTimer->start(DHCPCD_WPA_SCAN_LONG);
257         return true;
258 }
259
260 void DhcpcdWi::dispatch()
261 {
262
263         dhcpcd_wpa_dispatch(wpa);
264 }
265
266 void DhcpcdWi::ping()
267 {
268
269         if (!dhcpcd_wpa_ping(wpa))
270                 dhcpcd_wpa_close(wpa);
271 }
272
273 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
274 {
275         DHCPCD_WI_SCAN s;
276         int err;
277
278         /* Take a copy of scan incase it's destroyed by a scan update */
279         memcpy(&s, scan, sizeof(s));
280         s.next = NULL;
281
282         if (s.flags & WSF_PSK) {
283                 bool ok;
284                 QString pwd;
285
286                 ssid = new DhcpcdSsid(this, &s);
287                 pwd = ssid->getPsk(&ok);
288                 ssid->deleteLater();
289                 ssid = NULL;
290                 if (!ok)
291                         return;
292                 if (pwd.isNull() || pwd.isEmpty())
293                         err = dhcpcd_wpa_select(wpa, &s);
294                 else
295                         err = dhcpcd_wpa_configure(wpa, &s, pwd.toAscii());
296         } else
297                 err = dhcpcd_wpa_configure(wpa, &s, NULL);
298
299         QString errt;
300         switch (err) {
301         case DHCPCD_WPA_SUCCESS:
302                 return;
303         case DHCPCD_WPA_ERR_DISCONN:
304                 errt = tr("Failed to disconnect.");
305                 break;
306         case DHCPCD_WPA_ERR_RECONF:
307                 errt = tr("Faile to reconfigure.");
308                 break;
309         case DHCPCD_WPA_ERR_SET:
310                 errt = tr("Failed to set key management.");
311                 break;
312         case DHCPCD_WPA_ERR_SET_PSK:
313                 errt = tr("Failed to set password, probably too short.");
314                 break;
315         case DHCPCD_WPA_ERR_ENABLE:
316                 errt = tr("Failed to enable the network.");
317                 break;
318         case DHCPCD_WPA_ERR_SELECT:
319                 errt = tr("Failed to select the network.");
320                 break;
321         case DHCPCD_WPA_ERR_ASSOC:
322                 errt = tr("Failed to start association.");
323                 break;
324         case DHCPCD_WPA_ERR_WRITE:
325                 errt = tr("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
326                 break;
327         default:
328                 errt = strerror(errno);
329                 break;
330         }
331
332         QMessageBox::critical(dhcpcdQt, tr("Error setting wireless properties"),
333             errt);
334 }
335
336 void DhcpcdWi::scan()
337 {
338         DHCPCD_IF *i;
339
340         i = dhcpcd_wpa_if(wpa);
341         if (!i->up || dhcpcd_wpa_can_background_scan(wpa))
342                 dhcpcd_wpa_scan(wpa);
343 }
344
345 void DhcpcdWi::menuHidden()
346 {
347
348         if (scanTimer) {
349                 scanTimer->stop();
350                 scanTimer->start(DHCPCD_WPA_SCAN_LONG);
351         }
352 }
353
354 void DhcpcdWi::menuShown()
355 {
356
357         if (scanTimer) {
358                 scanTimer->stop();
359                 scanTimer->start(DHCPCD_WPA_SCAN_SHORT);
360         }
361 }