3 * Copyright 2014 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;
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);
84 /* This will have already been destroyed,
85 * but the reference may not be. */
100 DHCPCD_CONNECTION *DhcpcdQt::getConnection()
106 QList<DhcpcdWi *> *DhcpcdQt::getWis()
112 void DhcpcdQt::animate()
117 if (aniCounter++ > 6) {
123 if (aniCounter % 2 == 0)
124 icon = "network-idle";
126 icon = "network-transmit-receive";
128 switch(aniCounter++) {
130 icon = "network-transmit";
133 icon = "network-receive";
136 icon = "network-idle";
141 setIcon("status", icon);
144 void DhcpcdQt::updateOnline(bool showIf)
146 bool isOn, isCarrier;
151 isOn = isCarrier = false;
152 ifs = dhcpcd_interfaces(con);
153 for (i = ifs; i; i = i->next) {
154 if (strcmp(i->type, "link") == 0) {
161 msg = dhcpcd_if_message(i, NULL);
166 msgs = QString::fromAscii(msg);
168 msgs += '\n' + QString::fromAscii(msg);
171 qDebug() << i->ifname << i->reason;
174 if (onLine != isOn || carrier != isCarrier) {
181 aniTimer->start(300);
182 } else if (isCarrier) {
184 aniTimer->start(500);
186 setIcon("status", "network-offline");
189 trayIcon->setToolTip(msgs);
192 void DhcpcdQt::statusCallback(const char *status)
195 qDebug("Status changed to %s", status);
196 if (strcmp(status, "down") == 0) {
199 onLine = carrier = false;
200 setIcon("status", "network-offline");
201 trayIcon->setToolTip(tr("Not connected to dhcpcd"));
202 /* Close down everything */
215 preferencesAction->setEnabled(false);
219 if (lastStatus == NULL || strcmp(lastStatus, "down") == 0) {
220 qDebug("Connected to dhcpcd-%s", dhcpcd_version(con));
223 refresh = strcmp(lastStatus, "opened") ? false : true;
224 updateOnline(refresh);
228 lastStatus = strdup(status);
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);
240 void DhcpcdQt::dhcpcd_status_cb(_unused DHCPCD_CONNECTION *con,
241 const char *status, void *d)
243 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
245 dhcpcdQt->statusCallback(status);
248 void DhcpcdQt::ifCallback(DHCPCD_IF *i)
253 if (strcmp(i->reason, "RENEW") &&
254 strcmp(i->reason, "STOP") &&
255 strcmp(i->reason, "STOPPED"))
257 msg = dhcpcd_if_message(i, &new_msg);
261 QSystemTrayIcon::MessageIcon icon =
262 i->up ? QSystemTrayIcon::Information :
263 QSystemTrayIcon::Warning;
264 QString t = tr("Network Event");
275 for (auto &wi : *wis) {
276 DHCPCD_WPA *wpa = wi->getWpa();
277 if (dhcpcd_wpa_if(wpa) == i) {
278 DHCPCD_WI_SCAN *scans;
280 scans = dhcpcd_wi_scans(i);
281 processScans(wi, scans);
287 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
289 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
291 dhcpcdQt->ifCallback(i);
294 DhcpcdWi *DhcpcdQt::findWi(DHCPCD_WPA *wpa)
297 for (auto &wi : *wis) {
298 if (wi->getWpa() == wpa)
304 void DhcpcdQt::processScans(DhcpcdWi *wi, DHCPCD_WI_SCAN *scans)
306 DHCPCD_WI_SCAN *s1, *s2;
308 QString title = tr("New Access Point");
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)
316 if (!txt.isEmpty()) {
317 title = tr("New Access Points");
323 if (!txt.isEmpty() &&
324 (ssidMenu == NULL || !ssidMenu->isVisible()))
328 if (ssidMenu && ssidMenu->isVisible())
329 ssidMenu->popup(ssidMenuPos);
332 void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
334 DHCPCD_WI_SCAN *scans;
335 int fd = dhcpcd_wpa_get_fd(wpa);
340 qCritical("No fd for WPA");
348 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
350 qCritical("No interface for WPA");
358 qDebug("%s: Received scan results", i->ifname);
359 scans = dhcpcd_wi_scans(i);
361 wi = new DhcpcdWi(this, wpa);
365 processScans(wi, scans);
369 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
371 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
373 dhcpcdQt->scanCallback(wpa);
376 void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa, const char *status)
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);
391 void DhcpcdQt::dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status,
394 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
396 dhcpcdQt->wpaStatusCallback(wpa, status);
399 void DhcpcdQt::tryOpen() {
400 int fd = dhcpcd_open(con, true);
401 static int last_error;
404 if (errno == EACCES || errno == EPERM) {
405 if ((fd = dhcpcd_open(con, false)) != -1)
408 if (errno != last_error) {
410 const char *errt = strerror(errno);
411 qCritical("dhcpcd_open: %s", errt);
412 trayIcon->setToolTip(
413 tr("Error connecting to dhcpcd: %1").arg(errt));
415 if (retryOpenTimer == NULL) {
416 retryOpenTimer = new QTimer(this);
417 connect(retryOpenTimer, SIGNAL(timeout()),
418 this, SLOT(tryOpen()));
419 retryOpenTimer->start(DHCPCD_RETRYOPEN);
425 /* Start listening to WPA events */
426 dhcpcd_wpa_start(con);
428 if (retryOpenTimer) {
429 delete retryOpenTimer;
430 retryOpenTimer = NULL;
433 notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
434 connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
436 preferencesAction->setEnabled(dhcpcd_privileged(con));
439 void DhcpcdQt::dispatch() {
441 if (dhcpcd_get_fd(con) == -1) {
442 qWarning("dhcpcd connection lost");
446 dhcpcd_dispatch(con);
449 void DhcpcdQt::notify(QString &title, QString &msg,
451 QSystemTrayIcon::MessageIcon
453 QSystemTrayIcon::MessageIcon icon
459 KNotification *n = new KNotification("event", this);
464 //trayIcon->showMessage(title, msg, icon);
469 void DhcpcdQt::closeEvent(QCloseEvent *event)
472 if (trayIcon->isVisible()) {
478 QIcon DhcpcdQt::getIcon(QString category, QString name)
482 if (QIcon::hasThemeIcon(name))
483 icon = QIcon::fromTheme(name);
485 icon = QIcon(ICONDIR "/hicolor/scalable/" + category + "/" + name + ".svg");
490 void DhcpcdQt::setIcon(QString category, QString name)
492 QIcon icon = getIcon(category, name);
494 trayIcon->setIcon(icon);
497 QIcon DhcpcdQt::icon()
500 return getIcon("status", "network-transmit-receive");
503 void DhcpcdQt::createSsidMenu()
510 if (wis->size() == 0)
513 ssidMenu = new QMenu(this);
514 if (wis->size() == 1)
515 wis->first()->createMenu(ssidMenu);
517 for (auto &wi : *wis)
518 ssidMenu->addMenu(wi->createIfMenu(ssidMenu));
520 ssidMenuPos = QCursor::pos();
521 ssidMenu->popup(ssidMenuPos);
524 void DhcpcdQt::iconActivated(QSystemTrayIcon::ActivationReason reason)
527 if (reason == QSystemTrayIcon::Trigger)
531 void DhcpcdQt::dialogClosed(QDialog *dialog)
536 else if (dialog == preferences)
540 void DhcpcdQt::showPreferences()
543 if (preferences == NULL) {
544 preferences = new DhcpcdPreferences(this);
547 preferences->activateWindow();
550 void DhcpcdQt::showAbout()
554 about = new DhcpcdAbout(this);
557 about->activateWindow();
560 void DhcpcdQt::createActions()
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()));
569 aboutAction = new QAction(tr("&About"), this);
570 aboutAction->setIcon(QIcon::fromTheme("help-about"));
571 connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
573 quitAction = new QAction(tr("&Quit"), this);
574 quitAction->setIcon(QIcon::fromTheme("application-exit"));
575 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
579 void DhcpcdQt::createTrayIcon()
582 trayIconMenu = new QMenu(this);
583 trayIconMenu->addAction(preferencesAction);
584 trayIconMenu->addSeparator();
585 trayIconMenu->addAction(aboutAction);
586 trayIconMenu->addAction(quitAction);
588 trayIcon = new QSystemTrayIcon(this);
589 setIcon("status", "network-offline");
590 trayIcon->setContextMenu(trayIconMenu);
592 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
593 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));