kontact

profilemanager.cpp

00001 /*
00002     This file is part of KDE Kontact.
00003 
00004     Copyright (c) 2007 Frank Osterfeld <frank.osterfeld@kdemail.net>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "profilemanager.h"
00026 
00027 #include <kio/job.h>
00028 
00029 #include <kapplication.h>
00030 #include <kconfig.h>
00031 #include <kglobal.h>
00032 #include <kstandarddirs.h>
00033 #include <kstaticdeleter.h>
00034 #include <kurl.h>
00035 
00036 #include <qdir.h>
00037 #include <qstringlist.h>
00038 #include <qvaluelist.h>
00039 
00040 Kontact::Profile::Profile( const QString& id ) : m_id( id ), m_local( false )
00041 {
00042 }
00043 
00044 Kontact::Profile::Profile() : m_local( false )
00045 {
00046 }
00047 
00048 QString Kontact::Profile::id() const
00049 {
00050     return m_id;
00051 }
00052 
00053 QString Kontact::Profile::name() const
00054 {
00055     return m_name;
00056 }
00057 
00058 QString Kontact::Profile::description() const
00059 {
00060     return m_description;
00061 }
00062 
00063 bool Kontact::Profile::isNull() const
00064 {
00065     return m_id.isNull();
00066 }
00067 
00068 void Kontact::Profile::setId( const QString& id )
00069 {
00070     m_id = id;
00071 }
00072 
00073 void Kontact::Profile::setDescription( const QString& description )
00074 {
00075     m_description = description;
00076 }
00077 
00078 void Kontact::Profile::setName( const QString& name )
00079 {
00080     m_name = name;
00081 }
00082 
00083 void Kontact::Profile::setLocal( SetLocalMode mode )
00084 {
00085     if ( m_local )
00086         return;
00087 
00088     if ( mode == CopyProfileFiles )
00089         copyConfigFiles( m_originalLocation, localSaveLocation() );
00090 
00091     m_local = true;
00092 }
00093 
00094 bool Kontact::Profile::isLocal() const
00095 {
00096     return m_local;
00097 }
00098 
00099 void Kontact::Profile::setOriginalLocation( const QString& path )
00100 {
00101     m_originalLocation = path;
00102 }
00103 
00104 QString Kontact::Profile::localSaveLocation() const
00105 {
00106 
00107     return  m_id.isNull() ? QString() : locateLocal( "data", "kontact/profiles/" + m_id, /*create folder=*/true );
00108 }
00109 
00110 QString Kontact::Profile::saveLocation() const
00111 {
00112     return m_local ? localSaveLocation() : m_originalLocation;
00113 }
00114 
00115 bool Kontact::Profile::operator==( const Kontact::Profile& other ) const
00116 {
00117     return m_id == other.m_id && m_name == other.m_name && m_description == other.m_description;
00118 }
00119 
00120 Kontact::ProfileManager* Kontact::ProfileManager::m_self = 0;
00121 
00122 static KStaticDeleter<Kontact::ProfileManager> profileManagerSD;
00123 
00124 Kontact::ProfileManager* Kontact::ProfileManager::self() 
00125 {
00126     if ( m_self == 0 )
00127     {
00128         profileManagerSD.setObject( m_self, new Kontact::ProfileManager );
00129         m_self->readConfig();
00130     }
00131     return m_self;
00132 }
00133 
00134 Kontact::ProfileManager::ProfileManager( QObject* parent ) : QObject( parent )
00135 {
00136 }
00137 
00138 Kontact::ProfileManager::~ProfileManager()
00139 {
00140     writeConfig();
00141 }
00142 
00143 void Kontact::ProfileManager::writeConfig() const
00144 {
00145     const QValueList<Kontact::Profile> profiles = m_profiles.values();
00146     for ( QValueList<Kontact::Profile>::ConstIterator it = profiles.begin(), end = profiles.end(); it != end; ++it )
00147     {
00148         writeProfileConfig( *it );
00149     }
00150 }
00151 
00152 Kontact::Profile Kontact::ProfileManager::readFromConfiguration( const QString& configFile, bool isLocal )
00153 {
00154     KConfig profileCfg( configFile, true /*read-only*/, false /*no KDE global*/ );
00155     const QString configDir = configFile.left( configFile.findRev( QDir::separator(), -1 ) );
00156     profileCfg.setGroup( "Kontact Profile" );
00157     const QString id = profileCfg.readEntry( "Identifier" );
00158     Kontact::Profile profile( id );
00159     profile.setName( profileCfg.readEntry( "Name" ) );
00160     profile.setDescription( profileCfg.readEntry( "Description" ) );
00161     profile.setOriginalLocation( configDir );
00162     if ( isLocal )
00163         profile.setLocal( Kontact::Profile::DoNotCopyProfileFiles );
00164     return profile;
00165 }
00166 
00167 void Kontact::ProfileManager::writeProfileConfig( const Kontact::Profile& profile ) const
00168 {
00169     const QString profileDir = profile.saveLocation();
00170     const QString cfgPath = profileDir + "/profile.cfg";
00171     KConfig profileCfg( cfgPath, false /*read-only*/, false /*no KDE global*/ );
00172     profileCfg.setGroup( "Kontact Profile" );
00173     profileCfg.writeEntry( "Identifier", profile.id() );
00174     profileCfg.writeEntry( "Name", profile.name() );
00175     profileCfg.writeEntry( "Description", profile.description() );
00176 }
00177 
00178 void Kontact::ProfileManager::readConfig()
00179 {
00180     
00181     const QStringList profilePaths = KGlobal::dirs()->findAllResources( "data", QString::fromLatin1( "kontact/profiles/*/profile.cfg" ) );
00182 
00183     typedef QMap<QString, Kontact::Profile> ProfileMap;
00184     ProfileMap profiles;
00185     ProfileMap globalProfiles;
00186 
00187     const QString localPrefix = locateLocal( "data", "kontact/profiles/", /*createDir=*/false );
00188     for ( QStringList::ConstIterator it = profilePaths.begin(), end = profilePaths.end(); it != end; ++it )
00189     {
00190         const bool isLocal = (*it).startsWith( localPrefix );
00191         const Kontact::Profile profile = readFromConfiguration( *it, isLocal );
00192         if ( profile.isNull() )
00193             continue;
00194         if ( isLocal )
00195             profiles[profile.id()] = profile;
00196         else 
00197             globalProfiles[profile.id()] = profile;
00198     }
00199     
00200     for ( ProfileMap::ConstIterator it = globalProfiles.begin(), end = globalProfiles.end(); it != end; ++it )
00201     {
00202         if ( !profiles.contains( it.key() ) )
00203             profiles[it.key()] = it.data();
00204     }
00205 
00206     for ( ProfileMap::ConstIterator it = profiles.begin(), end = profiles.end(); it != end; ++it )
00207     {
00208         addProfile( *it );
00209     }
00210 }
00211 
00212 QValueList<Kontact::Profile> Kontact::ProfileManager::profiles() const
00213 {
00214     return m_profiles.values();
00215 }
00216 
00217 Kontact::Profile Kontact::ProfileManager::profileById( const QString& id ) const
00218 {
00219     return m_profiles[id];
00220 }
00221 
00222 void Kontact::ProfileManager::updateProfile( const Kontact::Profile& profile_ )
00223 {
00224     const QString id = profile_.id();
00225     if ( id.isNull() || m_profiles[id] == profile_ )
00226         return;
00227     Kontact::Profile profile( profile_ );
00228     m_profiles[id] = profile;
00229     profile.setLocal( Kontact::Profile::CopyProfileFiles );
00230     writeProfileConfig( profile );
00231     emit profileUpdated( id );
00232 }
00233 
00234 void Kontact::Profile::copyConfigFiles( const QString& source_, const QString& dest_ )
00235 {
00236     const KURL source = KURL::fromPathOrURL( source_+"/*rc" );
00237     const KURL dest = KURL::fromPathOrURL( dest_ );
00238     KIO::CopyJob* job = KIO::copy( source, dest, /*showProgressInfo=*/false );
00239     // TODO better check for the copy result
00240 }
00241 
00242 void Kontact::ProfileManager::saveToProfile( const QString& id )
00243 {
00244     Kontact::Profile profile = profileById( id );
00245     if ( profile.isNull() )
00246         return;
00247     profile.setLocal( Kontact::Profile::CopyProfileFiles );
00248     writeProfileConfig( profile );
00249     emit saveToProfileRequested( id );
00250 }
00251 
00252 bool Kontact::ProfileManager::addProfile( const Kontact::Profile& profile )
00253 {
00254     const QString id = profile.id();
00255     if ( m_profiles.contains( id ) )
00256         return false;
00257     m_profiles[id] = profile;
00258     emit profileAdded( id );
00259     emit saveToProfileRequested( id );
00260     return true;
00261 }
00262 
00263 void Kontact::ProfileManager::loadProfile( const QString& id )
00264 {
00265     if ( !m_profiles.contains( id ) )
00266         return;
00267     emit profileLoaded( id );
00268 }
00269 
00270 void Kontact::ProfileManager::removeProfile( const Kontact::Profile& profile )
00271 {
00272     removeProfile( profile.id() );
00273 }
00274 
00275 void Kontact::ProfileManager::removeProfile( const QString& id )
00276 {
00277     if ( !m_profiles.contains( id ) )
00278         return;
00279     
00280     m_profiles.remove( id );
00281     emit profileRemoved( id );
00282 }
00283 
00284 Kontact::ProfileManager::ExportError Kontact::ProfileManager::exportProfileToDirectory( const QString& id, const QString& path )
00285 {
00286     if ( !m_profiles.contains( id ) )
00287         return SuccessfulExport;
00288 
00289     if ( !QDir( path ).exists() )
00290         return DirectoryDoesNotExist;
00291 
00292     const Kontact::Profile profile = profileById( id );
00293     const KURL source = KURL::fromPathOrURL( profile.saveLocation() );
00294     const KURL target = KURL::fromPathOrURL( path + QDir::separator() + profile.name() );
00295 
00296     KIO::CopyJob* job = KIO::copy( source, target, /*showProgressInfo=*/false );
00297     // TODO check result
00298 
00299     return SuccessfulExport;
00300 }
00301 
00302 Kontact::ProfileManager::ImportError Kontact::ProfileManager::importProfileFromDirectory( const QString& path )
00303 {
00304     Kontact::Profile profile = readFromConfiguration( path + "/profile.cfg", /*isLocal=*/ true );
00305     if ( profile.isNull() )
00306         return NoValidProfile;
00307 
00308     profile.setId( generateNewId() );
00309 
00310     const KURL source = KURL::fromPathOrURL( path );
00311     const KURL target = KURL::fromPathOrURL( profile.saveLocation() );
00312 
00313     writeProfileConfig( profile );
00314     KIO::CopyJob* job = KIO::copy( source, target, /*showProgressInfo=*/false );
00315     // TODO better check for the copy result
00316 
00317     addProfile( profile );
00318 
00319     return SuccessfulImport;
00320 }
00321 
00322 QString Kontact::ProfileManager::generateNewId() const
00323 {
00324     while ( true )
00325     {
00326         const QString newId = KApplication::randomString( 10 );
00327         if ( !m_profiles.contains( newId ) )
00328             return newId;
00329     }
00330 }
00331 
00332 #include "profilemanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys