Remove the DhcpcdWi instance if the wpa_supplicant connection is lost.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-qt.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 <QCursor>
28 #include <QDebug>
29 #include <QList>
30 #include <QSocketNotifier>
31 #include <QtGui>
32
33 #include <cerrno>
34
35 #include "config.h"
36 #include "dhcpcd-qt.h"
37 #include "dhcpcd-about.h"
38 #include "dhcpcd-preferences.h"
39 #include "dhcpcd-wi.h"
40 #include "dhcpcd-ifmenu.h"
41 #include "dhcpcd-ssidmenu.h"
42
43 DhcpcdQt::DhcpcdQt()
44 {
45
46         createActions();
47         createTrayIcon();
48
49         onLine = carrier = false;
50         lastStatus = NULL;
51         aniTimer = new QTimer(this);
52         connect(aniTimer, SIGNAL(timeout()), this, SLOT(animate()));
53         notifier = NULL;
54         retryOpenTimer = NULL;
55
56         about = NULL;
57         preferences = NULL;
58
59         wis = new QList<DhcpcdWi *>();
60         ssidMenu = NULL;
61
62         qDebug("%s", "Connecting ...");
63         con = dhcpcd_new();
64         if (con == NULL) {
65                 qCritical("libdhcpcd: %s", strerror(errno));
66                 exit(EXIT_FAILURE);
67                 return;
68         }
69         dhcpcd_set_status_callback(con, dhcpcd_status_cb, this);
70         dhcpcd_set_if_callback(con, dhcpcd_if_cb, this);
71         dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, this);
72         dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, this);
73         tryOpen();
74 }
75
76 DhcpcdQt::~DhcpcdQt()
77 {
78
79         qDeleteAll(*wis);
80         delete wis;
81
82         if (con != NULL) {
83                 dhcpcd_close(con);
84                 dhcpcd_free(con);
85         }
86
87         free(lastStatus);
88 }
89
90 void DhcpcdQt::animate()
91 {
92         const char *icon;
93
94         if (onLine) {
95                 if (aniCounter++ > 6) {
96                         aniTimer->stop();
97                         aniCounter = 0;
98                         return;
99                 }
100
101                 if (aniCounter % 2 == 0)
102                         icon = "network-idle";
103                 else
104                         icon = "network-transmit-receive";
105         } else {
106                 switch(aniCounter++) {
107                 case 0:
108                         icon = "network-transmit";
109                         break;
110                 case 1:
111                         icon = "network-receive";
112                         break;
113                 default:
114                         icon = "network-idle";
115                         aniCounter = 0;
116                 }
117         }
118
119         setIcon("status", icon);
120 }
121
122 void DhcpcdQt::updateOnline(bool showIf)
123 {
124         bool isOn, isCarrier;
125         char *msg;
126         DHCPCD_IF *ifs, *i;
127         QString msgs;
128
129         isOn = isCarrier = false;
130         ifs = dhcpcd_interfaces(con);
131         for (i = ifs; i; i = i->next) {
132                 if (strcmp(i->type, "link") == 0) {
133                         if (i->up)
134                                 isCarrier = true;
135                 } else {
136                         if (i->up)
137                                 isOn = true;
138                 }
139                 msg = dhcpcd_if_message(i, NULL);
140                 if (msg) {
141                         if (showIf)
142                                 qDebug() << msg;
143                         if (msgs.isEmpty())
144                                 msgs = QString::fromAscii(msg);
145                         else
146                                 msgs += '\n' + QString::fromAscii(msg);
147                         free(msg);
148                 } else if (showIf)
149                         qDebug() << i->ifname << i->reason;
150         }
151
152         if (onLine != isOn || carrier != isCarrier) {
153                 onLine = isOn;
154                 carrier = isCarrier;
155                 aniTimer->stop();
156                 aniCounter = 0;
157                 if (isOn) {
158                         animate();
159                         aniTimer->start(300);
160                 } else if (isCarrier) {
161                         animate();
162                         aniTimer->start(500);
163                 } else
164                         setIcon("status", "network-offline");
165         }
166
167         trayIcon->setToolTip(msgs);
168 }
169
170 void DhcpcdQt::statusCallback(const char *status)
171 {
172
173         qDebug("Status changed to %s", status);
174         if (strcmp(status, "down") == 0) {
175                 aniTimer->stop();
176                 aniCounter = 0;
177                 setIcon("status", "network-offline");
178                 if (notifier) {
179                         delete notifier;
180                         notifier = NULL;
181                 }
182                 trayIcon->setToolTip(tr("Not connected to dhcpcd"));
183         } else {
184                 bool refresh;
185
186                 if (lastStatus == NULL || strcmp(lastStatus, "down") == 0) {
187                         qDebug("Connected to dhcpcd-%s", dhcpcd_version(con));
188                         refresh = true;
189                 } else
190                         refresh = strcmp(lastStatus, "opened") ? false : true;
191                 updateOnline(refresh);
192         }
193
194         free(lastStatus);
195         lastStatus = strdup(status);
196
197         if (strcmp(status, "down") == 0) {
198                 if (retryOpenTimer == NULL) {
199                         retryOpenTimer = new QTimer(this);
200                         connect(retryOpenTimer, SIGNAL(timeout()),
201                             this, SLOT(tryOpen()));
202                         retryOpenTimer->start(DHCPCD_RETRYOPEN);
203                 }
204         }
205 }
206
207 void DhcpcdQt::dhcpcd_status_cb(_unused DHCPCD_CONNECTION *con,
208     const char *status, void *d)
209 {
210         DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
211
212         dhcpcdQt->statusCallback(status);
213 }
214
215 void DhcpcdQt::ifCallback(DHCPCD_IF *i)
216 {
217         char *msg;
218         bool new_msg;
219
220         if (strcmp(i->reason, "RENEW") &&
221             strcmp(i->reason, "STOP") &&
222             strcmp(i->reason, "STOPPED"))
223         {
224                 msg = dhcpcd_if_message(i, &new_msg);
225                 if (msg) {
226                         qDebug("%s", msg);
227                         if (new_msg) {
228                                 QSystemTrayIcon::MessageIcon icon =
229                                     i->up ? QSystemTrayIcon::Information :
230                                     QSystemTrayIcon::Warning;
231                                 trayIcon->showMessage(tr("Network Event"),
232                                     msg, icon);
233                         }
234                         free(msg);
235                 }
236         }
237
238         updateOnline(false);
239 }
240
241 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
242 {
243         DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
244
245         dhcpcdQt->ifCallback(i);
246 }
247
248 DhcpcdWi *DhcpcdQt::findWi(DHCPCD_WPA *wpa)
249 {
250
251         for (auto &wi : *wis) {
252                 if (wi->getWpa() == wpa)
253                         return wi;
254         }
255         return NULL;
256 }
257
258 void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
259 {
260         DHCPCD_WI_SCAN *scans, *s1, *s2;
261         int fd = dhcpcd_wpa_get_fd(wpa);
262         DhcpcdWi *wi;
263
264         wi = findWi(wpa);
265         if (fd == -1) {
266                 qCritical("No fd for WPA");
267                 if (wi) {
268                         wis->removeOne(wi);
269                         delete wi;
270                 }
271                 return;
272         }
273
274         DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
275         if (i == NULL) {
276                 qCritical("No interface for WPA");
277                 if (wi) {
278                         wis->removeOne(wi);
279                         delete wi;
280                 }
281                 return;
282         }
283
284         qDebug("%s: Received scan results", i->ifname);
285         scans = dhcpcd_wi_scans(i);
286         if (wi == NULL) {
287                 wi = new DhcpcdWi(this, wpa);
288                 wis->append(wi);
289         } else {
290                 QString title = tr("New Access Point");
291                 QString txt;
292                 for (s1 = scans; s1; s1 = s1->next) {
293                         for (s2 = wi->getScans(); s2; s2 = s2->next) {
294                                 if (strcmp(s1->ssid, s2->ssid) == 0)
295                                         break;
296                         }
297                         if (s2 == NULL) {
298                                 if (!txt.isEmpty()) {
299                                         title = tr("New Access Points");
300                                         txt += '\n';
301                                 }
302                                 txt += s1->ssid;
303                         }
304                 }
305                 if (!txt.isEmpty() &&
306                     (ssidMenu == NULL || !ssidMenu->isVisible()))
307                         notify(title, txt);
308         }
309
310         if (wi->setScans(scans) && ssidMenu->isVisible())
311                 ssidMenu->popup(ssidMenuPos);
312 }
313
314 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
315 {
316         DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
317
318         dhcpcdQt->scanCallback(wpa);
319 }
320
321 void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa, const char *status)
322 {
323         DHCPCD_IF *i;
324
325         i = dhcpcd_wpa_if(wpa);
326         qDebug("%s: WPA status %s", i->ifname, status);
327         if (strcmp(status, "down") == 0) {
328                 DhcpcdWi *wi = findWi(wpa);
329                 if (wi) {
330                         wis->removeOne(wi);
331                         delete wi;
332                 }
333         }
334 }
335
336 void DhcpcdQt::dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status,
337     void *d)
338 {
339         DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
340
341         dhcpcdQt->wpaStatusCallback(wpa, status);
342 }
343
344 void DhcpcdQt::tryOpen() {
345         int fd = dhcpcd_open(con);
346         static int last_error;
347
348         if (fd == -1) {
349                 if (errno != last_error) {
350                         last_error = errno;
351                         qCritical("dhcpcd_open: %s", strerror(errno));
352                 }
353                 if (retryOpenTimer == NULL) {
354                         retryOpenTimer = new QTimer(this);
355                         connect(retryOpenTimer, SIGNAL(timeout()),
356                             this, SLOT(tryOpen()));
357                         retryOpenTimer->start(DHCPCD_RETRYOPEN);
358                 }
359                 return;
360         }
361
362         /* Start listening to WPA events */
363         dhcpcd_wpa_start(con);
364
365         if (retryOpenTimer) {
366                 delete retryOpenTimer;
367                 retryOpenTimer = NULL;
368         }
369
370         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
371         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
372 }
373
374 void DhcpcdQt::dispatch() {
375
376         if (dhcpcd_get_fd(con) == -1) {
377                 qWarning("dhcpcd connection lost");
378                 return;
379         }
380
381         dhcpcd_dispatch(con);
382 }
383
384 void DhcpcdQt::notify(QString &title, QString &msg,
385     QSystemTrayIcon::MessageIcon icon)
386 {
387
388         qDebug("%s", qPrintable(msg));
389         trayIcon->showMessage(title, msg, icon);
390 }
391
392
393 void DhcpcdQt::closeEvent(QCloseEvent *event)
394 {
395
396         if (trayIcon->isVisible()) {
397                 hide();
398                 event->ignore();
399         }
400 }
401
402 QIcon DhcpcdQt::getIcon(QString category, QString name)
403 {
404         QIcon icon;
405
406         if (QIcon::hasThemeIcon(name))
407                 icon = QIcon::fromTheme(name);
408         else
409                 icon = QIcon(ICONDIR "/hicolor/scalable/" + category + "/" + name + ".svg");
410         return icon;
411 }
412
413 void DhcpcdQt::setIcon(QString category, QString name)
414 {
415         QIcon icon = getIcon(category, name);
416
417         trayIcon->setIcon(icon);
418 }
419
420 QIcon DhcpcdQt::icon()
421 {
422
423         return getIcon("status", "network-transmit-receive");
424 }
425
426 void DhcpcdQt::createSsidMenu()
427 {
428
429         if (ssidMenu) {
430                 delete ssidMenu;
431                 ssidMenu = NULL;
432         }
433         if (wis->size() == 0)
434                 return;
435
436         ssidMenu = new QMenu(this);
437         if (wis->size() == 1)
438                 wis->first()->createMenu(ssidMenu);
439         else {
440                 for (auto &wi : *wis)
441                         ssidMenu->addMenu(wi->createIfMenu(ssidMenu));
442         }
443         ssidMenuPos = QCursor::pos();
444         ssidMenu->popup(ssidMenuPos);
445 }
446
447 void DhcpcdQt::iconActivated(QSystemTrayIcon::ActivationReason reason)
448 {
449
450         if (reason == QSystemTrayIcon::Trigger)
451                 createSsidMenu();
452 }
453
454 void DhcpcdQt::dialogClosed(QDialog *dialog)
455 {
456
457         if (dialog == about)
458                 about = NULL;
459         else if (dialog == preferences)
460                 preferences = NULL;
461 }
462
463 void DhcpcdQt::showPreferences()
464 {
465
466         if (preferences == NULL) {
467                 preferences = new DhcpcdPreferences(this);
468                 preferences->show();
469         } else
470                 preferences->activateWindow();
471 }
472
473 void DhcpcdQt::showAbout()
474 {
475
476         if (about == NULL) {
477                 about = new DhcpcdAbout(this);
478                 about->show();
479         } else
480                 about->activateWindow();
481 }
482
483 void DhcpcdQt::createActions()
484 {
485
486         preferencesAction = new QAction(tr("&Preferences"), this);
487         preferencesAction->setIcon(QIcon::fromTheme("preferences-system-network"));
488         connect(preferencesAction, SIGNAL(triggered()),
489             this, SLOT(showPreferences()));
490
491         aboutAction = new QAction(tr("&About"), this);
492         aboutAction->setIcon(QIcon::fromTheme("help-about"));
493         connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
494
495         quitAction = new QAction(tr("&Quit"), this);
496         quitAction->setIcon(QIcon::fromTheme("application-exit"));
497         connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
498
499 }
500
501 void DhcpcdQt::createTrayIcon()
502 {
503
504         trayIconMenu = new QMenu(this);
505         trayIconMenu->addAction(preferencesAction);
506         trayIconMenu->addSeparator();
507         trayIconMenu->addAction(aboutAction);
508         trayIconMenu->addAction(quitAction);
509
510         trayIcon = new QSystemTrayIcon(this);
511         setIcon("status", "network-offline");
512         trayIcon->setContextMenu(trayIconMenu);
513
514         connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
515             this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
516
517         trayIcon->show();
518 }