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