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