Close WPA listeners when we lose the connection.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-qt.cpp
index ee988f297417e8fbe5f6397f5b03d653eb6ce5eb..6cb9d9a9acad2a9a1acdd137edddaa34b30ec8f2 100644 (file)
@@ -66,6 +66,7 @@ DhcpcdQt::DhcpcdQt()
                exit(EXIT_FAILURE);
                return;
        }
+       dhcpcd_set_progname(con, "dhcpcd-qt");
        dhcpcd_set_status_callback(con, dhcpcd_status_cb, this);
        dhcpcd_set_if_callback(con, dhcpcd_if_cb, this);
        dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, this);
@@ -88,6 +89,18 @@ DhcpcdQt::~DhcpcdQt()
 
 }
 
+DHCPCD_CONNECTION *DhcpcdQt::getConnection()
+{
+
+       return con;
+}
+
+QList<DhcpcdWi *> *DhcpcdQt::getWis()
+{
+
+       return wis;
+}
+
 void DhcpcdQt::animate()
 {
        const char *icon;
@@ -177,11 +190,21 @@ void DhcpcdQt::statusCallback(const char *status)
                aniCounter = 0;
                onLine = carrier = false;
                setIcon("status", "network-offline");
+               trayIcon->setToolTip(tr("Not connected to dhcpcd"));
+               /* Close down everything */
                if (notifier) {
                        delete notifier;
                        notifier = NULL;
                }
-               trayIcon->setToolTip(tr("Not connected to dhcpcd"));
+               if (ssidMenu) {
+                       delete ssidMenu;
+                       ssidMenu = NULL;
+               }
+               if (preferences) {
+                       delete preferences;
+                       preferences = NULL;
+               }
+               preferencesAction->setEnabled(false);
        } else {
                bool refresh;
 
@@ -344,13 +367,20 @@ void DhcpcdQt::dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status,
 }
 
 void DhcpcdQt::tryOpen() {
-       int fd = dhcpcd_open(con);
+       int fd = dhcpcd_open(con, true);
        static int last_error;
 
        if (fd == -1) {
+               if (errno == EACCES || errno == EPERM) {
+                       if ((fd = dhcpcd_open(con, false)) != -1)
+                               goto unprived;
+               }
                if (errno != last_error) {
                        last_error = errno;
-                       qCritical("dhcpcd_open: %s", strerror(errno));
+                       const char *errt = strerror(errno);
+                       qCritical("dhcpcd_open: %s", errt);
+                       trayIcon->setToolTip(
+                           tr("Error connecting to dhcpcd: %1").arg(errt));
                }
                if (retryOpenTimer == NULL) {
                        retryOpenTimer = new QTimer(this);
@@ -361,6 +391,7 @@ void DhcpcdQt::tryOpen() {
                return;
        }
 
+unprived:
        /* Start listening to WPA events */
        dhcpcd_wpa_start(con);
 
@@ -371,6 +402,8 @@ void DhcpcdQt::tryOpen() {
 
        notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
        connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
+
+       preferencesAction->setEnabled(dhcpcd_privileged(con));
 }
 
 void DhcpcdQt::dispatch() {
@@ -487,6 +520,7 @@ void DhcpcdQt::createActions()
 
        preferencesAction = new QAction(tr("&Preferences"), this);
        preferencesAction->setIcon(QIcon::fromTheme("preferences-system-network"));
+       preferencesAction->setEnabled(false);
        connect(preferencesAction, SIGNAL(triggered()),
            this, SLOT(showPreferences()));