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