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