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