add test support of several active profiles to gui, library and helper

This commit is contained in:
arcan1s 2014-08-16 21:47:22 +04:00
parent f238ba3a6a
commit 87376f1a5c
11 changed files with 156 additions and 48 deletions

View File

@ -138,10 +138,11 @@ bool MainWindow::startProfileSlot(const QString profile)
if (useHelper) { if (useHelper) {
QList<QVariant> args; QList<QVariant> args;
args.append(profile); args.append(profile);
QString currentProfile = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, QStringList currentProfile = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH,
DBUS_HELPER_INTERFACE, QString("ActiveProfile"), DBUS_HELPER_INTERFACE, QString("ActiveProfile"),
QList<QVariant>(), true, debug)[0].toString(); QList<QVariant>(), true, debug)[0]
if ((currentProfile.isEmpty()) || (currentProfile == profile)) .toString().split(QChar('|'));
if ((currentProfile.isEmpty()) || (currentProfile.contains(profile)))
sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH,
DBUS_HELPER_INTERFACE, QString("Start"), DBUS_HELPER_INTERFACE, QString("Start"),
args, true, debug); args, true, debug);
@ -153,8 +154,8 @@ bool MainWindow::startProfileSlot(const QString profile)
DBUS_HELPER_INTERFACE, QString("isProfileActive"), DBUS_HELPER_INTERFACE, QString("isProfileActive"),
args, true, debug)[0].toBool(); args, true, debug)[0].toBool();
} else { } else {
QString currentProfile = netctlCommand->getActiveProfile(); QStringList currentProfile = netctlCommand->getActiveProfile();
if ((currentProfile.isEmpty()) || (currentProfile == profile)) if ((currentProfile.isEmpty()) || (currentProfile.contains(profile)))
netctlCommand->startProfile(profile); netctlCommand->startProfile(profile);
else else
netctlCommand->switchToProfile(profile); netctlCommand->switchToProfile(profile);
@ -165,6 +166,21 @@ bool MainWindow::startProfileSlot(const QString profile)
} }
bool MainWindow::stopAllProfilesSlot()
{
if (debug) qDebug() << PDEBUG;
if (useHelper)
sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH,
DBUS_HELPER_INTERFACE, QString("StopAll"),
QList<QVariant>(), true, debug);
else
netctlCommand->stopAllProfiles();
return true;
}
bool MainWindow::switchToProfileSlot(const QString profile) bool MainWindow::switchToProfileSlot(const QString profile)
{ {
if (debug) qDebug() << PDEBUG; if (debug) qDebug() << PDEBUG;

View File

@ -132,8 +132,12 @@ QStringList MainWindow::printInformation()
profile = netctlCommand->autoGetActiveProfile(); profile = netctlCommand->autoGetActiveProfile();
status = QString("netctl-auto"); status = QString("netctl-auto");
} else { } else {
profile = netctlCommand->getActiveProfile(); QStringList currentProfiles = netctlCommand->getActiveProfile();
status = netctlCommand->getProfileStatus(profile); profile = currentProfiles.join(QChar('|'));
QStringList statusList;
for (int i=0; i<currentProfiles.count(); i++)
statusList.append(netctlCommand->getProfileStatus(currentProfiles[i]));
status = statusList.join(QChar('|'));
} }
} }
QStringList output; QStringList output;
@ -163,7 +167,7 @@ QStringList MainWindow::printTrayInformation()
QStringList information; QStringList information;
QString current; QString current;
bool enabled = false; QString enabled;
bool netctlAutoStatus = false; bool netctlAutoStatus = false;
QList<netctlProfileInfo> profiles; QList<netctlProfileInfo> profiles;
if (useHelper) { if (useHelper) {
@ -176,25 +180,38 @@ QStringList MainWindow::printTrayInformation()
profiles = parseOutputNetctl(sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, profiles = parseOutputNetctl(sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH,
DBUS_HELPER_INTERFACE, QString("ProfileList"), DBUS_HELPER_INTERFACE, QString("ProfileList"),
QList<QVariant>(), true, debug), debug); QList<QVariant>(), true, debug), debug);
QList<QVariant> args; if (netctlAutoStatus) {
args.append(current); QList<QVariant> args;
if (netctlAutoStatus) args.append(current);
enabled = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, enabled = QString::number(sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH,
DBUS_HELPER_INTERFACE, QString("autoIsProfileEnabled"), DBUS_HELPER_INTERFACE, QString("autoIsProfileEnabled"),
args, true, debug)[0].toBool(); args, true, debug)[0].toBool());
else } else {
enabled = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH, QStringList currentProfiles = current.split(QChar('|'));
DBUS_HELPER_INTERFACE, QString("isProfileEnabled"), QStringList enabledList;
args, true, debug)[0].toBool(); for (int i=0; i<currentProfiles.count(); i++) {
QList<QVariant> args;
args.append(currentProfiles[i]);
enabledList.append(QString::number(
sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_LIB_PATH,
DBUS_HELPER_INTERFACE, QString("isProfileEnabled"),
args, true, debug)[0].toBool()));
enabled = enabledList.join(QChar('|'));
}
}
} else { } else {
netctlAutoStatus = netctlCommand->isNetctlAutoRunning(); netctlAutoStatus = netctlCommand->isNetctlAutoRunning();
if (netctlAutoStatus) { if (netctlAutoStatus) {
current = netctlCommand->autoGetActiveProfile(); current = netctlCommand->autoGetActiveProfile();
enabled = netctlCommand->autoIsProfileEnabled(current); enabled = QString::number(netctlCommand->autoIsProfileEnabled(current));
profiles = netctlCommand->getProfileListFromNetctlAuto(); profiles = netctlCommand->getProfileListFromNetctlAuto();
} else { } else {
current = netctlCommand->getActiveProfile(); QStringList currentProfiles = netctlCommand->getActiveProfile();
enabled = netctlCommand->isProfileEnabled(current); current = currentProfiles.join(QChar('|'));
QStringList enabledList;
for (int i=0; i<currentProfiles.count(); i++)
enabledList.append(QString::number(netctlCommand->isProfileEnabled(currentProfiles[i])));
enabled = enabledList.join(QChar('|'));
profiles = netctlCommand->getProfileList(); profiles = netctlCommand->getProfileList();
} }
} }
@ -205,7 +222,7 @@ QStringList MainWindow::printTrayInformation()
profileList.append(profiles[i].name); profileList.append(profiles[i].name);
information.append(profileList.join(QChar('|'))); information.append(profileList.join(QChar('|')));
information.append(current); information.append(current);
information.append(QString::number(enabled)); information.append(enabled);
return information; return information;
} }

View File

@ -76,6 +76,7 @@ public slots:
// trayicon control slots // trayicon control slots
bool enableProfileSlot(const QString profile); bool enableProfileSlot(const QString profile);
bool startProfileSlot(const QString profile); bool startProfileSlot(const QString profile);
bool stopAllProfilesSlot();
bool switchToProfileSlot(const QString profile); bool switchToProfileSlot(const QString profile);
bool restartProfileSlot(const QString profile); bool restartProfileSlot(const QString profile);
// open docs // open docs

View File

@ -89,26 +89,31 @@ void TrayIcon::updateMenu()
bool netctlAutoStatus = info[0].toInt(); bool netctlAutoStatus = info[0].toInt();
QStringList profiles = info[1].split(QChar('|')); QStringList profiles = info[1].split(QChar('|'));
QString current = info[2]; QString current = info[2];
bool enabled = info[3].toInt(); QString enabled = info[3];
if (current.isEmpty()) { if (current.isEmpty()) {
contextMenu[QString("title")]->setIcon(QIcon(QString(":network-offline-64x64"))); contextMenu[QString("title")]->setIcon(QIcon(QString(":network-offline-64x64")));
contextMenu[QString("title")]->setText(QApplication::translate("TrayIcon", "(inactive)")); contextMenu[QString("title")]->setText(QApplication::translate("TrayIcon", "(inactive)"));
} else { } else {
contextMenu[QString("title")]->setIcon(QIcon(QString(":network-idle-64x64"))); contextMenu[QString("title")]->setIcon(QIcon(QString(":network-idle-64x64")));
QString status; QStringList status;
if (netctlAutoStatus) if (netctlAutoStatus)
status = QApplication::translate("TrayIcon", "(netctl-auto)"); status.append(QApplication::translate("TrayIcon", "(netctl-auto)"));
else if (enabled) else {
status = QApplication::translate("TrayIcon", "(enabled)"); for (int i=0; i<enabled.split(QChar('|')).count(); i++)
else if (enabled.split(QChar('|'))[i] == QString("0"))
status = QApplication::translate("TrayIcon", "(static)"); status.append(QApplication::translate("TrayIcon", "static"));
contextMenu[QString("title")]->setText(current + QString(" ") + status); else
status.append(QApplication::translate("TrayIcon", "enabled"));
}
contextMenu[QString("title")]->setText(current + QString(" ") +
QString("(") + status.join(QChar('|')) + QString(")"));
} }
if (netctlAutoStatus) { if (netctlAutoStatus) {
contextMenu[QString("start")]->setVisible(false); contextMenu[QString("start")]->setVisible(false);
contextMenu[QString("stop")]->setVisible(false); contextMenu[QString("stop")]->setVisible(false);
contextMenu[QString("stopall")]->setVisible(false);
contextMenu[QString("switch")]->setVisible(true); contextMenu[QString("switch")]->setVisible(true);
contextMenu[QString("restart")]->setVisible(false); contextMenu[QString("restart")]->setVisible(false);
contextMenu[QString("enable")]->setVisible(false); contextMenu[QString("enable")]->setVisible(false);
@ -118,16 +123,26 @@ void TrayIcon::updateMenu()
switchToProfileMenu->addAction(profile); switchToProfileMenu->addAction(profile);
} }
} else { } else {
contextMenu[QString("start")]->setVisible(true); if (current.contains(QChar('|'))) {
contextMenu[QString("stop")]->setVisible(!current.isEmpty()); contextMenu[QString("start")]->setVisible(true);
contextMenu[QString("switch")]->setVisible(false); contextMenu[QString("stop")]->setVisible(false);
contextMenu[QString("restart")]->setVisible(!current.isEmpty()); contextMenu[QString("stopall")]->setVisible(true);
contextMenu[QString("enable")]->setVisible(!current.isEmpty()); contextMenu[QString("switch")]->setVisible(false);
contextMenu[QString("restart")]->setVisible(false);
contextMenu[QString("enable")]->setVisible(false);
} else {
contextMenu[QString("start")]->setVisible(true);
contextMenu[QString("stop")]->setVisible(!current.isEmpty());
contextMenu[QString("switch")]->setVisible(false);
contextMenu[QString("stopall")]->setVisible(false);
contextMenu[QString("restart")]->setVisible(!current.isEmpty());
contextMenu[QString("enable")]->setVisible(!current.isEmpty());
}
if (!current.isEmpty()) { if (!current.isEmpty()) {
contextMenu[QString("start")]->setText(QApplication::translate("TrayIcon", "Start another profile")); contextMenu[QString("start")]->setText(QApplication::translate("TrayIcon", "Start another profile"));
contextMenu[QString("stop")]->setText(QApplication::translate("TrayIcon", "Stop %1").arg(current)); contextMenu[QString("stop")]->setText(QApplication::translate("TrayIcon", "Stop %1").arg(current));
contextMenu[QString("restart")]->setText(QApplication::translate("TrayIcon", "Restart %1").arg(current)); contextMenu[QString("restart")]->setText(QApplication::translate("TrayIcon", "Restart %1").arg(current));
if (enabled) if (enabled.split(QChar('|'))[0].toInt())
contextMenu[QString("enable")]->setText(QApplication::translate("TrayIcon", "Disable %1").arg(current)); contextMenu[QString("enable")]->setText(QApplication::translate("TrayIcon", "Disable %1").arg(current));
else else
contextMenu[QString("enable")]->setText(QApplication::translate("TrayIcon", "Enable %1").arg(current)); contextMenu[QString("enable")]->setText(QApplication::translate("TrayIcon", "Enable %1").arg(current));
@ -168,6 +183,10 @@ void TrayIcon::createActions()
connect(contextMenu[QString("stop")], SIGNAL(triggered(bool)), this, SLOT(stopProfileSlot())); connect(contextMenu[QString("stop")], SIGNAL(triggered(bool)), this, SLOT(stopProfileSlot()));
menuActions->addAction(contextMenu[QString("stop")]); menuActions->addAction(contextMenu[QString("stop")]);
contextMenu[QString("stopall")] = new QAction(QIcon::fromTheme("process-stop"), QApplication::translate("TrayIcon", "Stop all profiles"), this);
connect(contextMenu[QString("stopall")], SIGNAL(triggered(bool)), this, SLOT(stopAllProfilesSlot()));
menuActions->addAction(contextMenu[QString("stopall")]);
contextMenu[QString("switch")] = new QAction(QIcon::fromTheme("system-run"), QApplication::translate("TrayIcon", "Switch to profile"), this); contextMenu[QString("switch")] = new QAction(QIcon::fromTheme("system-run"), QApplication::translate("TrayIcon", "Switch to profile"), this);
switchToProfileMenu = new QMenu(); switchToProfileMenu = new QMenu();
contextMenu[QString("switch")]->setMenu(switchToProfileMenu); contextMenu[QString("switch")]->setMenu(switchToProfileMenu);
@ -277,6 +296,14 @@ bool TrayIcon::stopProfileSlot()
} }
bool TrayIcon::stopAllProfilesSlot()
{
if (debug) qDebug() << PDEBUG;
return mainWindow->stopAllProfilesSlot();
}
bool TrayIcon::switchToProfileSlot(QAction *action) bool TrayIcon::switchToProfileSlot(QAction *action)
{ {
if (debug) qDebug() << PDEBUG; if (debug) qDebug() << PDEBUG;

View File

@ -45,6 +45,7 @@ private slots:
bool restartProfileSlot(); bool restartProfileSlot();
bool startProfileSlot(QAction *action); bool startProfileSlot(QAction *action);
bool stopProfileSlot(); bool stopProfileSlot();
bool stopAllProfilesSlot();
bool switchToProfileSlot(QAction *action); bool switchToProfileSlot(QAction *action);
private: private:

View File

@ -54,7 +54,9 @@ endif()
add_executable (${SUBPROJECT} ${SOURCES} ${HEADERS} ${MOC_SOURCES} ${QRC_SOURCES} ${TRANSLATIONS}) add_executable (${SUBPROJECT} ${SOURCES} ${HEADERS} ${MOC_SOURCES} ${QRC_SOURCES} ${TRANSLATIONS})
target_link_libraries (${SUBPROJECT} ${PROJECT_LIBRARY} ${QT_NEEDED_LIBS}) target_link_libraries (${SUBPROJECT} ${PROJECT_LIBRARY} ${QT_NEEDED_LIBS})
add_executable (${SUBPROJECT}-suid ${SOURCES} ${HEADERS} ${MOC_SOURCES} ${QRC_SOURCES} ${TRANSLATIONS})
target_link_libraries (${SUBPROJECT}-suid ${PROJECT_LIBRARY} ${QT_NEEDED_LIBS})
# install properties # install properties
install (TARGETS ${SUBPROJECT} DESTINATION bin) install (TARGETS ${SUBPROJECT} DESTINATION bin)
install (TARGETS ${SUBPROJECT} DESTINATION bin RENAME ${SUBPROJECT}-suid install (TARGETS ${SUBPROJECT}-suid DESTINATION bin
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE SETUID) PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE SETUID)

View File

@ -42,7 +42,7 @@ QString NetctlAdaptor::ActiveProfile()
if (isNetctlAutoActive()) if (isNetctlAutoActive())
return netctlCommand->autoGetActiveProfile(); return netctlCommand->autoGetActiveProfile();
else else
return netctlCommand->getActiveProfile(); return netctlCommand->getActiveProfile().join(QChar('|'));
} }
@ -50,8 +50,13 @@ QString NetctlAdaptor::ActiveProfileStatus()
{ {
if (isNetctlAutoActive()) if (isNetctlAutoActive())
return QString("netctl-auto"); return QString("netctl-auto");
else else {
return netctlCommand->getProfileStatus(ActiveProfile()); QStringList status;
QStringList profiles = ActiveProfile().split(QChar('|'));
for (int i=0; i<profiles.count(); i++)
status.append(netctlCommand->getProfileStatus(profiles[i]));
return status.join(QChar('|'));
}
} }

View File

@ -86,7 +86,7 @@ public:
* @brief method which returns active profile name * @brief method which returns active profile name
* @return profile name or "" * @return profile name or ""
*/ */
QString getActiveProfile(); QStringList getActiveProfile();
/** /**
* @brief method which returns active profile name from netctl-auto * @brief method which returns active profile name from netctl-auto
* @return profile name or "" * @return profile name or ""

View File

@ -147,17 +147,15 @@ QString Netctl::getCmdOutput(const bool sudo, const QString command, const QStri
/** /**
* @fn getActiveProfile * @fn getActiveProfile
*/ */
QString Netctl::getActiveProfile() QStringList Netctl::getActiveProfile()
{ {
if (debug) qDebug() << PDEBUG; if (debug) qDebug() << PDEBUG;
QString profile = QString(""); QStringList profile;
QList<netctlProfileInfo> fullProfilesInfo = getProfileList(); QList<netctlProfileInfo> fullProfilesInfo = getProfileList();
for (int i=0; i<fullProfilesInfo.count(); i++) for (int i=0; i<fullProfilesInfo.count(); i++)
if (fullProfilesInfo[i].active) { if (fullProfilesInfo[i].active)
profile = fullProfilesInfo[i].name; profile.append(fullProfilesInfo[i].name);
break;
}
return profile; return profile;
} }

View File

@ -23,6 +23,35 @@
#include <netctlgui/netctlgui.h> #include <netctlgui/netctlgui.h>
void TestNetctl::createTestProfile()
{
QMap<QString, QString> settings = NetctlProfile::getRecommendedConfiguration();
settings[QString("FORCE_SUDO")] = QString("true");
NetctlProfile *netctl = new NetctlProfile(false, settings);
QMap<QString, QString> profileSettings;
profileSettings["Connection"] = QString("dummy");
profileSettings["Description"] = QString("Simple test profile");
profileSettings["IP"] = QString("no");
profileSettings["IP6"] = QString("no");
profileSettings["Interface"] = QString("test");
netctl->copyProfile(netctl->createProfile(QString("aaatest"), settings));
delete netctl;
}
void TestNetctl::removeTestProfile()
{
QMap<QString, QString> settings = NetctlProfile::getRecommendedConfiguration();
settings[QString("FORCE_SUDO")] = QString("true");
NetctlProfile *netctl = new NetctlProfile(false, settings);
netctl->removeProfile(QString("aaatest"));
delete netctl;
}
void TestNetctl::test_getRecommendedConfiguration() void TestNetctl::test_getRecommendedConfiguration()
{ {
QStringList original; QStringList original;
@ -56,7 +85,15 @@ void TestNetctl::test_getActiveProfile()
settings[QString("FORCE_SUDO")] = QString("true"); settings[QString("FORCE_SUDO")] = QString("true");
Netctl *netctl = new Netctl(false, settings); Netctl *netctl = new Netctl(false, settings);
createTestProfile();
netctl->startProfile(QString("aaatest"));
QString original = QString("aaatest");
QString result = netctl->getActiveProfile();
netctl->startProfile(QString("aaatest"));
removeTestProfile();
delete netctl; delete netctl;
QCOMPARE(result, original);
} }

View File

@ -28,6 +28,10 @@ class TestNetctl : public QObject
private slots: private slots:
void test_getRecommendedConfiguration(); void test_getRecommendedConfiguration();
void test_getActiveProfile(); void test_getActiveProfile();
private:
void createTestProfile();
void removeTestProfile();
}; };