3 * Copyright 2014 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-ssidmenu.h"
45 DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
48 this->dhcpcdQt = parent;
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;
72 dhcpcd_wi_scans_free(scans);
75 DHCPCD_WPA *DhcpcdWi::getWpa()
81 DHCPCD_WI_SCAN *DhcpcdWi::getScans()
87 bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
92 QList<DhcpcdSsidMenu*> lst;
95 lst = menu->findChildren<DhcpcdSsidMenu*>();
96 for (scan = scans; scan; scan = scan->next) {
99 foreach(DhcpcdSsidMenu *sm, lst) {
100 DHCPCD_WI_SCAN *s = sm->getScan();
101 if (memcmp(scan->bssid, s->bssid,
102 sizeof(scan->bssid)) == 0)
111 createMenuItem(menu, scan);
116 foreach(DhcpcdSsidMenu *sm, lst) {
117 DHCPCD_WI_SCAN *s = sm->getScan();
118 for (scan = scans; scan; scan = scan->next) {
119 if (memcmp(scan->bssid, s->bssid,
120 sizeof(scan->bssid)) == 0)
124 menu->removeAction(sm);
130 dhcpcd_wi_scans_free(this->scans);
133 return !(changed == 0);
136 void DhcpcdWi::createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan)
138 DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, this, scan);
139 menu->addAction(ssidMenu);
140 connect(ssidMenu, SIGNAL(triggered(DHCPCD_WI_SCAN *)),
141 this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
144 void DhcpcdWi::createMenu1(QMenu *menu)
146 DHCPCD_WI_SCAN *scan;
148 for (scan = scans; scan; scan = scan->next)
149 createMenuItem(menu, scan);
152 void DhcpcdWi::createMenu(QMenu *menu)
159 QMenu *DhcpcdWi::createIfMenu(QMenu *parent)
164 ifp = dhcpcd_wpa_if(wpa);
165 menu = new DhcpcdIfMenu(ifp, parent);
166 icon = DhcpcdQt::getIcon("devices", "network-wireless");
172 void DhcpcdWi::wpaOpen()
174 int fd = dhcpcd_wpa_open(wpa);
175 static int last_error;
178 if (errno != last_error) {
180 qCritical("%s: dhcpcd_wpa_open: %s",
181 dhcpcd_wpa_if(wpa)->ifname,
182 strerror(last_error));
187 notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
188 connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
189 if (retryOpenTimer) {
190 delete retryOpenTimer;
191 retryOpenTimer = NULL;
195 void DhcpcdWi::dispatch()
198 if (dhcpcd_wpa_get_fd(wpa) == -1) {
201 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
203 strcmp(i->reason, "DEPARTED") == 0 ||
204 strcmp(i->reason, "STOPPED") == 0)
208 qPrintable(tr("dhcpcd WPA connection lost")));
209 if (retryOpenTimer == NULL) {
210 retryOpenTimer = new QTimer(this);
211 connect(retryOpenTimer, SIGNAL(timeout()),
212 this, SLOT(wpaOpen()));
213 retryOpenTimer->start(DHCPCD_RETRYOPEN);
218 dhcpcd_wpa_dispatch(wpa);
221 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
226 /* Take a copy of scan incase it's destroyed by a scan update */
227 memcpy(&s, scan, sizeof(s));
230 QString psk = QInputDialog::getText(dhcpcdQt, s.ssid,
231 tr("Pre Shared key"), QLineEdit::Normal, NULL, &ok);
238 switch (dhcpcd_wpa_configure_psk(wpa, &s, psk.toAscii())) {
239 case DHCPCD_WPA_SUCCESS:
241 case DHCPCD_WPA_ERR_SET:
242 errt = tr("Failed to set key management.");
244 case DHCPCD_WPA_ERR_SET_PSK:
245 errt = tr("Failed to set password, probably too short.");
247 case DHCPCD_WPA_ERR_ENABLE:
248 errt = tr("Failed to enable the network.");
250 case DHCPCD_WPA_ERR_ASSOC:
251 errt = tr("Failed to start association.");
253 case DHCPCD_WPA_ERR_WRITE:
254 errt = tr("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
257 errt = strerror(errno);
261 QMessageBox::critical(dhcpcdQt, tr("Error setting wireless properties"),