Make lxqt globals a public header
[lxde/liblxqt.git] / lxqtsettings.h
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 *
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
14 * version 2.1 of the License, or (at your option) any later version.
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
29 #ifndef LXQTSETTINGS_H
30 #define LXQTSETTINGS_H
31
32 #include <QObject>
33 #include <QSettings>
34 #include <QSharedDataPointer>
35 #include "lxqtglobals.h"
36
37 class QEvent;
38
39 namespace LXQt
40 {
41
42 class SettingsPrivate;
43 class GlobalSettings;
44
45 /*! \brief User settings handling */
46 class LXQT_API Settings : public QSettings
47 {
48 Q_OBJECT
49 public:
50
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.
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
56 panel create it as Settings("panel").
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 */
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();
67
68 static const GlobalSettings *globalSettings();
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
82 signals:
83 /*! /brief signal for backward compatibility (emitted whenever settingsChangedFromExternal() or settingsChangedByApp() is emitted)
84 */
85 void settingsChanged();
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();
92
93 protected:
94 bool event(QEvent *event);
95
96 protected slots:
97 /*! Called when the config file is changed */
98 virtual void fileChanged();
99
100 private slots:
101 void _fileChanged(QString path);
102
103 private:
104 void addWatchedFile(QString const & path);
105
106 private:
107 Q_DISABLE_COPY(Settings)
108
109 SettingsPrivate* const d_ptr;
110 Q_DECLARE_PRIVATE(Settings)
111 };
112
113
114 class LXQtThemeData;
115
116 /*! \brief QSS theme handling */
117 class LXQT_API LXQtTheme
118 {
119 public:
120 /// Constructs a null theme.
121 LXQtTheme();
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. */
126 LXQtTheme(const QString &path);
127
128 /// Constructs a copy of other. This is very fast.
129 LXQtTheme(const LXQtTheme &other);
130
131 LXQtTheme& operator=(const LXQtTheme &other);
132 ~LXQtTheme();
133
134 /// Returns the name of the theme.
135 QString name() const;
136
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 */
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
158 /// Returns the current lxqt theme.
159 static const LXQtTheme &currentTheme();
160
161 /// Returns the all themes found in the system.
162 static QList<LXQtTheme> allThemes();
163
164 private:
165 static LXQtTheme* mInstance;
166 QSharedDataPointer<LXQtThemeData> d;
167 };
168
169 /*!
170 A global pointer referring to the unique LXQtTheme object.
171 It is equivalent to the pointer returned by the LXQtTheme::instance() function.
172 Only one theme object can be created. !*/
173
174 #define lxqtTheme LXQtTheme::currentTheme()
175
176
177 class LXQT_API SettingsCache
178 {
179 public:
180 explicit SettingsCache(QSettings &settings);
181 explicit SettingsCache(QSettings *settings);
182 virtual ~SettingsCache() {}
183
184 void loadFromSettings();
185 void loadToSettings();
186
187 private:
188 QSettings &mSettings;
189 QHash<QString, QVariant> mCache;
190 };
191
192 class GlobalSettingsPrivate;
193
194 class GlobalSettings: public Settings
195 {
196 Q_OBJECT
197 public:
198 GlobalSettings();
199 ~GlobalSettings();
200
201 signals:
202 /// Signal emitted when the icon theme has changed.
203 void iconThemeChanged();
204
205 /// Signal emitted when the lxqt theme has changed.
206 void lxqtThemeChanged();
207
208 protected slots:
209 void fileChanged();
210
211 private:
212 GlobalSettingsPrivate* const d_ptr;
213 Q_DECLARE_PRIVATE(GlobalSettings)
214 };
215
216 } // namespace LXQt
217 #endif // LXQTSETTINGS_H