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