Associated scans are displayed first in a bold font.
[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         bool changed = false;
90
91         if (menu) {
92                 QList<DhcpcdSsidMenu*> lst;
93                 DHCPCD_WI_SCAN *scan;
94                 DHCPCD_IF *i;
95                 bool found, associated;
96                 QAction *before;
97
98                 i = dhcpcd_wpa_if(wpa);
99                 for (scan = scans; scan; scan = scan->next) {
100                         found = false;
101                         before = NULL;
102                         associated = dhcpcd_wi_associated(i, scan);
103
104                         lst = menu->findChildren<DhcpcdSsidMenu*>();
105                         foreach(DhcpcdSsidMenu *sm, lst) {
106                                 DHCPCD_WI_SCAN *s = sm->getScan();
107
108                                 if (strcmp(scan->ssid, s->ssid) == 0) {
109                                         /* If association changes, remove
110                                          * the entry and re-create it
111                                          * so assoicates entries appear at
112                                          * the top */
113                                         if (associated != sm->isAssociated()) {
114                                                 menu->removeAction(sm);
115                                                 break;
116                                         }
117                                         sm->setScan(scan);
118                                         found = true;
119                                         break;
120                                 }
121                                 if (!associated &&
122                                     dhcpcd_wi_scan_compare(scan, s) < 0)
123                                         before = sm;
124                         }
125
126                         if (!found) {
127                                 if (associated) {
128                                         lst = menu->findChildren<DhcpcdSsidMenu*>();
129                                         if (lst.empty())
130                                                 before = NULL;
131                                         else
132                                                 before = lst.at(0);
133                                 }
134                                 createMenuItem(menu, scan, before);
135                                 changed = true;
136                         }
137                 }
138
139                 lst = menu->findChildren<DhcpcdSsidMenu*>();
140                 foreach(DhcpcdSsidMenu *sm, lst) {
141                         DHCPCD_WI_SCAN *s = sm->getScan();
142                         for (scan = scans; scan; scan = scan->next) {
143                                 if (strcmp(scan->ssid, s->ssid) == 0)
144                                         break;
145                         }
146                         if (scan == NULL) {
147                                 menu->removeAction(sm);
148                                 changed = true;
149                         }
150                 }
151         }
152
153         dhcpcd_wi_scans_free(this->scans);
154         this->scans = scans;
155
156         return changed;
157 }
158
159 void DhcpcdWi::createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan,
160     QAction *before)
161 {
162         DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, this, scan);
163         menu->insertAction(before, ssidMenu);
164         connect(ssidMenu, SIGNAL(triggered(DHCPCD_WI_SCAN *)),
165             this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
166 }
167
168 void DhcpcdWi::createMenu1(QMenu *menu)
169 {
170         DHCPCD_WI_SCAN *scan;
171
172         for (scan = scans; scan; scan = scan->next)
173                 createMenuItem(menu, scan);
174 }
175
176 void DhcpcdWi::createMenu(QMenu *menu)
177 {
178
179         this->menu = menu;
180         createMenu1(menu);
181 }
182
183 QMenu *DhcpcdWi::createIfMenu(QMenu *parent)
184 {
185         DHCPCD_IF *ifp;
186         QIcon icon;
187
188         ifp = dhcpcd_wpa_if(wpa);
189         menu = new DhcpcdIfMenu(ifp, parent);
190         icon = DhcpcdQt::getIcon("devices", "network-wireless");
191         menu->setIcon(icon);
192         createMenu1(menu);
193         return menu;
194 }
195
196 void DhcpcdWi::wpaOpen()
197 {
198         int fd = dhcpcd_wpa_open(wpa);
199         static int last_error;
200
201         if (fd == -1) {
202                 if (errno != last_error) {
203                         last_error = errno;
204                         qCritical("%s: dhcpcd_wpa_open: %s",
205                             dhcpcd_wpa_if(wpa)->ifname,
206                             strerror(last_error));
207                 }
208                 return;
209         }
210
211         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
212         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
213         if (retryOpenTimer) {
214                 delete retryOpenTimer;
215                 retryOpenTimer = NULL;
216         }
217 }
218
219 void DhcpcdWi::dispatch()
220 {
221
222         if (dhcpcd_wpa_get_fd(wpa) == -1) {
223                 delete notifier;
224                 notifier = NULL;
225                 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
226                 if (i == NULL ||
227                     strcmp(i->reason, "DEPARTED") == 0 ||
228                     strcmp(i->reason, "STOPPED") == 0)
229                         return;
230                 qWarning("%s: %s",
231                     i->ifname,
232                     qPrintable(tr("dhcpcd WPA connection lost")));
233                 if (retryOpenTimer == NULL) {
234                         retryOpenTimer = new QTimer(this);
235                         connect(retryOpenTimer, SIGNAL(timeout()),
236                             this, SLOT(wpaOpen()));
237                         retryOpenTimer->start(DHCPCD_RETRYOPEN);
238                 }
239                 return;
240         }
241
242         dhcpcd_wpa_dispatch(wpa);
243 }
244
245 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
246 {
247         DHCPCD_WI_SCAN s;
248         int err;
249
250         /* Take a copy of scan incase it's destroyed by a scan update */
251         memcpy(&s, scan, sizeof(s));
252         s.next = NULL;
253
254         if (s.flags & WSF_PSK) {
255                 bool ok;
256                 QString pwd = QInputDialog::getText(dhcpcdQt, s.ssid,
257                     tr("Pre Shared key"), QLineEdit::Normal, NULL, &ok);
258                 if (!ok)
259                         return;
260                 if (pwd.isNull() || pwd.isEmpty())
261                         err = dhcpcd_wpa_select(wpa, &s);
262                 else
263                         err = dhcpcd_wpa_configure(wpa, &s, pwd.toAscii());
264         } else
265                 err = dhcpcd_wpa_configure(wpa, &s, NULL);
266
267         QString errt;
268
269         switch (err) {
270         case DHCPCD_WPA_SUCCESS:
271                 return;
272         case DHCPCD_WPA_ERR_DISCONN:
273                 errt = tr("Failed to disconnect.");
274                 break;
275         case DHCPCD_WPA_ERR_RECONF:
276                 errt = tr("Faile to reconfigure.");
277                 break;
278         case DHCPCD_WPA_ERR_SET:
279                 errt = tr("Failed to set key management.");
280                 break;
281         case DHCPCD_WPA_ERR_SET_PSK:
282                 errt = tr("Failed to set password, probably too short.");
283                 break;
284         case DHCPCD_WPA_ERR_ENABLE:
285                 errt = tr("Failed to enable the network.");
286                 break;
287         case DHCPCD_WPA_ERR_SELECT:
288                 errt = tr("Failed to select the network.");
289                 break;
290         case DHCPCD_WPA_ERR_ASSOC:
291                 errt = tr("Failed to start association.");
292                 break;
293         case DHCPCD_WPA_ERR_WRITE:
294                 errt = tr("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
295                 break;
296         default:
297                 errt = strerror(errno);
298                 break;
299         }
300
301         QMessageBox::critical(dhcpcdQt, tr("Error setting wireless properties"),
302             errt);
303 }