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"
49 onLine = carrier = false;
51 aniTimer = new QTimer(this);
52 connect(aniTimer, SIGNAL(timeout()), this, SLOT(animate()));
54 retryOpenTimer = NULL;
59 wis = new QList<DhcpcdWi *>();
62 qDebug("%s", "Connecting ...");
65 qCritical("libdhcpcd: %s", strerror(errno));
69 dhcpcd_set_progname(con, "dhcpcd-qt");
70 dhcpcd_set_status_callback(con, dhcpcd_status_cb, this);
71 dhcpcd_set_if_callback(con, dhcpcd_if_cb, this);
72 dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, this);
73 dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, this);
92 DHCPCD_CONNECTION *DhcpcdQt::getConnection()
98 QList<DhcpcdWi *> *DhcpcdQt::getWis()
104 void DhcpcdQt::animate()
109 if (aniCounter++ > 6) {
115 if (aniCounter % 2 == 0)
116 icon = "network-idle";
118 icon = "network-transmit-receive";
120 switch(aniCounter++) {
122 icon = "network-transmit";
125 icon = "network-receive";
128 icon = "network-idle";
133 setIcon("status", icon);
136 void DhcpcdQt::updateOnline(bool showIf)
138 bool isOn, isCarrier;
143 isOn = isCarrier = false;
144 ifs = dhcpcd_interfaces(con);
145 for (i = ifs; i; i = i->next) {
146 if (strcmp(i->type, "link") == 0) {
153 msg = dhcpcd_if_message(i, NULL);
158 msgs = QString::fromAscii(msg);
160 msgs += '\n' + QString::fromAscii(msg);
163 qDebug() << i->ifname << i->reason;
166 if (onLine != isOn || carrier != isCarrier) {
173 aniTimer->start(300);
174 } else if (isCarrier) {
176 aniTimer->start(500);
178 setIcon("status", "network-offline");
181 trayIcon->setToolTip(msgs);
184 void DhcpcdQt::statusCallback(const char *status)
187 qDebug("Status changed to %s", status);
188 if (strcmp(status, "down") == 0) {
191 onLine = carrier = false;
192 setIcon("status", "network-offline");
193 trayIcon->setToolTip(tr("Not connected to dhcpcd"));
194 /* Close down everything */
211 preferencesAction->setEnabled(false);
215 if (lastStatus == NULL || strcmp(lastStatus, "down") == 0) {
216 qDebug("Connected to dhcpcd-%s", dhcpcd_version(con));
219 refresh = strcmp(lastStatus, "opened") ? false : true;
220 updateOnline(refresh);
224 lastStatus = strdup(status);
226 if (strcmp(status, "down") == 0) {
227 if (retryOpenTimer == NULL) {
228 retryOpenTimer = new QTimer(this);
229 connect(retryOpenTimer, SIGNAL(timeout()),
230 this, SLOT(tryOpen()));
231 retryOpenTimer->start(DHCPCD_RETRYOPEN);
236 void DhcpcdQt::dhcpcd_status_cb(_unused DHCPCD_CONNECTION *con,
237 const char *status, void *d)
239 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
241 dhcpcdQt->statusCallback(status);
244 void DhcpcdQt::ifCallback(DHCPCD_IF *i)
249 if (strcmp(i->reason, "RENEW") &&
250 strcmp(i->reason, "STOP") &&
251 strcmp(i->reason, "STOPPED"))
253 msg = dhcpcd_if_message(i, &new_msg);
257 QSystemTrayIcon::MessageIcon icon =
258 i->up ? QSystemTrayIcon::Information :
259 QSystemTrayIcon::Warning;
260 trayIcon->showMessage(tr("Network Event"),
270 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
272 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
274 dhcpcdQt->ifCallback(i);
277 DhcpcdWi *DhcpcdQt::findWi(DHCPCD_WPA *wpa)
280 for (auto &wi : *wis) {
281 if (wi->getWpa() == wpa)
287 void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
289 DHCPCD_WI_SCAN *scans, *s1, *s2;
290 int fd = dhcpcd_wpa_get_fd(wpa);
295 qCritical("No fd for WPA");
303 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
305 qCritical("No interface for WPA");
313 qDebug("%s: Received scan results", i->ifname);
314 scans = dhcpcd_wi_scans(i);
316 wi = new DhcpcdWi(this, wpa);
319 QString title = tr("New Access Point");
321 for (s1 = scans; s1; s1 = s1->next) {
322 for (s2 = wi->getScans(); s2; s2 = s2->next) {
323 if (strcmp(s1->ssid, s2->ssid) == 0)
327 if (!txt.isEmpty()) {
328 title = tr("New Access Points");
334 if (!txt.isEmpty() &&
335 (ssidMenu == NULL || !ssidMenu->isVisible()))
339 if (wi->setScans(scans) && ssidMenu->isVisible())
340 ssidMenu->popup(ssidMenuPos);
343 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
345 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
347 dhcpcdQt->scanCallback(wpa);
350 void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa, const char *status)
354 i = dhcpcd_wpa_if(wpa);
355 qDebug("%s: WPA status %s", i->ifname, status);
356 if (strcmp(status, "down") == 0) {
357 DhcpcdWi *wi = findWi(wpa);
365 void DhcpcdQt::dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status,
368 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
370 dhcpcdQt->wpaStatusCallback(wpa, status);
373 void DhcpcdQt::tryOpen() {
374 int fd = dhcpcd_open(con, true);
375 static int last_error;
378 if (errno == EACCES || errno == EPERM) {
379 if ((fd = dhcpcd_open(con, false)) != -1)
382 if (errno != last_error) {
384 const char *errt = strerror(errno);
385 qCritical("dhcpcd_open: %s", errt);
386 trayIcon->setToolTip(
387 tr("Error connecting to dhcpcd: %1").arg(errt));
389 if (retryOpenTimer == NULL) {
390 retryOpenTimer = new QTimer(this);
391 connect(retryOpenTimer, SIGNAL(timeout()),
392 this, SLOT(tryOpen()));
393 retryOpenTimer->start(DHCPCD_RETRYOPEN);
399 /* Start listening to WPA events */
400 dhcpcd_wpa_start(con);
402 if (retryOpenTimer) {
403 delete retryOpenTimer;
404 retryOpenTimer = NULL;
407 notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
408 connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
410 preferencesAction->setEnabled(dhcpcd_privileged(con));
413 void DhcpcdQt::dispatch() {
415 if (dhcpcd_get_fd(con) == -1) {
416 qWarning("dhcpcd connection lost");
420 dhcpcd_dispatch(con);
423 void DhcpcdQt::notify(QString &title, QString &msg,
424 QSystemTrayIcon::MessageIcon icon)
427 qDebug("%s", qPrintable(msg));
428 trayIcon->showMessage(title, msg, icon);
432 void DhcpcdQt::closeEvent(QCloseEvent *event)
435 if (trayIcon->isVisible()) {
441 QIcon DhcpcdQt::getIcon(QString category, QString name)
445 if (QIcon::hasThemeIcon(name))
446 icon = QIcon::fromTheme(name);
448 icon = QIcon(ICONDIR "/hicolor/scalable/" + category + "/" + name + ".svg");
452 void DhcpcdQt::setIcon(QString category, QString name)
454 QIcon icon = getIcon(category, name);
456 trayIcon->setIcon(icon);
459 QIcon DhcpcdQt::icon()
462 return getIcon("status", "network-transmit-receive");
465 void DhcpcdQt::createSsidMenu()
472 if (wis->size() == 0)
475 ssidMenu = new QMenu(this);
476 if (wis->size() == 1)
477 wis->first()->createMenu(ssidMenu);
479 for (auto &wi : *wis)
480 ssidMenu->addMenu(wi->createIfMenu(ssidMenu));
482 ssidMenuPos = QCursor::pos();
483 ssidMenu->popup(ssidMenuPos);
486 void DhcpcdQt::iconActivated(QSystemTrayIcon::ActivationReason reason)
489 if (reason == QSystemTrayIcon::Trigger)
493 void DhcpcdQt::dialogClosed(QDialog *dialog)
498 else if (dialog == preferences)
502 void DhcpcdQt::showPreferences()
505 if (preferences == NULL) {
506 preferences = new DhcpcdPreferences(this);
509 preferences->activateWindow();
512 void DhcpcdQt::showAbout()
516 about = new DhcpcdAbout(this);
519 about->activateWindow();
522 void DhcpcdQt::createActions()
525 preferencesAction = new QAction(tr("&Preferences"), this);
526 preferencesAction->setIcon(QIcon::fromTheme("preferences-system-network"));
527 preferencesAction->setEnabled(false);
528 connect(preferencesAction, SIGNAL(triggered()),
529 this, SLOT(showPreferences()));
531 aboutAction = new QAction(tr("&About"), this);
532 aboutAction->setIcon(QIcon::fromTheme("help-about"));
533 connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
535 quitAction = new QAction(tr("&Quit"), this);
536 quitAction->setIcon(QIcon::fromTheme("application-exit"));
537 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
541 void DhcpcdQt::createTrayIcon()
544 trayIconMenu = new QMenu(this);
545 trayIconMenu->addAction(preferencesAction);
546 trayIconMenu->addSeparator();
547 trayIconMenu->addAction(aboutAction);
548 trayIconMenu->addAction(quitAction);
550 trayIcon = new QSystemTrayIcon(this);
551 setIcon("status", "network-offline");
552 trayIcon->setContextMenu(trayIconMenu);
554 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
555 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));