1 | /* |
2 | Copyright (c) 2010 Laurent Montel <montel@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "configfile.h" |
21 | |
22 | #include <KConfig> |
23 | #include <KConfigGroup> |
24 | #include <KLocalizedString> |
25 | #include <KStringHandler> |
26 | |
27 | ConfigFile::ConfigFile( const QString & configName, QObject *parent ) |
28 | : SetupObject( parent ) |
29 | { |
30 | m_name = configName; |
31 | m_config = new KConfig( configName ); |
32 | } |
33 | |
34 | ConfigFile::~ConfigFile() |
35 | { |
36 | delete m_config; |
37 | } |
38 | |
39 | void ConfigFile::write() |
40 | { |
41 | create(); |
42 | } |
43 | |
44 | void ConfigFile::create() |
45 | { |
46 | emit info( i18n( "Writing config file for %1..." ,m_name ) ); |
47 | |
48 | foreach ( const Config &c, m_configData ) { |
49 | KConfigGroup grp = m_config->group( c.group ); |
50 | if ( c.obscure ) |
51 | grp.writeEntry( c.key, KStringHandler::obscure( c.value ) ); |
52 | else |
53 | grp.writeEntry( c.key, c.value ); |
54 | } |
55 | |
56 | m_config->sync(); |
57 | emit finished( i18n( "Config file for %1 is writing." , m_name ) ); |
58 | } |
59 | |
60 | void ConfigFile::destroy() |
61 | { |
62 | emit info( i18n( "Config file for %1 was not changed." , m_name ) ); |
63 | } |
64 | |
65 | void ConfigFile::setName( const QString &name ) |
66 | { |
67 | m_name = name; |
68 | } |
69 | |
70 | |
71 | void ConfigFile::setConfig( const QString &group, const QString &key, const QString &value ) |
72 | { |
73 | Config conf; |
74 | conf.group = group; |
75 | conf.key = key; |
76 | conf.value = value; |
77 | conf.obscure = false; |
78 | m_configData.append( conf ); |
79 | } |
80 | |
81 | void ConfigFile::setPassword(const QString& group, const QString& key, const QString& value) |
82 | { |
83 | Config conf; |
84 | conf.group = group; |
85 | conf.key = key; |
86 | conf.value = value; |
87 | conf.obscure = true; |
88 | m_configData.append( conf ); |
89 | } |
90 | |
91 | |
92 | |