diff --git a/project/projectconfigskeleton.cpp b/project/projectconfigskeleton.cpp index 0e0614996..c4c97670c 100644 --- a/project/projectconfigskeleton.cpp +++ b/project/projectconfigskeleton.cpp @@ -1,169 +1,173 @@ /* This file is part of KDevelop Copyright 2006 Adam Treat Copyright 2007 Andreas Pakulat This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "projectconfigskeleton.h" #include "debug.h" #include #include #include using namespace KDevelop; struct KDevelop::ProjectConfigSkeletonPrivate { QString m_developerTempFile; QString m_projectTempFile; Path m_projectFile; Path m_developerFile; bool mUseDefaults; }; ProjectConfigSkeleton::ProjectConfigSkeleton( const QString & configname ) : KConfigSkeleton( configname ), d( new ProjectConfigSkeletonPrivate ) { d->m_developerTempFile = configname; } ProjectConfigSkeleton::ProjectConfigSkeleton( KSharedConfigPtr config ) : KConfigSkeleton( config ), d( new ProjectConfigSkeletonPrivate ) { + // FIXME: Check if that does the right thing. + // https://phabricator.kde.org/D3386 broke source compat in kconfig, thus requiring us to make this ctor public + Q_ASSERT(config); + d->m_developerTempFile = config->name(); } void ProjectConfigSkeleton::setDeveloperTempFile( const QString& cfg ) { d->m_developerTempFile = cfg; setSharedConfig( KSharedConfig::openConfig( cfg ) ); } void ProjectConfigSkeleton::setProjectTempFile( const QString& cfg ) { d->m_projectTempFile = cfg; config()->addConfigSources( QStringList() << cfg ); load(); } void ProjectConfigSkeleton::setProjectFile( const Path& cfg ) { d->m_projectFile = cfg; } void ProjectConfigSkeleton::setDeveloperFile( const Path& cfg ) { d->m_developerFile = cfg; } Path ProjectConfigSkeleton::projectFile() const { return d->m_projectFile; } Path ProjectConfigSkeleton::developerFile() const { return d->m_developerFile; } void ProjectConfigSkeleton::setDefaults() { qCDebug(PROJECT) << "Setting Defaults"; KConfig cfg( d->m_projectTempFile ); Q_FOREACH( KConfigSkeletonItem* item, items() ) { item->swapDefault(); if( cfg.hasGroup( item->group() ) ) { KConfigGroup grp = cfg.group( item->group() ); if( grp.hasKey( item->key() ) ) item->setProperty( grp.readEntry( item->key(), item->property() ) ); } } } bool ProjectConfigSkeleton::useDefaults( bool b ) { if( b == d->mUseDefaults ) return d->mUseDefaults; if( b ) { KConfig cfg( d->m_projectTempFile ); Q_FOREACH( KConfigSkeletonItem* item, items() ) { item->swapDefault(); if( cfg.hasGroup( item->group() ) ) { qCDebug(PROJECT) << "reading"; KConfigGroup grp = cfg.group( item->group() ); if( grp.hasKey( item->key() ) ) item->setProperty( grp.readEntry( item->key(), item->property() ) ); } } } else { KConfig cfg( d->m_developerTempFile ); KConfig defCfg( d->m_projectTempFile ); Q_FOREACH( KConfigSkeletonItem* item, items() ) { if( cfg.hasGroup( item->group() ) ) { KConfigGroup grp = cfg.group( item->group() ); if( grp.hasKey( item->key() ) ) item->setProperty( grp.readEntry( item->key(), item->property() ) ); else { KConfigGroup grp = defCfg.group( item->group() ); item->setProperty( grp.readEntry( item->key(), item->property() ) ); } } else { KConfigGroup grp = defCfg.group( item->group() ); item->setProperty( grp.readEntry( item->key(), item->property() ) ); } } } d->mUseDefaults = b; return !d->mUseDefaults; } bool ProjectConfigSkeleton::writeConfig() { KConfigSkeletonItem::List myitems = items(); KConfigSkeletonItem::List::ConstIterator it; for( it = myitems.constBegin(); it != myitems.constEnd(); ++it ) { (*it)->writeConfig( config() ); } config()->sync(); load(); auto copyJob = KIO::copy(QUrl::fromLocalFile(d->m_developerTempFile), d->m_developerFile.toUrl()); copyJob ->exec(); emit configChanged(); return true; } ProjectConfigSkeleton::~ProjectConfigSkeleton() { delete d; } diff --git a/project/projectconfigskeleton.h b/project/projectconfigskeleton.h index ed17ed099..c8314df9f 100644 --- a/project/projectconfigskeleton.h +++ b/project/projectconfigskeleton.h @@ -1,72 +1,68 @@ /* This file is part of KDevelop Copyright 2006 Adam Treat Copyright 2007 Andreas Pakulat This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KDEVPLATFORM_PROJECTCONFIGSKELETON_H #define KDEVPLATFORM_PROJECTCONFIGSKELETON_H #include "projectexport.h" #include namespace KDevelop { class Path; class KDEVPLATFORMPROJECT_EXPORT ProjectConfigSkeleton: public KConfigSkeleton { Q_OBJECT public: /** * Constructs a new skeleton, the skeleton will write to the developer * configuration file, which is by default located in projectdir/.kdev4 * The defaults will be set from the project file, which is in the projectdir * * @param configname The absolute filename of the developer configuration file */ explicit ProjectConfigSkeleton( const QString & configname ); ~ProjectConfigSkeleton() override; void setDeveloperTempFile( const QString& ); void setProjectTempFile( const QString& ); void setProjectFile( const Path& ); void setDeveloperFile( const Path& ); void setDefaults() override; bool useDefaults( bool b ) override; bool writeConfig(); Path projectFile() const; Path developerFile() const; + +protected: + explicit ProjectConfigSkeleton( KSharedConfigPtr config ); + private: - /** - * There's no way in KDE4 API to find out the file that the config object - * was created from, so we can't apply defaults when using this - * constructors. Thus I'm making this private, so we can find out when - * this constructor is used and see if we need to add appropriate API to - * kdelibs - */ - explicit ProjectConfigSkeleton( KSharedConfigPtr config ); - struct ProjectConfigSkeletonPrivate * const d; + struct ProjectConfigSkeletonPrivate * const d; }; } #endif