Settings.h 7.41 KB
Newer Older
1
/*****************************************************************************
2
 * Settings.h: Backend settings manager
3
 *****************************************************************************
4
 * Copyright (C) 2008-2014 VideoLAN
5
 *
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifndef SETTINGSMANAGER_H
#define SETTINGSMANAGER_H

26
#include "Main/Core.h"
27
#include "Main/Project.h"
28
#include "SettingValue.h"
29

30
#include <QString>
31
#include <QMap>
32
#include <QObject>
33 34
#include <QReadWriteLock>
#include <QVariant>
35
#include <QXmlStreamWriter>
36

37
class SettingValue;
38 39

class QFile;
40
class QDomElement;
41 42 43


//Var helpers :
44 45 46 47 48 49 50
#define VLMC_GET_STRING( key )      Core::getInstance()->settings()->value( key )->get().toString()
#define VLMC_GET_INT( key )         Core::getInstance()->settings()->value( key )->get().toInt()
#define VLMC_GET_UINT( key )        Core::getInstance()->settings()->value( key )->get().toUInt()
#define VLMC_GET_DOUBLE( key )      Core::getInstance()->settings()->value( key )->get().toDouble()
#define VLMC_GET_BOOL( key )        Core::getInstance()->settings()->value( key )->get().toBool()
#define VLMC_GET_STRINGLIST( key )  Core::getInstance()->settings()->value( key )->get().toStringList()
#define VLMC_GET_BYTEARRAY( key )   Core::getInstance()->settings()->value( key )->get().toByteArray()
51

52 53 54 55 56
#define VLMC_PROJECT_GET_STRING( key )  Project::getInstance()->settings()->value( key )->get().toString()
#define VLMC_PROJECT_GET_INT( key )     Project::getInstance()->settings()->value( key )->get().toInt()
#define VLMC_PROJECT_GET_UINT( key )    Project::getInstance()->settings()->value( key )->get().toUInt()
#define VLMC_PROJECT_GET_DOUBLE( key )  Project::getInstance()->settings()->value( key )->get().toDouble()
#define VLMC_PROJECT_GET_BOOL( key )    Project::getInstance()->settings()->value( key )->get().toBool()
57 58


59
#define VLMC_CREATE_PROJECT_VAR( type, key, defaultValue, name, desc, flags )  \
60 61
        Project::getInstance()->settings()->createVar( type, key, defaultValue, name, \
                                                       desc, flags );
62 63

#define VLMC_CREATE_PROJECT_INT( key, defaultValue, name, desc )  \
64
        VLMC_CREATE_PROJECT_VAR( SettingValue::Int, key, defaultValue, name, desc, SettingValue::Nothing )
65
#define VLMC_CREATE_PROJECT_STRING( key, defaultValue, name, desc )  \
66
        VLMC_CREATE_PROJECT_VAR( SettingValue::String, key, defaultValue, name, desc, SettingValue::Nothing )
67
#define VLMC_CREATE_PROJECT_DOUBLE( key, defaultValue, name, desc )  \
68
        VLMC_CREATE_PROJECT_VAR( SettingValue::Double, key, defaultValue, name, desc, SettingValue::Nothing )
69
#define VLMC_CREATE_PROJECT_BOOL( key, defaultValue, name, desc )  \
70
        VLMC_CREATE_PROJECT_VAR( SettingValue::Bool, key, defaultValue, name, desc, SettingValue::Nothing )
71 72 73
#define VLMC_CREATE_PROJECT_PATH( key, defaultValue, name, desc )  \
        VLMC_CREATE_PROJECT_PATH( SettingValue::Path, key, defaultValue, name, desc, SettingValue::Nothing )

74

75
#define VLMC_CREATE_PREFERENCE( type, key, defaultValue, name, desc, flags )  \
76 77
        Core::getInstance()->settings()->createVar( type, key, defaultValue, name,  \
                                                       desc, flags );
78

79
/// Vlmc preferences macros
80
#define VLMC_CREATE_PREFERENCE_INT( key, defaultValue, name, desc )  \
81
        VLMC_CREATE_PREFERENCE( SettingValue::Int, key, defaultValue, name, desc, SettingValue::Nothing )
82
#define VLMC_CREATE_PREFERENCE_STRING( key, defaultValue, name, desc )  \
83
        VLMC_CREATE_PREFERENCE( SettingValue::String, key, defaultValue, name, desc, SettingValue::Nothing )
84
#define VLMC_CREATE_PREFERENCE_DOUBLE( key, defaultValue, name, desc )  \
85
        VLMC_CREATE_PREFERENCE( SettingValue::Double, key, defaultValue, name, desc, SettingValue::Nothing )
86
#define VLMC_CREATE_PREFERENCE_LANGUAGE( key, defaultValue, name, desc )  \
87
        VLMC_CREATE_PREFERENCE( SettingValue::Language, key, defaultValue, name, desc, SettingValue::Nothing )
88
#define VLMC_CREATE_PREFERENCE_KEYBOARD( key, defaultValue, name, desc )  \
89
        VLMC_CREATE_PREFERENCE( SettingValue::KeyboardShortcut, key, defaultValue, name, desc, SettingValue::Nothing )
90
#define VLMC_CREATE_PREFERENCE_BOOL( key, defaultValue, name, desc )  \
91
        VLMC_CREATE_PREFERENCE( SettingValue::Bool, key, defaultValue, name, desc, SettingValue::Nothing )
92 93
#define VLMC_CREATE_PREFERENCE_PATH( key, defaultValue, name, desc )  \
        VLMC_CREATE_PREFERENCE( SettingValue::Path, key, defaultValue, name, desc, SettingValue::Nothing )
94

95
/// Convenience macros :
96 97
#define VLMC_CREATE_PRIVATE_PREFERENCE_STRING( key, defaultValue )  \
        VLMC_CREATE_PREFERENCE( SettingValue::String, key, defaultValue, "", "", SettingValue::Private )
98 99
#define VLMC_CREATE_PRIVATE_PREFERENCE_INT( key, defaultValue )  \
        VLMC_CREATE_PREFERENCE( SettingValue::Int, key, defaultValue, "", "", SettingValue::Private )
100 101 102 103 104
#define VLMC_CREATE_PRIVATE_PREFERENCE_BOOL( key, defaultValue )  \
        VLMC_CREATE_PREFERENCE( SettingValue::Bool, key, defaultValue, "", "", SettingValue::Private )
#define VLMC_CREATE_PRIVATE_PREFERENCE_BYTEARRAY( key, defaultValue )  \
        VLMC_CREATE_PREFERENCE( SettingValue::ByteArray, key, defaultValue, "", "", SettingValue::Private )

105 106
#define VLMC_CREATE_PREFERENCE_PASSWORD( key, defaultValue, name, desc )  \
        VLMC_CREATE_PREFERENCE( SettingValue::String, key, defaultValue, name, desc, SettingValue::Password )
107 108
#define VLMC_CREATE_PRIVATE_PROJECT_STRING( key, defaultValue )  \
        VLMC_CREATE_PROJECT_VAR( SettingValue::String, key, defaultValue, "", "", SettingValue::Private )
109

110

111
class   Settings
112
{
113 114
    public:
        typedef QList<SettingValue*>                SettingList;
115 116 117 118 119 120 121 122 123
        typedef QMap<QString, SettingValue*>        SettingMap;

        Settings( const QString& settingsFile );
        ~Settings();
        bool                        setValue(const QString &key, const QVariant &value );
        SettingValue*               value( const QString &key );
        SettingValue*               createVar( SettingValue::Type type, const QString &key, const QVariant &defaultValue, const char *name, const char *desc, SettingValue::Flags flags );
        SettingList                 group( const QString &groupName ) const;
        bool                        load( const QDomElement &element );
124
        void                        save() const;
125
        void                        save( QXmlStreamWriter& project ) const;
126
        bool                        watchValue( const QString &key, QObject* receiver, const char *method, Qt::ConnectionType cType = Qt::AutoConnection );
127
    private:
128 129 130
        SettingMap                  m_settings;
        mutable QReadWriteLock      m_rwLock;
        QFile*                      m_settingsFile;
131
};
132 133

#endif