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