Settings: unify duplicated code
[lxde/liblxqt.git] / lxqtsettings.h
CommitLineData
4ef6cee9 1/* BEGIN_COMMON_COPYRIGHT_HEADER
c4af778e 2 * (c)LGPL2+
4ef6cee9 3 *
b9223fe7 4 * LXQt - a lightweight, Qt based, desktop toolset
4ef6cee9 5 * http://razor-qt.org
6 *
7 * Copyright: 2010-2011 Razor team
8 * Authors:
626f1f31 9 * Alexander Sokoloff <sokoloff.a@gmail.com>
4ef6cee9 10 *
11 * This program or library is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
c4af778e 14 * version 2.1 of the License, or (at your option) any later version.
4ef6cee9 15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20
21 * You should have received a copy of the GNU Lesser General
22 * Public License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
25 *
26 * END_COMMON_COPYRIGHT_HEADER */
27
28
82830dc2
AS
29#ifndef LXQTSETTINGS_H
30#define LXQTSETTINGS_H
4ef6cee9 31
e6389bfc
HJYP
32#include <QObject>
33#include <QSettings>
34#include <QSharedDataPointer>
f9cba1ba 35#include "lxqtglobals.h"
4ef6cee9 36
37class QEvent;
552e1c4a 38
f05ba5af 39namespace LXQt
82830dc2
AS
40{
41
42class SettingsPrivate;
43class GlobalSettings;
de53bc2d 44
8966cbec 45/*! \brief User settings handling */
f9cba1ba 46class LXQT_API Settings : public QSettings
4ef6cee9 47{
48 Q_OBJECT
49public:
50
82830dc2
AS
51 /*! \brief Constructs a Settings object for accessing settings of the module called module, and with parent parent.
52 Settings can be accessed using the standard interface provided by QSettings, but it also provides some convenience functions and signals.
4ef6cee9 53
54 \param module a base name of the config file. It's a name without
55 the extension. For example: if you want to open settings for
82830dc2 56 panel create it as Settings("panel").
4ef6cee9 57 The function will create all parent directories necessary to create
58 the file.
59 \param parent It's no need to delete this class manually if it's set.
60 */
82830dc2
AS
61 explicit Settings(const QString& module, QObject* parent = 0);
62 //explicit Settings(QObject* parent=0);
63 explicit Settings(const QSettings* parentSettings, const QString& subGroup, QObject* parent=0);
64 explicit Settings(const QSettings& parentSettings, const QString& subGroup, QObject* parent=0);
65 Settings(const QString &fileName, QSettings::Format format, QObject *parent = 0);
66 ~Settings();
4ef6cee9 67
82830dc2 68 static const GlobalSettings *globalSettings();
de53bc2d
AS
69
70
71 /*! Returns the localized value for key. In the desktop file keys may be postfixed by [LOCALE]. If the
72 localized value doesn't exist, returns non lokalized value. If non localized value doesn't exist, returns defaultValue.
73 LOCALE must be of the form lang_COUNTRY.ENCODING@MODIFIER, where _COUNTRY, .ENCODING, and @MODIFIER may be omitted.
74
75 If no default value is specified, a default QVariant is returned. */
76 QVariant localizedValue(const QString& key, const QVariant& defaultValue = QVariant()) const;
77
78 /*! Sets the value of setting key to value. If a localized version of the key already exists, the previous value is
79 overwritten. Otherwise, it overwrites the the un-localized version. */
80 void setLocalizedValue(const QString &key, const QVariant &value);
81
4ef6cee9 82signals:
1b34ac93
PK
83 /*! /brief signal for backward compatibility (emitted whenever settingsChangedFromExternal() or settingsChangedByApp() is emitted)
84 */
c16a53ee 85 void settingsChanged();
1b34ac93
PK
86 /*! /brief signal emitted when the settings file is changed by external application
87 */
88 void settingsChangedFromExternal();
89 /*! /brief signal emitted when any setting is changed by this object
90 */
91 void settingsChangedByApp();
4ef6cee9 92
0de9564b
PV
93protected:
94 bool event(QEvent *event);
95
7ee52b88 96protected slots:
67dd270e 97 /*! Called when the config file is changed */
7ee52b88 98 virtual void fileChanged();
4ef6cee9 99
67dd270e
HJYP
100private slots:
101 void _fileChanged(QString path);
102
4ef6cee9 103private:
1b34ac93
PK
104 void addWatchedFile(QString const & path);
105
106private:
82830dc2 107 Q_DISABLE_COPY(Settings)
4ef6cee9 108
82830dc2
AS
109 SettingsPrivate* const d_ptr;
110 Q_DECLARE_PRIVATE(Settings)
4ef6cee9 111};
112
113
f05ba5af 114class LXQtThemeData;
63a934d5 115
8966cbec 116/*! \brief QSS theme handling */
f05ba5af 117class LXQT_API LXQtTheme
4ef6cee9 118{
4ef6cee9 119public:
63a934d5 120 /// Constructs a null theme.
f05ba5af 121 LXQtTheme();
63a934d5
AS
122
123 /*! Constructs an theme from the dir with the given path. If path not absolute
124 (i.e. the theme name only) the relevant dir must be found relative to the
125 $XDG_DATA_HOME + $XDG_DATA_DIRS directories. */
f05ba5af 126 LXQtTheme(const QString &path);
63a934d5
AS
127
128 /// Constructs a copy of other. This is very fast.
f05ba5af 129 LXQtTheme(const LXQtTheme &other);
63a934d5 130
f05ba5af
PL
131 LXQtTheme& operator=(const LXQtTheme &other);
132 ~LXQtTheme();
63a934d5
AS
133
134 /// Returns the name of the theme.
135 QString name() const;
4ef6cee9 136
63a934d5
AS
137 QString path() const;
138
139 QString previewImage() const;
140
141 /// Returns true if this theme is valid; otherwise returns false.
142 bool isValid() const;
143
144 /*! \brief Returns StyleSheet text (not file name, but real text) of the module called module.
145 Paths in url() C/QSS functions are parsed to be real values for the theme,
146 relative to full path
147 */
4ef6cee9 148 QString qss(const QString& module) const;
149
150 /*! \brief A full path to image used as a wallpaper
151 \param screen is an ID of the screen like in Qt. -1 means default (any) screen.
152 Any other value greater than -1 is the exact screen (in dualhead).
153 In themes the index starts from 1 (ix 1 means 1st screen).
154 \retval QString a file name (including path).
155 */
156 QString desktopBackground(int screen=-1) const;
157
db4aaddf 158 /// Returns the current lxqt theme.
f05ba5af 159 static const LXQtTheme &currentTheme();
63a934d5
AS
160
161 /// Returns the all themes found in the system.
f05ba5af 162 static QList<LXQtTheme> allThemes();
4ef6cee9 163
63a934d5 164private:
f05ba5af
PL
165 static LXQtTheme* mInstance;
166 QSharedDataPointer<LXQtThemeData> d;
4ef6cee9 167};
168
169/*!
f05ba5af
PL
170A global pointer referring to the unique LXQtTheme object.
171It is equivalent to the pointer returned by the LXQtTheme::instance() function.
4ef6cee9 172Only one theme object can be created. !*/
173
f05ba5af 174#define lxqtTheme LXQtTheme::currentTheme()
63a934d5 175
4ef6cee9 176
f9cba1ba 177class LXQT_API SettingsCache
4ef6cee9 178{
179public:
82830dc2
AS
180 explicit SettingsCache(QSettings &settings);
181 explicit SettingsCache(QSettings *settings);
182 virtual ~SettingsCache() {}
4ef6cee9 183
184 void loadFromSettings();
185 void loadToSettings();
186
187private:
188 QSettings &mSettings;
189 QHash<QString, QVariant> mCache;
190};
191
82830dc2 192class GlobalSettingsPrivate;
4ef6cee9 193
82830dc2 194class GlobalSettings: public Settings
4ef6cee9 195{
196 Q_OBJECT
197public:
82830dc2
AS
198 GlobalSettings();
199 ~GlobalSettings();
4ef6cee9 200
201signals:
202 /// Signal emitted when the icon theme has changed.
203 void iconThemeChanged();
204
db4aaddf
HJYP
205 /// Signal emitted when the lxqt theme has changed.
206 void lxqtThemeChanged();
4ef6cee9 207
7ee52b88 208protected slots:
4ef6cee9 209 void fileChanged();
210
211private:
82830dc2
AS
212 GlobalSettingsPrivate* const d_ptr;
213 Q_DECLARE_PRIVATE(GlobalSettings)
4ef6cee9 214};
215
f05ba5af 216} // namespace LXQt
82830dc2 217#endif // LXQTSETTINGS_H