* refactoring and optimization KF5 part (#26, #31)

* add info instrument
* implement needToBeConfigured() slot, see #29
* fix (#30)
some improvements
This commit is contained in:
arcan1s
2015-02-17 10:44:51 +03:00
parent 9033c51bb4
commit a73968f98b
22 changed files with 225 additions and 208 deletions

View File

@ -2,10 +2,13 @@
set (SUBPROJECT plasma_applet_netctl)
message (STATUS "Subproject ${SUBPROJECT}")
# prepare
configure_file (metadata.desktop ${CMAKE_CURRENT_SOURCE_DIR}/package/metadata.desktop)
find_package (ECM 0.0.12 REQUIRED NO_MODULE)
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR})
find_package (Qt5 REQUIRED COMPONENTS Core DBus Qml Quick)
find_package (Qt5 REQUIRED COMPONENTS Core DBus Qml)
find_package (KF5 REQUIRED COMPONENTS I18n Notifications Plasma)
include (KDEInstallDirs)

View File

@ -46,26 +46,13 @@ Item {
// external
property variant iconPath: {
"true": plasmoid.configuration.activeIconPath,
"false": plasmoid.configuration.inactiveIconPath
"false": plasmoid.configuration.inactiveIconPath,
"N\\A": plasmoid.configuration.inactiveIconPath
}
property variant info: {
"active": "false",
"current": "N\\A",
"extip4": "127.0.0.1",
"extip6": "::1",
"info": "N\\A",
"intip4": "127.0.0.1",
"intip6": "::1",
"interfaces": "lo",
"profiles": "",
"status": "N\\A"
}
property string pattern: plasmoid.configuration.textPattern
property bool status: false
property string sudoPath: plasmoid.configuration.useSudo ? plasmoid.configuration.sudoPath : ""
// signals
signal needUpdate
signal netctlStateChanged
signal needMenuUpdate
// init
Plasmoid.icon: iconPath["false"]
@ -76,27 +63,14 @@ Item {
PlasmaCore.DataSource {
id: mainData
engine: "netctl"
connectedSources: ["active", "current", "extip4", "extip6", "interfaces", "intip4", "intip6", "profiles", "status"]
connectedSources: ["active", "current", "extip4", "extip6", "info",
"interfaces", "intip4", "intip6", "profiles", "status"]
interval: plasmoid.configuration.autoUpdateInterval
onNewData: {
if (debug) console.log("[main::onNewData] : Update source " + sourceName)
var needToBeUpdated = false
if (data.value == "N\\A") return
if (info[sourceName] != data.value) needToBeUpdated = true
if (sourceName == "active") {
if (info[sourceName] != data.value)
// inverterd status
NetctlAdds.sendNotification("Info", i18n("Network status has been changed to '%1'",
status ? i18n("inactive") : i18n("active")))
status = data.value == "true"
} else if (sourceName == "current") {
info["info"] = NetctlAdds.getInfo(data.value, info["status"])
}
// update
info[sourceName] = data.value
if (needToBeUpdated) needUpdate()
NetctlAdds.setDataBySource(sourceName, data)
}
}
@ -133,22 +107,25 @@ Item {
plasmoid.setAction("switchToProfile", i18n("Switch to profile"))
plasmoid.setAction("restartProfile", i18n("Restart profile"), "stock-refresh")
plasmoid.setAction("enableProfile", i18n("Enable profile"))
plasmoid.setAction("startWifi", i18n("Show WiFi menu"), "image://netctl/wifi.png")
plasmoid.setAction("startWifi", i18n("Show WiFi menu"), "netctl-gui-wifi")
// helper
if (plasmoid.configuration.useHelper) {
NetctlAdds.runCmd(plasmoid.configuration.helperPath)
plasmoid.configuration.useHelper = NetctlAdds.checkHelperStatus()
}
NetctlAdds.needToBeUpdated.connect(needUpdate)
}
onNeedUpdate: {
if (debug) console.log("[main::onNeedUpdate]")
icon.source = iconPath[info["active"]]
Plasmoid.icon = iconPath[info["active"]]
text.text = NetctlAdds.parsePattern(pattern, info)
Plasmoid.toolTipSubText = info["info"]
netctlStateChanged()
var iconStatus = NetctlAdds.valueByKey("active")
icon.source = iconPath[iconStatus]
Plasmoid.icon = iconPath[iconStatus]
text.text = NetctlAdds.parsePattern(plasmoid.configuration.textPattern)
Plasmoid.toolTipSubText = NetctlAdds.valueByKey("info")
needMenuUpdate()
// updae geometry
text.update()
icon.height = text.contentHeight
@ -160,7 +137,7 @@ Item {
}
onNetctlStateChanged: {
onNeedMenuUpdate: {
if (debug) console.log("[main::onNetctlStateChanged]")
var titleAction = plasmoid.action("titleAction")
@ -172,10 +149,14 @@ Item {
var enableAction = plasmoid.action("enableProfile")
var wifiAction = plasmoid.action("startWifi")
titleAction.iconSource = plasmoid.icon
titleAction.text = info["current"] + " " + info["status"]
var current = NetctlAdds.valueByKey("current")
var status = NetctlAdds.valueByKey("active") == "true"
var stringStatus = NetctlAdds.valueByKey("status")
if (info["status"] == "(netctl-auto)") {
titleAction.iconSource = plasmoid.icon
titleAction.text = current + " " + stringStatus
if (stringStatus == "(netctl-auto)") {
startAction.visible = false
stopAction.visible = false
stopAllAction.visible = false
@ -183,7 +164,7 @@ Item {
restartAction.visible = false
enableAction.visible = false
} else {
if (info["current"].indexOf("|") > -1) {
if (current.indexOf("|") > -1) {
startAction.visible = true
stopAction.visible = false
stopAllAction.visible = true
@ -200,12 +181,12 @@ Item {
}
if (status) {
startAction.text = i18n("Start another profile")
stopAction.text = i18n("Stop %1", info["current"])
restartAction.text = i18n("Restart %1", info["current"])
if (info["status"].indexOf("enabled") > -1)
enableAction.text = i18n("Disable %1", info["current"])
stopAction.text = i18n("Stop %1", current)
restartAction.text = i18n("Restart %1", current)
if (stringStatus.indexOf("enabled") > -1)
enableAction.text = i18n("Disable %1", current)
else
enableAction.text = i18n("Enable %1", info["current"])
enableAction.text = i18n("Enable %1", current)
} else
startAction.text = i18n("Start profile")
}
@ -222,8 +203,7 @@ Item {
function action_startProfile() {
if (debug) console.log("[main::action_startProfile]")
NetctlAdds.startProfileSlot(info["profiles"].split(","), status,
plasmoid.configuration.useHelper,
NetctlAdds.startProfileSlot(plasmoid.configuration.useHelper,
plasmoid.configuration.netctlPath,
sudoPath)
}
@ -231,7 +211,7 @@ Item {
function action_stopProfile() {
if (debug) console.log("[main::action_stopProfile]")
NetctlAdds.stopProfileSlot(info, plasmoid.configuration.useHelper,
NetctlAdds.stopProfileSlot(plasmoid.configuration.useHelper,
plasmoid.configuration.netctlPath,
sudoPath)
}
@ -247,15 +227,14 @@ Item {
function action_switchToProfile() {
if (debug) console.log("[main::action_switchToProfile]")
NetctlAdds.switchToProfileSlot(info["profiles"].split(","),
plasmoid.configuration.useHelper,
NetctlAdds.switchToProfileSlot(plasmoid.configuration.useHelper,
plasmoid.configuration.netctlAutoPath)
}
function action_restartProfile() {
if (debug) console.log("[main::action_restartProfile]")
NetctlAdds.restartProfileSlot(info, plasmoid.configuration.useHelper,
NetctlAdds.restartProfileSlot(plasmoid.configuration.useHelper,
plasmoid.configuration.netctlPath,
sudoPath)
}
@ -263,7 +242,7 @@ Item {
function action_enableProfile() {
if (debug) console.log("[main::action_enableProfile]")
NetctlAdds.enableProfileSlot(info, plasmoid.configuration.useHelper,
NetctlAdds.enableProfileSlot(plasmoid.configuration.useHelper,
plasmoid.configuration.netctlPath,
sudoPath)
}

View File

@ -3,12 +3,10 @@ set (PLUGIN_NAME netctlplugin)
add_definitions (${Qt5Core_DEFINITIONS} ${Qt5DBus_DEFINITIONS} ${Qt5Qml_DEFINITIONS})
set (Qt_INCLUDE ${Qt5Core_INCLUDE_DIRS}
${Qt5DBus_INCLUDE_DIRS}
${Qt5Qml_INCLUDE_DIRS}
${Qt5Quick_INCLUDE_DIRS})
${Qt5Qml_INCLUDE_DIRS})
set (Qt_LIBRARIES ${Qt5Core_LIBRARIES}
${Qt5DBus_LIBRARIES}
${Qt5Qml_LIBRARIES}
${Qt5Quick_LIBRARIES})
${Qt5Qml_LIBRARIES})
set (Kf5_INCLUDE ${I18n_INCLUDE_DIR}
${Notifications_INCLUDE_DIR})
set (Kf5_LIBRARIES KF5::I18n
@ -24,10 +22,8 @@ include_directories (${CMAKE_SOURCE_DIR}
file (GLOB SUBPROJECT_SOURCE *.cpp)
file (GLOB SUBPROJECT_NOTIFY *.notifyrc)
set (RESOURCES ${PROJECT_RESOURCE_DIR}/resources-plasmoid.qrc)
qt5_add_resources (QRC_SOURCES ${RESOURCES})
add_library (${PLUGIN_NAME} SHARED ${SUBPROJECT_SOURCE} ${QRC_SOURCES})
add_library (${PLUGIN_NAME} SHARED ${SUBPROJECT_SOURCE})
target_link_libraries (${PLUGIN_NAME} ${Qt_LIBRARIES} ${Kf5_LIBRARIES})
install (TARGETS ${PLUGIN_NAME} DESTINATION ${QML_INSTALL_DIR}/org/kde/plasma/private/netctl)

View File

@ -30,14 +30,6 @@ static QObject *netctl_singletontype_provider(QQmlEngine *engine, QJSEngine *scr
}
void NetctlPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
{
Q_UNUSED(uri);
engine->addImageProvider("netctl", new NetctlAddsIconProvider());
}
void NetctlPlugin::registerTypes(const char *uri)
{
Q_ASSERT(uri == QLatin1String("org.kde.plasma.private.netctl"));

View File

@ -30,7 +30,6 @@ class NetctlPlugin : public QQmlExtensionPlugin
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
void initializeEngine(QQmlEngine *engine, const char *uri);
void registerTypes(const char *uri);
};

View File

@ -32,21 +32,6 @@
#include "version.h"
NetctlAddsIconProvider::NetctlAddsIconProvider()
: QQuickImageProvider(QQmlImageProviderBase::Pixmap)
{
}
QPixmap NetctlAddsIconProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
Q_UNUSED(size);
Q_UNUSED(requestedSize);
return QPixmap(QString("qrc:/") + id);
}
NetctlAdds::NetctlAdds(QObject *parent)
: QObject(parent)
{
@ -127,22 +112,6 @@ QString NetctlAdds::getAboutText(const QString type)
}
QString NetctlAdds::getInfo(const QString current, const QString status)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Current profiles" << current;
if (debug) qDebug() << PDEBUG << ":" << "Statuses" << status;
QStringList profiles;
for (int i=0; i<current.split(QChar('|')).count(); i++)
profiles.append(current.split(QChar('|'))[i] +
QString(" (") + status.split(QChar('|'))[i] + QString(")"));
if (profiles.isEmpty()) profiles.append(QString("N\\A"));
return profiles.join(QString(" | "));
}
bool NetctlAdds::isDebugEnabled()
{
if (debug) qDebug() << PDEBUG;
@ -151,14 +120,13 @@ bool NetctlAdds::isDebugEnabled()
}
QString NetctlAdds::parsePattern(const QString pattern, const QMap<QString, QVariant> dict)
QString NetctlAdds::parsePattern(const QString pattern)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Dictionary" << dict;
QString parsed = pattern;
for (int i=0; i<dict.keys().count(); i++)
parsed.replace(QString("$") + dict.keys()[i], dict[dict.keys()[i]].toString());
for (int i=0; i<values.keys().count(); i++)
parsed.replace(QString("$") + values.keys()[i], valueByKey(values.keys()[i]));
// fix newline
parsed.replace(QString("\n"), QString("<br>"));
@ -178,67 +146,88 @@ void NetctlAdds::runCmd(const QString cmd)
}
void NetctlAdds::sendNotification(const QString eventId, const QString message)
void NetctlAdds::setDataBySource(const QString sourceName, const QMap<QString, QVariant> data)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Event" << eventId;
if (debug) qDebug() << PDEBUG << ":" << "Message" << message;
if (debug) qDebug() << PDEBUG << ":" << "Source" << sourceName;
if (debug) qDebug() << PDEBUG << ":" << "Data" << data;
bool needUpdate = (values[sourceName] != data[QString("value")].toString());
values[sourceName] = data[QString("value")].toString();
if ((needUpdate) && (sourceName == QString("active"))) {
if (values[sourceName] == QString("true"))
sendNotification(QString("Info"), i18n("Network status has been changed to active"));
else
sendNotification(QString("Info"), i18n("Network status has been changed to inactive"));
}
if (needUpdate) emit(needToBeUpdated());
}
void NetctlAdds::sendNotification(const QString eventId, const QString message)
{
KNotification *notification = KNotification::event(eventId, QString("Netctl ::: ") + eventId, message);
notification->setComponentName(QString("plasma-applet-org.kde.plasma.netctl"));
}
QString NetctlAdds::valueByKey(const QString key)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Key" << key;
return values[key];
}
// context menu
void NetctlAdds::enableProfileSlot(const QMap<QString, QVariant> dict, const bool useHelper,
const QString cmd, const QString sudoCmd)
void NetctlAdds::enableProfileSlot(const bool useHelper, const QString cmd, const QString sudoCmd)
{
if (debug) qDebug() << PDEBUG;
QString enableStatus = QString("");
if (dict[QString("status")].toString().contains(QString("enabled"))) {
if (values[QString("status")].contains(QString("enabled"))) {
enableStatus = QString(" disable ");
sendNotification(QString("Info"), i18n("Set profile %1 disabled", dict[QString("current")].toString()));
sendNotification(QString("Info"), i18n("Set profile %1 disabled", values[QString("current")]));
} else {
enableStatus = QString(" enable ");
sendNotification(QString("Info"), i18n("Set profile %1 enabled", dict[QString("current")].toString()));
sendNotification(QString("Info"), i18n("Set profile %1 enabled", values[QString("current")]));
}
if (useHelper) {
QList<QVariant> args;
args.append(dict[QString("current")].toString());
args.append(values[QString("current")]);
sendDBusRequest(QString("Enable"), args);
} else {
QProcess command;
QString commandLine = sudoCmd + QString(" ") + cmd + enableStatus + dict[QString("current")].toString();
QString commandLine = sudoCmd + QString(" ") + cmd + enableStatus + values[QString("current")];
command.startDetached(commandLine);
}
}
void NetctlAdds::restartProfileSlot(const QMap<QString, QVariant> dict, const bool useHelper,
const QString cmd, const QString sudoCmd)
void NetctlAdds::restartProfileSlot(const bool useHelper, const QString cmd, const QString sudoCmd)
{
if (debug) qDebug() << PDEBUG;
sendNotification(QString("Info"), i18n("Restart profile %1", dict[QString("current")].toString()));
sendNotification(QString("Info"), i18n("Restart profile %1", values[QString("current")]));
if (useHelper) {
QList<QVariant> args;
args.append(dict[QString("current")].toString());
args.append(values[QString("current")]);
sendDBusRequest(QString("Restart"), args);
} else {
QProcess command;
QString commandLine = sudoCmd + QString(" ") + cmd + QString(" restart ") + dict[QString("current")].toString();
QString commandLine = sudoCmd + QString(" ") + cmd + QString(" restart ") + values[QString("current")];
command.startDetached(commandLine);
}
}
void NetctlAdds::startProfileSlot(const QStringList profiles, const bool status,
const bool useHelper, const QString cmd, const QString sudoCmd)
void NetctlAdds::startProfileSlot(const bool useHelper, const QString cmd, const QString sudoCmd)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Profiles" << profiles;
QStringList profiles = values[QString("profiles")].split(QChar(','));
bool ok;
QString profile = QInputDialog::getItem(0, i18n("Select profile"), i18n("Profile:"),
profiles, 0, false, &ok);
@ -248,14 +237,14 @@ void NetctlAdds::startProfileSlot(const QStringList profiles, const bool status,
if (useHelper) {
QList<QVariant> args;
args.append(profile);
if (status)
if (values[QString("active")] == QString("true"))
sendDBusRequest(QString("SwitchTo"), args);
else
sendDBusRequest(QString("Start"), args);
} else {
QProcess command;
QString commandLine = sudoCmd + QString(" ") + cmd;
if (status)
if (values[QString("active")] == QString("true"))
commandLine += QString(" switch-to ") + profile;
else
commandLine += QString(" start ") + profile;
@ -264,19 +253,18 @@ void NetctlAdds::startProfileSlot(const QStringList profiles, const bool status,
}
void NetctlAdds::stopProfileSlot(const QMap<QString, QVariant> dict, const bool useHelper,
const QString cmd, const QString sudoCmd)
void NetctlAdds::stopProfileSlot(const bool useHelper, const QString cmd, const QString sudoCmd)
{
if (debug) qDebug() << PDEBUG;
sendNotification(QString("Info"), i18n("Stop profile %1", dict[QString("current")].toString()));
sendNotification(QString("Info"), i18n("Stop profile %1", values[QString("current")]));
if (useHelper) {
QList<QVariant> args;
args.append(dict[QString("current")].toString());
args.append(values[QString("current")]);
sendDBusRequest(QString("Start"), args);
} else {
QProcess command;
QString commandLine = sudoCmd + QString(" ") + cmd + QString(" stop ") + dict[QString("current")].toString();
QString commandLine = sudoCmd + QString(" ") + cmd + QString(" stop ") + values[QString("current")];
command.startDetached(commandLine);
}
}
@ -297,12 +285,11 @@ void NetctlAdds::stopAllProfilesSlot(const bool useHelper, const QString cmd, co
}
void NetctlAdds::switchToProfileSlot(const QStringList profiles, const bool useHelper,
const QString cmd)
void NetctlAdds::switchToProfileSlot(const bool useHelper, const QString cmd)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Profiles" << profiles;
QStringList profiles = values[QString("profiles")].split(QChar(','));
bool ok;
QString profile = QInputDialog::getItem(0, i18n("Select profile"), i18n("Profile:"),
profiles, 0, false, &ok);

View File

@ -19,22 +19,11 @@
#ifndef NETCTLADDS_H
#define NETCTLADDS_H
#include <QImage>
#include <QMap>
#include <QObject>
#include <QQuickImageProvider>
#include <QVariant>
class NetctlAddsIconProvider : public QQuickImageProvider
{
public:
NetctlAddsIconProvider();
virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
};
class NetctlAdds : public QObject
{
Q_OBJECT
@ -45,41 +34,41 @@ public:
Q_INVOKABLE bool checkHelperStatus();
Q_INVOKABLE QString getAboutText(const QString type = "header");
Q_INVOKABLE QString getInfo(const QString current, const QString status);
Q_INVOKABLE bool isDebugEnabled();
Q_INVOKABLE QString parsePattern(const QString pattern, const QMap<QString, QVariant> dict);
Q_INVOKABLE QString parsePattern(const QString pattern);
Q_INVOKABLE void runCmd(const QString cmd);
Q_INVOKABLE void sendNotification(const QString eventId, const QString message);
Q_INVOKABLE void setDataBySource(const QString sourceName, const QMap<QString, QVariant> data);
Q_INVOKABLE static void sendNotification(const QString eventId, const QString message);
Q_INVOKABLE QString valueByKey(const QString key);
// context menu
Q_INVOKABLE void enableProfileSlot(const QMap<QString, QVariant> dict,
const bool useHelper = true,
Q_INVOKABLE void enableProfileSlot(const bool useHelper = true,
const QString cmd = QString("/usr/bin/netctl"),
const QString sudoCmd = QString(""));
Q_INVOKABLE void restartProfileSlot(const QMap<QString, QVariant> dict,
const bool useHelper = true,
Q_INVOKABLE void restartProfileSlot(const bool useHelper = true,
const QString cmd = QString("/usr/bin/netctl"),
const QString sudoCmd = QString(""));
Q_INVOKABLE void startProfileSlot(const QStringList profiles, const bool status,
const bool useHelper = true,
Q_INVOKABLE void startProfileSlot(const bool useHelper = true,
const QString cmd = QString("/usr/bin/netctl"),
const QString sudoCmd = QString(""));
Q_INVOKABLE void stopProfileSlot(const QMap<QString, QVariant> dict,
const bool useHelper = true,
Q_INVOKABLE void stopProfileSlot(const bool useHelper = true,
const QString cmd = QString("/usr/bin/netctl"),
const QString sudoCmd = QString(""));
Q_INVOKABLE void stopAllProfilesSlot(const bool useHelper = true,
const QString cmd = QString("/usr/bin/netctl"),
const QString sudoCmd = QString(""));
Q_INVOKABLE void switchToProfileSlot(const QStringList profiles,
const bool useHelper = true,
Q_INVOKABLE void switchToProfileSlot(const bool useHelper = true,
const QString cmd = QString("/usr/bin/netctl-auto"));
// dataengine
Q_INVOKABLE QMap<QString, QVariant> readDataEngineConfiguration();
Q_INVOKABLE void writeDataEngineConfiguration(const QMap<QString, QVariant> configuration);
signals:
void needToBeUpdated();
private:
bool debug = false;
QList<QVariant> sendDBusRequest(const QString cmd, const QList<QVariant> args = QList<QVariant>());
QMap<QString, QString> values;
};