Commit e3f31bc8 authored by Nicolas Fella's avatar Nicolas Fella
Browse files

Make Configuration a proper singleton

Configuration already kind of is a singleton since it is accessed via static methods. Only when used from QML it is instantiated.

By having the methods static we cannot have a changed signal for the properties since a signal can only be emitted on an object.

Make it a singleton for both C++ and QML.
parent 27166167
......@@ -58,10 +58,6 @@ ColumnLayout {
}
}
PlasmaNM.Configuration {
id: configuration
}
RowLayout {
spacing: units.smallSpacing * 3
......@@ -115,14 +111,14 @@ ColumnLayout {
}
Binding {
target: configuration
target: PlasmaNM.Configuration
property: "airplaneModeEnabled"
value: planeModeSwitchButton.airplaneModeEnabled
when: planeModeSwitchButton.initialized
}
Component.onCompleted: {
airplaneModeEnabled = configuration.airplaneModeEnabled
airplaneModeEnabled = PlasmaNM.Configuration.airplaneModeEnabled
initialized = true
}
......@@ -143,7 +139,7 @@ ColumnLayout {
icon.name: "network-wireless-on"
onClicked: {
if (configuration.hotspotConnectionPath) {
if (PlasmaNM.Configuration.hotspotConnectionPath) {
checked = false
handler.stopHotspot()
} else {
......@@ -170,8 +166,8 @@ ColumnLayout {
}
Component.onCompleted: {
checked = configuration.hotspotConnectionPath
tooltip.text = configuration.hotspotConnectionPath ? i18n("Disable Hotspot") : i18n("Create Hotspot")
checked = PlasmaNM.Configuration.hotspotConnectionPath
tooltip.text = PlasmaNM.Configuration.hotspotConnectionPath ? i18n("Disable Hotspot") : i18n("Create Hotspot")
}
}
......
......@@ -29,10 +29,6 @@ Dialog {
standardButtons: Dialog.Ok | Dialog.Cancel
title: i18nc("@title:window", "Configuration")
PlasmaNM.Configuration {
id: configuration
}
contentItem: Item {
implicitHeight: 230
implicitWidth: 400
......@@ -59,14 +55,14 @@ Dialog {
id: unlockModem
text: i18n("Ask for PIN on modem detection")
onClicked: configurationChanged()
Component.onCompleted: checked = configuration.unlockModemOnDetection
Component.onCompleted: checked = PlasmaNM.Configuration.unlockModemOnDetection
}
QQC2.CheckBox {
id: manageVirtualConnections
text: i18n("Show virtual connections")
onClicked: configurationChanged()
Component.onCompleted: checked = configuration.manageVirtualConnections
Component.onCompleted: checked = PlasmaNM.Configuration.manageVirtualConnections
}
Kirigami.Heading {
......@@ -81,7 +77,7 @@ Dialog {
Kirigami.FormData.label: i18n("Hotspot name:")
onTextChanged: configurationChanged()
Component.onCompleted: {
text = configuration.hotspotName
text = PlasmaNM.Configuration.hotspotName
visible = handler.hotspotSupported
}
}
......@@ -100,7 +96,7 @@ Dialog {
onAcceptableInputChanged: configurationChanged()
Component.onCompleted: {
text = configuration.hotspotPassword
text = PlasmaNM.Configuration.hotspotPassword
visible = handler.hotspotSupported
}
}
......@@ -146,18 +142,18 @@ Dialog {
onVisibleChanged: {
if (visible) {
unlockModem.checked = configuration.unlockModemOnDetection
manageVirtualConnections.checked = configuration.manageVirtualConnections
hotspotName.text = configuration.hotspotName
hotspotPassword.text = configuration.hotspotPassword
unlockModem.checked = PlasmaNM.Configuration.unlockModemOnDetection
manageVirtualConnections.checked = PlasmaNM.Configuration.manageVirtualConnections
hotspotName.text = PlasmaNM.Configuration.hotspotName
hotspotPassword.text = PlasmaNM.Configuration.hotspotPassword
}
}
onAccepted: {
configuration.unlockModemOnDetection = unlockModem.checked
configuration.manageVirtualConnections = manageVirtualConnections.checked
configuration.hotspotName = hotspotName.text
configuration.hotspotPassword = hotspotPassword.text
PlasmaNM.Configuration.unlockModemOnDetection = unlockModem.checked
PlasmaNM.Configuration.manageVirtualConnections = manageVirtualConnections.checked
PlasmaNM.Configuration.hotspotName = hotspotName.text
PlasmaNM.Configuration.hotspotPassword = hotspotPassword.text
}
}
......@@ -59,10 +59,6 @@ Item {
sourceModel: connectionModel
}
PlasmaNM.Configuration {
id: configuration
}
QQC2.TextField {
id: searchField
......
......@@ -411,7 +411,7 @@ bool SecretAgent::processGetSecrets(SecretsRequest &request) const
}
}
if (!Configuration::showPasswordDialog()) {
if (!Configuration::self().showPasswordDialog()) {
sendError(SecretAgent::NoSecrets, "Cannot authenticate", request.message);
emit secretsError(request.connection_path.path(), i18n("Authentication to %1 failed. Wrong password?", request.connection.value("connection").value("id").toString()));
return true;
......
......@@ -27,6 +27,12 @@
static bool propManageVirtualConnectionsInitialized = false;
static bool propManageVirtualConnections = false;
Configuration &Configuration::self()
{
static Configuration c;
return c;
}
bool Configuration::unlockModemOnDetection()
{
KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("plasma-nm"));
......
......@@ -38,25 +38,27 @@ class Q_DECL_EXPORT Configuration : public QObject
Q_PROPERTY(bool showPasswordDialog READ showPasswordDialog CONSTANT)
Q_OBJECT
public:
static bool unlockModemOnDetection();
static void setUnlockModemOnDetection(bool unlock);
bool unlockModemOnDetection();
void setUnlockModemOnDetection(bool unlock);
static bool manageVirtualConnections();
static void setManageVirtualConnections(bool manage);
bool manageVirtualConnections();
void setManageVirtualConnections(bool manage);
static bool airplaneModeEnabled();
static void setAirplaneModeEnabled(bool enabled);
bool airplaneModeEnabled();
void setAirplaneModeEnabled(bool enabled);
static QString hotspotName();
static void setHotspotName(const QString &name);
QString hotspotName();
void setHotspotName(const QString &name);
static QString hotspotPassword();
static void setHotspotPassword(const QString &password);
QString hotspotPassword();
void setHotspotPassword(const QString &password);
static QString hotspotConnectionPath();
static void setHotspotConnectionPath(const QString &path);
QString hotspotConnectionPath();
void setHotspotConnectionPath(const QString &path);
static bool showPasswordDialog();
bool showPasswordDialog();
static Configuration &self();
};
#endif // PLAMA_NM_CONFIGURATION_H
......
......@@ -415,7 +415,7 @@ void ConnectionIcon::setIcons()
void ConnectionIcon::setDisconnectedIcon()
{
if (Configuration::airplaneModeEnabled()) {
if (Configuration::self().airplaneModeEnabled()) {
setConnectionIcon(QStringLiteral("network-flightmode-on"));
return;
}
......
......@@ -44,8 +44,6 @@ void QmlPlugins::registerTypes(const char* uri)
qmlRegisterType<AvailableDevices>(uri, 0, 2, "AvailableDevices");
// @uri org.kde.plasma.networkmanagement.ConnectionIcon
qmlRegisterType<ConnectionIcon>(uri, 0, 2, "ConnectionIcon");
// @uri org.kde.plasma.networkmanagement.Configuration
qmlRegisterType<Configuration>(uri, 0, 2, "Configuration");
// @uri org.kde.plasma.networkmanagement.EnabledConnections
qmlRegisterType<EnabledConnections>(uri, 0, 2, "EnabledConnections");
// @uri org.kde.plasma.networkmanagement.Enums
......@@ -66,4 +64,12 @@ void QmlPlugins::registerTypes(const char* uri)
qmlRegisterType<CreatableConnectionsModel>(uri, 0, 2, "CreatableConnectionsModel");
// @uri org.kde.plasma.networkmanagement.MobileProxyModel
qmlRegisterType<MobileProxyModel>(uri, 0, 2, "MobileProxyModel");
QQmlEngine::setObjectOwnership(&Configuration::self(), QQmlEngine::CppOwnership);
qmlRegisterSingletonType<Configuration>(uri, 0, 2, "Configuration", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
return &Configuration::self();
});
}
......@@ -75,10 +75,10 @@ Handler::Handler(QObject *parent)
this, SLOT(secretAgentError(QString, QString)));
if (!Configuration::hotspotConnectionPath().isEmpty()) {
NetworkManager::ActiveConnection::Ptr hotspot = NetworkManager::findActiveConnection(Configuration::hotspotConnectionPath());
if (!Configuration::self().hotspotConnectionPath().isEmpty()) {
NetworkManager::ActiveConnection::Ptr hotspot = NetworkManager::findActiveConnection(Configuration::self().hotspotConnectionPath());
if (!hotspot) {
Configuration::setHotspotConnectionPath(QString());
Configuration::self().setHotspotConnectionPath(QString());
}
}
......@@ -551,7 +551,7 @@ void Handler::createHotspot()
NetworkManager::WirelessSetting::Ptr wifiSetting = connectionSettings->setting(NetworkManager::Setting::Wireless).dynamicCast<NetworkManager::WirelessSetting>();
wifiSetting->setMode(NetworkManager::WirelessSetting::Adhoc);
wifiSetting->setSsid(Configuration::hotspotName().toUtf8());
wifiSetting->setSsid(Configuration::self().hotspotName().toUtf8());
for (const NetworkManager::Device::Ptr &device : NetworkManager::networkInterfaces()) {
if (device->type() == NetworkManager::Device::Wifi) {
......@@ -586,21 +586,21 @@ void Handler::createHotspot()
wifiSetting->setInitialized(true);
wifiSetting->setMode(useApMode ? NetworkManager::WirelessSetting::Ap :NetworkManager::WirelessSetting::Adhoc);
if (!Configuration::hotspotPassword().isEmpty()) {
if (!Configuration::self().hotspotPassword().isEmpty()) {
NetworkManager::WirelessSecuritySetting::Ptr wifiSecurity = connectionSettings->setting(NetworkManager::Setting::WirelessSecurity).dynamicCast<NetworkManager::WirelessSecuritySetting>();
wifiSecurity->setInitialized(true);
if (useApMode) {
// Use WPA2
wifiSecurity->setKeyMgmt(NetworkManager::WirelessSecuritySetting::WpaPsk);
wifiSecurity->setPsk(Configuration::hotspotPassword());
wifiSecurity->setPsk(Configuration::self().hotspotPassword());
wifiSecurity->setPskFlags(NetworkManager::Setting::AgentOwned);
} else {
// Use WEP
wifiSecurity->setKeyMgmt(NetworkManager::WirelessSecuritySetting::Wep);
wifiSecurity->setWepKeyType(NetworkManager::WirelessSecuritySetting::Passphrase);
wifiSecurity->setWepTxKeyindex(0);
wifiSecurity->setWepKey0(Configuration::hotspotPassword());
wifiSecurity->setWepKey0(Configuration::self().hotspotPassword());
wifiSecurity->setWepKeyFlags(NetworkManager::Setting::AgentOwned);
wifiSecurity->setAuthAlg(NetworkManager::WirelessSecuritySetting::Open);
}
......@@ -610,7 +610,7 @@ void Handler::createHotspot()
ipv4Setting->setMethod(NetworkManager::Ipv4Setting::Shared);
ipv4Setting->setInitialized(true);
connectionSettings->setId(Configuration::hotspotName());
connectionSettings->setId(Configuration::self().hotspotName());
connectionSettings->setAutoconnect(false);
connectionSettings->setUuid(NetworkManager::ConnectionSettings::createNewUuid());
......@@ -619,14 +619,14 @@ void Handler::createHotspot()
QDBusPendingReply<QDBusObjectPath, QDBusObjectPath, QVariantMap> reply = NetworkManager::addAndActivateConnection2(connectionSettings->toMap(), wifiDev->uni(), QString(), options);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
watcher->setProperty("action", Handler::CreateHotspot);
watcher->setProperty("connection", Configuration::hotspotName());
watcher->setProperty("connection", Configuration::self().hotspotName());
connect(watcher, &QDBusPendingCallWatcher::finished, this, &Handler::replyFinished);
connect(watcher, &QDBusPendingCallWatcher::finished, this, QOverload<QDBusPendingCallWatcher *>::of(&Handler::hotspotCreated));
}
void Handler::stopHotspot()
{
const QString activeConnectionPath = Configuration::hotspotConnectionPath();
const QString activeConnectionPath = Configuration::self().hotspotConnectionPath();
if (activeConnectionPath.isEmpty()) {
return;
......@@ -639,7 +639,7 @@ void Handler::stopHotspot()
}
NetworkManager::deactivateConnection(activeConnectionPath);
Configuration::setHotspotConnectionPath(QString());
Configuration::self().setHotspotConnectionPath(QString());
Q_EMIT hotspotDisabled();
}
......@@ -835,7 +835,7 @@ void Handler::hotspotCreated(QDBusPendingCallWatcher *watcher)
return;
}
Configuration::setHotspotConnectionPath(activeConnectionPath);
Configuration::self().setHotspotConnectionPath(activeConnectionPath);
NetworkManager::ActiveConnection::Ptr hotspot = NetworkManager::findActiveConnection(activeConnectionPath);
......@@ -845,7 +845,7 @@ void Handler::hotspotCreated(QDBusPendingCallWatcher *watcher)
connect(hotspot.data(), &NetworkManager::ActiveConnection::stateChanged, [=] (NetworkManager::ActiveConnection::State state) {
if (state > NetworkManager::ActiveConnection::Activated) {
Configuration::setHotspotConnectionPath(QString());
Configuration::self().setHotspotConnectionPath(QString());
Q_EMIT hotspotDisabled();
}
});
......
......@@ -175,7 +175,7 @@ CreatableConnectionsModel::CreatableConnectionsModel(QObject *parent)
QString(), QString(), true); // VpnType and SpecificType are empty
m_list << connectionItem;
if (Configuration::manageVirtualConnections()) {
if (Configuration::self().manageVirtualConnections()) {
connectionItem = new CreatableConnectionItem(i18n("Bond"), i18n("Virtual connections"),
i18n("Some bond description"), QStringLiteral("network-wired"),
NetworkManager::ConnectionSettings::Bond,
......
......@@ -447,8 +447,8 @@ void NetworkModel::addWirelessNetwork(const NetworkManager::WirelessNetwork::Ptr
initializeSignals(network);
// Avoid duplicating entries in the model
if (!Configuration::hotspotConnectionPath().isEmpty()) {
NetworkManager::ActiveConnection::Ptr activeConnection = NetworkManager::findActiveConnection(Configuration::hotspotConnectionPath());
if (!Configuration::self().hotspotConnectionPath().isEmpty()) {
NetworkManager::ActiveConnection::Ptr activeConnection = NetworkManager::findActiveConnection(Configuration::self().hotspotConnectionPath());
// If we are trying to add an AP which is the one created by our hotspot, then we can skip this and don't add it twice
if (activeConnection && activeConnection->specificObject() == network->referenceAccessPoint()->uni()) {
......
......@@ -110,7 +110,7 @@ bool UiUtils::isConnectionTypeSupported(NetworkManager::ConnectionSettings::Conn
return false;
}
bool manageVirtualConnections = Configuration::manageVirtualConnections();
bool manageVirtualConnections = Configuration::self().manageVirtualConnections();
if (type == NetworkManager::ConnectionSettings::Bond ||
type == NetworkManager::ConnectionSettings::Bridge ||
......
......@@ -30,10 +30,6 @@ SimpleKCM {
id: handler
}
PlasmaNM.Configuration {
id: configuration
}
Kirigami.FormLayout {
Controls.Switch {
id: hotspotToggle
......@@ -50,20 +46,20 @@ SimpleKCM {
Controls.TextField {
id: hotspotName
Kirigami.FormData.label: i18n("SSID:")
text: configuration.hotspotName
text: PlasmaNM.Configuration.hotspotName
}
Kirigami.PasswordField {
id: hotspotPassword
Kirigami.FormData.label: i18n("Password:")
text: configuration.hotspotPassword
text: PlasmaNM.Configuration.hotspotPassword
}
Controls.Button {
text: i18n("Save")
onClicked: {
configuration.hotspotName = hotspotName.text
configuration.hotspotPassword = hotspotPassword.text
PlasmaNM.Configuration.hotspotName = hotspotName.text
PlasmaNM.Configuration.hotspotPassword = hotspotPassword.text
if (hotspotToggle.checked) {
handler.stopHotspot()
handler.createHotspot()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment