1 /*
2  * Cantata
3  *
4  * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #ifndef CONFIGURATION_H
25 #define CONFIGURATION_H
26 
27 #include <QString>
28 #include <QStringList>
29 #include <QByteArray>
30 #include <QSize>
31 #include <QDateTime>
32 #include <QSettings>
33 #include "utils.h"
34 
35 class Configuration : public QSettings
36 {
37 public:
38     static QLatin1String constMainGroup;
39     Configuration(const QString &group=constMainGroup);
40     ~Configuration() override;
41 
get(const QString & key,const QString & def)42     QString      get(const QString &key, const QString &def)     { return contains(key) ? value(key).toString() : def; }
get(const QString & key,const QStringList & def)43     QStringList  get(const QString &key, const QStringList &def) { return contains(key) ? value(key).toStringList() : def; }
get(const QString & key,bool def)44     bool         get(const QString &key, bool def)               { return contains(key) ? value(key).toBool() : def; }
get(const QString & key,int def)45     int          get(const QString &key, int def)                { return contains(key) ? value(key).toInt() : def; }
get(const QString & key,unsigned int def)46     unsigned int get(const QString &key, unsigned int def)       { return contains(key) ? value(key).toUInt() : def; }
get(const QString & key,const QByteArray & def)47     QByteArray   get(const QString &key, const QByteArray &def)  { return contains(key) ? value(key).toByteArray() : def; }
get(const QString & key,const QSize & def)48     QSize        get(const QString &key, const QSize &def)       { return contains(key) ? value(key).toSize() : def; }
get(const QString & key,const QDateTime & def)49     QDateTime    get(const QString &key, const QDateTime &def)   { return contains(key) ? value(key).toDateTime() : def; }
set(const QString & key,const QString & val)50     void         set(const QString &key, const QString &val)     { if (!hasEntry(key) || get(key, val)!=val) setValue(key, val); }
set(const QString & key,const char * val)51     void         set(const QString &key, const char *val)        { if (!hasEntry(key) || get(key, QLatin1String(val))!=QLatin1String(val)) setValue(key, val); }
set(const QString & key,const QStringList & val)52     void         set(const QString &key, const QStringList &val) { if (!hasEntry(key) || get(key, val)!=val) setValue(key, val); }
set(const QString & key,bool val)53     void         set(const QString &key, bool val)               { if (!hasEntry(key) || get(key, val)!=val) setValue(key, val); }
set(const QString & key,int val)54     void         set(const QString &key, int val)                { if (!hasEntry(key) || get(key, val)!=val) setValue(key, val); }
set(const QString & key,unsigned int val)55     void         set(const QString &key, unsigned int val)       { if (!hasEntry(key) || get(key, val)!=val) setValue(key, val); }
set(const QString & key,const QByteArray & val)56     void         set(const QString &key, const QByteArray &val)  { if (!hasEntry(key) || get(key, val)!=val) setValue(key, val); }
set(const QString & key,const QSize & val)57     void         set(const QString &key, const QSize &val)       { if (!hasEntry(key) || get(key, val)!=val) setValue(key, val); }
set(const QString & key,const QDateTime & val)58     void         set(const QString &key, const QDateTime &val)   { if (!hasEntry(key) || get(key, val)!=val) setValue(key, val); }
hasGroup(const QString & grp)59     bool         hasGroup(const QString &grp)                    { return -1!=childGroups().indexOf(grp); }
removeGroup(const QString & grp)60     void         removeGroup(const QString &grp)                 { remove(grp); }
hasEntry(const QString & key)61     bool         hasEntry(const QString &key)                    { return contains(key); }
removeEntry(const QString & key)62     void         removeEntry(const QString &key)                 { remove(key); }
63     int          get(const QString &key, int def, int min, int max);
64     QString      getFilePath(const QString &key, const QString &def);
setFilePath(const QString & key,const QString & val)65     void         setFilePath(const QString &key, const QString &val) { return set(key, Utils::homeToTilda(val)); }
66     QString      getDirPath(const QString &key, const QString &def);
setDirPath(const QString & key,const QString & val)67     void         setDirPath(const QString &key, const QString &val) { return set(key, Utils::homeToTilda(val)); }
68 };
69 
70 #endif
71