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()));
53 retryOpenTimer = NULL;
58 wis = new QList<DhcpcdWi *>();
61 qDebug("%s", "Connecting ...");
64 qCritical("libdhcpcd: %s", strerror(errno));
68 dhcpcd_set_status_callback(con, dhcpcd_status_cb, this);
69 dhcpcd_set_if_callback(con, dhcpcd_if_cb, this);
70 dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, this);
89 void DhcpcdQt::animate()
94 if (aniCounter++ > 6) {
100 if (aniCounter % 2 == 0)
101 icon = "network-idle";
103 icon = "network-transmit-receive";
105 switch(aniCounter++) {
107 icon = "network-transmit";
110 icon = "network-receive";
113 icon = "network-idle";
118 setIcon("status", icon);
121 void DhcpcdQt::updateOnline(bool showIf)
123 bool isOn, isCarrier;
128 isOn = isCarrier = false;
129 ifs = dhcpcd_interfaces(con);
130 for (i = ifs; i; i = i->next) {
131 if (strcmp(i->type, "link") == 0) {
138 msg = dhcpcd_if_message(i, NULL);
143 msgs = QString::fromAscii(msg);
145 msgs += '\n' + QString::fromAscii(msg);
148 qDebug() << i->ifname << i->reason;
151 if (onLine != isOn || carrier != isCarrier) {
157 aniTimer->start(300);
158 } else if (isCarrier) {
160 aniTimer->start(500);
162 setIcon("status", "network-offline");
165 trayIcon->setToolTip(msgs);
168 void DhcpcdQt::statusCallback(const char *status)
171 qDebug("Status changed to %s", status);
172 if (strcmp(status, "down") == 0) {
175 setIcon("status", "network-offline");
180 trayIcon->setToolTip(tr("Not connected to dhcpcd"));
184 if ((lastStatus == NULL || strcmp(lastStatus, "down") == 0)) {
185 qDebug("Connected to dhcpcd-%s", dhcpcd_version(con));
189 updateOnline(refresh);
193 lastStatus = strdup(status);
195 if (strcmp(status, "down") == 0) {
196 if (retryOpenTimer == NULL) {
197 retryOpenTimer = new QTimer(this);
198 connect(retryOpenTimer, SIGNAL(timeout()),
199 this, SLOT(tryOpen()));
200 retryOpenTimer->start(DHCPCD_RETRYOPEN);
205 void DhcpcdQt::dhcpcd_status_cb(_unused DHCPCD_CONNECTION *con,
206 const char *status, void *d)
208 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
210 dhcpcdQt->statusCallback(status);
213 void DhcpcdQt::ifCallback(DHCPCD_IF *i)
220 if (strcmp(i->reason, "RENEW") == 0 ||
221 strcmp(i->reason, "STOP") == 0 ||
222 strcmp(i->reason, "STOPPED") == 0)
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"), msg, icon);
238 void DhcpcdQt::dhcpcd_if_cb(DHCPCD_IF *i, void *d)
240 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
242 dhcpcdQt->ifCallback(i);
245 DhcpcdWi *DhcpcdQt::findWi(DHCPCD_WPA *wpa)
248 for (auto &wi : *wis) {
249 if (wi->getWpa() == wpa)
255 void DhcpcdQt::scanCallback(DHCPCD_WPA *wpa)
257 DHCPCD_WI_SCAN *scans, *s1, *s2;
258 int fd = dhcpcd_wpa_get_fd(wpa);
263 qCritical("No fd for WPA");
271 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
273 qCritical("No interface for WPA");
281 qDebug("%s: Received scan results", i->ifname);
282 scans = dhcpcd_wi_scans(i);
284 wi = new DhcpcdWi(this, wpa);
287 QString title = tr("New Access Point");
289 for (s1 = scans; s1; s1 = s1->next) {
290 for (s2 = wi->getScans(); s2; s2 = s2->next) {
291 if (strcmp(s1->ssid, s2->ssid) == 0)
294 if (!txt.isEmpty()) {
295 title = tr("New Access Points");
308 void DhcpcdQt::dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, void *d)
310 DhcpcdQt *dhcpcdQt = (DhcpcdQt *)d;
312 dhcpcdQt->scanCallback(wpa);
315 void DhcpcdQt::tryOpen() {
316 int fd = dhcpcd_open(con);
317 static int last_error;
320 if (errno != last_error) {
322 qCritical("dhcpcd_open: %s", strerror(errno));
324 if (retryOpenTimer == NULL) {
325 retryOpenTimer = new QTimer(this);
326 connect(retryOpenTimer, SIGNAL(timeout()),
327 this, SLOT(tryOpen()));
328 retryOpenTimer->start(DHCPCD_RETRYOPEN);
333 if (retryOpenTimer) {
334 delete retryOpenTimer;
335 retryOpenTimer = NULL;
338 notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
339 connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
342 void DhcpcdQt::dispatch() {
344 if (dhcpcd_get_fd(con) == -1) {
345 qWarning("dhcpcd connection lost");
349 dhcpcd_dispatch(con);
352 void DhcpcdQt::notify(QString &title, QString &msg,
353 QSystemTrayIcon::MessageIcon icon)
356 qDebug("%s", qPrintable(msg));
357 trayIcon->showMessage(title, msg, icon);
361 void DhcpcdQt::closeEvent(QCloseEvent *event)
364 if (trayIcon->isVisible()) {
370 QIcon DhcpcdQt::getIcon(QString category, QString name)
374 if (QIcon::hasThemeIcon(name))
375 icon = QIcon::fromTheme(name);
377 icon = QIcon(ICONDIR "/hicolor/scalable/" + category + "/" + name + ".svg");
381 void DhcpcdQt::setIcon(QString category, QString name)
383 QIcon icon = getIcon(category, name);
385 trayIcon->setIcon(icon);
388 QIcon DhcpcdQt::icon()
391 return getIcon("status", "network-transmit-receive");
394 void DhcpcdQt::addSsidMenu(QMenu *&menu, DHCPCD_IF *ifp, DhcpcdWi *&wi)
396 DHCPCD_WI_SCAN *scan;
398 for (scan = wi->getScans(); scan; scan = scan->next) {
399 QWidgetAction *wa = new QWidgetAction(menu);
400 DhcpcdSsidMenu *ssidMenu = new DhcpcdSsidMenu(menu, ifp, scan);
401 wa->setDefaultWidget(ssidMenu);
403 connect(ssidMenu, SIGNAL(selected(DHCPCD_IF *, DHCPCD_WI_SCAN *)),
404 this, SLOT(connectSsid(DHCPCD_IF *, DHCPCD_WI_SCAN *)));
408 void DhcpcdQt::connectSsid(DHCPCD_IF *, DHCPCD_WI_SCAN *)
411 QMessageBox::information(this, "Not implemented",
412 "SSID selection is not yet implemented");
415 void DhcpcdQt::createSsidMenu()
424 if (wis->size() == 0)
427 ssidMenu = new QMenu(this);
428 if (wis->size() == 1) {
429 DhcpcdWi *wi = wis->first();
431 ifp = dhcpcd_wpa_if(wpa);
432 addSsidMenu(ssidMenu, ifp, wi);
434 for (auto &wi : *wis) {
436 ifp = dhcpcd_wpa_if(wpa);
438 QMenu *ifmenu = ssidMenu->addMenu(ifp->ifname);
439 addSsidMenu(ifmenu, ifp, wi);
443 ssidMenu->popup(QCursor::pos());
446 void DhcpcdQt::iconActivated(QSystemTrayIcon::ActivationReason reason)
449 if (reason == QSystemTrayIcon::Trigger)
453 void DhcpcdQt::dialogClosed(QDialog *dialog)
458 else if (dialog == preferences)
462 void DhcpcdQt::showPreferences()
465 if (preferences == NULL) {
466 preferences = new DhcpcdPreferences(this);
469 preferences->activateWindow();
472 void DhcpcdQt::showAbout()
476 about = new DhcpcdAbout(this);
479 about->activateWindow();
482 void DhcpcdQt::createActions()
485 preferencesAction = new QAction(tr("&Preferences"), this);
486 preferencesAction->setIcon(QIcon::fromTheme("preferences-system-network"));
487 connect(preferencesAction, SIGNAL(triggered()),
488 this, SLOT(showPreferences()));
490 aboutAction = new QAction(tr("&About"), this);
491 aboutAction->setIcon(QIcon::fromTheme("help-about"));
492 connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
494 quitAction = new QAction(tr("&Quit"), this);
495 quitAction->setIcon(QIcon::fromTheme("application-exit"));
496 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
500 void DhcpcdQt::createTrayIcon()
503 trayIconMenu = new QMenu(this);
504 trayIconMenu->addAction(preferencesAction);
505 trayIconMenu->addSeparator();
506 trayIconMenu->addAction(aboutAction);
507 trayIconMenu->addAction(quitAction);
509 trayIcon = new QSystemTrayIcon(this);
510 setIcon("status", "network-offline");
511 trayIcon->setContextMenu(trayIconMenu);
513 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
514 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));