rewrite tray

This commit is contained in:
arcan1s 2014-08-10 14:22:23 +04:00
parent bf6d7cd53b
commit 10499691b3
11 changed files with 420 additions and 88 deletions

View File

@ -65,8 +65,8 @@ TODO (wish list)
---------------- ----------------
* plasmoid helper integration * plasmoid helper integration
* more tray actions * remove suid from helper (polkit integration or run through sudo)
* include security notes / project architecture * security notes / project architecture
* autotests * autotests
Links Links

View File

@ -307,6 +307,12 @@ th.sub {
<td><code>true</code> and shows settings window</td> <td><code>true</code> and shows settings window</td>
<td>no</td> <td>no</td>
</tr> </tr>
<tr>
<td>QStringList VerboseInformation()</td>
<td>returns information in format <code>[isNetctlAutoActive, ProfileList, ActiveProfile, isProfileEnabled/autoIsProfileEnabled]</code></td>
<td>no</td>
<td>no</td>
</tr>
</table> </table>
<h2><a href="#links" class="anchor" name="links"></a>External links</h2> <h2><a href="#links" class="anchor" name="links"></a>External links</h2>

View File

@ -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<QVariant> 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<QVariant> 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<QVariant> 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<QVariant>(), true, debug)[0].toBool();
else
netctlAutoStatus = netctlCommand->isNetctlAutoRunning();
bool current;
if (netctlAutoStatus) {
if (useHelper) {
QList<QVariant> 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<QVariant> 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() void MainWindow::showApi()
{ {
if (debug) qDebug() << "[MainWindow]" << "[showApi]"; if (debug) qDebug() << "[MainWindow]" << "[showApi]";
@ -400,23 +526,9 @@ void MainWindow::mainTabEnableProfile()
return; return;
ui->tabWidget->setDisabled(true); 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(); QString profile = ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 0)->text();
if (useHelper) { bool previous = !ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 3)->text().isEmpty();
QList<QVariant> args; bool current = enableProfileSlot(profile);
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);
}
if (current != previous) if (current != previous)
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done")); ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done"));
else else
@ -462,21 +574,7 @@ void MainWindow::mainTabRestartProfile()
ui->tabWidget->setDisabled(true); ui->tabWidget->setDisabled(true);
QString profile = ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 0)->text(); QString profile = ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 0)->text();
bool status = false; bool status = restartProfileSlot(profile);
if (useHelper) {
QList<QVariant> 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);
}
if (status) if (status)
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done")); ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done"));
else else
@ -495,23 +593,9 @@ void MainWindow::mainTabStartProfile()
return; return;
ui->tabWidget->setDisabled(true); 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(); QString profile = ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 0)->text();
if (useHelper) { bool previous = !ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 2)->text().isEmpty();
QList<QVariant> args; bool current = startProfileSlot(profile);
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);
}
if (current != previous) if (current != previous)
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done")); ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done"));
else else

View File

@ -116,7 +116,7 @@ MainWindow::~MainWindow()
} }
QString MainWindow::printInformation() QStringList MainWindow::printInformation()
{ {
if (debug) qDebug() << "[MainWindow]" << "[printInformation]"; if (debug) qDebug() << "[MainWindow]" << "[printInformation]";
@ -139,8 +139,9 @@ QString MainWindow::printInformation()
status = netctlCommand->getProfileStatus(profile); status = netctlCommand->getProfileStatus(profile);
} }
} }
QString output = QString("%1: %2\n").arg(QApplication::translate("MainWindow", "Profile")).arg(profile); QStringList output;
output += QString("%1: %2").arg(QApplication::translate("MainWindow", "Status")).arg(status); output.append(profile);
output.append(status);
return output; 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<netctlProfileInfo> profiles;
if (useHelper) {
current = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH,
DBUS_HELPER_INTERFACE, QString("ActiveProfile"),
QList<QVariant>(), true, debug)[0].toString();
netctlAutoStatus = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH,
DBUS_HELPER_INTERFACE, QString("isNetctlAutoActive"),
QList<QVariant>(), true, debug)[0].toBool();
profiles = parseOutputNetctl(sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH,
DBUS_HELPER_INTERFACE, QString("ProfileList"),
QList<QVariant>(), true, debug), debug);
QList<QVariant> 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<profiles.count(); i++)
profileList.append(profiles[i].name);
information.append(profileList.join(QChar('|')));
information.append(current);
information.append(QString::number(enabled));
return information;
}
bool MainWindow::isHelperActive() bool MainWindow::isHelperActive()
{ {
if (debug) qDebug() << "[MainWindow]" << "[isHelperActive]"; if (debug) qDebug() << "[MainWindow]" << "[isHelperActive]";

View File

@ -56,8 +56,9 @@ public:
const QMap<QString, QVariant> args = QMap<QString, QVariant>(), const QMap<QString, QVariant> args = QMap<QString, QVariant>(),
QTranslator *appTranslator = 0); QTranslator *appTranslator = 0);
~MainWindow(); ~MainWindow();
QString printInformation(); QStringList printInformation();
QStringList printSettings(); QStringList printSettings();
QStringList printTrayInformation();
bool isHelperActive(); bool isHelperActive();
bool isHelperServiceActive(); bool isHelperServiceActive();
@ -71,6 +72,11 @@ public slots:
void showMainWindow(); void showMainWindow();
void showNetctlAutoWindow(); void showNetctlAutoWindow();
void showSettingsWindow(); 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 // open docs
void showApi(); void showApi();
void showLibrary(); void showLibrary();

View File

@ -176,7 +176,7 @@ void NetctlAutoWindow::netctlAutoUpdateTable()
DBUS_HELPER_INTERFACE, QString("isNetctlAutoEnabled"), DBUS_HELPER_INTERFACE, QString("isNetctlAutoEnabled"),
QList<QVariant>(), true, debug)[0].toBool(); QList<QVariant>(), true, debug)[0].toBool();
running = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, running = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH,
DBUS_HELPER_INTERFACE, QString("isNetctlAutoRunning"), DBUS_HELPER_INTERFACE, QString("isNetctlAutoActive"),
QList<QVariant>(), true, debug)[0].toBool(); QList<QVariant>(), true, debug)[0].toBool();
} }
else { else {

View File

@ -45,7 +45,7 @@ bool NetctlGuiAdaptor::Close()
} }
QString NetctlGuiAdaptor::Information() QStringList NetctlGuiAdaptor::Information()
{ {
return mainWindow->printInformation(); return mainWindow->printInformation();
} }
@ -102,3 +102,9 @@ bool NetctlGuiAdaptor::ShowSettings()
mainWindow->showSettingsWindow(); mainWindow->showSettingsWindow();
return true; return true;
} }
QStringList NetctlGuiAdaptor::VerboseInformation()
{
return mainWindow->printTrayInformation();
}

View File

@ -36,7 +36,7 @@ public:
public slots: public slots:
void ApiDocs(); void ApiDocs();
bool Close(); bool Close();
QString Information(); QStringList Information();
void LibraryDocs(); void LibraryDocs();
bool Restore(); bool Restore();
void SecurityDocs(); void SecurityDocs();
@ -45,6 +45,7 @@ public slots:
bool ShowMain(); bool ShowMain();
bool ShowNetctlAuto(); bool ShowNetctlAuto();
bool ShowSettings(); bool ShowSettings();
QStringList VerboseInformation();
private: private:
MainWindow *mainWindow; MainWindow *mainWindow;

View File

@ -39,10 +39,14 @@ TrayIcon::~TrayIcon()
if (debug) qDebug() << "[TrayIcon]" << "[~TrayIcon]"; if (debug) qDebug() << "[TrayIcon]" << "[~TrayIcon]";
setContextMenu(0); setContextMenu(0);
delete exit; startProfileMenu->clear();
delete showMainWindow; switchToProfileMenu->clear();
delete showNetctlAutoWindow; menuActions->clear();
delete showStatus; delete startProfileMenu;
delete switchToProfileMenu;
delete menuActions;
for (int i=0; i<contextMenu.keys().count(); i++)
delete contextMenu[contextMenu.keys()[i]];
} }
@ -52,7 +56,9 @@ int TrayIcon::showInformation()
if (supportsMessages()) { if (supportsMessages()) {
QString title = QApplication::translate("TrayIcon", "netctl status"); 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]);
showMessage(title, message, QSystemTrayIcon::Information); showMessage(title, message, QSystemTrayIcon::Information);
} }
else else
@ -66,12 +72,137 @@ int TrayIcon::showInformationInWindow()
if (debug) qDebug() << "[TrayIcon]" << "[showInformationInWindow]"; if (debug) qDebug() << "[TrayIcon]" << "[showInformationInWindow]";
QString title = QApplication::translate("TrayIcon", "netctl status"); 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); 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; i<profiles.count(); i++) {
QAction *profile = new QAction(profiles[i], this);
switchToProfileMenu->addAction(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; i<profiles.count(); i++) {
QAction *profile = new QAction(profiles[i], this);
startProfileMenu->addAction(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() void TrayIcon::init()
{ {
if (debug) qDebug() << "[TrayIcon]" << "[init]"; if (debug) qDebug() << "[TrayIcon]" << "[init]";
@ -79,23 +210,8 @@ void TrayIcon::init()
setIcon(QIcon(":icon")); setIcon(QIcon(":icon"));
setToolTip(QString("netctl-gui")); setToolTip(QString("netctl-gui"));
exit = new QAction(QIcon::fromTheme("exit"), QApplication::translate("TrayIcon", "Quit"), this); createActions();
connect(exit, SIGNAL(triggered(bool)), mainWindow, SLOT(closeMainWindow())); setContextMenu(menuActions);
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);
connect(this, SIGNAL(messageClicked()), this, SLOT(showInformationInWindow())); connect(this, SIGNAL(messageClicked()), this, SLOT(showInformationInWindow()));
connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
@ -116,12 +232,59 @@ void TrayIcon::itemActivated(const QSystemTrayIcon::ActivationReason reason)
mainWindow->showMainWindow(); mainWindow->showMainWindow();
break; break;
case QSystemTrayIcon::Context: case QSystemTrayIcon::Context:
if (mainWindow->isHidden()) updateMenu();
showMainWindow->setText(QApplication::translate("TrayIcon", "Show"));
else
showMainWindow->setText(QApplication::translate("TrayIcon", "Hide"));
break; break;
default: default:
break; 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);
}

View File

@ -37,19 +37,26 @@ public:
public slots: public slots:
int showInformation(); int showInformation();
int showInformationInWindow(); int showInformationInWindow();
void updateMenu();
private slots: private slots:
void itemActivated(const QSystemTrayIcon::ActivationReason reason); void itemActivated(const QSystemTrayIcon::ActivationReason reason);
bool enableProfileSlot();
bool restartProfileSlot();
bool startProfileSlot(QAction *action);
bool stopProfileSlot();
bool switchToProfileSlot(QAction *action);
private: private:
bool debug; bool debug;
MainWindow *mainWindow; MainWindow *mainWindow;
// contextual actions // contextual actions
QMenu *menu; QMenu *menuActions;
QAction *exit; QMenu *startProfileMenu;
QAction *showMainWindow; QMenu *switchToProfileMenu;
QAction *showNetctlAutoWindow; QMap<QString, QAction*> contextMenu;
QAction *showStatus; // functions
void createActions();
void init(); void init();
}; };

View File

@ -6,5 +6,7 @@
<!-- icons --> <!-- icons -->
<file>icon.png</file> <file>icon.png</file>
<file>wifi.png</file> <file>wifi.png</file>
<file>network-idle-64x64.png</file>
<file>network-offline-64x64.png</file>
</qresource> </qresource>
</RCC> </RCC>