Changeset View
Changeset View
Standalone View
Standalone View
common/control.cpp
Show All 31 Lines | |||||
32 | } | 32 | } | ||
33 | 33 | | |||
34 | ControlConfig::ControlConfig(KScreen::ConfigPtr config) | 34 | ControlConfig::ControlConfig(KScreen::ConfigPtr config) | ||
35 | : m_config(config) | 35 | : m_config(config) | ||
36 | { | 36 | { | ||
37 | // qDebug() << "Looking for control file:" << configId; | 37 | // qDebug() << "Looking for control file:" << configId; | ||
38 | QFile file(filePath(config->connectedOutputsHash())); | 38 | QFile file(filePath(config->connectedOutputsHash())); | ||
39 | if (file.open(QIODevice::ReadOnly)) { | 39 | if (file.open(QIODevice::ReadOnly)) { | ||
40 | // This might not be reached, bus this is ok. The control file will | ||||
41 | // eventually be created on first write later on. | ||||
40 | QJsonDocument parser; | 42 | QJsonDocument parser; | ||
41 | m_info = parser.fromJson(file.readAll()).toVariant().toList(); | 43 | m_info = parser.fromJson(file.readAll()).toVariant().toList(); | ||
42 | } | 44 | } | ||
43 | 45 | | |||
44 | // TODO: use a file watcher in case of changes to the control file while | 46 | // TODO: use a file watcher in case of changes to the control file while | ||
45 | // object exists? | 47 | // object exists? | ||
46 | 48 | | |||
47 | // As global outputs are indexed by a hash of their edid, which is not unique, | 49 | // As global outputs are indexed by a hash of their edid, which is not unique, | ||
Show All 29 Lines | |||||
77 | QString ControlConfig::filePath() | 79 | QString ControlConfig::filePath() | ||
78 | { | 80 | { | ||
79 | if (!m_config) { | 81 | if (!m_config) { | ||
80 | return QString(); | 82 | return QString(); | ||
81 | } | 83 | } | ||
82 | return ControlConfig::filePath(m_config->connectedOutputsHash()); | 84 | return ControlConfig::filePath(m_config->connectedOutputsHash()); | ||
83 | } | 85 | } | ||
84 | 86 | | |||
85 | Control::OutputRetention ControlConfig::getOutputRetention(const QString &outputId, const QString &outputName) const | 87 | bool ControlConfig::infoIsOutput(const QVariantMap &info, const QString &outputId, const QString &outputName) const | ||
86 | { | 88 | { | ||
87 | for (const auto variantInfo : m_info) { | | |||
88 | const QVariantMap info = variantInfo.toMap(); | | |||
89 | | ||||
90 | const QString outputIdInfo = info[QStringLiteral("id")].toString(); | 89 | const QString outputIdInfo = info[QStringLiteral("id")].toString(); | ||
91 | if (outputIdInfo.isEmpty()) { | 90 | if (outputIdInfo.isEmpty()) { | ||
92 | continue; | 91 | return false; | ||
93 | } | 92 | } | ||
94 | if (outputId != outputIdInfo) { | 93 | if (outputId != outputIdInfo) { | ||
95 | continue; | 94 | return false; | ||
96 | } | 95 | } | ||
97 | 96 | | |||
98 | if (!outputName.isEmpty() && m_duplicateOutputIds.contains(outputId)) { | 97 | if (!outputName.isEmpty() && m_duplicateOutputIds.contains(outputId)) { | ||
99 | // We may have identical outputs connected, these will have the same id in the config | 98 | // We may have identical outputs connected, these will have the same id in the config | ||
100 | // in order to find the right one, also check the output's name (usually the connector) | 99 | // in order to find the right one, also check the output's name (usually the connector) | ||
101 | const auto metadata = info[QStringLiteral("metadata")].toMap(); | 100 | const auto metadata = info[QStringLiteral("metadata")].toMap(); | ||
102 | const auto outputNameInfo = metadata[QStringLiteral("name")].toString(); | 101 | const auto outputNameInfo = metadata[QStringLiteral("name")].toString(); | ||
103 | if (outputName != outputNameInfo) { | 102 | if (outputName != outputNameInfo) { | ||
104 | // was a duplicate id, but info not for this output | 103 | // was a duplicate id, but info not for this output | ||
105 | continue; | 104 | return false; | ||
105 | } | ||||
106 | } | 106 | } | ||
107 | return true; | ||||
107 | } | 108 | } | ||
108 | 109 | | |||
110 | Control::OutputRetention ControlConfig::getOutputRetention(const QString &outputId, const QString &outputName) const | ||||
111 | { | ||||
112 | for (const auto variantInfo : m_info) { | ||||
113 | const QVariantMap info = variantInfo.toMap(); | ||||
114 | if (!infoIsOutput(info, outputId, outputName)) { | ||||
115 | continue; | ||||
116 | } | ||||
109 | const auto retentionVariant = info[QStringLiteral("retention")]; | 117 | const auto retentionVariant = info[QStringLiteral("retention")]; | ||
110 | if (retentionVariant.canConvert<int>() && retentionVariant.toInt() == (int)OutputRetention::Individual) { | 118 | if (retentionVariant.canConvert<int>() && retentionVariant.toInt() == (int)OutputRetention::Individual) { | ||
111 | return OutputRetention::Individual; | 119 | return OutputRetention::Individual; | ||
112 | } | 120 | } | ||
113 | break; | 121 | break; | ||
114 | } | 122 | } | ||
115 | // default value | 123 | // default value | ||
116 | return OutputRetention::Global; | 124 | return OutputRetention::Global; | ||
117 | } | 125 | } | ||
118 | 126 | | |||
127 | static QVariantMap metadata(const QString &outputName) | ||||
128 | { | ||||
129 | QVariantMap metadata; | ||||
130 | metadata[QStringLiteral("name")] = outputName; | ||||
131 | return metadata; | ||||
132 | } | ||||
133 | | ||||
134 | void ControlConfig::setOutputRetention(const QString &outputId, const QString &outputName, OutputRetention value) | ||||
135 | { | ||||
136 | QList<QVariant>::iterator it; | ||||
137 | for (it = m_info.begin(); it != m_info.end(); ++it) { | ||||
138 | QVariantMap outputInfo = (*it).toMap(); | ||||
139 | if (!infoIsOutput(outputInfo, outputId, outputName)) { | ||||
140 | continue; | ||||
141 | } | ||||
142 | outputInfo[QStringLiteral("retention")] = (int)value; | ||||
143 | *it = outputInfo; | ||||
144 | return; | ||||
145 | } | ||||
146 | // no entry yet, create one | ||||
147 | QVariantMap outputInfo; | ||||
148 | outputInfo[QStringLiteral("id")] = outputId; | ||||
149 | outputInfo[QStringLiteral("metadata")] = metadata(outputName); | ||||
150 | outputInfo[QStringLiteral("retention")] = (int)value; | ||||
151 | m_info << outputInfo; | ||||
152 | } | ||||
153 | | ||||
154 | bool ControlConfig::writeFile() | ||||
155 | { | ||||
156 | // write updated data to file | ||||
157 | QFile file(filePath()); | ||||
158 | if (!file.open(QIODevice::WriteOnly)) { | ||||
159 | // TODO: logging category? | ||||
160 | // qCWarning(KSCREEN_COMMON) << "Failed to open config control file for writing! " << file.errorString(); | ||||
161 | return false; | ||||
162 | } | ||||
163 | file.write(QJsonDocument::fromVariant(m_info).toJson()); | ||||
mart: maybe check if it's actually a valid json document?, even just !document.isEmpty() after… | |||||
164 | // qCDebug(KSCREEN_COMMON) << "Config control saved on: " << file.fileName(); | ||||
165 | return true; | ||||
166 | } | ||||
167 | | ||||
119 | ControlOutput::ControlOutput(KScreen::OutputPtr output) | 168 | ControlOutput::ControlOutput(KScreen::OutputPtr output) | ||
120 | : m_output(output) | 169 | : m_output(output) | ||
121 | { | 170 | { | ||
122 | } | 171 | } | ||
123 | 172 | | |||
124 | QString ControlOutput::filePath(const QString &hash) | 173 | QString ControlOutput::filePath(const QString &hash) | ||
125 | { | 174 | { | ||
126 | const QString dir = dirPath() % QStringLiteral("outputs/"); | 175 | const QString dir = dirPath() % QStringLiteral("outputs/"); | ||
Show All 13 Lines |
maybe check if it's actually a valid json document?, even just !document.isEmpty() after creating the document