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-ssidmenu.h"
48 onLine = carrier = false;
50 aniTimer = new QTimer(this);
51 connect(aniTimer, SIGNAL(timeout()), this, SLOT(animate()));
52 retryOpenTimer = NULL;
57 wis = new QList<DhcpcdWi *>();
60 qDebug("%s", "Connecting ...");
63 qCritical("libdhcpcd: %s", strerror(errno));
67 dhcpcd_set_status_callback(con, dhcpcd_status_cb, this);
68 dhcpcd_set_if_callback(con, dhcpcd_if_cb, this);
69 dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, this);
88 void DhcpcdQt::animate()
93 if (aniCounter++ > 6) {
99 if (aniCounter % 2 == 0)
100 icon = "network-idle";
102 icon = "network-transmit-receive";
104 switch(aniCounter++) {
106 icon = "network-transmit";
109 icon = "network-receive";
112 icon = "network-idle";
117 setIcon("status", icon);
120 void DhcpcdQt::updateOnline(bool showIf)
122 bool isOn, isCarrier;
127 isOn = isCarrier = false;
128 ifs = dhcpcd_interfaces(con);
129 for (i = ifs; i; i = i->next) {
130 if (strcmp(i->type, "link") == 0) {
137 msg = dhcpcd_if_message(i, NULL);
142 msgs = QString::fromAscii(msg);
144 msgs += '\n' + QString::fromAscii(msg);
147 qDebug() << i->ifname << i->reason;
150 if (onLine != isOn || carrier != isCarrier) {
156 aniTimer->start(300);
157 } else if (isCarrier) {
159 aniTimer->start(500);
161 setIcon("status", "network-offline");
164 trayIcon->setToolTip(msgs);
167 void DhcpcdQt::statusCallback(const char *status)
170 qDebug("Status changed to %s", status);
171 if (strcmp(status, "down") == 0) {
174 msg = tr("Connection to dhcpcd lost");
176 msg = tr("dhcpcd not running");
179 setIcon("status", "network-offline");
183 if ((lastStatus == NULL || strcmp(lastStatus, "down") == 0)) {
184 qDebug("Connected to dhcpcd-%s", dhcpcd_version(con));
188 updateOnline(refresh);
192 lastStatus = strdup(status);
195 void DhcpcdQt::dhcpcd_status_cb(_unused DHCPCD_CONNECTION *con,
196 const char *status, void *d)
198 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
200 dhcpcdQt->statusCallback(status);
203 void DhcpcdQt::ifCallback(DHCPCD_IF *i)
210 if (strcmp(i->reason, "RENEW") == 0 ||
211 strcmp(i->reason, "STOP") == 0 ||
212 strcmp(i->reason, "STOPPED") == 0)
215 msg = dhcpcd_if_message(i, &new_msg);
219 QSystemTrayIcon::MessageIcon icon =
220 i->up ? QSystemTrayIcon::Information :
221 QSystemTrayIcon::Warning;
222 trayIcon->showMessage(tr("Network Event"), msg, icon);
228 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
230 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
232 dhcpcdQt->ifCallback(i);
235 DhcpcdWi *DhcpcdQt::findWi(DHCPCD_WPA *wpa)
238 for (auto &wi : *wis) {
239 if (wi->getWpa() == wpa)
245 void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
247 DHCPCD_WI_SCAN *scans, *s1, *s2;
248 int fd = dhcpcd_wpa_get_fd(wpa);
253 qCritical("No fd for WPA");
261 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
263 qCritical("No interface for WPA");
271 qDebug("%s: Received scan results", i->ifname);
272 scans = dhcpcd_wi_scans(i);
274 wi = new DhcpcdWi(this, wpa);
277 QString title = tr("New Access Point");
279 for (s1 = scans; s1; s1 = s1->next) {
280 for (s2 = wi->getScans(); s2; s2 = s2->next) {
281 if (strcmp(s1->ssid, s2->ssid) == 0)
284 if (!txt.isEmpty()) {
285 title = tr("New Access Points");
298 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
300 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
302 dhcpcdQt->scanCallback(wpa);
305 bool DhcpcdQt::tryOpen() {
306 int fd = dhcpcd_open(con);
307 static int last_error;
310 if (errno != last_error) {
312 qCritical("dhcpcd_open: %s", strerror(errno));
314 if (retryOpenTimer == NULL) {
315 retryOpenTimer = new QTimer(this);
316 connect(retryOpenTimer, SIGNAL(timeout()),
317 this, SLOT(tryOpen()));
318 retryOpenTimer->start(DHCPCD_RETRYOPEN);
323 if (retryOpenTimer) {
324 delete retryOpenTimer;
325 retryOpenTimer = NULL;
328 notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
329 connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
334 void DhcpcdQt::dispatch() {
336 if (dhcpcd_get_fd(con) == -1) {
337 qWarning("dhcpcd connection lost");
341 dhcpcd_dispatch(con);
344 void DhcpcdQt::notify(QString &title, QString &msg,
345 QSystemTrayIcon::MessageIcon icon)
348 qDebug("%s", qPrintable(msg));
349 trayIcon->showMessage(title, msg, icon);
353 void DhcpcdQt::closeEvent(QCloseEvent *event)
356 if (trayIcon->isVisible()) {
362 QIcon DhcpcdQt::getIcon(QString category, QString name)
366 if (QIcon::hasThemeIcon(name))
367 icon = QIcon::fromTheme(name);
369 icon = QIcon(ICONDIR "/hicolor/scalable/" + category + "/" + name + ".svg");
373 void DhcpcdQt::setIcon(QString category, QString name)
375 QIcon icon = getIcon(category, name);
377 trayIcon->setIcon(icon);
380 QIcon DhcpcdQt::icon()
383 return getIcon("status", "network-transmit-receive");
386 void DhcpcdQt::addSsidMenu(QMenu *&menu, DHCPCD_IF *ifp, DhcpcdWi *&wi)
388 DHCPCD_WI_SCAN *scan;
390 for (scan = wi->getScans(); scan; scan = scan->next) {
391 QWidgetAction *wa = new QWidgetAction(menu);
392 DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, ifp, scan);
393 wa->setDefaultWidget(ssidMenu);
395 connect(ssidMenu, SIGNAL(selected(DHCPCD_IF *, DHCPCD_WI_SCAN *)),
396 this, SLOT(connectSsid(DHCPCD_IF *, DHCPCD_WI_SCAN *)));
400 void DhcpcdQt::connectSsid(DHCPCD_IF *, DHCPCD_WI_SCAN *)
403 QMessageBox::information(this, "Not implemented",
404 "SSID selection is not yet implemented");
407 void DhcpcdQt::createSsidMenu()
416 if (wis->size() == 0)
419 ssidMenu = new QMenu(this);
420 if (wis->size() == 1) {
421 DhcpcdWi *wi = wis->first();
423 ifp = dhcpcd_wpa_if(wpa);
424 addSsidMenu(ssidMenu, ifp, wi);
426 for (auto &wi : *wis) {
428 ifp = dhcpcd_wpa_if(wpa);
430 QMenu *ifmenu = ssidMenu->addMenu(ifp->ifname);
431 addSsidMenu(ifmenu, ifp, wi);
435 ssidMenu->popup(QCursor::pos());
438 void DhcpcdQt::iconActivated(QSystemTrayIcon::ActivationReason reason)
441 if (reason == QSystemTrayIcon::Trigger)
445 void DhcpcdQt::dialogClosed(QDialog *dialog)
450 else if (dialog == preferences)
454 void DhcpcdQt::showPreferences()
457 if (preferences == NULL) {
458 preferences = new DhcpcdPreferences(this);
461 preferences->activateWindow();
464 void DhcpcdQt::showAbout()
468 about = new DhcpcdAbout(this);
471 about->activateWindow();
474 void DhcpcdQt::createActions()
477 preferencesAction = new QAction(tr("&Preferences"), this);
478 preferencesAction->setIcon(QIcon::fromTheme("preferences-system-network"));
479 connect(preferencesAction, SIGNAL(triggered()),
480 this, SLOT(showPreferences()));
482 aboutAction = new QAction(tr("&About"), this);
483 aboutAction->setIcon(QIcon::fromTheme("help-about"));
484 connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
486 quitAction = new QAction(tr("&Quit"), this);
487 quitAction->setIcon(QIcon::fromTheme("application-exit"));
488 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
492 void DhcpcdQt::createTrayIcon()
495 trayIconMenu = new QMenu(this);
496 trayIconMenu->addAction(preferencesAction);
497 trayIconMenu->addSeparator();
498 trayIconMenu->addAction(aboutAction);
499 trayIconMenu->addAction(quitAction);
501 trayIcon = new QSystemTrayIcon(this);
502 setIcon("status", "network-offline");
503 trayIcon->setContextMenu(trayIconMenu);
505 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
506 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));