Changeset View
Changeset View
Standalone View
Standalone View
common/control.cpp
Show All 12 Lines | |||||
13 | 13 | | |||
14 | You should have received a copy of the GNU General Public License | 14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | *********************************************************************/ | 16 | *********************************************************************/ | ||
17 | #include "control.h" | 17 | #include "control.h" | ||
18 | #include "globals.h" | 18 | #include "globals.h" | ||
19 | 19 | | |||
20 | #include <QFile> | 20 | #include <QFile> | ||
21 | #include <QFileSystemWatcher> | ||||
21 | #include <QJsonDocument> | 22 | #include <QJsonDocument> | ||
22 | #include <QDir> | 23 | #include <QDir> | ||
23 | 24 | | |||
24 | #include <kscreen/config.h> | 25 | #include <kscreen/config.h> | ||
25 | #include <kscreen/output.h> | 26 | #include <kscreen/output.h> | ||
26 | 27 | | |||
27 | QString Control::s_dirName = QStringLiteral("control/"); | 28 | QString Control::s_dirName = QStringLiteral("control/"); | ||
28 | 29 | | |||
29 | Control::Control(QObject *parent) | 30 | Control::Control(QObject *parent) | ||
30 | : QObject(parent) | 31 | : QObject(parent) | ||
31 | { | 32 | { | ||
32 | } | 33 | } | ||
33 | 34 | | |||
35 | void Control::activateWatcher() | ||||
36 | { | ||||
37 | if (m_watcher) { | ||||
38 | return; | ||||
39 | } | ||||
40 | m_watcher = new QFileSystemWatcher({filePath()}, this); | ||||
41 | connect(m_watcher, &QFileSystemWatcher::fileChanged, this, [this]() { | ||||
42 | readFile(); | ||||
43 | Q_EMIT changed(); | ||||
44 | }); | ||||
45 | } | ||||
46 | | ||||
47 | QFileSystemWatcher* Control::watcher() const | ||||
48 | { | ||||
49 | return m_watcher; | ||||
50 | } | ||||
51 | | ||||
34 | bool Control::writeFile() | 52 | bool Control::writeFile() | ||
35 | { | 53 | { | ||
36 | const QString path = filePath(); | 54 | const QString path = filePath(); | ||
37 | const auto infoMap = constInfo(); | 55 | const auto infoMap = constInfo(); | ||
38 | 56 | | |||
39 | if (infoMap.isEmpty()) { | 57 | if (infoMap.isEmpty()) { | ||
40 | // Nothing to write. Default control. Remove file if it exists. | 58 | // Nothing to write. Default control. Remove file if it exists. | ||
41 | QFile::remove(path); | 59 | QFile::remove(path); | ||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Line(s) | 128 | // qDebug() << "Looking for control file:" << config->connectedOutputsHash(); | |||
131 | } | 149 | } | ||
132 | 150 | | |||
133 | // TODO: this is same in Output::readInOutputs of the daemon. Combine? | 151 | // TODO: this is same in Output::readInOutputs of the daemon. Combine? | ||
134 | 152 | | |||
135 | // TODO: connect to outputs added/removed signals and reevaluate duplicate ids | 153 | // TODO: connect to outputs added/removed signals and reevaluate duplicate ids | ||
136 | // in case of such a change while object exists? | 154 | // in case of such a change while object exists? | ||
137 | } | 155 | } | ||
138 | 156 | | |||
157 | void ControlConfig::activateWatcher() | ||||
158 | { | ||||
159 | if (watcher()) { | ||||
160 | // Watcher was already activated. | ||||
161 | return; | ||||
162 | } | ||||
163 | for (auto *output : m_outputsControls) { | ||||
164 | output->activateWatcher(); | ||||
165 | connect(output, &ControlOutput::changed, this, &ControlConfig::changed); | ||||
166 | } | ||||
167 | } | ||||
168 | | ||||
139 | QString ControlConfig::dirPath() const | 169 | QString ControlConfig::dirPath() const | ||
140 | { | 170 | { | ||
141 | return Control::dirPath() % QStringLiteral("configs/"); | 171 | return Control::dirPath() % QStringLiteral("configs/"); | ||
142 | } | 172 | } | ||
143 | 173 | | |||
144 | QString ControlConfig::filePath() const | 174 | QString ControlConfig::filePath() const | ||
145 | { | 175 | { | ||
146 | if (!m_config) { | 176 | if (!m_config) { | ||
▲ Show 20 Lines • Show All 235 Lines • Show Last 20 Lines |