Use an icon if using >=Qt-5.9 in notificaions.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-qt.cpp
1 /*
2  * dhcpcd-qt
3  * Copyright 2014-2017 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 = DHC_UNKNOWN;
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         if (ssidMenu) {
85                 ssidMenu->setVisible(false);
86                 ssidMenu->deleteLater();
87         }
88
89         if (con != NULL) {
90                 dhcpcd_close(con);
91                 dhcpcd_free(con);
92         }
93
94         for (auto &wi : *wis)
95                 wi->deleteLater();
96         delete wis;
97 }
98
99 DHCPCD_CONNECTION *DhcpcdQt::getConnection()
100 {
101
102         return con;
103 }
104
105 QList<DhcpcdWi *> *DhcpcdQt::getWis()
106 {
107
108         return wis;
109 }
110
111 const char * DhcpcdQt::signalStrengthIcon(DHCPCD_WI_SCAN *scan)
112 {
113
114         if (scan == NULL)
115                 return "network-wireless-connected-00";
116         if (scan->strength.value > 80)
117                 return "network-wireless-connected-100";
118         if (scan->strength.value > 55)
119                 return "network-wireless-connected-75";
120         if (scan->strength.value > 30)
121                 return "network-wireless-connected-50";
122         if (scan->strength.value > 5)
123                 return "network-wireless-connected-25";
124         return "network-wireless-connected-00";
125 }
126
127 DHCPCD_WI_SCAN * DhcpcdQt::getStrongestSignal()
128 {
129         DHCPCD_WI_SCAN *scan, *scans, *s;
130         DHCPCD_WPA *wpa;
131         DHCPCD_IF *i;
132
133         scan = NULL;
134         for (auto &wi : *wis) {
135                 wpa = wi->getWpa();
136                 i = dhcpcd_wpa_if(wpa);
137                 scans = wi->getScans();
138                 for (s = scans; s; s = s->next) {
139                         if (dhcpcd_wi_associated(i, s) &&
140                             (scan == NULL ||
141                             s->strength.value > scan->strength.value))
142                                 scan = s;
143                 }
144         }
145         return scan;
146 }
147
148 void DhcpcdQt::animate()
149 {
150         const char *icon;
151         DHCPCD_WI_SCAN *scan;
152
153         scan = getStrongestSignal();
154
155         if (onLine) {
156                 if (aniCounter++ > 6) {
157                         aniTimer->stop();
158                         aniCounter = 0;
159                         return;
160                 }
161
162                 if (aniCounter % 2 == 0)
163                         icon = scan ? "network-wireless-connected-00" :
164                             "network-idle";
165                 else
166                         icon = scan ? DhcpcdQt::signalStrengthIcon(scan) :
167                             "network-transmit-receive";
168         } else {
169                 if (scan) {
170                         switch(aniCounter++) {
171                         case 0:
172                                 icon = "network-wireless-connected-00";
173                                 break;
174                         case 1:
175                                 icon = "network-wireless-connected-25";
176                                 break;
177                         case 2:
178                                 icon = "network-wireless-connected-50";
179                                 break;
180                         case 3:
181                                 icon = "network-wireless-connected-75";
182                                 break;
183                         default:
184                                 icon = "network-wireless-connected-100";
185                                 aniCounter = 0;
186                         }
187                 } else {
188                         switch(aniCounter++) {
189                         case 0:
190                                 icon = "network-transmit";
191                                 break;
192                         case 1:
193                                 icon = "network-receive";
194                                 break;
195                         default:
196                                 icon = "network-idle";
197                                 aniCounter = 0;
198                         }
199                 }
200         }
201
202         setIcon("status", icon);
203 }
204
205 void DhcpcdQt::updateOnline(bool showIf)
206 {
207         bool isOn, isCarrier;
208         char *msg;
209         DHCPCD_IF *ifs, *i;
210         QString msgs;
211
212         isOn = isCarrier = false;
213         ifs = dhcpcd_interfaces(con);
214         for (i = ifs; i; i = i->next) {
215                 if (i->type == DHT_LINK) {
216                         if (i->up)
217                                 isCarrier = true;
218                 } else {
219                         if (i->up)
220                                 isOn = true;
221                 }
222                 msg = dhcpcd_if_message(i, NULL);
223                 if (msg) {
224                         if (showIf)
225                                 qDebug("%s", msg);
226                         if (msgs.isEmpty())
227                                 msgs = QString::fromLatin1(msg);
228                         else
229                                 msgs += '\n' + QString::fromLatin1(msg);
230                         free(msg);
231                 } else if (showIf)
232                         qDebug("%s: %s", i->ifname, i->reason);
233         }
234
235         if (onLine != isOn || carrier != isCarrier) {
236                 onLine = isOn;
237                 carrier = isCarrier;
238                 aniTimer->stop();
239                 aniCounter = 0;
240                 if (isOn) {
241                         animate();
242                         aniTimer->start(300);
243                 } else if (isCarrier) {
244                         animate();
245                         aniTimer->start(500);
246                 } else
247                         setIcon("status", "network-offline");
248         }
249
250         trayIcon->setToolTip(msgs);
251 }
252
253 void DhcpcdQt::statusCallback(unsigned int status, const char *status_msg)
254 {
255
256         qDebug("Status changed to %s", status_msg);
257         if (status == DHC_DOWN) {
258                 aniTimer->stop();
259                 aniCounter = 0;
260                 onLine = carrier = false;
261                 setIcon("status", "network-offline");
262                 trayIcon->setToolTip(tr("Not connected to dhcpcd"));
263                 /* Close down everything */
264                 if (notifier) {
265                         notifier->setEnabled(false);
266                         notifier->deleteLater();
267                         notifier = NULL;
268                 }
269                 if (ssidMenu) {
270                         ssidMenu->deleteLater();
271                         ssidMenu = NULL;
272                 }
273                 preferencesAction->setEnabled(false);
274                 if (preferences) {
275                         preferences->deleteLater();
276                         preferences = NULL;
277                 }
278         } else {
279                 bool refresh;
280
281                 if (lastStatus == DHC_UNKNOWN || lastStatus == DHC_DOWN) {
282                         qDebug("Connected to dhcpcd-%s", dhcpcd_version(con));
283                         refresh = true;
284                 } else
285                         refresh = lastStatus == DHC_OPENED ? true : false;
286                 updateOnline(refresh);
287         }
288
289         lastStatus = status;
290
291         if (status == DHC_DOWN) {
292                 if (retryOpenTimer == NULL) {
293                         retryOpenTimer = new QTimer(this);
294                         connect(retryOpenTimer, SIGNAL(timeout()),
295                             this, SLOT(tryOpen()));
296                         retryOpenTimer->start(DHCPCD_RETRYOPEN);
297                 }
298         }
299 }
300
301 void DhcpcdQt::dhcpcd_status_cb(_unused DHCPCD_CONNECTION *con,
302     unsigned int status, const char *status_msg, void *d)
303 {
304         DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
305
306         dhcpcdQt->statusCallback(status, status_msg);
307 }
308
309 void DhcpcdQt::ifCallback(DHCPCD_IF *i)
310 {
311         char *msg;
312         bool new_msg;
313
314         if (i->state == DHS_RENEW ||
315             i->state == DHS_STOP || i->state == DHS_STOPPED)
316         {
317                 msg = dhcpcd_if_message(i, &new_msg);
318                 if (msg) {
319                         qDebug("%s", msg);
320                         if (new_msg) {
321                                 QString t = tr("Network Event");
322                                 QString m = msg;
323                                 QString icon;
324
325                                 if (i->up)
326                                         icon = "network-transmit-receive";
327                                 //else
328                                 //      icon = "network-transmit";
329                                 if (!i->up)
330                                         icon = "network-offline";
331                                 notify(t, m, icon);
332                         }
333                         free(msg);
334                 }
335         }
336
337         updateOnline(false);
338
339         if (i->wireless) {
340                 for (auto &wi : *wis) {
341                         DHCPCD_WPA *wpa = wi->getWpa();
342                         if (dhcpcd_wpa_if(wpa) == i) {
343                                 DHCPCD_WI_SCAN *scans;
344
345                                 scans = dhcpcd_wi_scans(i);
346                                 processScans(wi, scans);
347                         }
348                 }
349         }
350 }
351
352 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
353 {
354         DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
355
356         dhcpcdQt->ifCallback(i);
357 }
358
359 DhcpcdWi *DhcpcdQt::findWi(DHCPCD_WPA *wpa)
360 {
361
362         for (auto &wi : *wis) {
363                 if (wi->getWpa() == wpa)
364                         return wi;
365         }
366         return NULL;
367 }
368
369 void DhcpcdQt::processScans(DhcpcdWi *wi, DHCPCD_WI_SCAN *scans)
370 {
371         DHCPCD_WI_SCAN *s1, *s2;
372
373         QString title = tr("New Access Point");
374         QString txt;
375         for (s1 = scans; s1; s1 = s1->next) {
376                 for (s2 = wi->getScans(); s2; s2 = s2->next) {
377                         if (strcmp(s1->ssid, s2->ssid) == 0)
378                                 break;
379                 }
380                 if (s2 == NULL) {
381                         if (!txt.isEmpty()) {
382                                 title = tr("New Access Points");
383                                 txt += '\n';
384                         }
385                         txt += s1->ssid;
386                 }
387         }
388         if (!txt.isEmpty() &&
389             (ssidMenu == NULL || !ssidMenu->isVisible()))
390                 notify(title, txt, "network-wireless");
391
392         if (wi->setScans(scans) && ssidMenu && ssidMenu->isVisible())
393                 ssidMenu->popup(ssidMenuPos);
394 }
395
396 void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
397 {
398         DHCPCD_WI_SCAN *scans;
399         int fd = dhcpcd_wpa_get_fd(wpa);
400         DhcpcdWi *wi;
401
402         wi = findWi(wpa);
403         if (fd == -1) {
404                 qCritical("No fd for WPA");
405                 if (wi) {
406                         wis->removeOne(wi);
407                         wi->close();
408                         wi->deleteLater();
409                 }
410                 return;
411         }
412
413         DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
414         if (i == NULL) {
415                 qCritical("No interface for WPA");
416                 if (wi) {
417                         wis->removeOne(wi);
418                         wi->close();
419                         wi->deleteLater();
420                 }
421                 return;
422         }
423
424         qDebug("%s: Received scan results", i->ifname);
425         scans = dhcpcd_wi_scans(i);
426         if (wi == NULL) {
427                 wi = new DhcpcdWi(this, wpa);
428                 if (wi->open()) {
429                         wis->append(wi);
430                         wi->setScans(scans);
431                 } else {
432                         wi->close();
433                         wi->deleteLater();
434                 }
435         } else
436                 processScans(wi, scans);
437
438         if (!aniTimer->isActive()) {
439                 DHCPCD_WI_SCAN *scan;
440                 const char *icon;
441
442                 scan = getStrongestSignal();
443                 if (scan)
444                         icon = DhcpcdQt::signalStrengthIcon(scan);
445                 else if (onLine)
446                         icon = "network-transmit-receive";
447                 else
448                         icon = "network-offline";
449
450                 setIcon("status", icon);
451         }
452 }
453
454 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
455 {
456         DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
457
458         dhcpcdQt->scanCallback(wpa);
459 }
460
461 void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa,
462     unsigned int status, const char *status_msg)
463 {
464         DHCPCD_IF *i;
465
466         i = dhcpcd_wpa_if(wpa);
467         qDebug("%s: WPA status %s", i->ifname, status_msg);
468         if (status == DHC_DOWN) {
469                 DhcpcdWi *wi = findWi(wpa);
470                 if (wi) {
471                         wis->removeOne(wi);
472                         wi->close();
473                         wi->deleteLater();
474                 }
475         }
476 }
477
478 void DhcpcdQt::dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa,
479     unsigned int status, const char *status_msg, void *d)
480 {
481         DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
482
483         dhcpcdQt->wpaStatusCallback(wpa, status, status_msg);
484 }
485
486 void DhcpcdQt::tryOpen() {
487         int fd = dhcpcd_open(con, true);
488         static int last_error;
489
490         if (fd == -1) {
491                 if (errno == EACCES || errno == EPERM) {
492                         if ((fd = dhcpcd_open(con, false)) != -1)
493                                 goto unprived;
494                 }
495                 if (errno != last_error) {
496                         last_error = errno;
497                         const char *errt = strerror(errno);
498                         qCritical("dhcpcd_open: %s", errt);
499                         trayIcon->setToolTip(
500                             tr("Error connecting to dhcpcd: %1").arg(errt));
501                 }
502                 if (retryOpenTimer == NULL) {
503                         retryOpenTimer = new QTimer(this);
504                         connect(retryOpenTimer, SIGNAL(timeout()),
505                             this, SLOT(tryOpen()));
506                         retryOpenTimer->start(DHCPCD_RETRYOPEN);
507                 }
508                 return;
509         }
510
511 unprived:
512         /* Start listening to WPA events */
513         dhcpcd_wpa_start(con);
514
515         if (retryOpenTimer) {
516                 retryOpenTimer->stop();
517                 retryOpenTimer->deleteLater();
518                 retryOpenTimer = NULL;
519         }
520
521         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
522         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
523
524         preferencesAction->setEnabled(dhcpcd_privileged(con));
525 }
526
527 void DhcpcdQt::dispatch()
528 {
529
530         dhcpcd_dispatch(con);
531 }
532
533 void DhcpcdQt::notify(const QString &title, const QString &msg,
534     const QString &icon)
535 {
536
537 #ifdef NOTIFY
538         KNotification *n = new KNotification("event", this);
539         n->setTitle(title);
540         n->setText(msg);
541         n->sendEvent();
542 #else
543 #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
544         const QIcon i = getIcon("status", icon);
545 #else
546         QSystemTrayIcon::MessageIcon i = QSystemTrayIcon::Information;
547
548         if (icon.compare("network-offline") == 0)
549                 i = QSystemTrayIcon::Warning;
550 #endif
551         trayIcon->showMessage(title, msg, i);
552 #endif
553 }
554
555 void DhcpcdQt::closeEvent(QCloseEvent *event)
556 {
557
558         if (trayIcon->isVisible()) {
559                 hide();
560                 event->ignore();
561         }
562 }
563
564 QIcon DhcpcdQt::getIcon(QString category, QString name)
565 {
566         QIcon icon;
567
568         if (QIcon::hasThemeIcon(name))
569                 icon = QIcon::fromTheme(name);
570         else
571                 icon = QIcon(ICONDIR "/hicolor/scalable/" + category + "/" + name + ".svg");
572
573         return icon;
574 }
575
576 void DhcpcdQt::setIcon(QString category, QString name)
577 {
578         QIcon icon = getIcon(category, name);
579
580         trayIcon->setIcon(icon);
581 }
582
583 QIcon DhcpcdQt::icon()
584 {
585
586         return getIcon("status", "network-transmit-receive");
587 }
588
589 void DhcpcdQt::menuDeleted(QMenu *menu)
590 {
591
592         if (ssidMenu == menu)
593                 ssidMenu = NULL;
594 }
595
596 void DhcpcdQt::createSsidMenu()
597 {
598
599         if (ssidMenu) {
600                 ssidMenu->deleteLater();
601                 ssidMenu = NULL;
602         }
603         if (wis->size() == 0)
604                 return;
605
606         ssidMenu = new QMenu(this);
607         if (wis->size() == 1)
608                 wis->first()->createMenu(ssidMenu);
609         else {
610                 for (auto &wi : *wis)
611                         ssidMenu->addMenu(wi->createIfMenu(ssidMenu));
612         }
613         ssidMenuPos = QCursor::pos();
614         ssidMenu->popup(ssidMenuPos);
615 }
616
617 void DhcpcdQt::iconActivated(QSystemTrayIcon::ActivationReason reason)
618 {
619
620         if (reason == QSystemTrayIcon::Trigger)
621                 createSsidMenu();
622 }
623
624 void DhcpcdQt::dialogClosed(QDialog *dialog)
625 {
626
627         if (dialog == about)
628                 about = NULL;
629         else if (dialog == preferences)
630                 preferences = NULL;
631 }
632
633 void DhcpcdQt::showPreferences()
634 {
635
636         if (preferences == NULL) {
637                 preferences = new DhcpcdPreferences(this);
638                 preferences->show();
639         } else
640                 preferences->activateWindow();
641 }
642
643 void DhcpcdQt::showAbout()
644 {
645
646         if (about == NULL) {
647                 about = new DhcpcdAbout(this);
648                 about->show();
649         } else
650                 about->activateWindow();
651 }
652
653 void DhcpcdQt::createActions()
654 {
655
656         preferencesAction = new QAction(tr("&Preferences"), this);
657         preferencesAction->setIcon(QIcon::fromTheme("preferences-system-network"));
658         preferencesAction->setEnabled(false);
659         connect(preferencesAction, SIGNAL(triggered()),
660             this, SLOT(showPreferences()));
661
662         aboutAction = new QAction(tr("&About"), this);
663         aboutAction->setIcon(QIcon::fromTheme("help-about"));
664         connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
665
666         quitAction = new QAction(tr("&Quit"), this);
667         quitAction->setIcon(QIcon::fromTheme("application-exit"));
668         connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
669
670 }
671
672 void DhcpcdQt::createTrayIcon()
673 {
674
675         trayIconMenu = new QMenu(this);
676         trayIconMenu->addAction(preferencesAction);
677         trayIconMenu->addSeparator();
678         trayIconMenu->addAction(aboutAction);
679         trayIconMenu->addAction(quitAction);
680
681         trayIcon = new QSystemTrayIcon(this);
682         setIcon("status", "network-offline");
683         trayIcon->setContextMenu(trayIconMenu);
684
685         connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
686             this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
687
688         trayIcon->show();
689 }