Added debug_timestamp option to Windows registry
[wpasupplicant] / wpa_supplicant / wpa_gui-qt4 / wpagui.cpp
index ca3be73..f4847c0 100644 (file)
@@ -37,7 +37,7 @@ WpaGui::WpaGui(QWidget *parent, const char *, Qt::WFlags)
                SLOT(eventHistory()));
        connect(fileSaveConfigAction, SIGNAL(triggered()), this,
                SLOT(saveConfig()));
-       connect(fileExitAction, SIGNAL(triggered()), this, SLOT(close()));
+       connect(fileExitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
        connect(networkAddAction, SIGNAL(triggered()), this,
                SLOT(addNetwork()));
        connect(networkEditAction, SIGNAL(triggered()), this,
@@ -79,6 +79,8 @@ WpaGui::WpaGui(QWidget *parent, const char *, Qt::WFlags)
        eh = NULL;
        scanres = NULL;
        udr = NULL;
+       tray_icon = NULL;
+       startInTray = false;
        ctrl_iface = NULL;
        ctrl_conn = NULL;
        monitor_conn = NULL;
@@ -87,6 +89,11 @@ WpaGui::WpaGui(QWidget *parent, const char *, Qt::WFlags)
 
        parse_argv();
 
+       if (QSystemTrayIcon::isSystemTrayAvailable())
+               createTrayIcon(startInTray);
+       else
+               show();
+
        textStatus->setText("connecting to wpa_supplicant");
        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), SLOT(ping()));
@@ -154,7 +161,7 @@ void WpaGui::parse_argv()
 {
        int c;
        for (;;) {
-               c = getopt(qApp->argc(), qApp->argv(), "i:p:");
+               c = getopt(qApp->argc(), qApp->argv(), "i:p:t");
                if (c < 0)
                        break;
                switch (c) {
@@ -166,6 +173,9 @@ void WpaGui::parse_argv()
                        free(ctrl_iface_dir);
                        ctrl_iface_dir = strdup(optarg);
                        break;
+               case 't':
+                       startInTray = true;
+                       break;
                }
        }
 }
@@ -710,6 +720,14 @@ void WpaGui::processMsg(char *msg)
                processCtrlReq(pos + strlen(WPA_CTRL_REQ));
        else if (str_match(pos, WPA_EVENT_SCAN_RESULTS) && scanres)
                scanres->updateResults();
+       else if (str_match(pos, WPA_EVENT_DISCONNECTED))
+               showTrayMessage(QSystemTrayIcon::Information, 3,
+                               "Disconnected from network.");
+       else if (str_match(pos, WPA_EVENT_CONNECTED)) {
+               showTrayMessage(QSystemTrayIcon::Information, 3,
+                               "Connection to network established.");
+               QTimer::singleShot(5 * 1000, this, SLOT(showTrayStatus()));
+       }
 }
 
 
@@ -1070,6 +1088,141 @@ void WpaGui::selectAdapter( const QString & sel )
 }
 
 
+void WpaGui::createTrayIcon(bool trayOnly)
+{
+       QApplication::setQuitOnLastWindowClosed(false);
+
+       tray_icon = new QSystemTrayIcon(this);
+       tray_icon->setToolTip(qAppName() + " - wpa_supplicant user interface");
+       tray_icon->setIcon(QIcon(":/icons/wpa_gui.svg"));
+
+       connect(tray_icon,
+               SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
+               this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
+
+       ackTrayIcon = false;
+
+       tray_menu = new QMenu(this);
+
+       disconnectAction = new QAction("&Disconnect", this);
+       reconnectAction = new QAction("Re&connect", this);
+       connect(disconnectAction, SIGNAL(triggered()), this,
+               SLOT(disconnect()));
+       connect(reconnectAction, SIGNAL(triggered()), this,
+               SLOT(connectB()));
+       tray_menu->addAction(disconnectAction);
+       tray_menu->addAction(reconnectAction);
+       tray_menu->addSeparator();
+
+       eventAction = new QAction("&Event History", this);
+       scanAction = new QAction("Scan &Results", this);
+       statAction = new QAction("S&tatus", this);
+       connect(eventAction, SIGNAL(triggered()), this, SLOT(eventHistory()));
+       connect(scanAction, SIGNAL(triggered()), this, SLOT(scan()));
+       connect(statAction, SIGNAL(triggered()), this, SLOT(showTrayStatus()));
+       tray_menu->addAction(eventAction);
+       tray_menu->addAction(scanAction);
+       tray_menu->addAction(statAction);
+       tray_menu->addSeparator();
+
+       showAction = new QAction("&Show Window", this);
+       hideAction = new QAction("&Hide Window", this);
+       quitAction = new QAction("&Quit", this);
+       connect(showAction, SIGNAL(triggered()), this, SLOT(show()));
+       connect(hideAction, SIGNAL(triggered()), this, SLOT(hide()));
+       connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
+       tray_menu->addAction(showAction);
+       tray_menu->addAction(hideAction);
+       tray_menu->addSeparator();
+       tray_menu->addAction(quitAction);
+
+       tray_icon->setContextMenu(tray_menu);
+
+       tray_icon->show();
+
+       if (!trayOnly)
+               show();
+}
+
+
+void WpaGui::showTrayMessage(QSystemTrayIcon::MessageIcon type, int sec,
+                            const QString & msg)
+{
+       if (!QSystemTrayIcon::supportsMessages())
+               return;
+
+       if (isVisible() || !tray_icon || !tray_icon->isVisible())
+               return;
+
+       tray_icon->showMessage(qAppName(), msg, type, sec * 1000);
+}
+
+
+void WpaGui::trayActivated(QSystemTrayIcon::ActivationReason how)
+ {
+       switch (how) {
+       /* use close() here instead of hide() and allow the
+        * custom closeEvent handler take care of children */
+       case QSystemTrayIcon::Trigger:
+               ackTrayIcon = true;
+               if (isVisible())
+                       close();
+               else
+                       show();
+               break;
+       case QSystemTrayIcon::MiddleClick:
+               showTrayStatus();
+               break;
+       default:
+               break;
+       }
+}
+
+
+void WpaGui::showTrayStatus()
+{
+       char buf[2048];
+       size_t len;
+
+       len = sizeof(buf) - 1;
+       if (ctrlRequest("STATUS", buf, &len) < 0)
+               return;
+       buf[len] = '\0';
+
+       QString msg, status(buf);
+
+       QStringList lines = status.split(QRegExp("\\n"));
+       for (QStringList::Iterator it = lines.begin();
+            it != lines.end(); it++) {
+               int pos = (*it).indexOf('=') + 1;
+               if (pos < 1)
+                       continue;
+
+               if ((*it).startsWith("bssid="))
+                       msg.append("BSSID:\t" + (*it).mid(pos) + "\n");
+               else if ((*it).startsWith("ssid="))
+                       msg.append("SSID: \t" + (*it).mid(pos) + "\n");
+               else if ((*it).startsWith("pairwise_cipher="))
+                       msg.append("PAIR: \t" + (*it).mid(pos) + "\n");
+               else if ((*it).startsWith("group_cipher="))
+                       msg.append("GROUP:\t" + (*it).mid(pos) + "\n");
+               else if ((*it).startsWith("key_mgmt="))
+                       msg.append("AUTH: \t" + (*it).mid(pos) + "\n");
+               else if ((*it).startsWith("wpa_state="))
+                       msg.append("STATE:\t" + (*it).mid(pos) + "\n");
+               else if ((*it).startsWith("ip_address="))
+                       msg.append("IP:   \t" + (*it).mid(pos) + "\n");
+               else if ((*it).startsWith("Supplicant PAE state="))
+                       msg.append("PAE:  \t" + (*it).mid(pos) + "\n");
+               else if ((*it).startsWith("EAP state="))
+                       msg.append("EAP:  \t" + (*it).mid(pos) + "\n");
+       }
+
+       if (!msg.isEmpty())
+               showTrayMessage(QSystemTrayIcon::Information, 10, msg);
+}
+
+
 void WpaGui::closeEvent(QCloseEvent *event)
 {
        if (eh) {
@@ -1090,5 +1243,21 @@ void WpaGui::closeEvent(QCloseEvent *event)
                udr = NULL;
        }
 
+       if (tray_icon && !ackTrayIcon) {
+               /* give user a visual hint that the tray icon exists */
+               if (QSystemTrayIcon::supportsMessages()) {
+                       hide();
+                       showTrayMessage(QSystemTrayIcon::Information, 3,
+                                       qAppName() + " will keep running in "
+                                       "the system tray.");
+               } else {
+                       QMessageBox::information(this, qAppName() + " systray",
+                                                "The program will keep "
+                                                "running in the system "
+                                                "tray.");
+               }
+               ackTrayIcon = true;
+       }
+
        event->accept();
 }