Merge commit 'garage/master'
[wpasupplicant] / wpa_supplicant / wpa_gui-qt4 / wpagui.cpp
index 20bb353..dcd33b9 100644 (file)
@@ -25,6 +25,7 @@
 #include <QMessageBox>
 #include <QCloseEvent>
 #include <QImageReader>
+#include <QSettings>
 
 #include "wpagui.h"
 #include "dirent.h"
@@ -41,8 +42,8 @@ static int wpagui_printf(const char *, ...)
 }
 #endif
 
-WpaGui::WpaGui(QWidget *parent, const char *, Qt::WFlags)
-       : QMainWindow(parent)
+WpaGui::WpaGui(QApplication *_app, QWidget *parent, const char *, Qt::WFlags)
+       : QMainWindow(parent), app(_app)
 {
        setupUi(this);
 
@@ -61,10 +62,24 @@ WpaGui::WpaGui(QWidget *parent, const char *, Qt::WFlags)
                SLOT(startService()));
        connect(fileStopServiceAction, SIGNAL(triggered()), this,
                SLOT(stopService()));
+
+       addInterfaceAction = new QAction(this);
+       addInterfaceAction->setIconText("Add Interface");
+       fileMenu->insertAction(fileStartServiceAction, addInterfaceAction);
+
+       connect(addInterfaceAction, SIGNAL(triggered()), this,
+               SLOT(addInterface()));
 #endif /* CONFIG_NATIVE_WINDOWS */
 
        (void) statusBar();
 
+       /*
+        * Disable WPS tab by default; it will be enabled if wpa_supplicant is
+        * built with WPS support.
+        */
+       wpsTab->setEnabled(false);
+       wpaguiTab->setTabEnabled(wpaguiTab->indexOf(wpsTab), false);
+
        connect(fileEventHistoryAction, SIGNAL(triggered()), this,
                SLOT(eventHistory()));
        connect(fileSaveConfigAction, SIGNAL(triggered()), this,
@@ -118,6 +133,7 @@ WpaGui::WpaGui(QWidget *parent, const char *, Qt::WFlags)
 
        eh = NULL;
        scanres = NULL;
+       add_iface = NULL;
        udr = NULL;
        tray_icon = NULL;
        startInTray = false;
@@ -129,11 +145,21 @@ WpaGui::WpaGui(QWidget *parent, const char *, Qt::WFlags)
 
        parse_argv();
 
+       if (app->isSessionRestored()) {
+               QSettings settings("wpa_supplicant", "wpa_gui");
+               settings.beginGroup("state");
+               if (app->sessionId().compare(settings.value("session_id").
+                                            toString()) == 0)
+                       startInTray = settings.value("in_tray").toBool();
+               settings.endGroup();
+       }
+
        if (QSystemTrayIcon::isSystemTrayAvailable())
                createTrayIcon(startInTray);
        else
                show();
 
+       connectedToService = false;
        textStatus->setText("connecting to wpa_supplicant");
        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), SLOT(ping()));
@@ -177,6 +203,12 @@ WpaGui::~WpaGui()
                scanres = NULL;
        }
 
+       if (add_iface) {
+               add_iface->close();
+               delete add_iface;
+               add_iface = NULL;
+       }
+
        if (udr) {
                udr->close();
                delete udr;
@@ -279,6 +311,7 @@ int WpaGui::openCtrlConnection(const char *ifname)
                        ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf,
                                               &len, NULL);
                        if (ret >= 0) {
+                               connectedToService = true;
                                buf[len] = '\0';
                                pos = strchr(buf, '\n');
                                if (pos)
@@ -390,7 +423,10 @@ int WpaGui::openCtrlConnection(const char *ifname)
 
                QString res(buf);
                QStringList types = res.split(QChar(' '));
-               actionWPS->setEnabled(types.contains("WSC"));
+               bool wps = types.contains("WSC");
+               actionWPS->setEnabled(wps);
+               wpsTab->setEnabled(wps);
+               wpaguiTab->setTabEnabled(wpaguiTab->indexOf(wpsTab), wps);
        }
 
        return 0;
@@ -438,6 +474,21 @@ void WpaGui::updateStatus()
                textSsid->clear();
                textBssid->clear();
                textIpAddress->clear();
+
+#ifdef CONFIG_NATIVE_WINDOWS
+               static bool first = true;
+               if (first && connectedToService &&
+                   (ctrl_iface == NULL || *ctrl_iface == '\0')) {
+                       first = false;
+                       if (QMessageBox::information(
+                                   this, qAppName(),
+                                   "No network interfaces in use.\n"
+                                   "Would you like to add one?",
+                                   QMessageBox::Yes | QMessageBox::No) ==
+                           QMessageBox::Yes)
+                               addInterface();
+               }
+#endif /* CONFIG_NATIVE_WINDOWS */
                return;
        }
 
@@ -1262,6 +1313,7 @@ void WpaGui::createTrayIcon(bool trayOnly)
 
        if (!trayOnly)
                show();
+       inTray = trayOnly;
 }
 
 
@@ -1285,10 +1337,13 @@ void WpaGui::trayActivated(QSystemTrayIcon::ActivationReason how)
         * custom closeEvent handler take care of children */
        case QSystemTrayIcon::Trigger:
                ackTrayIcon = true;
-               if (isVisible())
+               if (isVisible()) {
                        close();
-               else
+                       inTray = true;
+               } else {
                        show();
+                       inTray = false;
+               }
                break;
        case QSystemTrayIcon::MiddleClick:
                showTrayStatus();
@@ -1620,3 +1675,25 @@ bool WpaGui::serviceRunning()
 }
 
 #endif /* CONFIG_NATIVE_WINDOWS */
+
+
+void WpaGui::addInterface()
+{
+       if (add_iface) {
+               add_iface->close();
+               delete add_iface;
+       }
+       add_iface = new AddInterface(this, this);
+       add_iface->show();
+       add_iface->exec();
+}
+
+
+void WpaGui::saveState()
+{
+       QSettings settings("wpa_supplicant", "wpa_gui");
+       settings.beginGroup("state");
+       settings.setValue("session_id", app->sessionId());
+       settings.setValue("in_tray", inTray);
+       settings.endGroup();
+}