Changeset View
Changeset View
Standalone View
Standalone View
common/control.cpp
Show All 15 Lines | |||||
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 <QJsonDocument> | 21 | #include <QJsonDocument> | ||
22 | #include <QDir> | 22 | #include <QDir> | ||
23 | 23 | | |||
24 | #include <kscreen/config.h> | ||||
25 | #include <kscreen/output.h> | ||||
26 | | ||||
24 | QString Control::s_dirName = QStringLiteral("control/"); | 27 | QString Control::s_dirName = QStringLiteral("control/"); | ||
25 | 28 | | |||
26 | QString Control::dirPath() | 29 | QString Control::dirPath() | ||
27 | { | 30 | { | ||
28 | return Globals::dirPath() % s_dirName; | 31 | return Globals::dirPath() % s_dirName; | ||
29 | } | 32 | } | ||
30 | 33 | | |||
31 | QString Control::outputFilePath(const QString &hash) | 34 | Control::OutputRetention Control::getOutputRetention(const QString &outputId, const QMap<QString, Control::OutputRetention> &retentions) | ||
32 | { | 35 | { | ||
33 | const QString dir = dirPath() % QStringLiteral("outputs/"); | 36 | if (retentions.contains(outputId)) { | ||
34 | if (!QDir().mkpath(dir)) { | 37 | return retentions[outputId]; | ||
35 | return QString(); | | |||
36 | } | 38 | } | ||
37 | return dir % hash; | 39 | // info for output not found | ||
40 | return OutputRetention::Undefined; | ||||
38 | } | 41 | } | ||
39 | 42 | | |||
40 | QString Control::configFilePath(const QString &hash) | 43 | ControlConfig::ControlConfig(KScreen::ConfigPtr config) | ||
44 | : m_config(config) | ||||
45 | { | ||||
46 | } | ||||
47 | | ||||
48 | QString ControlConfig::filePath(const QString &hash) | ||||
41 | { | 49 | { | ||
42 | const QString dir = dirPath() % QStringLiteral("configs/"); | 50 | const QString dir = dirPath() % QStringLiteral("configs/"); | ||
43 | if (!QDir().mkpath(dir)) { | 51 | if (!QDir().mkpath(dir)) { | ||
44 | return QString(); | 52 | return QString(); | ||
45 | } | 53 | } | ||
46 | return dir % hash; | 54 | return dir % hash; | ||
47 | } | 55 | } | ||
48 | 56 | | |||
57 | QString ControlConfig::filePath() | ||||
58 | { | ||||
59 | if (!m_config) { | ||||
60 | return QString(); | ||||
61 | } | ||||
62 | return ControlConfig::filePath(m_config->connectedOutputsHash()); | ||||
63 | } | ||||
64 | | ||||
49 | Control::OutputRetention Control::convertVariantToOutputRetention(QVariant variant) | 65 | Control::OutputRetention Control::convertVariantToOutputRetention(QVariant variant) | ||
50 | { | 66 | { | ||
51 | if (variant.canConvert<int>()) { | 67 | if (variant.canConvert<int>()) { | ||
52 | const auto retention = variant.toInt(); | 68 | const auto retention = variant.toInt(); | ||
53 | if (retention == (int)OutputRetention::Global) { | 69 | if (retention == (int)OutputRetention::Global) { | ||
54 | return OutputRetention::Global; | 70 | return OutputRetention::Global; | ||
55 | } | 71 | } | ||
56 | if (retention == (int)OutputRetention::Individual) { | 72 | if (retention == (int)OutputRetention::Individual) { | ||
57 | return OutputRetention::Individual; | 73 | return OutputRetention::Individual; | ||
58 | } | 74 | } | ||
59 | } | 75 | } | ||
60 | return OutputRetention::Undefined; | 76 | return OutputRetention::Undefined; | ||
61 | } | 77 | } | ||
62 | 78 | | |||
63 | QMap<QString, Control::OutputRetention> Control::readInOutputRetentionValues(const QString &configId) | 79 | QMap<QString, Control::OutputRetention> ControlConfig::readInOutputRetentionValues() | ||
64 | { | 80 | { | ||
65 | // qDebug() << "Looking for control file:" << configId; | 81 | // qDebug() << "Looking for control file:" << m_config->connectedOutputsHash(); | ||
66 | QFile file(configFilePath(configId)); | 82 | QFile file(filePath(m_config->connectedOutputsHash())); | ||
67 | if (!file.open(QIODevice::ReadOnly)) { | 83 | if (!file.open(QIODevice::ReadOnly)) { | ||
68 | // TODO: have a logging category | 84 | // TODO: have a logging category | ||
69 | // qCDebug(KSCREEN_COMMON) << "Failed to open file" << file.fileName(); | 85 | // qCDebug(KSCREEN_COMMON) << "Failed to open file" << file.fileName(); | ||
70 | return QMap<QString, Control::OutputRetention>(); | 86 | return QMap<QString, Control::OutputRetention>(); | ||
71 | } | 87 | } | ||
72 | 88 | | |||
73 | QJsonDocument parser; | 89 | QJsonDocument parser; | ||
74 | const QVariantMap controlInfo = parser.fromJson(file.readAll()).toVariant().toMap(); | 90 | const QVariantMap controlInfo = parser.fromJson(file.readAll()).toVariant().toMap(); | ||
Show All 9 Lines | 99 | if (outputHash.isEmpty()) { | |||
84 | continue; | 100 | continue; | ||
85 | } | 101 | } | ||
86 | retentions[outputHash] = convertVariantToOutputRetention(info[QStringLiteral("retention")]); | 102 | retentions[outputHash] = convertVariantToOutputRetention(info[QStringLiteral("retention")]); | ||
87 | } | 103 | } | ||
88 | 104 | | |||
89 | return retentions; | 105 | return retentions; | ||
90 | } | 106 | } | ||
91 | 107 | | |||
92 | Control::OutputRetention Control::getOutputRetention(const QString &outputId, const QMap<QString, Control::OutputRetention> &retentions) | 108 | ControlOutput::ControlOutput(KScreen::OutputPtr output) | ||
109 | : m_output(output) | ||||
93 | { | 110 | { | ||
94 | if (retentions.contains(outputId)) { | | |||
95 | return retentions[outputId]; | | |||
96 | } | 111 | } | ||
97 | // info for output not found | 112 | | ||
98 | return OutputRetention::Undefined; | 113 | QString ControlOutput::filePath(const QString &hash) | ||
114 | { | ||||
115 | const QString dir = dirPath() % QStringLiteral("outputs/"); | ||||
116 | if (!QDir().mkpath(dir)) { | ||||
117 | return QString(); | ||||
118 | } | ||||
119 | return dir % hash; | ||||
120 | } | ||||
121 | | ||||
122 | QString ControlOutput::filePath() | ||||
123 | { | ||||
124 | if (!m_output) { | ||||
125 | return QString(); | ||||
126 | } | ||||
127 | return ControlOutput::filePath(m_output->hash()); | ||||
99 | } | 128 | } |