From 10499691b3b5b736267603ce7bbaa8d646fa98c6 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Sun, 10 Aug 2014 14:22:23 +0400 Subject: [PATCH] rewrite tray --- README.md | 4 +- sources/gui/docs/netctl-gui-dbus-api.html | 6 + sources/gui/src/mainactions.cpp | 178 +++++++++++++----- sources/gui/src/mainwindow.cpp | 63 ++++++- sources/gui/src/mainwindow.h | 8 +- sources/gui/src/netctlautowindow.cpp | 2 +- sources/gui/src/netctlguiadaptor.cpp | 8 +- sources/gui/src/netctlguiadaptor.h | 3 +- sources/gui/src/trayicon.cpp | 217 +++++++++++++++++++--- sources/gui/src/trayicon.h | 17 +- sources/resources/resources.qrc | 2 + 11 files changed, 420 insertions(+), 88 deletions(-) diff --git a/README.md b/README.md index c770670..cc21fbe 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,8 @@ TODO (wish list) ---------------- * plasmoid helper integration -* more tray actions -* include security notes / project architecture +* remove suid from helper (polkit integration or run through sudo) +* security notes / project architecture * autotests Links diff --git a/sources/gui/docs/netctl-gui-dbus-api.html b/sources/gui/docs/netctl-gui-dbus-api.html index 67e7047..497d535 100644 --- a/sources/gui/docs/netctl-gui-dbus-api.html +++ b/sources/gui/docs/netctl-gui-dbus-api.html @@ -307,6 +307,12 @@ th.sub { true and shows settings window no + + QStringList VerboseInformation() + returns information in format [isNetctlAutoActive, ProfileList, ActiveProfile, isProfileEnabled/autoIsProfileEnabled] + no + no +

External links

diff --git a/sources/gui/src/mainactions.cpp b/sources/gui/src/mainactions.cpp index 0255433..1468ca8 100644 --- a/sources/gui/src/mainactions.cpp +++ b/sources/gui/src/mainactions.cpp @@ -117,6 +117,132 @@ void MainWindow::showSettingsWindow() } +bool MainWindow::enableProfileSlot(const QString profile) +{ + if (debug) qDebug() << "[MainWindow]" << "[enableProfileSlot]"; + if (debug) qDebug() << "[MainWindow]" << "[enableProfileSlot]" << ":" << "Profile" << profile; + + bool current; + if (useHelper) { + QList args; + args.append(profile); + sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, + DBUS_HELPER_INTERFACE, QString("Enable"), + args, true, debug); + current = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("isProfileEnabled"), + args, true, debug)[0].toBool(); + } + else { + netctlCommand->enableProfile(profile); + current = netctlCommand->isProfileEnabled(profile); + } + + return current; +} + + +bool MainWindow::restartProfileSlot(const QString profile) +{ + if (debug) qDebug() << "[MainWindow]" << "[restartProfileSlot]"; + if (debug) qDebug() << "[MainWindow]" << "[restartProfileSlot]" << ":" << "Profile" << profile; + + bool current; + if (useHelper) { + QList args; + args.append(profile); + sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, + DBUS_HELPER_INTERFACE, QString("Restart"), + args, true, debug)[0].toBool(); + current = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("isProfileActive"), + args, true, debug)[0].toBool(); + } + else { + netctlCommand->restartProfile(profile); + current = netctlCommand->isProfileActive(profile); + } + + return current; +} + + +bool MainWindow::startProfileSlot(const QString profile) +{ + if (debug) qDebug() << "[MainWindow]" << "[startProfileSlot]"; + if (debug) qDebug() << "[MainWindow]" << "[startProfileSlot]" << ":" << "Profile" << profile; + + bool current; + if (useHelper) { + QList args; + args.append(profile); + sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, + DBUS_HELPER_INTERFACE, QString("Start"), + args, true, debug); + current = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("isProfileActive"), + args, true, debug)[0].toBool(); + } + else { + netctlCommand->startProfile(profile); + current = netctlCommand->isProfileActive(profile); + } + + return current; +} + + +bool MainWindow::switchToProfileSlot(const QString profile) +{ + if (debug) qDebug() << "[MainWindow]" << "[switchToProfileSlot]"; + if (debug) qDebug() << "[MainWindow]" << "[switchToProfileSlot]" << ":" << "Profile" << profile; + + bool netctlAutoStatus = false; + if (useHelper) + netctlAutoStatus = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("isNetctlAutoActive"), + QList(), true, debug)[0].toBool(); + else + netctlAutoStatus = netctlCommand->isNetctlAutoRunning(); + + bool current; + if (netctlAutoStatus) { + if (useHelper) { + QList args; + args.append(profile); + sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, + DBUS_HELPER_INTERFACE, QString("autoStart"), + args, true, debug); + current = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("autoIsProfileActive"), + args, true, debug)[0].toBool(); + } + else { + netctlCommand->autoStartProfile(profile); + current = netctlCommand->autoIsProfileActive(profile); + } + } + else { + if (useHelper) { + QList args; + args.append(profile); + sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, + DBUS_HELPER_INTERFACE, QString("SwitchTo"), + args, true, debug); + current = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("isProfileActive"), + args, true, debug)[0].toBool(); + } + else { + netctlCommand->switchToProfile(profile); + current = netctlCommand->isProfileActive(profile); + } + } + + return current; +} + + void MainWindow::showApi() { if (debug) qDebug() << "[MainWindow]" << "[showApi]"; @@ -400,23 +526,9 @@ void MainWindow::mainTabEnableProfile() return; ui->tabWidget->setDisabled(true); - bool previous = !ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 3)->text().isEmpty(); - bool current; QString profile = ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 0)->text(); - if (useHelper) { - QList args; - args.append(profile); - sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, - DBUS_HELPER_INTERFACE, QString("Enable"), - args, true, debug); - current = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, - DBUS_HELPER_INTERFACE, QString("isProfileEnabled"), - args, true, debug)[0].toBool(); - } - else { - netctlCommand->enableProfile(profile); - current = netctlCommand->isProfileEnabled(profile); - } + bool previous = !ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 3)->text().isEmpty(); + bool current = enableProfileSlot(profile); if (current != previous) ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done")); else @@ -462,21 +574,7 @@ void MainWindow::mainTabRestartProfile() ui->tabWidget->setDisabled(true); QString profile = ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 0)->text(); - bool status = false; - if (useHelper) { - QList args; - args.append(profile); - sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, - DBUS_HELPER_INTERFACE, QString("Restart"), - args, true, debug)[0].toBool(); - status = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, - DBUS_HELPER_INTERFACE, QString("isProfileActive"), - args, true, debug)[0].toBool(); - } - else { - netctlCommand->restartProfile(profile); - status = netctlCommand->isProfileActive(profile); - } + bool status = restartProfileSlot(profile); if (status) ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done")); else @@ -495,23 +593,9 @@ void MainWindow::mainTabStartProfile() return; ui->tabWidget->setDisabled(true); - bool previous = !ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 2)->text().isEmpty(); - bool current; QString profile = ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 0)->text(); - if (useHelper) { - QList args; - args.append(profile); - sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, - DBUS_HELPER_INTERFACE, QString("Start"), - args, true, debug); - current = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, - DBUS_HELPER_INTERFACE, QString("isProfileActive"), - args, true, debug)[0].toBool(); - } - else { - netctlCommand->startProfile(profile); - current = netctlCommand->isProfileActive(profile); - } + bool previous = !ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 2)->text().isEmpty(); + bool current = startProfileSlot(profile); if (current != previous) ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done")); else diff --git a/sources/gui/src/mainwindow.cpp b/sources/gui/src/mainwindow.cpp index df69857..6cd1e4d 100644 --- a/sources/gui/src/mainwindow.cpp +++ b/sources/gui/src/mainwindow.cpp @@ -116,7 +116,7 @@ MainWindow::~MainWindow() } -QString MainWindow::printInformation() +QStringList MainWindow::printInformation() { if (debug) qDebug() << "[MainWindow]" << "[printInformation]"; @@ -139,8 +139,9 @@ QString MainWindow::printInformation() status = netctlCommand->getProfileStatus(profile); } } - QString output = QString("%1: %2\n").arg(QApplication::translate("MainWindow", "Profile")).arg(profile); - output += QString("%1: %2").arg(QApplication::translate("MainWindow", "Status")).arg(status); + QStringList output; + output.append(profile); + output.append(status); return output; } @@ -159,6 +160,62 @@ QStringList MainWindow::printSettings() } +QStringList MainWindow::printTrayInformation() +{ + if (debug) qDebug() << "[MainWindow]" << "[printTrayInformation]"; + + QStringList information; + QString current; + bool enabled = false; + bool netctlAutoStatus = false; + QList profiles; + if (useHelper) { + current = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("ActiveProfile"), + QList(), true, debug)[0].toString(); + netctlAutoStatus = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("isNetctlAutoActive"), + QList(), true, debug)[0].toBool(); + profiles = parseOutputNetctl(sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("ProfileList"), + QList(), true, debug), debug); + QList args; + args.append(current); + if (netctlAutoStatus) + enabled = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("autoIsProfileEnabled"), + args, true, debug)[0].toBool(); + else + enabled = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, + DBUS_HELPER_INTERFACE, QString("isProfileEnabled"), + args, true, debug)[0].toBool(); + } + else { + netctlAutoStatus = netctlCommand->isNetctlAutoRunning(); + if (netctlAutoStatus) { + current = netctlCommand->autoGetActiveProfile(); + enabled = netctlCommand->autoIsProfileEnabled(current); + profiles = netctlCommand->getProfileListFromNetctlAuto(); + } + else { + current = netctlCommand->getActiveProfile(); + enabled = netctlCommand->isProfileEnabled(current); + profiles = netctlCommand->getProfileList(); + } + } + + information.append(QString::number(netctlAutoStatus)); + QStringList profileList; + for (int i=0; i args = QMap(), QTranslator *appTranslator = 0); ~MainWindow(); - QString printInformation(); + QStringList printInformation(); QStringList printSettings(); + QStringList printTrayInformation(); bool isHelperActive(); bool isHelperServiceActive(); @@ -71,6 +72,11 @@ public slots: void showMainWindow(); void showNetctlAutoWindow(); void showSettingsWindow(); + // trayicon control slots + bool enableProfileSlot(const QString profile); + bool startProfileSlot(const QString profile); + bool switchToProfileSlot(const QString profile); + bool restartProfileSlot(const QString profile); // open docs void showApi(); void showLibrary(); diff --git a/sources/gui/src/netctlautowindow.cpp b/sources/gui/src/netctlautowindow.cpp index 041c8b4..0736c49 100644 --- a/sources/gui/src/netctlautowindow.cpp +++ b/sources/gui/src/netctlautowindow.cpp @@ -176,7 +176,7 @@ void NetctlAutoWindow::netctlAutoUpdateTable() DBUS_HELPER_INTERFACE, QString("isNetctlAutoEnabled"), QList(), true, debug)[0].toBool(); running = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, - DBUS_HELPER_INTERFACE, QString("isNetctlAutoRunning"), + DBUS_HELPER_INTERFACE, QString("isNetctlAutoActive"), QList(), true, debug)[0].toBool(); } else { diff --git a/sources/gui/src/netctlguiadaptor.cpp b/sources/gui/src/netctlguiadaptor.cpp index a47cfc9..4b67220 100644 --- a/sources/gui/src/netctlguiadaptor.cpp +++ b/sources/gui/src/netctlguiadaptor.cpp @@ -45,7 +45,7 @@ bool NetctlGuiAdaptor::Close() } -QString NetctlGuiAdaptor::Information() +QStringList NetctlGuiAdaptor::Information() { return mainWindow->printInformation(); } @@ -102,3 +102,9 @@ bool NetctlGuiAdaptor::ShowSettings() mainWindow->showSettingsWindow(); return true; } + + +QStringList NetctlGuiAdaptor::VerboseInformation() +{ + return mainWindow->printTrayInformation(); +} diff --git a/sources/gui/src/netctlguiadaptor.h b/sources/gui/src/netctlguiadaptor.h index 828fe0a..b9c992d 100644 --- a/sources/gui/src/netctlguiadaptor.h +++ b/sources/gui/src/netctlguiadaptor.h @@ -36,7 +36,7 @@ public: public slots: void ApiDocs(); bool Close(); - QString Information(); + QStringList Information(); void LibraryDocs(); bool Restore(); void SecurityDocs(); @@ -45,6 +45,7 @@ public slots: bool ShowMain(); bool ShowNetctlAuto(); bool ShowSettings(); + QStringList VerboseInformation(); private: MainWindow *mainWindow; diff --git a/sources/gui/src/trayicon.cpp b/sources/gui/src/trayicon.cpp index 7b11730..0ac4598 100644 --- a/sources/gui/src/trayicon.cpp +++ b/sources/gui/src/trayicon.cpp @@ -39,10 +39,14 @@ TrayIcon::~TrayIcon() if (debug) qDebug() << "[TrayIcon]" << "[~TrayIcon]"; setContextMenu(0); - delete exit; - delete showMainWindow; - delete showNetctlAutoWindow; - delete showStatus; + startProfileMenu->clear(); + switchToProfileMenu->clear(); + menuActions->clear(); + delete startProfileMenu; + delete switchToProfileMenu; + delete menuActions; + for (int i=0; iprintInformation(); + QStringList info = mainWindow->printInformation(); + QString message = QString("%1: %2\n").arg(QApplication::translate("TrayIcon", "Profile")).arg(info[0]); + message += QString("%1: %2").arg(QApplication::translate("TrayIcon", "Status")).arg(info[1]); showMessage(title, message, QSystemTrayIcon::Information); } else @@ -66,12 +72,137 @@ int TrayIcon::showInformationInWindow() if (debug) qDebug() << "[TrayIcon]" << "[showInformationInWindow]"; QString title = QApplication::translate("TrayIcon", "netctl status"); - QString message = mainWindow->printInformation(); + QStringList info = mainWindow->printInformation(); + QString message = QString("%1: %2\n").arg(QApplication::translate("TrayIcon", "Profile")).arg(info[0]); + message += QString("%1: %2").arg(QApplication::translate("TrayIcon", "Status")).arg(info[1]); return QMessageBox::information(0, title, message); } +void TrayIcon::updateMenu() +{ + if (debug) qDebug() << "[TrayIcon]" << "[showInformationInWindow]"; + + QStringList info = mainWindow->printTrayInformation(); + bool netctlAutoStatus = info[0].toInt(); + QStringList profiles = info[1].split(QChar('|')); + QString current = info[2]; + bool enabled = info[3].toInt(); + + if (current.isEmpty()) { + contextMenu[QString("title")]->setIcon(QIcon(QString(":network-offline-64x64"))); + contextMenu[QString("title")]->setText(QApplication::translate("TrayIcon", "(inactive)")); + } + else { + contextMenu[QString("title")]->setIcon(QIcon(QString(":network-idle-64x64"))); + QString status; + if (netctlAutoStatus) + status = QApplication::translate("TrayIcon", "(netctl-auto)"); + else if (enabled) + status = QApplication::translate("TrayIcon", "(enabled)"); + else + status = QApplication::translate("TrayIcon", "(static)"); + contextMenu[QString("title")]->setText(current + QString(" ") + status); + } + + if (netctlAutoStatus) { + contextMenu[QString("start")]->setVisible(false); + contextMenu[QString("stop")]->setVisible(false); + contextMenu[QString("switch")]->setVisible(true); + contextMenu[QString("restart")]->setVisible(false); + contextMenu[QString("enable")]->setVisible(false); + + switchToProfileMenu->clear(); + for (int i=0; iaddAction(profile); + } + } + else { + contextMenu[QString("start")]->setVisible(true); + contextMenu[QString("stop")]->setVisible(!current.isEmpty()); + contextMenu[QString("switch")]->setVisible(false); + contextMenu[QString("restart")]->setVisible(!current.isEmpty()); + contextMenu[QString("enable")]->setVisible(!current.isEmpty()); + if (!current.isEmpty()) { + contextMenu[QString("start")]->setText(QApplication::translate("TrayIcon", "Start another profile")); + contextMenu[QString("stop")]->setText(QApplication::translate("TrayIcon", "Stop %1").arg(current)); + contextMenu[QString("restart")]->setText(QApplication::translate("TrayIcon", "Restart %1").arg(current)); + if (enabled) + contextMenu[QString("enable")]->setText(QApplication::translate("TrayIcon", "Disable %1").arg(current)); + else + contextMenu[QString("enable")]->setText(QApplication::translate("TrayIcon", "Enable %1").arg(current)); + } + else + contextMenu[QString("start")]->setText(QApplication::translate("TrayIcon", "Start profile")); + startProfileMenu->clear(); + for (int i=0; iaddAction(profile); + } + } + + if (mainWindow->isHidden()) + contextMenu[QString("gui")]->setText(QApplication::translate("TrayIcon", "Show")); + else + contextMenu[QString("gui")]->setText(QApplication::translate("TrayIcon", "Hide")); +} + + +void TrayIcon::createActions() +{ + if (debug) qDebug() << "[TrayIcon]" << "[createActions]"; + + menuActions = new QMenu(); + + contextMenu[QString("title")] = new QAction(QIcon(":icon"), QApplication::translate("TrayIcon", "Status"), this); + menuActions->addAction(contextMenu[QString("title")]); + + menuActions->addSeparator(); + + contextMenu[QString("start")] = new QAction(QIcon::fromTheme("system-run"), QApplication::translate("TrayIcon", "Start profile"), this); + startProfileMenu = new QMenu(); + contextMenu[QString("start")]->setMenu(startProfileMenu); + connect(startProfileMenu, SIGNAL(triggered(QAction *)), this, SLOT(startProfileSlot(QAction *))); + menuActions->addAction(contextMenu[QString("start")]); + + contextMenu[QString("stop")] = new QAction(QIcon::fromTheme("process-stop"), QApplication::translate("TrayIcon", "Stop profile"), this); + connect(contextMenu[QString("stop")], SIGNAL(triggered(bool)), this, SLOT(stopProfileSlot())); + menuActions->addAction(contextMenu[QString("stop")]); + + contextMenu[QString("switch")] = new QAction(QIcon::fromTheme("system-run"), QApplication::translate("TrayIcon", "Switch to profile"), this); + switchToProfileMenu = new QMenu(); + contextMenu[QString("switch")]->setMenu(switchToProfileMenu); + connect(switchToProfileMenu, SIGNAL(triggered(QAction *)), this, SLOT(switchToProfileSlot(QAction *))); + menuActions->addAction(contextMenu[QString("switch")]); + + contextMenu[QString("restart")] = new QAction(QIcon::fromTheme("stock-refresh"), QApplication::translate("TrayIcon", "Restart profile"), this); + connect(contextMenu[QString("restart")], SIGNAL(triggered(bool)), this, SLOT(restartProfileSlot())); + menuActions->addAction(contextMenu[QString("restart")]); + + contextMenu[QString("enable")] = new QAction(QApplication::translate("TrayIcon", "Enable profile"), this); + connect(contextMenu[QString("enable")], SIGNAL(triggered(bool)), this, SLOT(enableProfileSlot())); + menuActions->addAction(contextMenu[QString("enable")]); + + menuActions->addSeparator(); + + contextMenu[QString("gui")] = new QAction(QApplication::translate("TrayIcon", "Show"), this); + connect(contextMenu[QString("gui")], SIGNAL(triggered(bool)), mainWindow, SLOT(showMainWindow())); + menuActions->addAction(contextMenu[QString("gui")]); + + contextMenu[QString("auto")] = new QAction(QApplication::translate("TrayIcon", "Show netctl-auto"), this); + connect(contextMenu[QString("auto")], SIGNAL(triggered(bool)), mainWindow, SLOT(showNetctlAutoWindow())); + menuActions->addAction(contextMenu[QString("auto")]); + + menuActions->addSeparator(); + + contextMenu[QString("quit")] = new QAction(QApplication::translate("TrayIcon", "Quit"), this); + connect(contextMenu[QString("quit")], SIGNAL(triggered(bool)), mainWindow, SLOT(closeMainWindow())); + menuActions->addAction(contextMenu[QString("quit")]); +} + + void TrayIcon::init() { if (debug) qDebug() << "[TrayIcon]" << "[init]"; @@ -79,23 +210,8 @@ void TrayIcon::init() setIcon(QIcon(":icon")); setToolTip(QString("netctl-gui")); - exit = new QAction(QIcon::fromTheme("exit"), QApplication::translate("TrayIcon", "Quit"), this); - connect(exit, SIGNAL(triggered(bool)), mainWindow, SLOT(closeMainWindow())); - showMainWindow = new QAction(QApplication::translate("TrayIcon", "Show"), this); - connect(showMainWindow, SIGNAL(triggered(bool)), mainWindow, SLOT(showMainWindow())); - showNetctlAutoWindow = new QAction(QApplication::translate("TrayIcon", "Show netctl-auto"), this); - connect(showNetctlAutoWindow, SIGNAL(triggered(bool)),mainWindow, SLOT(showNetctlAutoWindow())); - showStatus = new QAction(QIcon(":icon"), QApplication::translate("TrayIcon", "Status"), this); - connect(showStatus, SIGNAL(triggered(bool)), this, SLOT(showInformation())); - - QMenu *menu = new QMenu(); - menu->addAction(showStatus); - menu->addSeparator(); - menu->addAction(showMainWindow); - menu->addAction(showNetctlAutoWindow); - menu->addSeparator(); - menu->addAction(exit); - setContextMenu(menu); + createActions(); + setContextMenu(menuActions); connect(this, SIGNAL(messageClicked()), this, SLOT(showInformationInWindow())); connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), @@ -116,12 +232,59 @@ void TrayIcon::itemActivated(const QSystemTrayIcon::ActivationReason reason) mainWindow->showMainWindow(); break; case QSystemTrayIcon::Context: - if (mainWindow->isHidden()) - showMainWindow->setText(QApplication::translate("TrayIcon", "Show")); - else - showMainWindow->setText(QApplication::translate("TrayIcon", "Hide")); + updateMenu(); break; default: break; } } + + +bool TrayIcon::enableProfileSlot() +{ + if (debug) qDebug() << "[TrayIcon]" << "[enableProfileSlot]"; + + QString profile = mainWindow->printInformation()[0]; + + return mainWindow->enableProfileSlot(profile); +} + + +bool TrayIcon::restartProfileSlot() +{ + if (debug) qDebug() << "[TrayIcon]" << "[restartProfileSlot]"; + + QString profile = mainWindow->printInformation()[0]; + + return mainWindow->restartProfileSlot(profile); +} + + +bool TrayIcon::startProfileSlot(QAction *action) +{ + if (debug) qDebug() << "[TrayIcon]" << "[startProfileSlot]"; + + QString profile = action->text().remove(QChar('&')); + + return mainWindow->switchToProfileSlot(profile); +} + + +bool TrayIcon::stopProfileSlot() +{ + if (debug) qDebug() << "[TrayIcon]" << "[stopProfileSlot]"; + + QString profile = mainWindow->printInformation()[0]; + + return mainWindow->startProfileSlot(profile); +} + + +bool TrayIcon::switchToProfileSlot(QAction *action) +{ + if (debug) qDebug() << "[TrayIcon]" << "[switchToProfileSlot]"; + + QString profile = action->text().remove(QChar('&')); + + return mainWindow->switchToProfileSlot(profile); +} diff --git a/sources/gui/src/trayicon.h b/sources/gui/src/trayicon.h index 0c1fa9b..e595eb0 100644 --- a/sources/gui/src/trayicon.h +++ b/sources/gui/src/trayicon.h @@ -37,19 +37,26 @@ public: public slots: int showInformation(); int showInformationInWindow(); + void updateMenu(); private slots: void itemActivated(const QSystemTrayIcon::ActivationReason reason); + bool enableProfileSlot(); + bool restartProfileSlot(); + bool startProfileSlot(QAction *action); + bool stopProfileSlot(); + bool switchToProfileSlot(QAction *action); private: bool debug; MainWindow *mainWindow; // contextual actions - QMenu *menu; - QAction *exit; - QAction *showMainWindow; - QAction *showNetctlAutoWindow; - QAction *showStatus; + QMenu *menuActions; + QMenu *startProfileMenu; + QMenu *switchToProfileMenu; + QMap contextMenu; + // functions + void createActions(); void init(); }; diff --git a/sources/resources/resources.qrc b/sources/resources/resources.qrc index e353a07..3db93ce 100644 --- a/sources/resources/resources.qrc +++ b/sources/resources/resources.qrc @@ -6,5 +6,7 @@ icon.png wifi.png + network-idle-64x64.png + network-offline-64x64.png