• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDevelop Platform Libraries
  • Sitemap
  • Contact Us
 

util

environmentgrouplist.cpp

00001 /* This file is part of KDevelop
00002 Copyright 2007 Andreas Pakulat <apaku@gmx.de>
00003 
00004 This library is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU Library General Public
00006 License as published by the Free Software Foundation; either
00007 version 2 of the License, or (at your option) any later version.
00008 
00009 This library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 Library General Public License for more details.
00013 
00014 You should have received a copy of the GNU Library General Public License
00015 along with this library; see the file COPYING.LIB.  If not, write to
00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017 Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "environmentgrouplist.h"
00021 
00022 #include <QtCore/QMap>
00023 #include <QtCore/QStringList>
00024 #include <QtCore/QString>
00025 
00026 #include <ksharedconfig.h>
00027 #include <kconfiggroup.h>
00028 #include <kdebug.h>
00029 
00030 namespace KDevelop
00031 {
00032 
00033 class EnvironmentGroupListPrivate
00034 {
00035 public:
00036     QMap<QString, QMap<QString,QString> > m_groups;
00037     QString m_defaultGroup;
00038 };
00039 
00040 
00041 void decode( KConfigGroup cfg, EnvironmentGroupListPrivate* d )
00042 {
00043     d->m_defaultGroup = cfg.readEntry( "Default EnvironmentGroup", QString( "default" ) );
00044     foreach( QString envgrpname, cfg.groupList() )
00045     {
00046         KConfigGroup envgrp( &cfg, envgrpname );
00047         QMap<QString,QString> variables;
00048         foreach( QString varname, envgrp.keyList() )
00049         {
00050             variables[varname] = envgrp.readEntry( varname, QString("") );
00051         }
00052         d->m_groups.insert( envgrpname, variables );
00053     }
00054 
00055     // If the defaultgroup doesn't exist yet create it
00056     if( !d->m_groups.contains( d->m_defaultGroup ) )
00057     {
00058         d->m_groups.insert( d->m_defaultGroup, QMap<QString,QString>() );
00059     }
00060 }
00061 
00062 void encode( KConfigGroup cfg, EnvironmentGroupListPrivate* d )
00063 {
00064     cfg.writeEntry( "Default Environment Group", d->m_defaultGroup );
00065     foreach( QString group, d->m_groups.keys() )
00066     {
00067         KConfigGroup envgrp( &cfg, group );
00068         foreach( QString var, d->m_groups[group].keys() )
00069         {
00070             envgrp.writeEntry( var, d->m_groups[group][var] );
00071         }
00072     }
00073     cfg.sync();
00074 }
00075 
00076 EnvironmentGroupList::EnvironmentGroupList( KSharedConfigPtr config )
00077     : d(new EnvironmentGroupListPrivate)
00078 {
00079     KConfigGroup cfg( config, "Environment Settings" );
00080     decode( cfg, d );
00081 }
00082 
00083 EnvironmentGroupList::EnvironmentGroupList( KConfig* config )
00084     : d(new EnvironmentGroupListPrivate)
00085 {
00086     KConfigGroup cfg( config, "Environment Settings" );
00087     decode( cfg, d );
00088 }
00089 
00090 
00091 EnvironmentGroupList::~EnvironmentGroupList()
00092 {
00093     delete d;
00094 }
00095 
00096 const QMap<QString, QString> EnvironmentGroupList::variables( const QString& group ) const
00097 {
00098     return d->m_groups[group];
00099 }
00100 
00101 QMap<QString, QString>& EnvironmentGroupList::variables( const QString& group )
00102 {
00103     return d->m_groups[group];
00104 }
00105 
00106 
00107 QString EnvironmentGroupList::defaultGroup() const
00108 {
00109     return d->m_defaultGroup;
00110 }
00111 
00112 void EnvironmentGroupList::setDefaultGroup( const QString& group )
00113 {
00114     if( d->m_groups.contains( group ) )
00115     {
00116         d->m_defaultGroup = group;
00117     }
00118 }
00119 
00120 void EnvironmentGroupList::saveSettings( KConfig* config ) const
00121 {
00122     KConfigGroup cfg(config, "Environment Settings" );
00123     encode( cfg, d );
00124 }
00125 
00126 void EnvironmentGroupList::loadSettings( KConfig* config )
00127 {
00128     d->m_groups.clear();
00129     KConfigGroup cfg(config, "Environment Settings" );
00130     decode( cfg, d );
00131 }
00132 
00133 QStringList EnvironmentGroupList::groups() const
00134 {
00135     return d->m_groups.keys();
00136 }
00137 
00138 void EnvironmentGroupList::removeGroup( const QString& group )
00139 {
00140     d->m_groups.remove( group );
00141 }
00142 
00143 EnvironmentGroupList::EnvironmentGroupList()
00144     : d( new EnvironmentGroupListPrivate)
00145 {
00146 }
00147 
00148 QStringList EnvironmentGroupList::createEnvironment(const QString & group, const QStringList & defaults) const
00149 {
00150     QMap<QString, QString> retMap;
00151     foreach( QString line, defaults )
00152     {
00153         QString varName = line.section( '=', 0, 0 );
00154         QString varValue = line.section( '=', 1 );
00155         retMap.insert( varName, varValue );
00156     }
00157 
00158     if( !group.isEmpty() ) {
00159       QMap<QString, QString> userMap = variables(group);
00160 
00161       for( QMap<QString, QString>::const_iterator it = userMap.begin();
00162           it != userMap.end(); ++it )
00163       {
00164           retMap.insert( it.key(), it.value() );
00165       }
00166     }
00167 
00168     QStringList env;
00169     for( QMap<QString, QString>::const_iterator it = retMap.begin();
00170         it != retMap.end(); ++it )
00171     {
00172         env << it.key() + '=' + it.value();
00173     }
00174 
00175     return env;
00176 }
00177 
00178 }

util

Skip menu "util"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDevelop Platform Libraries

Skip menu "KDevelop Platform Libraries"
  • interfaces
  • language
  •   codegen
  •   duchain
  •   editor
  • outputview
  •     interfaces
  • project
  • shell
  • sublime
  • util
  • vcs
Generated for KDevelop Platform Libraries by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal