Fix no newlines warnings. Patch by Peter Oberndorfer
[kdevelopdvcssupport.git] / util / environmentgrouplist.cpp
blob08e3c74aa982ddb897b7568dd014360a53933f22
1 /* This file is part of KDevelop
2 Copyright 2007 Andreas Pakulat <apaku@gmx.de>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
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
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "environmentgrouplist.h"
22 #include <QtCore/QMap>
23 #include <QtCore/QStringList>
24 #include <QtCore/QString>
26 #include <ksharedconfig.h>
27 #include <kconfiggroup.h>
28 #include <kdebug.h>
30 namespace KDevelop
33 class EnvironmentGroupListPrivate
35 public:
36 QMap<QString, QMap<QString,QString> > m_groups;
37 QString m_defaultGroup;
41 void decode( KConfigGroup cfg, EnvironmentGroupListPrivate* d )
43 d->m_defaultGroup = cfg.readEntry( "Default EnvironmentGroup", QString( "default" ) );
44 foreach( QString envgrpname, cfg.groupList() )
46 KConfigGroup envgrp( &cfg, envgrpname );
47 QMap<QString,QString> variables;
48 foreach( QString varname, envgrp.keyList() )
50 variables[varname] = envgrp.readEntry( varname, QString("") );
52 d->m_groups.insert( envgrpname, variables );
55 // If the defaultgroup doesn't exist yet create it
56 if( !d->m_groups.contains( d->m_defaultGroup ) )
58 d->m_groups.insert( d->m_defaultGroup, QMap<QString,QString>() );
62 void encode( KConfigGroup cfg, EnvironmentGroupListPrivate* d )
64 cfg.writeEntry( "Default Environment Group", d->m_defaultGroup );
65 foreach( QString group, d->m_groups.keys() )
67 KConfigGroup envgrp( &cfg, group );
68 foreach( QString var, d->m_groups[group].keys() )
70 envgrp.writeEntry( var, d->m_groups[group][var] );
73 cfg.sync();
76 EnvironmentGroupList::EnvironmentGroupList( KSharedConfigPtr config )
77 : d(new EnvironmentGroupListPrivate)
79 KConfigGroup cfg( config, "Environment Settings" );
80 decode( cfg, d );
83 EnvironmentGroupList::EnvironmentGroupList( KConfig* config )
84 : d(new EnvironmentGroupListPrivate)
86 KConfigGroup cfg( config, "Environment Settings" );
87 decode( cfg, d );
91 EnvironmentGroupList::~EnvironmentGroupList()
93 delete d;
96 const QMap<QString, QString> EnvironmentGroupList::variables( const QString& group ) const
98 return d->m_groups[group];
101 QMap<QString, QString>& EnvironmentGroupList::variables( const QString& group )
103 return d->m_groups[group];
107 QString EnvironmentGroupList::defaultGroup() const
109 return d->m_defaultGroup;
112 void EnvironmentGroupList::setDefaultGroup( const QString& group )
114 if( d->m_groups.contains( group ) )
116 d->m_defaultGroup = group;
120 void EnvironmentGroupList::saveSettings( KConfig* config ) const
122 KConfigGroup cfg(config, "Environment Settings" );
123 encode( cfg, d );
126 void EnvironmentGroupList::loadSettings( KConfig* config )
128 d->m_groups.clear();
129 KConfigGroup cfg(config, "Environment Settings" );
130 decode( cfg, d );
133 QStringList EnvironmentGroupList::groups() const
135 return d->m_groups.keys();
138 void EnvironmentGroupList::removeGroup( const QString& group )
140 d->m_groups.remove( group );
143 EnvironmentGroupList::EnvironmentGroupList()
144 : d( new EnvironmentGroupListPrivate)
148 QStringList EnvironmentGroupList::createEnvironment(const QString & group, const QStringList & defaults) const
150 QMap<QString, QString> retMap;
151 foreach( QString line, defaults )
153 QString varName = line.section( '=', 0, 0 );
154 QString varValue = line.section( '=', 1 );
155 retMap.insert( varName, varValue );
158 if( !group.isEmpty() ) {
159 QMap<QString, QString> userMap = variables(group);
161 for( QMap<QString, QString>::const_iterator it = userMap.begin();
162 it != userMap.end(); ++it )
164 retMap.insert( it.key(), it.value() );
168 QStringList env;
169 for( QMap<QString, QString>::const_iterator it = retMap.begin();
170 it != retMap.end(); ++it )
172 env << it.key() + '=' + it.value();
175 return env;