SettingsManager.cpp 7.04 KB
Newer Older
Clement CHAVANCE's avatar
Clement CHAVANCE committed
1
/*****************************************************************************
2
 * SettingsManager.cpp: Backend settings manager
Clement CHAVANCE's avatar
Clement CHAVANCE committed
3
 *****************************************************************************
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
4
 * Copyright (C) 2008-2009 VideoLAN
Clement CHAVANCE's avatar
Clement CHAVANCE committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * Authors: Clement CHAVANCE <kinder@vlmc.org>
 *
 * 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
28
#include <QSettings>
#include <QDomDocument>
29
30
#include <QWriteLocker>
#include <QReadLocker>
31
#include <QXmlStreamWriter>
32

33
#include <QtDebug>
Clement CHAVANCE's avatar
Clement CHAVANCE committed
34

35
36
37
38
void
SettingsManager::setValue( const QString &key,
                           const QVariant &value,
                           SettingsManager::Type type )
Clement CHAVANCE's avatar
Clement CHAVANCE committed
39
{
40
41
42
43
44
    if ( type == XML )
        m_tmpXmlSettings.insert( key, new SettingValue( value ) );
    else if ( type == QSett )
        m_tmpClassicSettings.insert( key, new SettingValue( value ) );
    return ;
Clement CHAVANCE's avatar
Clement CHAVANCE committed
45
46
}

47
48
49
50
void
SettingsManager::setImmediateValue( const QString &key,
                                    const QVariant &value,
                                    SettingsManager::Type type )
Clement CHAVANCE's avatar
Clement CHAVANCE committed
51
{
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    QWriteLocker    wlock( &m_rwLock );
    SettingHash  *settMap;
    if ( type == XML )
        settMap = &m_xmlSettings;
    else if ( type == QSett )
    {
        QSettings    sett;
        sett.setValue( key, value );
        sett.sync();
        settMap = &m_classicSettings;
    }
    if ( settMap->contains( key ) )
        settMap->value( key )->set( value );
    else
        settMap->insert( key, new SettingValue( value ) );

    return ;
Clement CHAVANCE's avatar
Clement CHAVANCE committed
69
70
}

71
72
73
74
QVariant
SettingsManager::value( const QString &key,
                        const QVariant &defaultValue,
                        SettingsManager::Type type )
Clement CHAVANCE's avatar
Clement CHAVANCE committed
75
{
76
77
78
79
80
81
    qDebug() << "value :" << key << "default" << defaultValue;
    QReadLocker rl( &m_rwLock );

    if ( ( type == XML || type == All ) && m_xmlSettings.contains( key ) )
        return m_xmlSettings.value( key )->get();
    else if ( ( type == QSett || type == All ) )
82
    {
83
84
85
86
87
88
89
90
91
92
93
        if ( m_classicSettings.contains( key ) )
            return m_classicSettings.value( key )->get();
        else
        {
            QSettings    sett;
            QVariant val = sett.value( key, defaultValue );
            if ( val != defaultValue )
                m_classicSettings.insert( key, new SettingValue( val ) );

            return val;
        }
94
    }
95
96

    return defaultValue;
Clement CHAVANCE's avatar
Clement CHAVANCE committed
97
98
}

99
100
QHash<QString, QVariant>
SettingsManager::group( const QString& groupName, SettingsManager::Type type )
Clement CHAVANCE's avatar
Clement CHAVANCE committed
101
102
103
{
}

104
105
106
107
108
bool
SettingsManager::watchValue( const QString &key,
                             QObject* receiver,
                             const char *method,
                             SettingsManager::Type type )
Clement CHAVANCE's avatar
Clement CHAVANCE committed
109
{
110
111
112
    QReadLocker rl( &m_rwLock );

    if ( ( type == XML || type == All ) && m_xmlSettings.contains( key ) )
113
    {
114
115
116
        connect( m_xmlSettings[key], SIGNAL( changed( const QVariant& ) ),
                 receiver, method );
        return true;
117
    }
118
    else if ( ( type == QSett || type == All ) )
119
    {
120
121
122
123
124
125
        if ( m_classicSettings.contains( key ) )
        {
            connect( m_classicSettings[key], SIGNAL( changed( const QVariant& ) ),
                    receiver, method );
            return true;
        }
126
    }
Clement CHAVANCE's avatar
Clement CHAVANCE committed
127

128
    return false;
Clement CHAVANCE's avatar
Clement CHAVANCE committed
129
}
130

131
132
void
SettingsManager::save() const
133
{
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    QReadLocker rl( &m_rwLock );
    QSettings    sett;
    SettingHash::const_iterator it = m_classicSettings.begin();
    SettingHash::const_iterator ed = m_classicSettings.end();

    for ( ; it != ed; ++it )
        sett.setValue( it.key(), it.value()->get() );
    sett.sync();
}

void
SettingsManager::save( QXmlStreamWriter &stream ) const
{
    typedef QPair<QString, SettingValue*> settingPair;
    QMultiHash<QString, settingPair>  parts;

    QReadLocker rl( &m_rwLock );
    SettingHash::const_iterator it;
    SettingHash::const_iterator ed = m_xmlSettings.end();

    for ( it = m_xmlSettings.begin(); it != ed; ++it )
155
    {
156
157
158
159
160
161
162
163
164
        QString key = it.key();
        if ( key.count( "/" ) == 1 )
        {
            int idx = key.indexOf( "/" );
            QString part = key.left( idx );
            QString name = key.right( idx );

            parts.insert( part, settingPair( name, it.value() ) );
        }
165
    }
166
167
168
169
    QMultiHash<QString, settingPair>::const_iterator iter;
    QMultiHash<QString, settingPair>::const_iterator end = parts.end();
    stream.writeStartElement( "Settings" );
    for ( iter = parts.begin(); iter != end; ++iter )
170
    {
171
172
173
        stream.writeStartElement( iter.key() );
        QList<settingPair>  pairs = parts.values( iter.key() );
        foreach( settingPair pair, pairs )
174
        {
175
176
177
            stream.writeStartElement( pair.first );
            stream.writeAttribute( "value", pair.second->get().toString() );
            stream.writeEndElement();
178
        }
179
        stream.writeEndElement();
180
    }
181
    stream.writeEndElement();
182
}
183

184
185
bool
SettingsManager::load( QDomElement &element )
186
{
Clement CHAVANCE's avatar
Clement CHAVANCE committed
187
188
}

189
190
bool
SettingsManager::commit( SettingsManager::Type type )
Clement CHAVANCE's avatar
Clement CHAVANCE committed
191
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
192
    {
193
194
        QWriteLocker    wlock( &m_rwLock );
        if ( type == XML || type == All )
195
        {
196
197
198
            SettingHash::iterator it;
            SettingHash::iterator ed = m_tmpXmlSettings.end();
            for ( it = m_tmpXmlSettings.begin() ; it != ed; ++it )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
199
            {
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
                if ( m_xmlSettings.contains( it.key() ) )
                    m_xmlSettings[it.key()]->set( it.value()->get() );
                else
                    m_xmlSettings.insert( it.key(), it.value() );
            }
        }
        if ( type == QSett || type == All )
        {
            QSettings sett;
            SettingHash::iterator it;
            SettingHash::iterator ed = m_classicSettings.end();
            for ( it = m_classicSettings.begin(); it != ed; ++it )
            {
                sett.setValue( it.key(), it.value()->get() );
                if ( m_classicSettings.contains( it.key() ) )
                    m_classicSettings[it.key()]->set( it.value()->get() );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
216
                else
217
                    m_classicSettings.insert( it.key(), it.value() );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
218
            }
219
        }
Clement CHAVANCE's avatar
Clement CHAVANCE committed
220
    }
221
    flush();
Clement CHAVANCE's avatar
Clement CHAVANCE committed
222
223
}

224
225
void
SettingsManager::flush()
Clement CHAVANCE's avatar
Clement CHAVANCE committed
226
{
227
228
229
    QWriteLocker    wl( &m_rwLock );
    m_tmpXmlSettings.clear();
    m_tmpClassicSettings.clear();
230
231
}

232
233
234
235
236
237
SettingsManager::SettingsManager()
    : m_classicSettings(),
    m_xmlSettings(),
    m_tmpClassicSettings(),
    m_tmpXmlSettings(),
    m_rwLock()
238
{
239
240
}

241
SettingsManager::~SettingsManager()
242
243
{
}