scan flags -> wpa_flags.
[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-ssidmenu.h"
44
45 DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
46 {
47
48         this->dhcpcdQt = parent;
49         this->wpa = wpa;
50         menu = NULL;
51         scans = NULL;
52
53         int fd = dhcpcd_wpa_get_fd(wpa);
54         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
55         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
56         retryOpenTimer = NULL;
57 }
58
59 DhcpcdWi::~DhcpcdWi()
60 {
61
62         if (menu) {
63                 delete menu;
64                 menu = NULL;
65         }
66
67         if (notifier) {
68                 delete notifier;
69                 notifier = NULL;
70         }
71
72         dhcpcd_wi_scans_free(scans);
73 }
74
75 DHCPCD_WPA *DhcpcdWi::getWpa()
76 {
77
78         return wpa;
79 }
80
81 DHCPCD_WI_SCAN *DhcpcdWi::getScans()
82 {
83
84         return scans;
85 }
86
87 bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
88 {
89         int changed = 0;
90
91         if (menu) {
92                 QList<DhcpcdSsidMenu*> lst;
93                 DHCPCD_WI_SCAN *scan;
94
95                 for (scan = scans; scan; scan = scan->next) {
96                         bool found = false;
97                         QAction *before = NULL;
98
99                         lst = menu->findChildren<DhcpcdSsidMenu*>();
100                         foreach(DhcpcdSsidMenu *sm, lst) {
101                                 DHCPCD_WI_SCAN *s = sm->getScan();
102                                 if (memcmp(scan->bssid, s->bssid,
103                                     sizeof(scan->bssid)) == 0)
104                                 {
105                                         sm->setScan(scan);
106                                         found = true;
107                                         break;
108                                 }
109                                 if (dhcpcd_wi_scan_compare(scan, s) < 0)
110                                         before = sm;
111                         }
112
113                         if (!found) {
114                                 createMenuItem(menu, scan, before);
115                                 changed++;
116                         }
117                 }
118
119                 lst = menu->findChildren<DhcpcdSsidMenu*>();
120                 foreach(DhcpcdSsidMenu *sm, lst) {
121                         DHCPCD_WI_SCAN *s = sm->getScan();
122                         for (scan = scans; scan; scan = scan->next) {
123                                 if (memcmp(scan->bssid, s->bssid,
124                                     sizeof(scan->bssid)) == 0)
125                                         break;
126                         }
127                         if (scan == NULL) {
128                                 menu->removeAction(sm);
129                                 changed--;
130                         }
131                 }
132         }
133
134         dhcpcd_wi_scans_free(this->scans);
135         this->scans = scans;
136
137         return !(changed == 0);
138 }
139
140 void DhcpcdWi::createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan,
141     QAction *before)
142 {
143         DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, this, scan);
144         menu->insertAction(before, ssidMenu);
145         connect(ssidMenu, SIGNAL(triggered(DHCPCD_WI_SCAN *)),
146             this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
147 }
148
149 void DhcpcdWi::createMenu1(QMenu *menu)
150 {
151         DHCPCD_WI_SCAN *scan;
152
153         for (scan = scans; scan; scan = scan->next)
154                 createMenuItem(menu, scan);
155 }
156
157 void DhcpcdWi::createMenu(QMenu *menu)
158 {
159
160         this->menu = menu;
161         createMenu1(menu);
162 }
163
164 QMenu *DhcpcdWi::createIfMenu(QMenu *parent)
165 {
166         DHCPCD_IF *ifp;
167         QIcon icon;
168
169         ifp = dhcpcd_wpa_if(wpa);
170         menu = new DhcpcdIfMenu(ifp, parent);
171         icon = DhcpcdQt::getIcon("devices", "network-wireless");
172         menu->setIcon(icon);
173         createMenu1(menu);
174         return menu;
175 }
176
177 void DhcpcdWi::wpaOpen()
178 {
179         int fd = dhcpcd_wpa_open(wpa);
180         static int last_error;
181
182         if (fd == -1) {
183                 if (errno != last_error) {
184                         last_error = errno;
185                         qCritical("%s: dhcpcd_wpa_open: %s",
186                             dhcpcd_wpa_if(wpa)->ifname,
187                             strerror(last_error));
188                 }
189                 return;
190         }
191
192         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
193         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
194         if (retryOpenTimer) {
195                 delete retryOpenTimer;
196                 retryOpenTimer = NULL;
197         }
198 }
199
200 void DhcpcdWi::dispatch()
201 {
202
203         if (dhcpcd_wpa_get_fd(wpa) == -1) {
204                 delete notifier;
205                 notifier = NULL;
206                 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
207                 if (i == NULL ||
208                     strcmp(i->reason, "DEPARTED") == 0 ||
209                     strcmp(i->reason, "STOPPED") == 0)
210                         return;
211                 qWarning("%s: %s",
212                     i->ifname,
213                     qPrintable(tr("dhcpcd WPA connection lost")));
214                 if (retryOpenTimer == NULL) {
215                         retryOpenTimer = new QTimer(this);
216                         connect(retryOpenTimer, SIGNAL(timeout()),
217                             this, SLOT(wpaOpen()));
218                         retryOpenTimer->start(DHCPCD_RETRYOPEN);
219                 }
220                 return;
221         }
222
223         dhcpcd_wpa_dispatch(wpa);
224 }
225
226 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
227 {
228         DHCPCD_WI_SCAN s;
229         const char *psk;
230
231         /* Take a copy of scan incase it's destroyed by a scan update */
232         memcpy(&s, scan, sizeof(s));
233         s.next = NULL;
234
235         if (s.flags & WSF_PSK) {
236                 bool ok;
237                 QString pwd = QInputDialog::getText(dhcpcdQt, s.ssid,
238                     tr("Pre Shared key"), QLineEdit::Normal, NULL, &ok);
239                 if (!ok)
240                         return;
241                 psk = pwd.toAscii();
242         } else
243                 psk = NULL;
244
245         QString errt;
246
247         switch (dhcpcd_wpa_configure_psk(wpa, &s, psk)) {
248         case DHCPCD_WPA_SUCCESS:
249                 return;
250         case DHCPCD_WPA_ERR_SET:
251                 errt = tr("Failed to set key management.");
252                 break;
253         case DHCPCD_WPA_ERR_SET_PSK:
254                 errt = tr("Failed to set password, probably too short.");
255                 break;
256         case DHCPCD_WPA_ERR_ENABLE:
257                 errt = tr("Failed to enable the network.");
258                 break;
259         case DHCPCD_WPA_ERR_ASSOC:
260                 errt = tr("Failed to start association.");
261                 break;
262         case DHCPCD_WPA_ERR_WRITE:
263                 errt = tr("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
264                 break;
265         default:
266                 errt = strerror(errno);
267                 break;
268         }
269
270         QMessageBox::critical(dhcpcdQt, tr("Error setting wireless properties"),
271             errt);
272 }