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 for (scan = scans; scan; scan = scan->next) {
97 QAction *before = NULL;
99 lst = menu->findChildren<DhcpcdSsidMenu*>();
100 foreach(DhcpcdSsidMenu *sm, lst) {
101 DHCPCD_WI_SCAN *s = sm->getScan();
102 if (memcmp(scan->ssid, s->ssid,
103 sizeof(scan->ssid)) == 0)
109 if (dhcpcd_wi_scan_compare(scan, s) < 0)
114 createMenuItem(menu, scan, before);
119 lst = menu->findChildren<DhcpcdSsidMenu*>();
120 foreach(DhcpcdSsidMenu *sm, lst) {
121 DHCPCD_WI_SCAN *s = sm->getScan();
122 for (scan = scans; scan; scan = scan->next) {
123 if (memcmp(scan->ssid, s->ssid,
124 sizeof(scan->ssid)) == 0)
128 menu->removeAction(sm);
134 dhcpcd_wi_scans_free(this->scans);
137 return !(changed == 0);
140 void DhcpcdWi::createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan,
143 DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, this, scan);
144 menu->insertAction(before, ssidMenu);
145 connect(ssidMenu, SIGNAL(triggered(DHCPCD_WI_SCAN *)),
146 this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
149 void DhcpcdWi::createMenu1(QMenu *menu)
151 DHCPCD_WI_SCAN *scan;
153 for (scan = scans; scan; scan = scan->next)
154 createMenuItem(menu, scan);
157 void DhcpcdWi::createMenu(QMenu *menu)
164 QMenu *DhcpcdWi::createIfMenu(QMenu *parent)
169 ifp = dhcpcd_wpa_if(wpa);
170 menu = new DhcpcdIfMenu(ifp, parent);
171 icon = DhcpcdQt::getIcon("devices", "network-wireless");
177 void DhcpcdWi::wpaOpen()
179 int fd = dhcpcd_wpa_open(wpa);
180 static int last_error;
183 if (errno != last_error) {
185 qCritical("%s: dhcpcd_wpa_open: %s",
186 dhcpcd_wpa_if(wpa)->ifname,
187 strerror(last_error));
192 notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
193 connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
194 if (retryOpenTimer) {
195 delete retryOpenTimer;
196 retryOpenTimer = NULL;
200 void DhcpcdWi::dispatch()
203 if (dhcpcd_wpa_get_fd(wpa) == -1) {
206 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
208 strcmp(i->reason, "DEPARTED") == 0 ||
209 strcmp(i->reason, "STOPPED") == 0)
213 qPrintable(tr("dhcpcd WPA connection lost")));
214 if (retryOpenTimer == NULL) {
215 retryOpenTimer = new QTimer(this);
216 connect(retryOpenTimer, SIGNAL(timeout()),
217 this, SLOT(wpaOpen()));
218 retryOpenTimer->start(DHCPCD_RETRYOPEN);
223 dhcpcd_wpa_dispatch(wpa);
226 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
231 /* Take a copy of scan incase it's destroyed by a scan update */
232 memcpy(&s, scan, sizeof(s));
235 if (s.flags & WSF_PSK) {
237 QString pwd = QInputDialog::getText(dhcpcdQt, s.ssid,
238 tr("Pre Shared key"), QLineEdit::Normal, NULL, &ok);
241 if (pwd.isNull() || pwd.isEmpty())
242 err = dhcpcd_wpa_select(wpa, &s);
244 err = dhcpcd_wpa_configure(wpa, &s, pwd.toAscii());
246 err = dhcpcd_wpa_configure(wpa, &s, NULL);
251 case DHCPCD_WPA_SUCCESS:
253 case DHCPCD_WPA_ERR_DISCONN:
254 errt = tr("Failed to disconnect.");
256 case DHCPCD_WPA_ERR_RECONF:
257 errt = tr("Faile to reconfigure.");
259 case DHCPCD_WPA_ERR_SET:
260 errt = tr("Failed to set key management.");
262 case DHCPCD_WPA_ERR_SET_PSK:
263 errt = tr("Failed to set password, probably too short.");
265 case DHCPCD_WPA_ERR_ENABLE:
266 errt = tr("Failed to enable the network.");
268 case DHCPCD_WPA_ERR_SELECT:
269 errt = tr("Failed to select the network.");
271 case DHCPCD_WPA_ERR_ASSOC:
272 errt = tr("Failed to start association.");
274 case DHCPCD_WPA_ERR_WRITE:
275 errt = tr("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
278 errt = strerror(errno);
282 QMessageBox::critical(dhcpcdQt, tr("Error setting wireless properties"),