3 * Copyright 2014-2017 Roy Marples <roy@marples.name>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <QInputDialog>
32 #include <QMessageBox>
33 #include <QSocketNotifier>
35 #include <QWidgetAction>
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"
46 DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
49 this->dhcpcdQt = parent;
65 dhcpcdQt->menuDeleted(menu);
71 notifier->deleteLater();
76 pingTimer->deleteLater();
86 scanTimer->deleteLater();
91 DHCPCD_WPA *DhcpcdWi::getWpa()
97 DHCPCD_WI_SCAN *DhcpcdWi::getScans()
103 bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
105 bool changed = false;
108 QList<DhcpcdSsidMenu*> lst;
109 DHCPCD_WI_SCAN *scan;
111 bool found, associated;
114 i = dhcpcd_wpa_if(wpa);
115 for (scan = scans; scan; scan = scan->next) {
118 associated = dhcpcd_wi_associated(i, scan);
120 lst = menu->findChildren<DhcpcdSsidMenu*>();
121 foreach(DhcpcdSsidMenu *sm, lst) {
122 DHCPCD_WI_SCAN *s = sm->getScan();
124 if (strcmp(scan->ssid, s->ssid) == 0) {
125 /* If association changes, remove
126 * the entry and re-create it
127 * so assoicates entries appear at
129 if (associated != sm->isAssociated()) {
130 menu->removeAction(sm);
138 dhcpcd_wi_scan_compare(scan, s) < 0)
144 lst = menu->findChildren<DhcpcdSsidMenu*>();
150 createMenuItem(menu, scan, before);
155 lst = menu->findChildren<DhcpcdSsidMenu*>();
156 foreach(DhcpcdSsidMenu *sm, lst) {
157 DHCPCD_WI_SCAN *s = sm->getScan();
158 for (scan = scans; scan; scan = scan->next) {
159 if (strcmp(scan->ssid, s->ssid) == 0)
163 menu->removeAction(sm);
169 dhcpcd_wi_scans_free(this->scans);
172 return (changed && menu && menu->isVisible());
175 void DhcpcdWi::createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan,
178 DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, this, scan);
179 menu->insertAction(before, ssidMenu);
180 connect(ssidMenu, SIGNAL(triggered(DHCPCD_WI_SCAN *)),
181 this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
184 void DhcpcdWi::createMenu1(QMenu *menu)
187 DHCPCD_WI_SCAN *scan;
190 connect(menu, SIGNAL(aboutToShow()), this, SLOT(menuShown()));
191 connect(menu, SIGNAL(aboutToHide()), this, SLOT(menuHidden()));
193 i = dhcpcd_wpa_if(wpa);
194 for (scan = scans; scan; scan = scan->next) {
196 if (dhcpcd_wi_associated(i, scan)) {
197 QList<DhcpcdSsidMenu*> lst;
199 lst = menu->findChildren<DhcpcdSsidMenu*>();
203 createMenuItem(menu, scan, before);
207 void DhcpcdWi::createMenu(QMenu *menu)
210 if (this->menu && this->menu != menu)
211 this->menu->deleteLater();
216 QMenu *DhcpcdWi::createIfMenu(QMenu *parent)
221 ifp = dhcpcd_wpa_if(wpa);
223 this->menu->deleteLater();
224 menu = new DhcpcdIfMenu(ifp, parent);
225 icon = DhcpcdQt::getIcon("devices", "network-wireless");
231 bool DhcpcdWi::open()
233 int fd = dhcpcd_wpa_open(wpa);
236 qCritical("%s: dhcpcd_wpa_open: %s",
237 dhcpcd_wpa_if(wpa)->ifname,
239 dhcpcd_wpa_close(wpa);
243 notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
244 connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
245 pingTimer = new QTimer(this);
246 connect(pingTimer, SIGNAL(timeout()), this, SLOT(ping()));
247 pingTimer->start(DHCPCD_WPA_PING);
248 scanTimer = new QTimer(this);
249 connect(scanTimer, SIGNAL(timeout()), this, SLOT(scan()));
250 scanTimer->start(DHCPCD_WPA_SCAN_LONG);
254 void DhcpcdWi::close()
258 menu->setVisible(false);
261 notifier->setEnabled(false);
273 dhcpcd_wi_scans_free(scans);
277 dhcpcd_wpa_close(wpa);
282 void DhcpcdWi::dispatch()
285 dhcpcd_wpa_dispatch(wpa);
288 void DhcpcdWi::ping()
291 if (!dhcpcd_wpa_ping(wpa))
292 dhcpcd_wpa_close(wpa);
295 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
300 /* Take a copy of scan incase it's destroyed by a scan update */
301 memcpy(&s, scan, sizeof(s));
304 if (s.flags & WSF_PSK) {
308 ssid = new DhcpcdSsid(this, &s);
309 pwd = ssid->getPsk(&ok);
314 if (pwd.isNull() || pwd.isEmpty())
315 err = dhcpcd_wpa_select(wpa, &s);
317 err = dhcpcd_wpa_configure(wpa, &s, pwd.toLatin1());
319 err = dhcpcd_wpa_configure(wpa, &s, NULL);
323 case DHCPCD_WPA_SUCCESS:
325 case DHCPCD_WPA_ERR_DISCONN:
326 errt = tr("Failed to disconnect.");
328 case DHCPCD_WPA_ERR_RECONF:
329 errt = tr("Faile to reconfigure.");
331 case DHCPCD_WPA_ERR_SET:
332 errt = tr("Failed to set key management.");
334 case DHCPCD_WPA_ERR_SET_PSK:
335 errt = tr("Failed to set password, probably too short.");
337 case DHCPCD_WPA_ERR_ENABLE:
338 errt = tr("Failed to enable the network.");
340 case DHCPCD_WPA_ERR_SELECT:
341 errt = tr("Failed to select the network.");
343 case DHCPCD_WPA_ERR_ASSOC:
344 errt = tr("Failed to start association.");
346 case DHCPCD_WPA_ERR_WRITE:
347 errt = tr("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
350 errt = strerror(errno);
354 QMessageBox::critical(dhcpcdQt, tr("Error setting wireless properties"),
358 void DhcpcdWi::scan()
362 i = dhcpcd_wpa_if(wpa);
363 if (!i->up || dhcpcd_wpa_can_background_scan(wpa))
364 dhcpcd_wpa_scan(wpa);
367 void DhcpcdWi::menuHidden()
372 scanTimer->start(DHCPCD_WPA_SCAN_LONG);
376 void DhcpcdWi::menuShown()
381 scanTimer->start(DHCPCD_WPA_SCAN_SHORT);