Fix a crash by clearing our reference to the menuwidget when it is deleted
[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 <QAction>
28 #include <QObject>
29 #include <QInputDialog>
30 #include <QLineEdit>
31 #include <QMenu>
32 #include <QMessageBox>
33 #include <QSocketNotifier>
34 #include <QTimer>
35 #include <QWidgetAction>
36
37 #include <cerrno>
38
39 #include "config.h"
40 #include "dhcpcd-wi.h"
41 #include "dhcpcd-qt.h"
42 #include "dhcpcd-ifmenu.h"
43 #include "dhcpcd-ssidmenu.h"
44
45 DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
46 {
47
48         this->dhcpcdQt = parent;
49         this->wpa = wpa;
50         menu = NULL;
51         scans = NULL;
52
53         notifier = NULL;
54         pingTimer = NULL;
55 }
56
57 DhcpcdWi::~DhcpcdWi()
58 {
59
60         if (menu) {
61                 dhcpcdQt->menuDeleted(menu);
62                 menu->setVisible(false);
63                 menu->deleteLater();
64                 menu = NULL;
65         }
66
67         if (notifier) {
68                 notifier->setEnabled(false);
69                 notifier->deleteLater();
70                 notifier = NULL;
71         }
72
73         if (pingTimer) {
74                 pingTimer->stop();
75                 pingTimer->deleteLater();
76                 pingTimer = NULL;
77         }
78
79         dhcpcd_wi_scans_free(scans);
80 }
81
82 DHCPCD_WPA *DhcpcdWi::getWpa()
83 {
84
85         return wpa;
86 }
87
88 DHCPCD_WI_SCAN *DhcpcdWi::getScans()
89 {
90
91         return scans;
92 }
93
94 bool DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
95 {
96         bool changed = false;
97
98         if (menu) {
99                 QList<DhcpcdSsidMenu*> lst;
100                 DHCPCD_WI_SCAN *scan;
101                 DHCPCD_IF *i;
102                 bool found, associated;
103                 QAction *before;
104
105                 i = dhcpcd_wpa_if(wpa);
106                 for (scan = scans; scan; scan = scan->next) {
107                         found = false;
108                         before = NULL;
109                         associated = dhcpcd_wi_associated(i, scan);
110
111                         lst = menu->findChildren<DhcpcdSsidMenu*>();
112                         foreach(DhcpcdSsidMenu *sm, lst) {
113                                 DHCPCD_WI_SCAN *s = sm->getScan();
114
115                                 if (strcmp(scan->ssid, s->ssid) == 0) {
116                                         /* If association changes, remove
117                                          * the entry and re-create it
118                                          * so assoicates entries appear at
119                                          * the top */
120                                         if (associated != sm->isAssociated()) {
121                                                 menu->removeAction(sm);
122                                                 break;
123                                         }
124                                         sm->setScan(scan);
125                                         found = true;
126                                         break;
127                                 }
128                                 if (!associated &&
129                                     dhcpcd_wi_scan_compare(scan, s) < 0)
130                                         before = sm;
131                         }
132
133                         if (!found) {
134                                 if (associated) {
135                                         lst = menu->findChildren<DhcpcdSsidMenu*>();
136                                         if (lst.empty())
137                                                 before = NULL;
138                                         else
139                                                 before = lst.at(0);
140                                 }
141                                 createMenuItem(menu, scan, before);
142                                 changed = true;
143                         }
144                 }
145
146                 lst = menu->findChildren<DhcpcdSsidMenu*>();
147                 foreach(DhcpcdSsidMenu *sm, lst) {
148                         DHCPCD_WI_SCAN *s = sm->getScan();
149                         for (scan = scans; scan; scan = scan->next) {
150                                 if (strcmp(scan->ssid, s->ssid) == 0)
151                                         break;
152                         }
153                         if (scan == NULL) {
154                                 menu->removeAction(sm);
155                                 changed = true;
156                         }
157                 }
158         }
159
160         dhcpcd_wi_scans_free(this->scans);
161         this->scans = scans;
162
163         return changed;
164 }
165
166 void DhcpcdWi::createMenuItem(QMenu *menu, DHCPCD_WI_SCAN *scan,
167     QAction *before)
168 {
169         DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, this, scan);
170         menu->insertAction(before, ssidMenu);
171         connect(ssidMenu, SIGNAL(triggered(DHCPCD_WI_SCAN *)),
172             this, SLOT(connectSsid(DHCPCD_WI_SCAN *)));
173 }
174
175 void DhcpcdWi::createMenu1(QMenu *menu)
176 {
177         DHCPCD_IF *i;
178         DHCPCD_WI_SCAN *scan;
179         QAction *before;
180
181         i = dhcpcd_wpa_if(wpa);
182         for (scan = scans; scan; scan = scan->next) {
183                 before = NULL;
184                 if (dhcpcd_wi_associated(i, scan)) {
185                         QList<DhcpcdSsidMenu*> lst;
186
187                         lst = menu->findChildren<DhcpcdSsidMenu*>();
188                         if (!lst.empty())
189                                 before = lst.at(0);
190                 }
191                 createMenuItem(menu, scan, before);
192         }
193 }
194
195 void DhcpcdWi::createMenu(QMenu *menu)
196 {
197
198         if (this->menu && this->menu != menu)
199                 this->menu->deleteLater();
200         this->menu = menu;
201         createMenu1(menu);
202 }
203
204 QMenu *DhcpcdWi::createIfMenu(QMenu *parent)
205 {
206         DHCPCD_IF *ifp;
207         QIcon icon;
208
209         ifp = dhcpcd_wpa_if(wpa);
210         if (this->menu)
211                 this->menu->deleteLater();
212         menu = new DhcpcdIfMenu(ifp, parent);
213         icon = DhcpcdQt::getIcon("devices", "network-wireless");
214         menu->setIcon(icon);
215         createMenu1(menu);
216         return menu;
217 }
218
219 bool DhcpcdWi::open()
220 {
221         int fd = dhcpcd_wpa_open(wpa);
222
223         if (fd == -1) {
224                 qCritical("%s: dhcpcd_wpa_open: %s",
225                     dhcpcd_wpa_if(wpa)->ifname,
226                     strerror(errno));
227                 dhcpcd_wpa_close(wpa);
228                 return false;
229         }
230
231         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
232         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
233         pingTimer = new QTimer(this);
234         connect(pingTimer, SIGNAL(timeout()), this, SLOT(ping()));
235         pingTimer->start(DHCPCD_WPA_PING);
236         return true;
237 }
238
239 void DhcpcdWi::dispatch()
240 {
241
242         dhcpcd_wpa_dispatch(wpa);
243 }
244
245 void DhcpcdWi::ping()
246 {
247
248         if (!dhcpcd_wpa_ping(wpa))
249                 dhcpcd_wpa_close(wpa);
250 }
251
252 void DhcpcdWi::connectSsid(DHCPCD_WI_SCAN *scan)
253 {
254         DHCPCD_WI_SCAN s;
255         int err;
256
257         /* Take a copy of scan incase it's destroyed by a scan update */
258         memcpy(&s, scan, sizeof(s));
259         s.next = NULL;
260
261         if (s.flags & WSF_PSK) {
262                 bool ok;
263                 QString pwd = QInputDialog::getText(dhcpcdQt, s.ssid,
264                     tr("Pre Shared key"), QLineEdit::Normal, NULL, &ok);
265                 if (!ok)
266                         return;
267                 if (pwd.isNull() || pwd.isEmpty())
268                         err = dhcpcd_wpa_select(wpa, &s);
269                 else
270                         err = dhcpcd_wpa_configure(wpa, &s, pwd.toAscii());
271         } else
272                 err = dhcpcd_wpa_configure(wpa, &s, NULL);
273
274         QString errt;
275
276         switch (err) {
277         case DHCPCD_WPA_SUCCESS:
278                 return;
279         case DHCPCD_WPA_ERR_DISCONN:
280                 errt = tr("Failed to disconnect.");
281                 break;
282         case DHCPCD_WPA_ERR_RECONF:
283                 errt = tr("Faile to reconfigure.");
284                 break;
285         case DHCPCD_WPA_ERR_SET:
286                 errt = tr("Failed to set key management.");
287                 break;
288         case DHCPCD_WPA_ERR_SET_PSK:
289                 errt = tr("Failed to set password, probably too short.");
290                 break;
291         case DHCPCD_WPA_ERR_ENABLE:
292                 errt = tr("Failed to enable the network.");
293                 break;
294         case DHCPCD_WPA_ERR_SELECT:
295                 errt = tr("Failed to select the network.");
296                 break;
297         case DHCPCD_WPA_ERR_ASSOC:
298                 errt = tr("Failed to start association.");
299                 break;
300         case DHCPCD_WPA_ERR_WRITE:
301                 errt = tr("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
302                 break;
303         default:
304                 errt = strerror(errno);
305                 break;
306         }
307
308         QMessageBox::critical(dhcpcdQt, tr("Error setting wireless properties"),
309             errt);
310 }