WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZSettings.h
Go to the documentation of this file.
1 /*
2  * Zuble - A run-time system for QML/Javascript applications
3  * Copyright (C) 2015 Bob Dinitto
4  *
5  * Filename: ZSettings.h
6  * Created on: 5/16/2015
7  * Author: Bob Dinitto
8  *
9  * Zuble is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #ifndef ZSETTINGS_H
26 #define ZSETTINGS_H
27 
28 #include <QObject>
29 #include <QSettings>
30 #include <QFileSystemWatcher>
31 #include "zblcore_global.h"
32 #include "zglobal.h"
33 
34 namespace Zbl
35 {
36 
37 class ZFileSystemWatcher;
38 
44 class ZBLCORESHARED_EXPORT ZSettings : public QObject
45 {
46  Q_OBJECT
47 
56  {
57  public:
59 
60  SettingsBundle(const QString& dataPath)
61  : m_dataPath(dataPath){}
62 
63  SettingsBundle(const QString& metaPath, const QString& dataPath)
64  : m_metaPath(metaPath),
65  m_dataPath(dataPath){}
66 
67  SettingsBundle(const QString& meta, const QString& dataPath, bool metaAtomic)
68  :m_dataPath(dataPath){if(metaAtomic) m_metaData = meta; else m_metaPath = meta;}
69 
70  SettingsBundle(const QString& metaPath,
71  QSettings::Scope scope = QSettings::UserScope,
72  const QString& organization = QString(),
73  const QString& application = QString())
74  : m_metaPath(metaPath),
75  m_organization(organization),
76  m_application(application),
77  m_scope(scope){}
78 
79  QString m_metaPath;
80  QString m_metaData;
81  QString m_dataPath;
82  QString m_organization;
83  QString m_application;
84  QSettings::Scope m_scope;
85  };
86 
87  typedef QMap<QString, SettingsBundle> bundleMap;
88 
89 
90 
91 public:
92  explicit ZSettings(QObject *parent = nullptr);
93  virtual ~ZSettings();
94 
99  static void registerType();
100 
104  static void zInit();
105 
106 
111  Q_PROPERTY(QVariant tags READ getTags)
112 
113 
117  Q_PROPERTY(QVariant fileName READ getFileName CONSTANT)
118 
119 
121 
126  Q_PROPERTY(QStringList allKeys READ getAllKeys)
127 
132  Q_PROPERTY(QStringList childKeys READ getChildKeys)
133 
138  Q_PROPERTY(QStringList childGroups READ getChildGroups)
139 
144  Q_PROPERTY(bool isWritable READ getWritable)
145 
150  Q_PROPERTY(int format READ getFormat)
151 
156  Q_PROPERTY(int scope READ getScope)
157 
162  Q_PROPERTY(QString organizationName READ getOrganizationName)
163 
168  Q_PROPERTY(QString applicationName READ getApplicationName)
169 
170 
172 
173 
174  QVariant getTags();
175  QString getFileName() const;
176 
177  QStringList getAllKeys() const;
178  QStringList getChildKeys() const;
179  QStringList getChildGroups() const;
180  bool getWritable() const;
181  int getFormat() const;
182  int getScope() const;
183  QString getOrganizationName() const;
184  QString getApplicationName() const;
185 
186  //void setFallbacksEnabled(bool b);
187  //bool fallbacksEnabled() const;
188 
189 
190 
198  static bool readFunc(QIODevice &device, QSettings::SettingsMap &map);
199 
207  static bool writeFunc(QIODevice &device, const QSettings::SettingsMap &map);
208 
218  Q_INVOKABLE bool open(int scope = static_cast<int>(QSettings::UserScope),
219  const QString& application = QString(),
220  const QString& organization = QString());
221 
228  Q_INVOKABLE bool open(const QString& filePath);
229 
246  Q_INVOKABLE bool setFileWatcher(bool enabled);
247 
248 
249  Q_INVOKABLE void clear();
250  Q_INVOKABLE void sync();
251  Q_INVOKABLE int status() const;
252 
253  Q_INVOKABLE void setValue(const QString &key, const QVariant &value);
254  Q_INVOKABLE QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
255 
256  Q_INVOKABLE void remove(const QString &key);
257  Q_INVOKABLE bool contains(const QString &key) const;
258 
259  Q_INVOKABLE void beginGroup(const QString &prefix);
260  Q_INVOKABLE void endGroup();
261  Q_INVOKABLE QString group() const;
262 
263  Q_INVOKABLE int beginReadArray(const QString &prefix);
264  Q_INVOKABLE void beginWriteArray(const QString &prefix, int size = -1);
265  Q_INVOKABLE void endArray();
266  Q_INVOKABLE void setArrayIndex(int i);
267 
268 
269  static bool containsBundle(const QString& id);
270 
281  static bool insertBundle(
282  const QString& id,
283  const QString& metaPath,
284  QSettings::Scope scope = QSettings::UserScope,
285  const QString organization = QString(),
286  const QString application = QString());
287 
316  static bool insertBundle(
317  const QString& id,
318  const QString& meta,
319  const QString& dataPath,
320  bool metaAtomic = false);
321 
322  static QVariant getBundle(const QString& id);
323 
334  static ZSettings* getBundleSettings(const QString& id);
335  static QString getBundleMetapath(const QString& id);
336  static QString getBundleMetadata(const QString& id);
337  static QVariant getAllBundles();
338 
339  static bool mapConfigSettings(const char* configFilePath);
340 
341  //static bool realizeSettings(const QString& settingsMetafilePath);
342 
351  static bool realizeSettings(const QString& id, const SettingsBundle& settingsBundle);
352 
353  static QString scopeToText(QSettings::Scope scope);
354  static QSettings::Scope textToScope(const QString& text);
355 
356 
357  Q_INVOKABLE bool hasBundle(const QString& id) const;
358  Q_INVOKABLE bool insertBundle(const QString& id, QVariant bundle);
359  Q_INVOKABLE QVariant bundle(const QString& id) const;
360  Q_INVOKABLE QVariant allBundles() const;
361 
362 
363 
364 
365 /*
366 
367  //void beginGroup(const QString &prefix);
368  //void endGroup();
369  //QString group() const;
370 
371  //int beginReadArray(const QString &prefix);
372  //void beginWriteArray(const QString &prefix, int size = -1);
373  //void endArray();
374  //void setArrayIndex(int i);
375 
376  QStringList allKeys() const;
377  QStringList childKeys() const;
378  QStringList childGroups() const;
379  bool isWritable() const;
380 
381 
382  void setFallbacksEnabled(bool b);
383  bool fallbacksEnabled() const;
384 
385  //QString fileName() const;
386  Format format() const;
387  Scope scope() const;
388  QString organizationName() const;
389  QString applicationName() const;
390 
391 
393 
394 //#ifndef QT_NO_TEXTCODEC
395 // void setIniCodec(QTextCodec *codec);
396 // void setIniCodec(const char *codecName);
397 // QTextCodec *iniCodec() const;
398 //#endif
399 
400 
401  static void setDefaultFormat(Format format);
402  static Format defaultFormat();
403  static void setSystemIniPath(const QString &dir); // ### Qt 6: remove (use setPath() instead)
404  static void setUserIniPath(const QString &dir); // ### Qt 6: remove (use setPath() instead)
405  static void setPath(Format format, Scope scope, const QString &path);
406 
408 */
409 
410 signals:
411 
418  void settingsModified(const QString& path);
419 
435  void updateValue(
436  const QString& settingPath,
437  const QString& settingName,
438  QVariant oldValue,
439  QVariant newValue);
440 
441 public slots:
442 
443 protected:
444 
445  //static bool insertBundle(const QString& id, const SettingsBundle& bundle);
446 
447 
448  static void createTags();
449 
450  static void dumpMap(const QSettings::SettingsMap &map);
451  static void dumpGroup(const QString& groupKey, const QVariant& group);
452  inline void validateStatus() const;
453  bool open(const SettingsBundle& settingsBundle);
454  bool validateOpen();
455  static QString rectifySettingsDataPath(const QString& dataPath);
456  static bool ensureFileExists(const QString& filePath);
457  inline void validateWatcher() const;
458 
459  void onFileChanged(const QString &path);
460 
461 
466  static QVariant m_tags;
467 
472  QSettings* m_s;
473 
480  static QSettings::Format m_format;
481 
486  static const QString m_ext;
487 
492  static qint64 m_maxFileSize;
493 
498  static bundleMap m_bundles;
499 
505  static ZFileSystemWatcher* m_watcher;
506 
511  bool m_watching;
512 
514 };
515 
516 typedef QScopedPointer<ZSettings> ZScopedSettings;
517 
518 inline void ZSettings::validateStatus() const
519 {
520  if(!m_s)
521  throw ZblException("Settings object not initialized. "
522  "Use ZSettings::open method to open the settings object.");
523 }
524 
525 inline void ZSettings::validateWatcher() const
526 {
527  if(!m_watcher)
528  throw ZblException("Settings watcher not initialized.");
529 }
530 
531 
532 
533 
534 } // Zbl
535 
536 #endif // ZSETTINGS_H
SettingsBundle(const QString &metaPath, const QString &dataPath)
Definition: ZSettings.h:63
#define ZBLCORESHARED_EXPORT
This class wraps QFileSystemWatcher and adds path reference counting.
SettingsBundle(const QString &meta, const QString &dataPath, bool metaAtomic)
Definition: ZSettings.h:67
#define ZBL_DECLARE_LOGGED_OBJECT
Definition: zglobal.h:94
SettingsBundle(const QString &dataPath)
Definition: ZSettings.h:60
QMap< QString, SettingsBundle > bundleMap
Definition: ZSettings.h:87
SettingsBundle(const QString &metaPath, QSettings::Scope scope=QSettings::UserScope, const QString &organization=QString(), const QString &application=QString())
Definition: ZSettings.h:70
Definition: ZAndGate.cpp:6
Wraps the QSettings class and implements a JSON-based backend for storing the settings data...
Definition: ZSettings.h:44
QSettings::Scope m_scope
Definition: ZSettings.h:84
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45
void validateWatcher() const
Definition: ZSettings.h:525
QScopedPointer< ZSettings > ZScopedSettings
Definition: ZSettings.h:516
This inner class allows ZSettings to store settings bundles.
Definition: ZSettings.h:55