Commit ee2f99d9 authored by Roman Gilg's avatar Roman Gilg
Browse files

feat: add Control file watcher

Summary:
A simple QFileSystemWatcher is added to Control objects that can be activated
on demand.

On change the Control::changed signal is emitted.

Test Plan: Compiles.

Reviewers: #plasma

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D26033
parent 6ac5ae08
......@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "globals.h"
#include <QFile>
#include <QFileSystemWatcher>
#include <QJsonDocument>
#include <QDir>
......@@ -31,6 +32,23 @@ Control::Control(QObject *parent)
{
}
void Control::activateWatcher()
{
if (m_watcher) {
return;
}
m_watcher = new QFileSystemWatcher({filePath()}, this);
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, [this]() {
readFile();
Q_EMIT changed();
});
}
QFileSystemWatcher* Control::watcher() const
{
return m_watcher;
}
bool Control::writeFile()
{
const QString path = filePath();
......@@ -136,6 +154,18 @@ ControlConfig::ControlConfig(KScreen::ConfigPtr config, QObject *parent)
// in case of such a change while object exists?
}
void ControlConfig::activateWatcher()
{
if (watcher()) {
// Watcher was already activated.
return;
}
for (auto *output : m_outputsControls) {
output->activateWatcher();
connect(output, &ControlOutput::changed, this, &ControlConfig::changed);
}
}
QString ControlConfig::dirPath() const
{
return Control::dirPath() % QStringLiteral("configs/");
......
......@@ -22,6 +22,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QObject>
#include <QVariantMap>
class QFileSystemWatcher;
class Control : public QObject
{
Q_OBJECT
......@@ -39,6 +41,10 @@ public:
~Control() override = default;
virtual bool writeFile();
virtual void activateWatcher();
Q_SIGNALS:
void changed();
protected:
virtual QString dirPath() const;
......@@ -47,12 +53,14 @@ protected:
void readFile();
QVariantMap& info();
const QVariantMap& constInfo() const;
QFileSystemWatcher* watcher() const;
static OutputRetention convertVariantToOutputRetention(QVariant variant);
private:
static QString s_dirName;
QVariantMap m_info;
QFileSystemWatcher *m_watcher = nullptr;
};
class ControlOutput;
......@@ -77,6 +85,7 @@ public:
QString filePath() const override;
bool writeFile() override;
void activateWatcher() override;
private:
QVariantList getOutputs() const;
......
......@@ -62,6 +62,12 @@ QString Config::id() const
return m_data->connectedOutputsHash();
}
void Config::activateControlWatching()
{
connect(m_control, &ControlConfig::changed, this, &Config::controlChanged);
m_control->activateWatcher();
}
bool Config::fileExists() const
{
return (QFile::exists(configsDirPath() % id()) || QFile::exists(configsDirPath() % s_fixedConfigFileName));
......@@ -171,10 +177,6 @@ bool Config::writeFile(const QString &filePath)
}
const KScreen::OutputList outputs = m_data->outputs();
// TODO: until we have the file watcher this is necessary to reload control files.
delete m_control;
m_control = new ControlConfig(m_data, this);
const auto oldConfig = readFile();
KScreen::OutputList oldOutputs;
if (oldConfig) {
......
......@@ -43,6 +43,7 @@ public:
return m_data;
}
void activateControlWatching();
void log();
void setValidityFlags(KScreen::Config::ValidityFlags flags) {
......@@ -51,6 +52,9 @@ public:
bool canBeApplied() const;
Q_SIGNALS:
void controlChanged();
private:
friend class TestConfig;
......
......@@ -130,6 +130,10 @@ void KScreenDaemon::doApplyConfig(const KScreen::ConfigPtr& config)
qCDebug(KSCREEN_KDED) << "Do set and apply specific config";
auto configWrapper = std::unique_ptr<Config>(new Config(config));
configWrapper->setValidityFlags(KScreen::Config::ValidityFlag::RequireAtLeastOneEnabledScreen);
configWrapper->activateControlWatching();
connect(configWrapper.get(), &Config::controlChanged, this, [this]() {
// TODO
});
doApplyConfig(std::move(configWrapper));
}
......
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