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_status_callback(con, dhcpcd_status_cb, this);
70 dhcpcd_set_if_callback(con, dhcpcd_if_cb, this);
71 dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, this);
72 dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, this);
90 void DhcpcdQt::animate()
95 if (aniCounter++ > 6) {
101 if (aniCounter % 2 == 0)
102 icon = "network-idle";
104 icon = "network-transmit-receive";
106 switch(aniCounter++) {
108 icon = "network-transmit";
111 icon = "network-receive";
114 icon = "network-idle";
119 setIcon("status", icon);
122 void DhcpcdQt::updateOnline(bool showIf)
124 bool isOn, isCarrier;
129 isOn = isCarrier = false;
130 ifs = dhcpcd_interfaces(con);
131 for (i = ifs; i; i = i->next) {
132 if (strcmp(i->type, "link") == 0) {
139 msg = dhcpcd_if_message(i, NULL);
144 msgs = QString::fromAscii(msg);
146 msgs += '\n' + QString::fromAscii(msg);
149 qDebug() << i->ifname << i->reason;
152 if (onLine != isOn || carrier != isCarrier) {
159 aniTimer->start(300);
160 } else if (isCarrier) {
162 aniTimer->start(500);
164 setIcon("status", "network-offline");
167 trayIcon->setToolTip(msgs);
170 void DhcpcdQt::statusCallback(const char *status)
173 qDebug("Status changed to %s", status);
174 if (strcmp(status, "down") == 0) {
177 onLine = carrier = false;
178 setIcon("status", "network-offline");
183 trayIcon->setToolTip(tr("Not connected to dhcpcd"));
187 if (lastStatus == NULL || strcmp(lastStatus, "down") == 0) {
188 qDebug("Connected to dhcpcd-%s", dhcpcd_version(con));
191 refresh = strcmp(lastStatus, "opened") ? false : true;
192 updateOnline(refresh);
196 lastStatus = strdup(status);
198 if (strcmp(status, "down") == 0) {
199 if (retryOpenTimer == NULL) {
200 retryOpenTimer = new QTimer(this);
201 connect(retryOpenTimer, SIGNAL(timeout()),
202 this, SLOT(tryOpen()));
203 retryOpenTimer->start(DHCPCD_RETRYOPEN);
208 void DhcpcdQt::dhcpcd_status_cb(_unused DHCPCD_CONNECTION *con,
209 const char *status, void *d)
211 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
213 dhcpcdQt->statusCallback(status);
216 void DhcpcdQt::ifCallback(DHCPCD_IF *i)
221 if (strcmp(i->reason, "RENEW") &&
222 strcmp(i->reason, "STOP") &&
223 strcmp(i->reason, "STOPPED"))
225 msg = dhcpcd_if_message(i, &new_msg);
229 QSystemTrayIcon::MessageIcon icon =
230 i->up ? QSystemTrayIcon::Information :
231 QSystemTrayIcon::Warning;
232 trayIcon->showMessage(tr("Network Event"),
242 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
244 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
246 dhcpcdQt->ifCallback(i);
249 DhcpcdWi *DhcpcdQt::findWi(DHCPCD_WPA *wpa)
252 for (auto &wi : *wis) {
253 if (wi->getWpa() == wpa)
259 void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
261 DHCPCD_WI_SCAN *scans, *s1, *s2;
262 int fd = dhcpcd_wpa_get_fd(wpa);
267 qCritical("No fd for WPA");
275 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
277 qCritical("No interface for WPA");
285 qDebug("%s: Received scan results", i->ifname);
286 scans = dhcpcd_wi_scans(i);
288 wi = new DhcpcdWi(this, wpa);
291 QString title = tr("New Access Point");
293 for (s1 = scans; s1; s1 = s1->next) {
294 for (s2 = wi->getScans(); s2; s2 = s2->next) {
295 if (strcmp(s1->ssid, s2->ssid) == 0)
299 if (!txt.isEmpty()) {
300 title = tr("New Access Points");
306 if (!txt.isEmpty() &&
307 (ssidMenu == NULL || !ssidMenu->isVisible()))
311 if (wi->setScans(scans) && ssidMenu->isVisible())
312 ssidMenu->popup(ssidMenuPos);
315 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
317 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
319 dhcpcdQt->scanCallback(wpa);
322 void DhcpcdQt::wpaStatusCallback(DHCPCD_WPA *wpa, const char *status)
326 i = dhcpcd_wpa_if(wpa);
327 qDebug("%s: WPA status %s", i->ifname, status);
328 if (strcmp(status, "down") == 0) {
329 DhcpcdWi *wi = findWi(wpa);
337 void DhcpcdQt::dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status,
340 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
342 dhcpcdQt->wpaStatusCallback(wpa, status);
345 void DhcpcdQt::tryOpen() {
346 int fd = dhcpcd_open(con);
347 static int last_error;
350 if (errno != last_error) {
352 qCritical("dhcpcd_open: %s", strerror(errno));
354 if (retryOpenTimer == NULL) {
355 retryOpenTimer = new QTimer(this);
356 connect(retryOpenTimer, SIGNAL(timeout()),
357 this, SLOT(tryOpen()));
358 retryOpenTimer->start(DHCPCD_RETRYOPEN);
363 /* Start listening to WPA events */
364 dhcpcd_wpa_start(con);
366 if (retryOpenTimer) {
367 delete retryOpenTimer;
368 retryOpenTimer = NULL;
371 notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
372 connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
375 void DhcpcdQt::dispatch() {
377 if (dhcpcd_get_fd(con) == -1) {
378 qWarning("dhcpcd connection lost");
382 dhcpcd_dispatch(con);
385 void DhcpcdQt::notify(QString &title, QString &msg,
386 QSystemTrayIcon::MessageIcon icon)
389 qDebug("%s", qPrintable(msg));
390 trayIcon->showMessage(title, msg, icon);
394 void DhcpcdQt::closeEvent(QCloseEvent *event)
397 if (trayIcon->isVisible()) {
403 QIcon DhcpcdQt::getIcon(QString category, QString name)
407 if (QIcon::hasThemeIcon(name))
408 icon = QIcon::fromTheme(name);
410 icon = QIcon(ICONDIR "/hicolor/scalable/" + category + "/" + name + ".svg");
414 void DhcpcdQt::setIcon(QString category, QString name)
416 QIcon icon = getIcon(category, name);
418 trayIcon->setIcon(icon);
421 QIcon DhcpcdQt::icon()
424 return getIcon("status", "network-transmit-receive");
427 void DhcpcdQt::createSsidMenu()
434 if (wis->size() == 0)
437 ssidMenu = new QMenu(this);
438 if (wis->size() == 1)
439 wis->first()->createMenu(ssidMenu);
441 for (auto &wi : *wis)
442 ssidMenu->addMenu(wi->createIfMenu(ssidMenu));
444 ssidMenuPos = QCursor::pos();
445 ssidMenu->popup(ssidMenuPos);
448 void DhcpcdQt::iconActivated(QSystemTrayIcon::ActivationReason reason)
451 if (reason == QSystemTrayIcon::Trigger)
455 void DhcpcdQt::dialogClosed(QDialog *dialog)
460 else if (dialog == preferences)
464 void DhcpcdQt::showPreferences()
467 if (preferences == NULL) {
468 preferences = new DhcpcdPreferences(this);
471 preferences->activateWindow();
474 void DhcpcdQt::showAbout()
478 about = new DhcpcdAbout(this);
481 about->activateWindow();
484 void DhcpcdQt::createActions()
487 preferencesAction = new QAction(tr("&Preferences"), this);
488 preferencesAction->setIcon(QIcon::fromTheme("preferences-system-network"));
489 connect(preferencesAction, SIGNAL(triggered()),
490 this, SLOT(showPreferences()));
492 aboutAction = new QAction(tr("&About"), this);
493 aboutAction->setIcon(QIcon::fromTheme("help-about"));
494 connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
496 quitAction = new QAction(tr("&Quit"), this);
497 quitAction->setIcon(QIcon::fromTheme("application-exit"));
498 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
502 void DhcpcdQt::createTrayIcon()
505 trayIconMenu = new QMenu(this);
506 trayIconMenu->addAction(preferencesAction);
507 trayIconMenu->addSeparator();
508 trayIconMenu->addAction(aboutAction);
509 trayIconMenu->addAction(quitAction);
511 trayIcon = new QSystemTrayIcon(this);
512 setIcon("status", "network-offline");
513 trayIcon->setContextMenu(trayIconMenu);
515 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
516 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));