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