3 * Copyright 2014-2015 Roy Marples <roy@marples.name>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 #include <QSocketNotifier>
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"
44 #include <knotification.h>
53 onLine = carrier = false;
54 lastStatus = DHC_UNKNOWN;
55 aniTimer = new QTimer(this);
56 connect(aniTimer, SIGNAL(timeout()), this, SLOT(animate()));
58 retryOpenTimer = NULL;
63 wis = new QList<DhcpcdWi *>();
66 qDebug("%s", "Connecting ...");
69 qCritical("libdhcpcd: %s", strerror(errno));
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);
85 ssidMenu->setVisible(false);
86 ssidMenu->deleteLater();
99 DHCPCD_CONNECTION *DhcpcdQt::getConnection()
105 QList<DhcpcdWi *> *DhcpcdQt::getWis()
111 const char * DhcpcdQt::signalStrengthIcon(DHCPCD_WI_SCAN *scan)
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";
127 DHCPCD_WI_SCAN * DhcpcdQt::getStrongestSignal()
129 DHCPCD_WI_SCAN *scan, *scans, *s;
134 for (auto &wi : *wis) {
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) &&
141 s->strength.value > scan->strength.value))
148 void DhcpcdQt::animate()
151 DHCPCD_WI_SCAN *scan;
153 scan = getStrongestSignal();
156 if (aniCounter++ > 6) {
162 if (aniCounter % 2 == 0)
163 icon = scan ? "network-wireless-connected-00" :
166 icon = scan ? DhcpcdQt::signalStrengthIcon(scan) :
167 "network-transmit-receive";
170 switch(aniCounter++) {
172 icon = "network-wireless-connected-00";
175 icon = "network-wireless-connected-25";
178 icon = "network-wireless-connected-50";
181 icon = "network-wireless-connected-75";
184 icon = "network-wireless-connected-100";
188 switch(aniCounter++) {
190 icon = "network-transmit";
193 icon = "network-receive";
196 icon = "network-idle";
202 setIcon("status", icon);
205 void DhcpcdQt::updateOnline(bool showIf)
207 bool isOn, isCarrier;
212 isOn = isCarrier = false;
213 ifs = dhcpcd_interfaces(con);
214 for (i = ifs; i; i = i->next) {
215 if (i->type == DHT_LINK) {
222 msg = dhcpcd_if_message(i, NULL);
227 msgs = QString::fromLatin1(msg);
229 msgs += '\n' + QString::fromLatin1(msg);
232 qDebug() << i->ifname << i->reason;
235 if (onLine != isOn || carrier != isCarrier) {
242 aniTimer->start(300);
243 } else if (isCarrier) {
245 aniTimer->start(500);
247 setIcon("status", "network-offline");
250 trayIcon->setToolTip(msgs);
253 void DhcpcdQt::statusCallback(unsigned int status, const char *status_msg)
256 qDebug("Status changed to %s", status_msg);
257 if (status == DHC_DOWN) {
260 onLine = carrier = false;
261 setIcon("status", "network-offline");
262 trayIcon->setToolTip(tr("Not connected to dhcpcd"));
263 /* Close down everything */
265 notifier->setEnabled(false);
266 notifier->deleteLater();
270 ssidMenu->deleteLater();
273 preferencesAction->setEnabled(false);
275 preferences->deleteLater();
281 if (lastStatus == DHC_UNKNOWN || lastStatus == DHC_DOWN) {
282 qDebug("Connected to dhcpcd-%s", dhcpcd_version(con));
285 refresh = lastStatus == DHC_OPENED ? true : false;
286 updateOnline(refresh);
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);
301 void DhcpcdQt::dhcpcd_status_cb(_unused DHCPCD_CONNECTION *con,
302 unsigned int status, const char *status_msg, void *d)
304 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
306 dhcpcdQt->statusCallback(status, status_msg);
309 void DhcpcdQt::ifCallback(DHCPCD_IF *i)
314 if (i->state == DHS_RENEW ||
315 i->state == DHS_STOP || i->state == DHS_STOPPED)
317 msg = dhcpcd_if_message(i, &new_msg);
321 QSystemTrayIcon::MessageIcon icon =
322 i->up ? QSystemTrayIcon::Information :
323 QSystemTrayIcon::Warning;
324 QString t = tr("Network Event");
335 for (auto &wi : *wis) {
336 DHCPCD_WPA *wpa = wi->getWpa();
337 if (dhcpcd_wpa_if(wpa) == i) {
338 DHCPCD_WI_SCAN *scans;
340 scans = dhcpcd_wi_scans(i);
341 processScans(wi, scans);
347 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
349 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
351 dhcpcdQt->ifCallback(i);
354 DhcpcdWi *DhcpcdQt::findWi(DHCPCD_WPA *wpa)
357 for (auto &wi : *wis) {
358 if (wi->getWpa() == wpa)
364 void DhcpcdQt::processScans(DhcpcdWi *wi, DHCPCD_WI_SCAN *scans)
366 DHCPCD_WI_SCAN *s1, *s2;
368 QString title = tr("New Access Point");
370 for (s1 = scans; s1; s1 = s1->next) {
371 for (s2 = wi->getScans(); s2; s2 = s2->next) {
372 if (strcmp(s1->ssid, s2->ssid) == 0)
376 if (!txt.isEmpty()) {
377 title = tr("New Access Points");
383 if (!txt.isEmpty() &&
384 (ssidMenu == NULL || !ssidMenu->isVisible()))
387 if (wi->setScans(scans) && ssidMenu && ssidMenu->isVisible())
388 ssidMenu->popup(ssidMenuPos);
391 void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
393 DHCPCD_WI_SCAN *scans;
394 int fd = dhcpcd_wpa_get_fd(wpa);
399 qCritical("No fd for WPA");
408 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
410 qCritical("No interface for WPA");
419 qDebug("%s: Received scan results", i->ifname);
420 scans = dhcpcd_wi_scans(i);
422 wi = new DhcpcdWi(this, wpa);
431 processScans(wi, scans);
433 if (!aniTimer->isActive()) {
434 DHCPCD_WI_SCAN *scan;
437 scan = getStrongestSignal();
439 icon = DhcpcdQt::signalStrengthIcon(scan);
441 icon = "network-transmit-receive";
443 icon = "network-offline";
445 setIcon("status", icon);
449 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
451 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
453 dhcpcdQt->scanCallback(wpa);
456 void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa,
457 unsigned int status, const char *status_msg)
461 i = dhcpcd_wpa_if(wpa);
462 qDebug("%s: WPA status %s", i->ifname, status_msg);
463 if (status == DHC_DOWN) {
464 DhcpcdWi *wi = findWi(wpa);
473 void DhcpcdQt::dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa,
474 unsigned int status, const char *status_msg, void *d)
476 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
478 dhcpcdQt->wpaStatusCallback(wpa, status, status_msg);
481 void DhcpcdQt::tryOpen() {
482 int fd = dhcpcd_open(con, true);
483 static int last_error;
486 if (errno == EACCES || errno == EPERM) {
487 if ((fd = dhcpcd_open(con, false)) != -1)
490 if (errno != last_error) {
492 const char *errt = strerror(errno);
493 qCritical("dhcpcd_open: %s", errt);
494 trayIcon->setToolTip(
495 tr("Error connecting to dhcpcd: %1").arg(errt));
497 if (retryOpenTimer == NULL) {
498 retryOpenTimer = new QTimer(this);
499 connect(retryOpenTimer, SIGNAL(timeout()),
500 this, SLOT(tryOpen()));
501 retryOpenTimer->start(DHCPCD_RETRYOPEN);
507 /* Start listening to WPA events */
508 dhcpcd_wpa_start(con);
510 if (retryOpenTimer) {
511 retryOpenTimer->stop();
512 retryOpenTimer->deleteLater();
513 retryOpenTimer = NULL;
516 notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
517 connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
519 preferencesAction->setEnabled(dhcpcd_privileged(con));
522 void DhcpcdQt::dispatch()
525 dhcpcd_dispatch(con);
528 void DhcpcdQt::notify(QString &title, QString &msg,
530 QSystemTrayIcon::MessageIcon
532 QSystemTrayIcon::MessageIcon icon
538 KNotification *n = new KNotification("event", this);
546 //trayIcon->showMessage(title, msg, icon);
550 void DhcpcdQt::closeEvent(QCloseEvent *event)
553 if (trayIcon->isVisible()) {
559 QIcon DhcpcdQt::getIcon(QString category, QString name)
563 if (QIcon::hasThemeIcon(name))
564 icon = QIcon::fromTheme(name);
566 icon = QIcon(ICONDIR "/hicolor/scalable/" + category + "/" + name + ".svg");
571 void DhcpcdQt::setIcon(QString category, QString name)
573 QIcon icon = getIcon(category, name);
575 trayIcon->setIcon(icon);
578 QIcon DhcpcdQt::icon()
581 return getIcon("status", "network-transmit-receive");
584 void DhcpcdQt::menuDeleted(QMenu *menu)
587 if (ssidMenu == menu)
591 void DhcpcdQt::createSsidMenu()
595 ssidMenu->deleteLater();
598 if (wis->size() == 0)
601 ssidMenu = new QMenu(this);
602 if (wis->size() == 1)
603 wis->first()->createMenu(ssidMenu);
605 for (auto &wi : *wis)
606 ssidMenu->addMenu(wi->createIfMenu(ssidMenu));
608 ssidMenuPos = QCursor::pos();
609 ssidMenu->popup(ssidMenuPos);
612 void DhcpcdQt::iconActivated(QSystemTrayIcon::ActivationReason reason)
615 if (reason == QSystemTrayIcon::Trigger)
619 void DhcpcdQt::dialogClosed(QDialog *dialog)
624 else if (dialog == preferences)
628 void DhcpcdQt::showPreferences()
631 if (preferences == NULL) {
632 preferences = new DhcpcdPreferences(this);
635 preferences->activateWindow();
638 void DhcpcdQt::showAbout()
642 about = new DhcpcdAbout(this);
645 about->activateWindow();
648 void DhcpcdQt::createActions()
651 preferencesAction = new QAction(tr("&Preferences"), this);
652 preferencesAction->setIcon(QIcon::fromTheme("preferences-system-network"));
653 preferencesAction->setEnabled(false);
654 connect(preferencesAction, SIGNAL(triggered()),
655 this, SLOT(showPreferences()));
657 aboutAction = new QAction(tr("&About"), this);
658 aboutAction->setIcon(QIcon::fromTheme("help-about"));
659 connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
661 quitAction = new QAction(tr("&Quit"), this);
662 quitAction->setIcon(QIcon::fromTheme("application-exit"));
663 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
667 void DhcpcdQt::createTrayIcon()
670 trayIconMenu = new QMenu(this);
671 trayIconMenu->addAction(preferencesAction);
672 trayIconMenu->addSeparator();
673 trayIconMenu->addAction(aboutAction);
674 trayIconMenu->addAction(quitAction);
676 trayIcon = new QSystemTrayIcon(this);
677 setIcon("status", "network-offline");
678 trayIcon->setContextMenu(trayIconMenu);
680 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
681 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));