Settings.cpp 4.89 KB
Newer Older
1
/*****************************************************************************
2
 * Settings.cpp: 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
 *
 * 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.
 *****************************************************************************/

23
#include "Settings.h"
24
#include "SettingValue.h"
25
#include "Tools/VlmcDebug.h"
26

27 28
#include <QByteArray>
#include <QFile>
29 30
#include <QWriteLocker>
#include <QReadLocker>
31
#include <QStringList>
32
#include <QXmlStreamWriter>
33

34
#include <QDomElement>
35 36


37 38
Settings::Settings(const QString &settingsFile)
    : m_settingsFile( NULL )
39
{
40 41
    if ( settingsFile.isEmpty() == false )
        m_settingsFile = new QFile( settingsFile );
42 43
}

44
Settings::~Settings()
45
{
46
    delete m_settingsFile;
47 48
}

49
bool
50
Settings::watchValue( const QString &key, QObject* receiver, const char *method, Qt::ConnectionType cType )
51
{
52
    SettingValue* s = value( key );
53
    if ( s != NULL )
54
    {
55
        QObject::connect( s, SIGNAL( changed( const QVariant& ) ),
56 57
                 receiver, method , cType );
        return true;
58
    }
59
    Q_ASSERT_X( false, __FILE__, "watching value without a created variable" );
60
    return false;
61
}
62

63
void
64
Settings::save( QXmlStreamWriter& project ) const
65
{
66
    QReadLocker lock( &m_rwLock );
67

68 69
    SettingMap::const_iterator     it = m_settings.begin();
    SettingMap::const_iterator     end = m_settings.end();
70

71
    project.writeStartElement( "settings" );
72
    for ( ; it != end; ++it )
73
    {
74
        if ( ( (*it)->flags() & SettingValue::Runtime ) != 0 )
75
            continue ;
76
        project.writeStartElement( "setting" );
77 78
        project.writeAttribute( "key", (*it)->key() );
        project.writeAttribute( "value", (*it)->get().toString() );
79
        project.writeEndElement();
80
    }
81
    project.writeEndElement();
82
}
83

84
bool
85
Settings::load( const QDomElement &root )
86
{
87
    QDomElement     element = root.firstChildElement( "settings" );
88
    if ( element.isNull() == true )
89
    {
90
        vlmcWarning() << "Invalid settings node";
91 92
        return false ;
    }
93
    QDomElement s = element.firstChildElement( "setting" );
94
    while ( s.isNull() == false )
95
    {
96 97 98 99
        QString     key = s.attribute( "key" );
        QString     value = s.attribute( "value" );

        if ( key.isEmpty() == true || value.isEmpty() == true )
100
            vlmcWarning() << "Invalid setting node.";
101
        else
102
            if ( setValue( key, value ) == false )
103
                vlmcWarning() << "Loaded invalid project setting:" << key;
104
        s = s.nextSiblingElement();
105
    }
106
    return true;
107 108
}

109 110
void
Settings::save() const
111
{
112 113 114 115 116 117 118 119
    if ( m_settingsFile == NULL )
        return ;
    QByteArray          settingsContent;
    QXmlStreamWriter    streamWriter( &settingsContent );
    save( streamWriter );
    m_settingsFile->open( QFile::WriteOnly );
    m_settingsFile->write( settingsContent );
    m_settingsFile->close();
120 121 122
}

bool
123
Settings::setValue(const QString &key, const QVariant &value)
124 125 126
{
    SettingMap::iterator   it = m_settings.find( key );
    if ( it != m_settings.end() )
127
    {
128 129
        (*it)->set( value );
        return true;
130
    }
131 132 133 134
    return false;
}

SettingValue*
135
Settings::value(const QString &key)
136 137 138 139 140 141 142 143 144 145
{
    QReadLocker lock( &m_rwLock );

    SettingMap::iterator it = m_settings.find( key );
    if ( it != m_settings.end() )
        return *it;
    return NULL;
}

SettingValue*
146
Settings::createVar(SettingValue::Type type, const QString &key, const QVariant &defaultValue, const char *name, const char *desc, SettingValue::Flags flags)
147 148 149 150 151 152 153 154 155 156
{
    QWriteLocker lock( &m_rwLock );

    if ( m_settings.contains( key ) )
        return NULL;
    SettingValue* val = new SettingValue( key, type, defaultValue, name, desc, flags );
    m_settings.insert( key, val );
    return val;
}

157 158
Settings::SettingList
Settings::group(const QString &groupName) const
159 160 161 162
{
    QReadLocker lock( &m_rwLock );
    SettingMap::const_iterator          it = m_settings.begin();
    SettingMap::const_iterator          ed = m_settings.end();
163
    SettingList        ret;
164 165 166

    QString grp = groupName + '/';
    for ( ; it != ed; ++it )
167
    {
168 169
        if ( (*it)->key().startsWith( grp ) )
            ret.push_back( *it );
170
    }
171 172
    return ret;
}