Changeset View
Changeset View
Standalone View
Standalone View
core/kdeconnectconfig.cpp
Context not available. | |||||
43 | 43 | | |||
---|---|---|---|---|---|
44 | // The Initializer object sets things up, and also does cleanup when it goes out of scope | 44 | // The Initializer object sets things up, and also does cleanup when it goes out of scope | ||
45 | // Note it's not being used anywhere. That's intended | 45 | // Note it's not being used anywhere. That's intended | ||
46 | QCA::Initializer mQcaInitializer; | 46 | QCA::Initializer m_qcaInitializer; | ||
47 | 47 | | |||
48 | QCA::PrivateKey privateKey; | 48 | QCA::PrivateKey m_privateKey; | ||
49 | QSslCertificate certificate; // Use QSslCertificate instead of QCA::Certificate due to compatibility with QSslSocket | 49 | QSslCertificate m_certificate; // Use QSslCertificate instead of QCA::Certificate due to compatibility with QSslSocket | ||
50 | 50 | | |||
51 | QSettings* config; | 51 | QSettings* m_config; | ||
52 | QSettings* trusted_devices; | 52 | QSettings* m_trustedDevices; | ||
53 | 53 | | |||
54 | }; | 54 | }; | ||
55 | 55 | | |||
Context not available. | |||||
77 | QDir().mkpath(baseConfigDir().path()); | 77 | QDir().mkpath(baseConfigDir().path()); | ||
78 | 78 | | |||
79 | //.config/kdeconnect/config | 79 | //.config/kdeconnect/config | ||
80 | d->config = new QSettings(baseConfigDir().absoluteFilePath(QStringLiteral("config")), QSettings::IniFormat); | 80 | d->m_config = new QSettings(baseConfigDir().absoluteFilePath(QStringLiteral("config")), QSettings::IniFormat); | ||
81 | d->trusted_devices = new QSettings(baseConfigDir().absoluteFilePath(QStringLiteral("trusted_devices")), QSettings::IniFormat); | 81 | d->m_trustedDevices = new QSettings(baseConfigDir().absoluteFilePath(QStringLiteral("trusted_devices")), QSettings::IniFormat); | ||
82 | 82 | | |||
83 | const QFile::Permissions strict = QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser; | 83 | const QFile::Permissions strict = QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser; | ||
84 | 84 | | |||
Context not available. | |||||
86 | QFile privKey(keyPath); | 86 | QFile privKey(keyPath); | ||
87 | if (privKey.exists() && privKey.open(QIODevice::ReadOnly)) { | 87 | if (privKey.exists() && privKey.open(QIODevice::ReadOnly)) { | ||
88 | 88 | | |||
89 | d->privateKey = QCA::PrivateKey::fromPEM(privKey.readAll()); | 89 | d->m_privateKey = QCA::PrivateKey::fromPEM(privKey.readAll()); | ||
90 | 90 | | |||
91 | } else { | 91 | } else { | ||
92 | 92 | | |||
93 | d->privateKey = QCA::KeyGenerator().createRSA(2048); | 93 | d->m_privateKey = QCA::KeyGenerator().createRSA(2048); | ||
94 | 94 | | |||
95 | if (!privKey.open(QIODevice::ReadWrite | QIODevice::Truncate)) { | 95 | if (!privKey.open(QIODevice::ReadWrite | QIODevice::Truncate)) { | ||
96 | Daemon::instance()->reportError(QStringLiteral("KDE Connect"), i18n("Could not store private key file: %1", keyPath)); | 96 | Daemon::instance()->reportError(QStringLiteral("KDE Connect"), i18n("Could not store private key file: %1", keyPath)); | ||
97 | } else { | 97 | } else { | ||
98 | privKey.setPermissions(strict); | 98 | privKey.setPermissions(strict); | ||
99 | privKey.write(d->privateKey.toPEM().toLatin1()); | 99 | privKey.write(d->m_privateKey.toPEM().toLatin1()); | ||
100 | } | 100 | } | ||
101 | } | 101 | } | ||
102 | 102 | | |||
Context not available. | |||||
104 | QFile cert(certPath); | 104 | QFile cert(certPath); | ||
105 | if (cert.exists() && cert.open(QIODevice::ReadOnly)) { | 105 | if (cert.exists() && cert.open(QIODevice::ReadOnly)) { | ||
106 | 106 | | |||
107 | d->certificate = QSslCertificate::fromPath(certPath).at(0); | 107 | d->m_certificate = QSslCertificate::fromPath(certPath).at(0); | ||
108 | 108 | | |||
109 | } else { | 109 | } else { | ||
110 | 110 | | |||
Context not available. | |||||
130 | certificateOptions.setSerialNumber(QCA::BigInteger(10)); | 130 | certificateOptions.setSerialNumber(QCA::BigInteger(10)); | ||
131 | certificateOptions.setValidityPeriod(startTime, endTime); | 131 | certificateOptions.setValidityPeriod(startTime, endTime); | ||
132 | 132 | | |||
133 | d->certificate = QSslCertificate(QCA::Certificate(certificateOptions, d->privateKey).toPEM().toLatin1()); | 133 | d->m_certificate = QSslCertificate(QCA::Certificate(certificateOptions, d->m_privateKey).toPEM().toLatin1()); | ||
134 | 134 | | |||
135 | if (!cert.open(QIODevice::ReadWrite | QIODevice::Truncate)) { | 135 | if (!cert.open(QIODevice::ReadWrite | QIODevice::Truncate)) { | ||
136 | Daemon::instance()->reportError(QStringLiteral("KDE Connect"), i18n("Could not store certificate file: %1", certPath)); | 136 | Daemon::instance()->reportError(QStringLiteral("KDE Connect"), i18n("Could not store certificate file: %1", certPath)); | ||
137 | } else { | 137 | } else { | ||
138 | cert.setPermissions(strict); | 138 | cert.setPermissions(strict); | ||
139 | cert.write(d->certificate.toPem()); | 139 | cert.write(d->m_certificate.toPem()); | ||
140 | } | 140 | } | ||
141 | } | 141 | } | ||
142 | 142 | | |||
Context not available. | |||||
149 | QString KdeConnectConfig::name() | 149 | QString KdeConnectConfig::name() | ||
150 | { | 150 | { | ||
151 | QString defaultName = qgetenv("USER") + '@' + QHostInfo::localHostName(); | 151 | QString defaultName = qgetenv("USER") + '@' + QHostInfo::localHostName(); | ||
152 | QString name = d->config->value(QStringLiteral("name"), defaultName).toString(); | 152 | QString name = d->m_config->value(QStringLiteral("name"), defaultName).toString(); | ||
153 | return name; | 153 | return name; | ||
154 | } | 154 | } | ||
155 | 155 | | |||
156 | void KdeConnectConfig::setName(const QString& name) | 156 | void KdeConnectConfig::setName(const QString& name) | ||
157 | { | 157 | { | ||
158 | d->config->setValue(QStringLiteral("name"), name); | 158 | d->m_config->setValue(QStringLiteral("name"), name); | ||
159 | d->config->sync(); | 159 | d->m_config->sync(); | ||
160 | } | 160 | } | ||
161 | 161 | | |||
162 | QString KdeConnectConfig::deviceType() | 162 | QString KdeConnectConfig::deviceType() | ||
Context not available. | |||||
166 | 166 | | |||
167 | QString KdeConnectConfig::deviceId() | 167 | QString KdeConnectConfig::deviceId() | ||
168 | { | 168 | { | ||
169 | return d->certificate.subjectInfo( QSslCertificate::CommonName ).constFirst(); | 169 | return d->m_certificate.subjectInfo( QSslCertificate::CommonName ).constFirst(); | ||
170 | } | 170 | } | ||
171 | 171 | | |||
172 | QString KdeConnectConfig::privateKeyPath() | 172 | QString KdeConnectConfig::privateKeyPath() | ||
Context not available. | |||||
176 | 176 | | |||
177 | QCA::PrivateKey KdeConnectConfig::privateKey() | 177 | QCA::PrivateKey KdeConnectConfig::privateKey() | ||
178 | { | 178 | { | ||
179 | return d->privateKey; | 179 | return d->m_privateKey; | ||
180 | } | 180 | } | ||
181 | 181 | | |||
182 | QCA::PublicKey KdeConnectConfig::publicKey() | 182 | QCA::PublicKey KdeConnectConfig::publicKey() | ||
183 | { | 183 | { | ||
184 | return d->privateKey.toPublicKey(); | 184 | return d->m_privateKey.toPublicKey(); | ||
185 | } | 185 | } | ||
186 | 186 | | |||
187 | QString KdeConnectConfig::certificatePath() | 187 | QString KdeConnectConfig::certificatePath() | ||
Context not available. | |||||
191 | 191 | | |||
192 | QSslCertificate KdeConnectConfig::certificate() | 192 | QSslCertificate KdeConnectConfig::certificate() | ||
193 | { | 193 | { | ||
194 | return d->certificate; | 194 | return d->m_certificate; | ||
195 | } | 195 | } | ||
196 | 196 | | |||
197 | QDir KdeConnectConfig::baseConfigDir() | 197 | QDir KdeConnectConfig::baseConfigDir() | ||
Context not available. | |||||
203 | 203 | | |||
204 | QStringList KdeConnectConfig::trustedDevices() | 204 | QStringList KdeConnectConfig::trustedDevices() | ||
205 | { | 205 | { | ||
206 | const QStringList& list = d->trusted_devices->childGroups(); | 206 | const QStringList& list = d->m_trustedDevices->childGroups(); | ||
207 | return list; | 207 | return list; | ||
208 | } | 208 | } | ||
209 | 209 | | |||
210 | 210 | | |||
211 | void KdeConnectConfig::addTrustedDevice(const QString &id, const QString &name, const QString &type) | 211 | void KdeConnectConfig::addTrustedDevice(const QString& id, const QString& name, const QString& type) | ||
212 | { | 212 | { | ||
213 | d->trusted_devices->beginGroup(id); | 213 | d->m_trustedDevices->beginGroup(id); | ||
214 | d->trusted_devices->setValue(QStringLiteral("name"), name); | 214 | d->m_trustedDevices->setValue(QStringLiteral("name"), name); | ||
215 | d->trusted_devices->setValue(QStringLiteral("type"), type); | 215 | d->m_trustedDevices->setValue(QStringLiteral("type"), type); | ||
216 | d->trusted_devices->endGroup(); | 216 | d->m_trustedDevices->endGroup(); | ||
217 | d->trusted_devices->sync(); | 217 | d->m_trustedDevices->sync(); | ||
218 | 218 | | |||
219 | QDir().mkpath(deviceConfigDir(id).path()); | 219 | QDir().mkpath(deviceConfigDir(id).path()); | ||
220 | } | 220 | } | ||
221 | 221 | | |||
222 | KdeConnectConfig::DeviceInfo KdeConnectConfig::getTrustedDevice(const QString &id) | 222 | KdeConnectConfig::DeviceInfo KdeConnectConfig::getTrustedDevice(const QString& id) | ||
223 | { | 223 | { | ||
224 | d->trusted_devices->beginGroup(id); | 224 | d->m_trustedDevices->beginGroup(id); | ||
225 | 225 | | |||
226 | KdeConnectConfig::DeviceInfo info; | 226 | KdeConnectConfig::DeviceInfo info; | ||
227 | info.deviceName = d->trusted_devices->value(QStringLiteral("name"), QLatin1String("unnamed")).toString(); | 227 | info.deviceName = d->m_trustedDevices->value(QStringLiteral("name"), QLatin1String("unnamed")).toString(); | ||
228 | info.deviceType = d->trusted_devices->value(QStringLiteral("type"), QLatin1String("unknown")).toString(); | 228 | info.deviceType = d->m_trustedDevices->value(QStringLiteral("type"), QLatin1String("unknown")).toString(); | ||
229 | 229 | | |||
230 | d->trusted_devices->endGroup(); | 230 | d->m_trustedDevices->endGroup(); | ||
231 | return info; | 231 | return info; | ||
232 | } | 232 | } | ||
233 | 233 | | |||
234 | void KdeConnectConfig::removeTrustedDevice(const QString &deviceId) | 234 | void KdeConnectConfig::removeTrustedDevice(const QString& deviceId) | ||
235 | { | 235 | { | ||
236 | d->trusted_devices->remove(deviceId); | 236 | d->m_trustedDevices->remove(deviceId); | ||
237 | d->trusted_devices->sync(); | 237 | d->m_trustedDevices->sync(); | ||
238 | //We do not remove the config files. | 238 | //We do not remove the config files. | ||
239 | } | 239 | } | ||
240 | 240 | | |||
241 | // Utility functions to set and get a value | 241 | // Utility functions to set and get a value | ||
242 | void KdeConnectConfig::setDeviceProperty(const QString& deviceId, const QString& key, const QString& value) | 242 | void KdeConnectConfig::setDeviceProperty(const QString& deviceId, const QString& key, const QString& value) | ||
243 | { | 243 | { | ||
244 | d->trusted_devices->beginGroup(deviceId); | 244 | d->m_trustedDevices->beginGroup(deviceId); | ||
245 | d->trusted_devices->setValue(key, value); | 245 | d->m_trustedDevices->setValue(key, value); | ||
246 | d->trusted_devices->endGroup(); | 246 | d->m_trustedDevices->endGroup(); | ||
247 | d->trusted_devices->sync(); | 247 | d->m_trustedDevices->sync(); | ||
248 | } | 248 | } | ||
249 | 249 | | |||
250 | QString KdeConnectConfig::getDeviceProperty(const QString& deviceId, const QString& key, const QString& defaultValue) | 250 | QString KdeConnectConfig::getDeviceProperty(const QString& deviceId, const QString& key, const QString& defaultValue) | ||
251 | { | 251 | { | ||
252 | QString value; | 252 | QString value; | ||
253 | d->trusted_devices->beginGroup(deviceId); | 253 | d->m_trustedDevices->beginGroup(deviceId); | ||
254 | value = d->trusted_devices->value(key, defaultValue).toString(); | 254 | value = d->m_trustedDevices->value(key, defaultValue).toString(); | ||
255 | d->trusted_devices->endGroup(); | 255 | d->m_trustedDevices->endGroup(); | ||
256 | return value; | 256 | return value; | ||
257 | } | 257 | } | ||
258 | 258 | | |||
259 | 259 | | |||
260 | QDir KdeConnectConfig::deviceConfigDir(const QString &deviceId) | 260 | QDir KdeConnectConfig::deviceConfigDir(const QString& deviceId) | ||
261 | { | 261 | { | ||
262 | QString deviceConfigPath = baseConfigDir().absoluteFilePath(deviceId); | 262 | QString deviceConfigPath = baseConfigDir().absoluteFilePath(deviceId); | ||
263 | return QDir(deviceConfigPath); | 263 | return QDir(deviceConfigPath); | ||
264 | } | 264 | } | ||
265 | 265 | | |||
266 | QDir KdeConnectConfig::pluginConfigDir(const QString &deviceId, const QString &pluginName) | 266 | QDir KdeConnectConfig::pluginConfigDir(const QString& deviceId, const QString& pluginName) | ||
267 | { | 267 | { | ||
268 | QString deviceConfigPath = baseConfigDir().absoluteFilePath(deviceId); | 268 | QString deviceConfigPath = baseConfigDir().absoluteFilePath(deviceId); | ||
269 | QString pluginConfigDir = QDir(deviceConfigPath).absoluteFilePath(pluginName); | 269 | QString pluginConfigDir = QDir(deviceConfigPath).absoluteFilePath(pluginName); | ||
Context not available. |