forked from nemomobile/mlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfiledatastore.cpp
331 lines (307 loc) · 10.9 KB
/
mfiledatastore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation ([email protected])
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at [email protected].
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include "mfiledatastore.h"
#include "mfiledatastore_p.h"
#include <QTemporaryFile>
#include <QFileInfo>
/*!
* Creates a temporary file in the directory of an original file.
* \param originalPath Absolute path that is used as a base for generating
* the temporary file.
* \return Path of the created file, or an empty string if creating the
* file fails. The returned value is a copy, so for uses where it's assigned
* to a variable, it need not be const. For cases where it's passed as a
* parameter, it need not be const because references to non-const will
* not bind to function return values, because they are rvalues. When
* C++0x brings rvalue references, the value should not be const in order
* to allow rvalue references to bind to it and thus enable moving from it.
*/
static QString createTempFile(const QString &originalPath)
{
QString returnValue;
QTemporaryFile tempFile(originalPath);
if (tempFile.open()) {
tempFile.setAutoRemove(false);
returnValue = tempFile.fileName();
}
return returnValue;
}
/*!
* Copies settings from a QSettings object to another QSettings object.
* \param originalSettings Settings to copy.
* \param newSettings Target of the copy.
* \return true if copying succeeds, false if it fails.
*/
static bool copySettings(const QSettings &originalSettings,
QSettings &newSettings)
{
QStringList keys = originalSettings.allKeys();
foreach(const QString & key, originalSettings.allKeys()) {
newSettings.setValue(key, originalSettings.value(key));
if (newSettings.status() != QSettings::NoError) {
return false;
}
}
return true;
}
/*!
* Renames a file. Ensures that a file with the new name
* doesn't exist.
* \param oldname Name of the file to rename.
* \param newName New name.
*/
static void renameSettingFile(const QString &oldName,
const QString &newName)
{
QFile::remove(newName);
QFile::rename(oldName, newName);
}
/*!
* Adds the necessary paths to the file system watcher
* \param filePath Path (including name) of the file to watch.
* \param watcher The file system watcher.
*/
static void addPathsToWatcher(const QString &filePath,
QScopedPointer<QFileSystemWatcher>& watcher)
{
QFileInfo fileInfo(filePath);
QString directory;
bool fileExists = fileInfo.exists();
if (fileExists) {
// If the file exists, we can take the canonical path directly
directory = fileInfo.canonicalPath();
} else {
// If the file doesn't exist, canonicalPath would return an empty string. That's why
// we need to get the parent directory first.
QFileInfo parentPath(fileInfo.absolutePath());
if (parentPath.exists()) {
directory = parentPath.canonicalFilePath();
}
}
if (!directory.isEmpty()) {
// Watch the directory if it's not being watched yet
if (!watcher->directories().contains(directory)) {
watcher->addPath(directory);
}
}
// Watch the file itself if it's not being watched yet
if (fileExists && !watcher->files().contains(filePath)) {
watcher->addPath(filePath);
}
}
/*!
* Saves the settings to a file. The settings are first
* saved to a temporary file, and then that file is copied
* over the original settings. This avoids clearing settings
* when there's no disk space.
* \param originalSettings Settings to save.
*/
static bool doSync(QSettings &originalSettings, QScopedPointer<QFileSystemWatcher>& watcher)
{
bool returnValue = false;
QString tempFileName = createTempFile(originalSettings.fileName());
if (!tempFileName.isEmpty()) {
QSettings copiedSettings(tempFileName, QSettings::IniFormat);
if (copySettings(originalSettings, copiedSettings)) {
copiedSettings.sync();
if (copiedSettings.status() == QSettings::NoError) {
renameSettingFile(tempFileName, originalSettings.fileName());
returnValue = true;
}
}
}
addPathsToWatcher(originalSettings.fileName(), watcher);
return returnValue;
}
MFileDataStorePrivate::MFileDataStorePrivate(const QString &filePath) :
settings(filePath, QSettings::IniFormat),
watcher(new QFileSystemWatcher())
{
settings.sync();
}
MFileDataStore::MFileDataStore(const QString &filePath) :
d_ptr(new MFileDataStorePrivate(filePath))
{
Q_D(MFileDataStore);
takeSnapshot();
addPathsToWatcher(filePath, d->watcher);
connect(d->watcher.data(), SIGNAL(fileChanged(QString)),
this, SLOT(fileChanged(QString)));
connect(d->watcher.data(), SIGNAL(directoryChanged(QString)),
this, SLOT(directoryChanged(QString)));
}
MFileDataStore::~MFileDataStore()
{
delete d_ptr;
}
bool MFileDataStore::createValue(const QString &key, const QVariant &value)
{
Q_D(MFileDataStore);
bool returnValue = false;
// QSettings has some kind of a cache so we'll prevent any temporary writes
// by checking if the data can be actually stored before doing anything
if (isWritable()) {
bool originalValueSet = d->settings.contains(key);
QVariant originalValue = d->settings.value(key);
d->settings.setValue(key, value);
bool syncOk = doSync(d->settings, d->watcher);
if (syncOk) {
returnValue = true;
// Emit valueChanged signal when value is changed or a new key is added
if ((originalValueSet && originalValue != value)
|| !originalValueSet) {
d->settingsSnapshot[key] = value;
emit valueChanged(key, value);
}
} else if (originalValueSet) {
// if sync fails, make sure the value in memory is the original
d->settings.setValue(key, originalValue);
} else {
d->settings.remove(key);
}
}
return returnValue;
}
bool MFileDataStore::setValue(const QString &key, const QVariant &value)
{
Q_D(MFileDataStore);
bool returnValue = false;
// QSettings has some kind of a cache so we'll prevent any temporary writes
// by checking if the data can be actually stored before doing anything
if (isWritable() && d->settings.contains(key)) {
QVariant originalValue = d->settings.value(key);
d->settings.setValue(key, value);
bool syncOk = doSync(d->settings, d->watcher);
if (syncOk) {
returnValue = true;
// Emit valueChanged signal when value is changed
if (originalValue != value) {
d->settingsSnapshot[key] = value;
emit valueChanged(key, value);
}
} else {
// if sync fails, make sure the value in memory is the original
d->settings.setValue(key, originalValue);
}
}
return returnValue;
}
QVariant MFileDataStore::value(const QString &key) const
{
Q_D(const MFileDataStore);
return d->settings.value(key);
}
QStringList MFileDataStore::allKeys() const
{
Q_D(const MFileDataStore);
return d->settings.allKeys();
}
void MFileDataStore::remove(const QString &key)
{
Q_D(MFileDataStore);
// QSettings has some kind of a cache so we'll prevent any temporary writes
// by checking if the data can be actually stored before doing anything
if (isWritable()) {
bool originalValueSet = d->settings.contains(key);
if (!originalValueSet) {
return;
}
QVariant originalValue = d->settings.value(key);
d->settings.remove(key);
bool syncOk = doSync(d->settings, d->watcher);
if (!syncOk) {
if (originalValueSet) {
// if sync fails, make sure the value in memory is the original
d->settings.setValue(key, originalValue);
}
} else {
d->settingsSnapshot.remove(key);
emit valueChanged(key, QVariant());
}
}
}
void MFileDataStore::clear()
{
Q_D(MFileDataStore);
// QSettings has some kind of a cache so we'll prevent any temporary writes
// by checking if the data can be actually stored before doing anything
if (isWritable()) {
d->settings.clear();
d->settings.sync();
takeSnapshot();
}
}
bool MFileDataStore::contains(const QString &key) const
{
Q_D(const MFileDataStore);
return d->settings.contains(key);
}
bool MFileDataStore::isReadable() const
{
Q_D(const MFileDataStore);
return d->settings.status() == QSettings::NoError;
}
bool MFileDataStore::isWritable() const
{
Q_D(const MFileDataStore);
return d->settings.isWritable() && d->settings.status() == QSettings::NoError;
}
void MFileDataStore::takeSnapshot()
{
Q_D(MFileDataStore);
d->settingsSnapshot.clear();
foreach(const QString & key, d->settings.allKeys()) {
d->settingsSnapshot.insert(key, d->settings.value(key));
}
}
void MFileDataStore::fileChanged(const QString &fileName)
{
Q_D(MFileDataStore);
// sync the settings and add the path, for observing
// the file even if it was deleted
d->settings.sync();
addPathsToWatcher(d->settings.fileName(), d->watcher);
if (d->settings.fileName() == fileName && isWritable()) {
// Check whether the values for existing keys have changed or
// if keys have been deleted
foreach(const QString & key, d->settingsSnapshot.keys()) {
if ((d->settings.contains(key)
&& d->settings.value(key) != d->settingsSnapshot.value(key))
|| (!d->settings.contains(key))) {
emit valueChanged(key, d->settings.value(key));
}
}
// Check whether new keys have been added
foreach(const QString & key, d->settings.allKeys()) {
if (!d->settingsSnapshot.contains(key)) {
emit valueChanged(key, d->settings.value(key));
}
}
takeSnapshot();
}
}
void MFileDataStore::directoryChanged(const QString &fileName)
{
Q_D(MFileDataStore);
if (fileName == QFileInfo(d->settings.fileName()).canonicalPath()) {
// we can't know which file changed, so we'll sync at this
// point. This is not very optimal, but it at least works.
fileChanged(d->settings.fileName());
}
}