Updated Russian translation
[lxde/liblxqt.git] / lxqtsettings.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
5 * http://razor-qt.org
6 *
7 * Copyright: 2010-2011 Razor team
8 * Authors:
9 * Alexander Sokoloff <sokoloff.a@gmail.com>
10 * Petr Vanek <petr@scribus.info>
11 *
12 * This program or library is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21
22 * You should have received a copy of the GNU Lesser General
23 * Public License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301 USA
26 *
27 * END_COMMON_COPYRIGHT_HEADER */
28
29 #include "lxqtsettings.h"
30 #include <QDebug>
31 #include <QEvent>
32 #include <QDir>
33 #include <QStringList>
34 #include <QMutex>
35 #include <QFileSystemWatcher>
36 #include <QSharedData>
37 #include <QTimerEvent>
38
39 #include <XdgIcon>
40 #include <XdgDirs>
41
42 using namespace LXQt;
43
44 class LXQt::SettingsPrivate
45 {
46 public:
47 SettingsPrivate(Settings* parent):
48 mChangeTimer(0),
49 mParent(parent)
50 {
51 }
52
53 QString localizedKey(const QString& key) const;
54
55 QFileSystemWatcher mWatcher;
56 int mChangeTimer;
57
58 private:
59 Settings* mParent;
60 };
61
62
63 LXQtTheme* LXQtTheme::mInstance = 0;
64
65 class LXQt::LXQtThemeData: public QSharedData {
66 public:
67 LXQtThemeData(): mValid(false) {}
68 QString loadQss(const QString& qssFile) const;
69 QString findTheme(const QString &themeName);
70
71 QString mName;
72 QString mPath;
73 QString mPreviewImg;
74 bool mValid;
75
76 };
77
78
79 class LXQt::GlobalSettingsPrivate
80 {
81 public:
82 GlobalSettingsPrivate(GlobalSettings *parent):
83 mParent(parent),
84 mThemeUpdated(0ull)
85 {
86
87 }
88
89 GlobalSettings *mParent;
90 QString mIconTheme;
91 QString mLXQtTheme;
92 qlonglong mThemeUpdated;
93
94 };
95
96
97 /************************************************
98
99 ************************************************/
100 Settings::Settings(const QString& module, QObject* parent) :
101 QSettings("lxqt", module, parent),
102 d_ptr(new SettingsPrivate(this))
103 {
104 // HACK: we need to ensure that the user (~/.config/lxqt/<module>.conf)
105 // exists to have functional mWatcher
106 if (!contains("__userfile__"))
107 {
108 setValue("__userfile__", true);
109 sync();
110 }
111 d_ptr->mWatcher.addPath(this->fileName());
112 connect(&(d_ptr->mWatcher), &QFileSystemWatcher::fileChanged, this, &Settings::_fileChanged);
113 }
114
115
116 /************************************************
117
118 ************************************************/
119 Settings::Settings(const QString &fileName, QSettings::Format format, QObject *parent):
120 QSettings(fileName, format, parent),
121 d_ptr(new SettingsPrivate(this))
122 {
123 // HACK: we need to ensure that the user (~/.config/lxqt/<module>.conf)
124 // exists to have functional mWatcher
125 if (!contains("__userfile__"))
126 {
127 setValue("__userfile__", true);
128 sync();
129 }
130 d_ptr->mWatcher.addPath(this->fileName());
131 connect(&(d_ptr->mWatcher), &QFileSystemWatcher::fileChanged, this, &Settings::_fileChanged);
132 }
133
134
135 /************************************************
136
137 ************************************************/
138 Settings::Settings(const QSettings* parentSettings, const QString& subGroup, QObject* parent):
139 QSettings(parentSettings->organizationName(), parentSettings->applicationName(), parent),
140 d_ptr(new SettingsPrivate(this))
141 {
142 beginGroup(subGroup);
143 }
144
145
146 /************************************************
147
148 ************************************************/
149 Settings::Settings(const QSettings& parentSettings, const QString& subGroup, QObject* parent):
150 QSettings(parentSettings.organizationName(), parentSettings.applicationName(), parent),
151 d_ptr(new SettingsPrivate(this))
152 {
153 beginGroup(subGroup);
154 }
155
156
157 /************************************************
158
159 ************************************************/
160 Settings::~Settings()
161 {
162 // because in the Settings::Settings(const QString& module, QObject* parent)
163 // constructor there is no beginGroup() called...
164 if (!group().isEmpty())
165 endGroup();
166
167 delete d_ptr;
168 }
169
170 bool Settings::event(QEvent *event)
171 {
172 if (event->type() == QEvent::UpdateRequest)
173 {
174 emit settingsChanged();
175 }
176 else if (event->type() == QEvent::Timer)
177 {
178 if(static_cast<QTimerEvent*>(event)->timerId() == d_ptr->mChangeTimer)
179 {
180 fileChanged(); // invoke the real fileChanged() handler.
181 killTimer(d_ptr->mChangeTimer);
182 d_ptr->mChangeTimer = 0;
183 }
184 }
185
186 return QSettings::event(event);
187 }
188
189 void Settings::fileChanged()
190 {
191 sync();
192 emit settingsChanged();
193 }
194
195 void Settings::_fileChanged(QString path)
196 {
197 // delay the change notification for 100 ms to avoid
198 // unnecessary repeated loading of the same config file if
199 // the file is changed for several times rapidly.
200 if(d_ptr->mChangeTimer)
201 killTimer(d_ptr->mChangeTimer);
202 d_ptr->mChangeTimer = startTimer(100);
203
204 // D*mn! yet another Qt 5.4 regression!!!
205 // See the bug report: https://github.com/lxde/lxqt/issues/441
206 // Since Qt 5.4, QSettings uses QSaveFile to save the config files.
207 // https://github.com/qtproject/qtbase/commit/8d15068911d7c0ba05732e2796aaa7a90e34a6a1#diff-e691c0405f02f3478f4f50a27bdaecde
208 // QSaveFile will save the content to a new temp file, and replace the old file later.
209 // Hence the existing config file is not changed. Instead, it's deleted and then replaced.
210 // This new behaviour unfortunately breaks QFileSystemWatcher.
211 // After file deletion, we can no longer receive any new change notifications.
212 // The most ridiculous thing is, QFileSystemWatcher does not provide a
213 // way for us to know if a file is deleted. WT*?
214 // Luckily, I found a workaround: If the file path no longer exists
215 // in the watcher's files(), this file is deleted.
216 if(!d_ptr->mWatcher.files().contains(path))
217 d_ptr->mWatcher.addPath(path);
218 }
219
220
221 /************************************************
222
223 ************************************************/
224 const GlobalSettings *Settings::globalSettings()
225 {
226 static QMutex mutex;
227 static GlobalSettings *instance = 0;
228 if (!instance)
229 {
230 mutex.lock();
231
232 if (!instance)
233 instance = new GlobalSettings();
234
235 mutex.unlock();
236 }
237
238 return instance;
239 }
240
241
242 /************************************************
243 LC_MESSAGES value Possible keys in order of matching
244 lang_COUNTRY@MODIFIER lang_COUNTRY@MODIFIER, lang_COUNTRY, lang@MODIFIER, lang,
245 default value
246 lang_COUNTRY lang_COUNTRY, lang, default value
247 lang@MODIFIER lang@MODIFIER, lang, default value
248 lang lang, default value
249 ************************************************/
250 QString SettingsPrivate::localizedKey(const QString& key) const
251 {
252
253 QString lang = getenv("LC_MESSAGES");
254
255 if (lang.isEmpty())
256 lang = getenv("LC_ALL");
257
258 if (lang.isEmpty())
259 lang = getenv("LANG");
260
261
262 QString modifier = lang.section('@', 1);
263 if (!modifier.isEmpty())
264 lang.truncate(lang.length() - modifier.length() - 1);
265
266 QString encoding = lang.section('.', 1);
267 if (!encoding.isEmpty())
268 lang.truncate(lang.length() - encoding.length() - 1);
269
270
271 QString country = lang.section('_', 1);
272 if (!country.isEmpty())
273 lang.truncate(lang.length() - country.length() - 1);
274
275
276
277 //qDebug() << "LC_MESSAGES: " << getenv("LC_MESSAGES");
278 //qDebug() << "Lang:" << lang;
279 //qDebug() << "Country:" << country;
280 //qDebug() << "Encoding:" << encoding;
281 //qDebug() << "Modifier:" << modifier;
282
283 if (!modifier.isEmpty() && !country.isEmpty())
284 {
285 QString k = QString("%1[%2_%3@%4]").arg(key, lang, country, modifier);
286 //qDebug() << "\t try " << k << mParent->contains(k);
287 if (mParent->contains(k))
288 return k;
289 }
290
291 if (!country.isEmpty())
292 {
293 QString k = QString("%1[%2_%3]").arg(key, lang, country);
294 //qDebug() << "\t try " << k << mParent->contains(k);
295 if (mParent->contains(k))
296 return k;
297 }
298
299 if (!modifier.isEmpty())
300 {
301 QString k = QString("%1[%2@%3]").arg(key, lang, modifier);
302 //qDebug() << "\t try " << k << mParent->contains(k);
303 if (mParent->contains(k))
304 return k;
305 }
306
307 QString k = QString("%1[%2]").arg(key, lang);
308 //qDebug() << "\t try " << k << mParent->contains(k);
309 if (mParent->contains(k))
310 return k;
311
312
313 //qDebug() << "\t try " << key << mParent->contains(key);
314 return key;
315 }
316
317 /************************************************
318
319 ************************************************/
320 QVariant Settings::localizedValue(const QString& key, const QVariant& defaultValue) const
321 {
322 Q_D(const Settings);
323 return value(d->localizedKey(key), defaultValue);
324 }
325
326
327 /************************************************
328
329 ************************************************/
330 void Settings::setLocalizedValue(const QString &key, const QVariant &value)
331 {
332 Q_D(const Settings);
333 setValue(d->localizedKey(key), value);
334 }
335
336
337 /************************************************
338
339 ************************************************/
340 LXQtTheme::LXQtTheme():
341 d(new LXQtThemeData)
342 {
343 }
344
345
346 /************************************************
347
348 ************************************************/
349 LXQtTheme::LXQtTheme(const QString &path):
350 d(new LXQtThemeData)
351 {
352 if (path.isEmpty())
353 return;
354
355 QFileInfo fi(path);
356 if (fi.isAbsolute())
357 {
358 d->mPath = path;
359 d->mName = fi.fileName();
360 d->mValid = fi.isDir();
361 }
362 else
363 {
364 d->mName = path;
365 d->mPath = d->findTheme(path);
366 d->mValid = !(d->mPath.isEmpty());
367 }
368
369 if (QDir(path).exists("preview.png"))
370 d->mPreviewImg = path + "/preview.png";
371 }
372
373
374 /************************************************
375
376 ************************************************/
377 QString LXQtThemeData::findTheme(const QString &themeName)
378 {
379 if (themeName.isEmpty())
380 return QString();
381
382 QStringList paths;
383 paths << XdgDirs::dataHome(false);
384 paths << XdgDirs::dataDirs();
385 // TODO/FIXME: this is fallback path for standard CMAKE_INSTALL_PREFIX
386 paths << "/usr/local/share";
387
388 foreach(QString path, paths)
389 {
390 QDir dir(QString("%1/lxqt/themes/%2").arg(path, themeName));
391 if (dir.isReadable())
392 return dir.absolutePath();
393 }
394
395 return QString();
396 }
397
398
399 /************************************************
400
401 ************************************************/
402 LXQtTheme::LXQtTheme(const LXQtTheme &other):
403 d(other.d)
404 {
405 }
406
407
408 /************************************************
409
410 ************************************************/
411 LXQtTheme::~LXQtTheme()
412 {
413 }
414
415
416 /************************************************
417
418 ************************************************/
419 LXQtTheme& LXQtTheme::operator=(const LXQtTheme &other)
420 {
421 d = other.d;
422 return *this;
423 }
424
425
426 /************************************************
427
428 ************************************************/
429 bool LXQtTheme::isValid() const
430 {
431 return d->mValid;
432 }
433
434
435 /************************************************
436
437 ************************************************/
438 QString LXQtTheme::name() const
439 {
440 return d->mName;
441 }
442
443 /************************************************
444
445 ************************************************/
446 QString LXQtTheme::path() const
447 {
448 return d->mPath;
449 }
450
451
452 /************************************************
453
454 ************************************************/
455 QString LXQtTheme::previewImage() const
456 {
457 return d->mPreviewImg;
458 }
459
460
461 /************************************************
462
463 ************************************************/
464 QString LXQtTheme::qss(const QString& module) const
465 {
466 QString styleSheet = d->loadQss(QStringLiteral("%1/%2.qss").arg(d->mPath, module));
467
468 // Single/double click ...........................
469 Settings s("desktop");
470 bool singleClick = s.value("icon-launch-mode", "singleclick").toString() == "singleclick";
471 styleSheet += QString("QAbstractItemView {activate-on-singleclick : %1; }").arg(singleClick ? 1 : 0);
472
473 return styleSheet;
474 }
475
476
477 /************************************************
478
479 ************************************************/
480 QString LXQtThemeData::loadQss(const QString& qssFile) const
481 {
482 QFile f(qssFile);
483 if (! f.open(QIODevice::ReadOnly | QIODevice::Text))
484 {
485 return QString();
486 }
487
488 QString qss = f.readAll();
489 f.close();
490
491 if (qss.isEmpty())
492 return QString();
493
494 // handle relative paths
495 QString qssDir = QFileInfo(qssFile).canonicalPath();
496 qss.replace(QRegExp("url.[ \\t\\s]*", Qt::CaseInsensitive, QRegExp::RegExp2), "url(" + qssDir + "/");
497
498 return qss;
499 }
500
501
502 /************************************************
503
504 ************************************************/
505 QString LXQtTheme::desktopBackground(int screen) const
506 {
507 QString wallpaperCfgFileName = QString("%1/wallpaper.cfg").arg(d->mPath);
508
509 if (wallpaperCfgFileName.isEmpty())
510 return QString();
511
512 QSettings s(wallpaperCfgFileName, QSettings::IniFormat);
513 QString themeDir = QFileInfo(wallpaperCfgFileName).absolutePath();
514 // There is something strange... If I remove next line the wallpapers array is not found...
515 s.childKeys();
516 s.beginReadArray("wallpapers");
517
518 s.setArrayIndex(screen - 1);
519 if (s.contains("file"))
520 return QString("%1/%2").arg(themeDir, s.value("file").toString());
521
522 s.setArrayIndex(0);
523 if (s.contains("file"))
524 return QString("%1/%2").arg(themeDir, s.value("file").toString());
525
526 return QString();
527 }
528
529
530 /************************************************
531
532 ************************************************/
533 const LXQtTheme &LXQtTheme::currentTheme()
534 {
535 static LXQtTheme theme;
536 QString name = Settings::globalSettings()->value("theme").toString();
537 if (theme.name() != name)
538 {
539 theme = LXQtTheme(name);
540 }
541 return theme;
542 }
543
544
545 /************************************************
546
547 ************************************************/
548 QList<LXQtTheme> LXQtTheme::allThemes()
549 {
550 QList<LXQtTheme> ret;
551 QSet<QString> processed;
552
553 QStringList paths;
554 paths << XdgDirs::dataHome(false);
555 paths << XdgDirs::dataDirs();
556
557 foreach(QString path, paths)
558 {
559 QDir dir(QString("%1/lxqt/themes").arg(path));
560 QFileInfoList dirs = dir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot);
561
562 foreach(QFileInfo dir, dirs)
563 {
564 if (!processed.contains(dir.fileName()) &&
565 QDir(dir.absoluteFilePath()).exists("lxqt-panel.qss"))
566 {
567 processed << dir.fileName();
568 ret << LXQtTheme(dir.absoluteFilePath());
569 }
570
571 }
572 }
573
574 return ret;
575 }
576
577
578 /************************************************
579
580 ************************************************/
581 SettingsCache::SettingsCache(QSettings &settings) :
582 mSettings(settings)
583 {
584 loadFromSettings();
585 }
586
587
588 /************************************************
589
590 ************************************************/
591 SettingsCache::SettingsCache(QSettings *settings) :
592 mSettings(*settings)
593 {
594 loadFromSettings();
595 }
596
597
598 /************************************************
599
600 ************************************************/
601 void SettingsCache::loadFromSettings()
602 {
603 foreach (QString key, mSettings.allKeys())
604 {
605 mCache.insert(key, mSettings.value(key));
606 }
607 }
608
609
610 /************************************************
611
612 ************************************************/
613 void SettingsCache::loadToSettings()
614 {
615 QHash<QString, QVariant>::const_iterator i = mCache.constBegin();
616
617 while(i != mCache.constEnd())
618 {
619 mSettings.setValue(i.key(), i.value());
620 ++i;
621 }
622
623 mSettings.sync();
624 }
625
626
627 /************************************************
628
629 ************************************************/
630 GlobalSettings::GlobalSettings():
631 Settings("lxqt"),
632 d_ptr(new GlobalSettingsPrivate(this))
633 {
634 if (value("icon_theme").toString().isEmpty())
635 {
636 QStringList failback;
637 failback << "oxygen";
638 failback << "Tango";
639 failback << "Prudence-icon";
640 failback << "Humanity";
641 failback << "elementary";
642 failback << "gnome";
643
644
645 QDir dir("/usr/share/icons/");
646 foreach (QString s, failback)
647 {
648 if (dir.exists(s))
649 {
650 setValue("icon_theme", s);
651 sync();
652 break;
653 }
654 }
655 }
656
657 fileChanged();
658 }
659
660 GlobalSettings::~GlobalSettings()
661 {
662 delete d_ptr;
663 }
664
665
666 /************************************************
667
668 ************************************************/
669 void GlobalSettings::fileChanged()
670 {
671 Q_D(GlobalSettings);
672 sync();
673
674
675 QString it = value("icon_theme").toString();
676 if (d->mIconTheme != it)
677 {
678 d->mIconTheme = it;
679 XdgIcon::setThemeName(it);
680 emit iconThemeChanged();
681 }
682
683 QString rt = value("theme").toString();
684 qlonglong themeUpdated = value("__theme_updated__").toLongLong();
685 if ((d->mLXQtTheme != rt) || (d->mThemeUpdated != themeUpdated))
686 {
687 d->mLXQtTheme = rt;
688 emit lxqtThemeChanged();
689 }
690
691 emit settingsChanged();
692 }
693