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