Commit d759da72 authored by Cyril Rossi's avatar Cyril Rossi
Browse files

KCM Desktop path settings : implement default indicator introduced in KCModule

KCModule introduce an indicator to show which settings have changed from
default value, unfortunately some KCM have settings which cannot be
handled automatically, thus require to handle them manually.

see frameworks/kconfigwidgets!9
parent f3427911
......@@ -4,6 +4,7 @@ add_definitions(-DTRANSLATION_DOMAIN=\"kcm_desktoppaths\")
set(kcm_desktoppaths_PART_SRCS
globalpaths.cpp
desktoppathssettings.cpp
desktoppathsdata.cpp
)
ki18n_wrap_ui(kcm_desktoppaths_PART_SRCS globalpaths.ui)
......
/*
* Copyright (c) 2020 Cyril Rossi <cyril.rossi@enioka.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "desktoppathsdata.h"
#include "desktoppathssettings.h"
DesktopPathsData::DesktopPathsData(QObject *parent, const QVariantList &args)
: KCModuleData(parent, args)
, m_settings(new DesktopPathsSettings(this))
{
autoRegisterSkeletons();
}
DesktopPathsSettings *DesktopPathsData::settings() const
{
return m_settings;
}
#include "desktoppathsdata.moc"
/*
* Copyright (c) 2020 Cyril Rossi <cyril.rossi@enioka.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DESKTOPPATHSDATA_H
#define DESKTOPPATHSDATA_H
#include <QObject>
#include <KCModuleData>
class DesktopPathsSettings;
class DesktopPathsData : public KCModuleData
{
Q_OBJECT
public:
explicit DesktopPathsData(QObject *parent = nullptr, const QVariantList &args = QVariantList());
DesktopPathsSettings *settings() const;
private:
DesktopPathsSettings *m_settings;
};
#endif // DESKTOPPATHSDATA_H
......@@ -27,41 +27,6 @@ namespace {
const QString userDirsFilePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/user-dirs.dirs");
return KSharedConfig::openConfig(userDirsFilePath, KConfig::SimpleConfig);
}
QUrl defaultAutostartLocation()
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/.config/autostart"));
}
QUrl defaultDesktopLocation()
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Desktop"));
}
QUrl defaultDocumentsLocation()
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Documents"));
}
QUrl defaultDownloadsLocation()
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Downloads"));
}
QUrl defaultMusicLocation()
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Music"));
}
QUrl defaultPicturesLocation()
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Pictures"));
}
QUrl defaultVideosLocation()
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Videos"));
}
}
class PathsSettingsStore : public QObject
......@@ -69,20 +34,21 @@ class PathsSettingsStore : public QObject
Q_OBJECT
Q_PROPERTY(QUrl autostartLocation READ autostartLocation WRITE setAutostartLocation)
public:
PathsSettingsStore(QObject *parent = nullptr)
PathsSettingsStore(DesktopPathsSettings *parent = nullptr)
: QObject(parent)
, m_config(KSharedConfig::openConfig())
, m_settings(parent)
{
}
QUrl autostartLocation() const
{
return readUrl(QStringLiteral("Autostart"), defaultAutostartLocation());
return readUrl(QStringLiteral("Autostart"), m_settings->defaultAutostartLocation());
}
void setAutostartLocation(const QUrl &url)
{
if (url.matches(defaultAutostartLocation(), QUrl::StripTrailingSlash)) {
if (url.matches(m_settings->defaultAutostartLocation(), QUrl::StripTrailingSlash)) {
resetUrl(QStringLiteral("Autostart"));
} else {
writeUrl(QStringLiteral("Autostart"), url);
......@@ -121,6 +87,7 @@ private:
}
KSharedConfig::Ptr m_config;
DesktopPathsSettings *m_settings;
};
class XdgPathsSettingsStore : public QObject
......@@ -141,7 +108,7 @@ public:
QUrl desktopLocation() const
{
return readUrl(QStringLiteral("XDG_DESKTOP_DIR"), defaultDesktopLocation());
return readUrl(QStringLiteral("XDG_DESKTOP_DIR"), m_settings->defaultDesktopLocation());
}
void setDesktopLocation(const QUrl &url)
......@@ -151,7 +118,7 @@ public:
QUrl documentsLocation() const
{
return readUrl(QStringLiteral("XDG_DOCUMENTS_DIR"), defaultDocumentsLocation());
return readUrl(QStringLiteral("XDG_DOCUMENTS_DIR"), m_settings->defaultDocumentsLocation());
}
void setDocumentsLocation(const QUrl &url)
......@@ -161,7 +128,7 @@ public:
QUrl downloadsLocation() const
{
return readUrl(QStringLiteral("XDG_DOWNLOAD_DIR"), defaultDownloadsLocation());
return readUrl(QStringLiteral("XDG_DOWNLOAD_DIR"), m_settings->defaultDownloadsLocation());
}
void setDownloadsLocation(const QUrl &url)
......@@ -171,7 +138,7 @@ public:
QUrl musicLocation() const
{
return readUrl(QStringLiteral("XDG_MUSIC_DIR"), defaultMusicLocation());
return readUrl(QStringLiteral("XDG_MUSIC_DIR"), m_settings->defaultMusicLocation());
}
void setMusicLocation(const QUrl &url)
......@@ -181,7 +148,7 @@ public:
QUrl picturesLocation() const
{
return readUrl(QStringLiteral("XDG_PICTURES_DIR"), defaultPicturesLocation());
return readUrl(QStringLiteral("XDG_PICTURES_DIR"), m_settings->defaultPicturesLocation());
}
void setPicturesLocation(const QUrl &url)
......@@ -191,7 +158,7 @@ public:
QUrl videosLocation() const
{
return readUrl(QStringLiteral("XDG_VIDEOS_DIR"), defaultVideosLocation());
return readUrl(QStringLiteral("XDG_VIDEOS_DIR"), m_settings->defaultVideosLocation());
}
void setVideosLocation(const QUrl &url)
......@@ -228,13 +195,23 @@ DesktopPathsSettings::DesktopPathsSettings(QObject *parent)
, m_pathsStore(new PathsSettingsStore(this))
, m_xdgPathsStore(new XdgPathsSettingsStore(this))
{
addItem(new KPropertySkeletonItem(m_xdgPathsStore, "desktopLocation", defaultDesktopLocation()), "desktopLocation");
addItem(new KPropertySkeletonItem(m_pathsStore, "autostartLocation", defaultAutostartLocation()), "autostartLocation");
addItem(new KPropertySkeletonItem(m_xdgPathsStore, "documentsLocation", defaultDocumentsLocation()), "documentsLocation");
addItem(new KPropertySkeletonItem(m_xdgPathsStore, "downloadsLocation", defaultDownloadsLocation()), "downloadsLocation");
addItem(new KPropertySkeletonItem(m_xdgPathsStore, "musicLocation", defaultMusicLocation()), "musicLocation");
addItem(new KPropertySkeletonItem(m_xdgPathsStore, "picturesLocation", defaultPicturesLocation()), "picturesLocation");
addItem(new KPropertySkeletonItem(m_xdgPathsStore, "videosLocation", defaultVideosLocation()), "videosLocation");
addItemInternal("desktopLocation", defaultDesktopLocation());
addItemInternal("documentsLocation", defaultDocumentsLocation());
addItemInternal("downloadsLocation", defaultDownloadsLocation());
addItemInternal("musicLocation", defaultMusicLocation());
addItemInternal("picturesLocation", defaultPicturesLocation());
addItemInternal("videosLocation", defaultVideosLocation());
auto *item = new KPropertySkeletonItem(m_pathsStore, "autostartLocation", defaultAutostartLocation());
item->setNotifyFunction([this] { emit this->widgetChanged(); });
addItem(item, "autostartLocation");
}
void DesktopPathsSettings::addItemInternal(const QByteArray &propertyName, const QVariant &defaultValue)
{
auto *item = new KPropertySkeletonItem(m_xdgPathsStore, propertyName, defaultValue);
item->setNotifyFunction([this] { emit this->widgetChanged(); });
addItem(item, propertyName);
}
QUrl DesktopPathsSettings::autostartLocation() const
......@@ -247,6 +224,11 @@ void DesktopPathsSettings::setAutostartLocation(const QUrl &url)
findItem("autostartLocation")->setProperty(url);
}
QUrl DesktopPathsSettings::defaultAutostartLocation() const
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/.config/autostart"));
}
QUrl DesktopPathsSettings::desktopLocation() const
{
return findItem("desktopLocation")->property().toUrl();
......@@ -257,6 +239,11 @@ void DesktopPathsSettings::setDesktopLocation(const QUrl &url)
findItem("desktopLocation")->setProperty(url);
}
QUrl DesktopPathsSettings::defaultDesktopLocation() const
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Desktop"));
}
QUrl DesktopPathsSettings::documentsLocation() const
{
return findItem("documentsLocation")->property().toUrl();
......@@ -267,6 +254,11 @@ void DesktopPathsSettings::setDocumentsLocation(const QUrl &url)
findItem("documentsLocation")->setProperty(url);
}
QUrl DesktopPathsSettings::defaultDocumentsLocation() const
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Documents"));
}
QUrl DesktopPathsSettings::downloadsLocation() const
{
return findItem("downloadsLocation")->property().toUrl();
......@@ -277,6 +269,11 @@ void DesktopPathsSettings::setDownloadsLocation(const QUrl &url)
findItem("downloadsLocation")->setProperty(url);
}
QUrl DesktopPathsSettings::defaultDownloadsLocation() const
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Downloads"));
}
QUrl DesktopPathsSettings::musicLocation() const
{
return findItem("musicLocation")->property().toUrl();
......@@ -287,6 +284,11 @@ void DesktopPathsSettings::setMusicLocation(const QUrl &url)
findItem("musicLocation")->setProperty(url);
}
QUrl DesktopPathsSettings::defaultMusicLocation() const
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Music"));
}
QUrl DesktopPathsSettings::picturesLocation() const
{
return findItem("picturesLocation")->property().toUrl();
......@@ -297,6 +299,11 @@ void DesktopPathsSettings::setPicturesLocation(const QUrl &url)
findItem("picturesLocation")->setProperty(url);
}
QUrl DesktopPathsSettings::defaultPicturesLocation() const
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Pictures"));
}
QUrl DesktopPathsSettings::videosLocation() const
{
return findItem("videosLocation")->property().toUrl();
......@@ -307,6 +314,11 @@ void DesktopPathsSettings::setVideosLocation(const QUrl &url)
findItem("videosLocation")->setProperty(url);
}
QUrl DesktopPathsSettings::defaultVideosLocation() const
{
return QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/Videos"));
}
bool DesktopPathsSettings::usrSave()
{
m_pathsStore->save();
......
......@@ -32,27 +32,39 @@ public:
QUrl autostartLocation() const;
void setAutostartLocation(const QUrl &url);
QUrl defaultAutostartLocation() const;
QUrl desktopLocation() const;
void setDesktopLocation(const QUrl &url);
QUrl defaultDesktopLocation() const;
QUrl documentsLocation() const;
void setDocumentsLocation(const QUrl &url);
QUrl defaultDocumentsLocation() const;
QUrl downloadsLocation() const;
void setDownloadsLocation(const QUrl &url);
QUrl defaultDownloadsLocation() const;
QUrl musicLocation() const;
void setMusicLocation(const QUrl &url);
QUrl defaultMusicLocation() const;
QUrl picturesLocation() const;
void setPicturesLocation(const QUrl &url);
QUrl defaultPicturesLocation() const;
QUrl videosLocation() const;
void setVideosLocation(const QUrl &url);
QUrl defaultVideosLocation() const;
Q_SIGNALS:
void widgetChanged();
private:
bool usrSave() override;
void addItemInternal(const QByteArray &propertyName, const QVariant &defaultValue);
private:
PathsSettingsStore *m_pathsStore;
......
......@@ -39,28 +39,53 @@
#include "globalpaths.h"
#include "ui_globalpaths.h"
#include "desktoppathssettings.h"
#include "desktoppathsdata.h"
#include <KPluginFactory>
#include <KUrlRequester>
#include <KLineEdit>
K_PLUGIN_FACTORY(KcmDesktopPathsFactory, registerPlugin<DesktopPathConfig>();)
K_PLUGIN_FACTORY(KcmDesktopPathsFactory, registerPlugin<DesktopPathConfig>(); registerPlugin<DesktopPathsData>();)
//-----------------------------------------------------------------------------
DesktopPathConfig::DesktopPathConfig(QWidget *parent, const QVariantList &)
: KCModule( parent )
, m_ui(new Ui::DesktopPathsView)
, m_pathsSettings(new DesktopPathsSettings(this))
, m_data(new DesktopPathsData(this))
{
m_ui->setupUi(this);
setQuickHelp(i18n("<h1>Paths</h1>\n"
"This module allows you to choose where in the filesystem the "
"files on your desktop should be stored.\n"
"Use the \"Whats This?\" (Shift+F1) to get help on specific options."));
addConfig(m_pathsSettings, this);
addConfig(m_data->settings(), this);
connect(this, &DesktopPathConfig::defaultsIndicatorsVisibleChanged, this, &DesktopPathConfig::updateDefaultIndicator);
connect(m_data->settings(), &DesktopPathsSettings::widgetChanged, this, &DesktopPathConfig::updateDefaultIndicator);
}
DesktopPathConfig::~DesktopPathConfig()
{
}
void DesktopPathConfig::updateDefaultIndicator()
{
setDefaultIndicatorVisible(m_ui->kcfg_desktopLocation, m_data->settings()->defaultDesktopLocation());
setDefaultIndicatorVisible(m_ui->kcfg_autostartLocation, m_data->settings()->defaultAutostartLocation());
setDefaultIndicatorVisible(m_ui->kcfg_documentsLocation, m_data->settings()->defaultDocumentsLocation());
setDefaultIndicatorVisible(m_ui->kcfg_downloadsLocation, m_data->settings()->defaultDownloadsLocation());
setDefaultIndicatorVisible(m_ui->kcfg_musicLocation, m_data->settings()->defaultMusicLocation());
setDefaultIndicatorVisible(m_ui->kcfg_picturesLocation, m_data->settings()->defaultPicturesLocation());
setDefaultIndicatorVisible(m_ui->kcfg_videosLocation, m_data->settings()->defaultVideosLocation());
}
void DesktopPathConfig::setDefaultIndicatorVisible(KUrlRequester *widget, const QVariant &defaultValue)
{
bool isDefault = widget->url() == defaultValue.toUrl();
widget->lineEdit()->setProperty("_kde_highlight_neutral", defaultsIndicatorsVisible() && !isDefault);
widget->update();
}
#include "globalpaths.moc"
......@@ -34,7 +34,9 @@
namespace Ui { class DesktopPathsView; }
class KUrlRequester;
class DesktopPathsSettings;
class DesktopPathsData;
//-----------------------------------------------------------------------------
// The "Path" Tab contains :
......@@ -47,9 +49,14 @@ public:
DesktopPathConfig(QWidget *parent, const QVariantList &args);
~DesktopPathConfig() override;
private Q_SLOTS:
void updateDefaultIndicator();
private:
void setDefaultIndicatorVisible(KUrlRequester *widget, const QVariant &defaultValue);
QScopedPointer<Ui::DesktopPathsView> m_ui;
DesktopPathsSettings *m_pathsSettings;
DesktopPathsData *m_data;
};
#endif
......
Markdown is supported
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