forked from lxqt/liblxqt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxqtsettings.cpp
710 lines (544 loc) · 18.6 KB
/
lxqtsettings.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2010-2011 Razor team
* Authors:
* Alexander Sokoloff <[email protected]>
* Petr Vanek <[email protected]>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "lxqtsettings.h"
#include <QDebug>
#include <QEvent>
#include <QDir>
#include <QStringList>
#include <QMutex>
#include <QFileSystemWatcher>
#include <QSharedData>
#include <QTimerEvent>
#include <XdgDirs>
using namespace LXQt;
class LXQt::SettingsPrivate
{
public:
SettingsPrivate(Settings* parent):
mFileChangeTimer(0),
mAppChangeTimer(0),
mAddWatchTimer(0),
mParent(parent)
{
// HACK: we need to ensure that the user (~/.config/lxqt/<module>.conf)
// exists to have functional mWatcher
if (!mParent->contains(QL1S("__userfile__")))
{
mParent->setValue(QL1S("__userfile__"), true);
mParent->sync();
}
mWatcher.addPath(mParent->fileName());
QObject::connect(&(mWatcher), &QFileSystemWatcher::fileChanged, mParent, &Settings::_fileChanged);
}
QString localizedKey(const QString& key) const;
QFileSystemWatcher mWatcher;
int mFileChangeTimer;
int mAppChangeTimer;
int mAddWatchTimer;
private:
Settings* mParent;
};
LXQtTheme* LXQtTheme::mInstance = nullptr;
class LXQt::LXQtThemeData: public QSharedData {
public:
LXQtThemeData(): mValid(false) {}
QString loadQss(const QString& qssFile) const;
QString findTheme(const QString &themeName);
QString mName;
QString mPath;
QString mPreviewImg;
bool mValid;
};
class LXQt::GlobalSettingsPrivate
{
public:
GlobalSettingsPrivate(GlobalSettings *parent):
mParent(parent),
mThemeUpdated(0ull)
{
}
GlobalSettings *mParent;
QString mIconTheme;
QString mLXQtTheme;
qlonglong mThemeUpdated;
};
/************************************************
************************************************/
Settings::Settings(const QString& module, QObject* parent) :
QSettings(QL1S("lxqt"), module, parent),
d_ptr(new SettingsPrivate(this))
{
}
/************************************************
************************************************/
Settings::Settings(const QString &fileName, QSettings::Format format, QObject *parent):
QSettings(fileName, format, parent),
d_ptr(new SettingsPrivate(this))
{
}
/************************************************
************************************************/
Settings::Settings(const QSettings* parentSettings, const QString& subGroup, QObject* parent):
QSettings(parentSettings->organizationName(), parentSettings->applicationName(), parent),
d_ptr(new SettingsPrivate(this))
{
beginGroup(subGroup);
}
/************************************************
************************************************/
Settings::Settings(const QSettings& parentSettings, const QString& subGroup, QObject* parent):
QSettings(parentSettings.organizationName(), parentSettings.applicationName(), parent),
d_ptr(new SettingsPrivate(this))
{
beginGroup(subGroup);
}
/************************************************
************************************************/
Settings::~Settings()
{
// because in the Settings::Settings(const QString& module, QObject* parent)
// constructor there is no beginGroup() called...
if (!group().isEmpty())
endGroup();
delete d_ptr;
}
bool Settings::event(QEvent *event)
{
if (event->type() == QEvent::UpdateRequest)
{
// delay the settingsChanged* signal emitting for:
// - checking in _fileChanged
// - merging emitting the signals
if(d_ptr->mAppChangeTimer)
killTimer(d_ptr->mAppChangeTimer);
d_ptr->mAppChangeTimer = startTimer(100);
}
else if (event->type() == QEvent::Timer)
{
const int timer = static_cast<QTimerEvent*>(event)->timerId();
killTimer(timer);
if (timer == d_ptr->mFileChangeTimer)
{
d_ptr->mFileChangeTimer = 0;
fileChanged(); // invoke the real fileChanged() handler.
} else if (timer == d_ptr->mAppChangeTimer)
{
d_ptr->mAppChangeTimer = 0;
// do emit the signals
Q_EMIT settingsChangedByApp();
Q_EMIT settingsChanged();
} else if (timer == d_ptr->mAddWatchTimer)
{
d_ptr->mAddWatchTimer = 0;
//try to re-add filename for watching
addWatchedFile(fileName());
}
}
return QSettings::event(event);
}
void Settings::fileChanged()
{
sync();
Q_EMIT settingsChangedFromExternal();
Q_EMIT settingsChanged();
}
void Settings::_fileChanged(const QString& path)
{
// check if the file isn't changed by our logic
// FIXME: this is poor implementation; should we rather compute some hash of values if changed by external?
if (0 == d_ptr->mAppChangeTimer)
{
// delay the change notification for 100 ms to avoid
// unnecessary repeated loading of the same config file if
// the file is changed for several times rapidly.
if(d_ptr->mFileChangeTimer)
killTimer(d_ptr->mFileChangeTimer);
d_ptr->mFileChangeTimer = startTimer(1000);
}
addWatchedFile(path);
}
void Settings::addWatchedFile(QString const & path)
{
// D*mn! yet another Qt 5.4 regression!!!
// See the bug report: https://github.com/lxqt/lxqt/issues/441
// Since Qt 5.4, QSettings uses QSaveFile to save the config files.
// https://github.com/qtproject/qtbase/commit/8d15068911d7c0ba05732e2796aaa7a90e34a6a1#diff-e691c0405f02f3478f4f50a27bdaecde
// QSaveFile will save the content to a new temp file, and replace the old file later.
// Hence the existing config file is not changed. Instead, it's deleted and then replaced.
// This new behaviour unfortunately breaks QFileSystemWatcher.
// After file deletion, we can no longer receive any new change notifications.
// The most ridiculous thing is, QFileSystemWatcher does not provide a
// way for us to know if a file is deleted. WT*?
// Luckily, I found a workaround: If the file path no longer exists
// in the watcher's files(), this file is deleted.
if(!d_ptr->mWatcher.files().contains(path))
// in some situations adding fails because of non-existing file (e.g. editting file in external program)
if (!d_ptr->mWatcher.addPath(path) && 0 == d_ptr->mAddWatchTimer)
d_ptr->mAddWatchTimer = startTimer(100);
}
/************************************************
************************************************/
const GlobalSettings *Settings::globalSettings()
{
static QMutex mutex;
static GlobalSettings *instance = nullptr;
if (!instance)
{
mutex.lock();
if (!instance)
instance = new GlobalSettings();
mutex.unlock();
}
return instance;
}
/************************************************
LC_MESSAGES value Possible keys in order of matching
lang_COUNTRY@MODIFIER lang_COUNTRY@MODIFIER, lang_COUNTRY, lang@MODIFIER, lang,
default value
lang_COUNTRY lang_COUNTRY, lang, default value
lang@MODIFIER lang@MODIFIER, lang, default value
lang lang, default value
************************************************/
QString SettingsPrivate::localizedKey(const QString& key) const
{
QString lang = QString::fromLocal8Bit(qgetenv("LC_MESSAGES"));
if (lang.isEmpty())
lang = QString::fromLocal8Bit(qgetenv("LC_ALL"));
if (lang.isEmpty())
lang = QString::fromLocal8Bit(qgetenv("LANG"));
QString modifier = lang.section(QL1C('@'), 1);
if (!modifier.isEmpty())
lang.truncate(lang.length() - modifier.length() - 1);
QString encoding = lang.section(QL1C('.'), 1);
if (!encoding.isEmpty())
lang.truncate(lang.length() - encoding.length() - 1);
QString country = lang.section(QL1C('_'), 1);
if (!country.isEmpty())
lang.truncate(lang.length() - country.length() - 1);
//qDebug() << "LC_MESSAGES: " << getenv("LC_MESSAGES");
//qDebug() << "Lang:" << lang;
//qDebug() << "Country:" << country;
//qDebug() << "Encoding:" << encoding;
//qDebug() << "Modifier:" << modifier;
if (!modifier.isEmpty() && !country.isEmpty())
{
QString k = QString::fromLatin1("%1[%2_%3@%4]").arg(key, lang, country, modifier);
//qDebug() << "\t try " << k << mParent->contains(k);
if (mParent->contains(k))
return k;
}
if (!country.isEmpty())
{
QString k = QString::fromLatin1("%1[%2_%3]").arg(key, lang, country);
//qDebug() << "\t try " << k << mParent->contains(k);
if (mParent->contains(k))
return k;
}
if (!modifier.isEmpty())
{
QString k = QString::fromLatin1("%1[%2@%3]").arg(key, lang, modifier);
//qDebug() << "\t try " << k << mParent->contains(k);
if (mParent->contains(k))
return k;
}
QString k = QString::fromLatin1("%1[%2]").arg(key, lang);
//qDebug() << "\t try " << k << mParent->contains(k);
if (mParent->contains(k))
return k;
//qDebug() << "\t try " << key << mParent->contains(key);
return key;
}
/************************************************
************************************************/
QVariant Settings::localizedValue(const QString& key, const QVariant& defaultValue) const
{
Q_D(const Settings);
return value(d->localizedKey(key), defaultValue);
}
/************************************************
************************************************/
void Settings::setLocalizedValue(const QString &key, const QVariant &value)
{
Q_D(const Settings);
setValue(d->localizedKey(key), value);
}
/************************************************
************************************************/
LXQtTheme::LXQtTheme():
d(new LXQtThemeData)
{
}
/************************************************
************************************************/
LXQtTheme::LXQtTheme(const QString &path):
d(new LXQtThemeData)
{
if (path.isEmpty())
return;
QFileInfo fi(path);
if (fi.isAbsolute())
{
d->mPath = path;
d->mName = fi.fileName();
d->mValid = fi.isDir();
}
else
{
d->mName = path;
d->mPath = d->findTheme(path);
d->mValid = !(d->mPath.isEmpty());
}
if (QDir(path).exists(QL1S("preview.png")))
d->mPreviewImg = path + QL1S("/preview.png");
}
/************************************************
************************************************/
QString LXQtThemeData::findTheme(const QString &themeName)
{
if (themeName.isEmpty())
return QString();
QStringList paths;
QLatin1String fallback(LXQT_INSTALL_PREFIX);
paths << XdgDirs::dataHome(false);
paths << XdgDirs::dataDirs();
if (!paths.contains(fallback))
paths << fallback;
for(const QString &path : qAsConst(paths))
{
QDir dir(QString::fromLatin1("%1/lxqt/themes/%2").arg(path, themeName));
if (dir.isReadable())
return dir.absolutePath();
}
return QString();
}
/************************************************
************************************************/
LXQtTheme::LXQtTheme(const LXQtTheme &other):
d(other.d)
{
}
/************************************************
************************************************/
LXQtTheme::~LXQtTheme() = default;
/************************************************
************************************************/
LXQtTheme& LXQtTheme::operator=(const LXQtTheme &other)
{
if (this != &other)
d = other.d;
return *this;
}
/************************************************
************************************************/
bool LXQtTheme::isValid() const
{
return d->mValid;
}
/************************************************
************************************************/
QString LXQtTheme::name() const
{
return d->mName;
}
/************************************************
************************************************/
QString LXQtTheme::path() const
{
return d->mPath;
}
/************************************************
************************************************/
QString LXQtTheme::previewImage() const
{
return d->mPreviewImg;
}
/************************************************
************************************************/
QString LXQtTheme::qss(const QString& module) const
{
return d->loadQss(QStringLiteral("%1/%2.qss").arg(d->mPath, module));
}
/************************************************
************************************************/
QString LXQtThemeData::loadQss(const QString& qssFile) const
{
QFile f(qssFile);
if (! f.open(QIODevice::ReadOnly | QIODevice::Text))
{
return QString();
}
QString qss = QString::fromLocal8Bit(f.readAll());
f.close();
if (qss.isEmpty())
return QString();
// handle relative paths
QString qssDir = QFileInfo(qssFile).canonicalPath();
qss.replace(QRegExp(QL1S("url.[ \\t\\s]*"), Qt::CaseInsensitive, QRegExp::RegExp2), QL1S("url(") + qssDir + QL1C('/'));
return qss;
}
/************************************************
************************************************/
QString LXQtTheme::desktopBackground(int screen) const
{
QString wallpaperCfgFileName = QString::fromLatin1("%1/wallpaper.cfg").arg(d->mPath);
if (wallpaperCfgFileName.isEmpty())
return QString();
QSettings s(wallpaperCfgFileName, QSettings::IniFormat);
QString themeDir = QFileInfo(wallpaperCfgFileName).absolutePath();
// There is something strange... If I remove next line the wallpapers array is not found...
s.childKeys();
s.beginReadArray(QL1S("wallpapers"));
s.setArrayIndex(screen - 1);
if (s.contains(QL1S("file")))
return QDir::cleanPath(QString::fromLatin1("%1/%2").arg(themeDir, s.value(QL1S("file")).toString()));
s.setArrayIndex(0);
if (s.contains(QL1S("file")))
return QDir::cleanPath(QString::fromLatin1("%1/%2").arg(themeDir, s.value(QL1S("file")).toString()));
return QString();
}
/************************************************
************************************************/
const LXQtTheme &LXQtTheme::currentTheme()
{
static LXQtTheme theme;
QString name = Settings::globalSettings()->value(QL1S("theme")).toString();
if (theme.name() != name)
{
theme = LXQtTheme(name);
}
return theme;
}
/************************************************
************************************************/
QList<LXQtTheme> LXQtTheme::allThemes()
{
QList<LXQtTheme> ret;
QSet<QString> processed;
QStringList paths;
paths << XdgDirs::dataHome(false);
paths << XdgDirs::dataDirs();
for(const QString &path : qAsConst(paths))
{
QDir dir(QString::fromLatin1("%1/lxqt/themes").arg(path));
const QFileInfoList dirs = dir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot);
for(const QFileInfo &dir : dirs)
{
if (!processed.contains(dir.fileName()) &&
QDir(dir.absoluteFilePath()).exists(QL1S("lxqt-panel.qss")))
{
processed << dir.fileName();
ret << LXQtTheme(dir.absoluteFilePath());
}
}
}
return ret;
}
/************************************************
************************************************/
SettingsCache::SettingsCache(QSettings &settings) :
mSettings(settings)
{
loadFromSettings();
}
/************************************************
************************************************/
SettingsCache::SettingsCache(QSettings *settings) :
mSettings(*settings)
{
loadFromSettings();
}
/************************************************
************************************************/
void SettingsCache::loadFromSettings()
{
mCache.clear();
const QStringList keys = mSettings.allKeys();
const int N = keys.size();
for (int i = 0; i < N; ++i) {
mCache.insert(keys.at(i), mSettings.value(keys.at(i)));
}
}
/************************************************
************************************************/
void SettingsCache::loadToSettings()
{
QHash<QString, QVariant>::const_iterator i = mCache.constBegin();
while(i != mCache.constEnd())
{
mSettings.setValue(i.key(), i.value());
++i;
}
mSettings.sync();
}
/************************************************
************************************************/
GlobalSettings::GlobalSettings():
Settings(QL1S("lxqt")),
d_ptr(new GlobalSettingsPrivate(this))
{
if (value(QL1S("icon_theme")).toString().isEmpty())
{
qWarning() << QString::fromLatin1("Icon Theme not set. Fallbacking to Oxygen, if installed");
const QString fallback(QLatin1String("oxygen"));
const QDir dir(QLatin1String(LXQT_DATA_DIR) + QLatin1String("/icons"));
if (dir.exists(fallback))
{
setValue(QL1S("icon_theme"), fallback);
sync();
}
else
{
qWarning() << QString::fromLatin1("Fallback Icon Theme (Oxygen) not found");
}
}
fileChanged();
}
GlobalSettings::~GlobalSettings()
{
delete d_ptr;
}
/************************************************
************************************************/
void GlobalSettings::fileChanged()
{
Q_D(GlobalSettings);
sync();
QString it = value(QL1S("icon_theme")).toString();
if (d->mIconTheme != it)
{
Q_EMIT iconThemeChanged();
}
QString rt = value(QL1S("theme")).toString();
qlonglong themeUpdated = value(QL1S("__theme_updated__")).toLongLong();
if ((d->mLXQtTheme != rt) || (d->mThemeUpdated != themeUpdated))
{
d->mLXQtTheme = rt;
Q_EMIT lxqtThemeChanged();
}
Q_EMIT settingsChangedFromExternal();
Q_EMIT settingsChanged();
}