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