SettingsManager.cpp 7.99 KB
Newer Older
1
/*****************************************************************************
2
 * SettingsManager.cpp: Backend settings manager
3
 *****************************************************************************
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
4
 * Copyright (C) 2008-2009 VideoLAN
5
 *
6
 * Authors: Hugo Beauzée-Luyssen <beauze.h@gmail.com>
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 24
#include "SettingsManager.h"

25
#include "SettingValue.h"
26

27
#include <QSettings>
28 29
#include <QWriteLocker>
#include <QReadLocker>
30
#include <QStringList>
31

32
#include <QtDebug>
33
#include <QDomElement>
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
static SettingsManager::SettingList::iterator
getPair( SettingsManager::SettingList &list, const QString &key )
{
    SettingsManager::SettingList::iterator        it = list.begin();
    SettingsManager::SettingList::iterator        end = list.end();

    while ( it != end )
    {
        if ( *it == key )
            return it;
        ++it;
    }
    return end;
}

50 51 52 53
void
SettingsManager::setValue( const QString &key,
                           const QVariant &value,
                           SettingsManager::Type type )
54
{
55
    if ( type == Project )
56
    {
57 58 59 60 61 62
        SettingList::iterator   it = getPair( m_xmlSettings, key );
        if ( it != m_xmlSettings.end() )
        {
            (*it).value->set( value );
            return ;
        }
63
    }
64
    else// ( type == Vlmc && m_classicSettings.contains( key) == true )
65
    {
66 67 68 69 70 71 72 73 74
        SettingList::iterator   it = getPair( m_classicSettings, key );
        if ( it != m_classicSettings.end() )
        {
            QSettings    sett;
            sett.setValue( key, value );
            sett.sync();
            (*it).value->set( value );
            return ;
        }
75
    }
76 77
    Q_ASSERT_X( false, __FILE__, "set value without a created variable" );
    qWarning() << "Setting" << key << "does not exist.";
78 79
}

80
SettingValue*
81 82
SettingsManager::value( const QString &key,
                        SettingsManager::Type type )
83
{
84 85
    QReadLocker rl( &m_rwLock );

86
    if ( type == Project )
87
    {
88 89 90
        SettingList::iterator   it = getPair( m_xmlSettings, key );
        if ( it != m_xmlSettings.end() )
            return (*it).value;
91
    }
92
    else
93
    {
94 95 96
        SettingList::iterator   it = getPair( m_classicSettings, key );
        if ( it != m_classicSettings.end() )
            return (*it).value;
97
    }
98
    Q_ASSERT_X( false, __FILE__, "get value without a created variable" );
99 100
    qWarning() << "Setting" << key << "does not exist.";
    return NULL;
101 102
}

103
SettingsManager::SettingList
104
SettingsManager::group( const QString &groupName, SettingsManager::Type type )
105
{
106
    SettingsManager::SettingList        ret;
107
    QReadLocker                         rl( &m_rwLock );
108 109

    QString grp = groupName + '/';
110
    if ( type == Project )
111
    {
112 113
         SettingList::const_iterator it = m_xmlSettings.begin();
         SettingList::const_iterator ed = m_xmlSettings.end();
114 115 116

         for ( ; it != ed; ++it )
         {
117 118
             if ( (*it).key.startsWith( grp ) )
                 ret.push_back( *it );
119 120
         }
    }
121
    else if ( type == Vlmc )
122
    {
123 124
         SettingList::const_iterator it = m_classicSettings.begin();
         SettingList::const_iterator ed = m_classicSettings.end();
125 126 127

         for ( ; it != ed; ++it )
         {
128 129
             if ( (*it).key.startsWith( grp ) )
                 ret.push_back( *it );
130 131 132
         }
    }
    return ret;
133 134
}

135 136 137 138
bool
SettingsManager::watchValue( const QString &key,
                             QObject* receiver,
                             const char *method,
139 140
                             SettingsManager::Type type,
                             Qt::ConnectionType cType )
141
{
142 143
    QReadLocker rl( &m_rwLock );

144
    if ( type == Project )
145
    {
146 147 148 149 150 151 152
        SettingList::iterator   it = getPair( m_xmlSettings, key );
        if ( it != m_xmlSettings.end() )
        {
            connect( (*it).value, SIGNAL( changed( const QVariant& ) ),
                     receiver, method );
            return true;
        }
153
    }
154
    else if ( type == Vlmc )
155
    {
156 157
        SettingList::iterator   it = getPair( m_classicSettings, key );
        if ( it != m_classicSettings.end() )
158
        {
159
            connect( (*it).value, SIGNAL( changed( const QVariant& ) ),
160
                    receiver, method, cType );
161 162
            return true;
        }
163
    }
164
    Q_ASSERT_X( false, __FILE__, "watching value without a created variable" );
165
    return false;
166
}
167

168 169
void
SettingsManager::save() const
170
{
171 172 173 174
    QReadLocker     rl( &m_rwLock );
    QSettings       sett;
    SettingList::const_iterator it = m_classicSettings.begin();
    SettingList::const_iterator ed = m_classicSettings.end();
175 176

    for ( ; it != ed; ++it )
177
    {
178
        if ( ( (*it).value->flags() & SettingValue::Private ) != 0 )
179
            continue ;
180
        sett.setValue( (*it).key, (*it).value->get() );
181
    }
182 183 184 185
    sett.sync();
}

void
186
SettingsManager::save( QXmlStreamWriter& project ) const
187
{
188 189
    SettingList::const_iterator     it = m_xmlSettings.begin();
    SettingList::const_iterator     end = m_xmlSettings.end();
190

191
    project.writeStartElement( "project" );
192
    for ( ; it != end; ++it )
193
    {
194
        if ( ( (*it).value->flags() & SettingValue::Private ) != 0 )
195
            continue ;
196
        project.writeStartElement( "property" );
197 198
        project.writeAttribute( "key", (*it).key );
        project.writeAttribute( "value", (*it).value->get().toString() );
199
        project.writeEndElement();
200
    }
201
    project.writeEndElement();
202
}
203

204
bool
205
SettingsManager::load( const QDomElement &root )
206
{
207
    //For now it only handle a project node.
208 209
    QDomElement     element = root.firstChildElement( "project" );
    if ( element.isNull() == true )
210 211 212 213 214
    {
        qWarning() << "Invalid settings node";
        return false ;
    }
    QWriteLocker    wLock( &m_rwLock );
215 216
    QDomElement s = element.firstChild().toElement();
    while ( s.isNull() == false )
217
    {
218 219 220 221 222 223 224 225
        QString     key = s.attribute( "key" );
        QString     value = s.attribute( "value" );

        if ( key.isEmpty() == true || value.isEmpty() == true )
            qWarning() << "Invalid setting node.";
        else
            setValue( key, value, SettingsManager::Project );
        s = s.nextSibling().toElement();
226
    }
227
    return true;
228 229
}

230
SettingValue*
231
SettingsManager::createVar( SettingValue::Type type, const QString &key,
232
                            const QVariant &defaultValue, const char *name,
233
                            const char *desc, SettingsManager::Type varType /*= Vlmc*/,
234
                            QFlags<SettingValue::Flag> flags /*= SettingValue::Nothing*/ )
235 236 237
{
    QWriteLocker    wlock( &m_rwLock );

238
    SettingValue    *val = NULL;
239
    if ( varType == Vlmc && getPair( m_classicSettings, key ) == m_classicSettings.end() )
240 241 242 243
    {
        val = new SettingValue( type, defaultValue, name, desc, flags );
        m_classicSettings.push_back( Pair( key, val ) );
    }
244
    else if ( varType == Project && getPair( m_xmlSettings, key ) == m_xmlSettings.end() )
245 246 247 248
    {
        val = new SettingValue( type, defaultValue, name, desc, flags );
        m_xmlSettings.push_back( Pair( key, val ) );
    }
249 250
    else
        Q_ASSERT_X( false, __FILE__, "creating an already created variable" );
251
    return val;
252
}
253 254 255 256 257 258 259 260 261 262 263 264

SettingsManager::Pair::Pair( const QString &_key, SettingValue *_value ) :
        key( _key ),
        value( _value )
{
}

bool
SettingsManager::Pair::operator ==( const QString &_key ) const
{
    return key == _key;
}