kitchensync

profileconfig.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2002 Holger Freyther <zecke@handhelds.org>
00005     Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include "profileconfig.h"
00024 
00025 #include <kconfig.h>
00026 #include <kstandarddirs.h>
00027 #include <klocale.h>
00028 #include <kdebug.h>
00029 
00030 using namespace KSync;
00031 
00032 ProfileConfig::ProfileConfig()
00033 {
00034   QString configPath = locateLocal( "appdata", "profiles" );
00035   
00036   mConfig = new KConfig( configPath );
00037 }
00038 
00039 ProfileConfig::~ProfileConfig()
00040 {
00041   delete mConfig;
00042 }
00043 
00044 /*
00045  * saves Profils
00046  */
00047 void ProfileConfig::save( const QValueList<Profile> &profiles )
00048 {
00049   kdDebug() << "ProfileConfig::save()" << endl;
00050 
00051   clear( mConfig );
00052 
00053   QStringList ids;
00054   Profile::List::ConstIterator it;
00055   for ( it = profiles.begin(); it != profiles.end(); ++it ) {
00056     ids << (*it).uid();
00057     saveProfile( mConfig, (*it) );
00058   }
00059   mConfig->setGroup( "General" );
00060   mConfig->writeEntry( "Keys", ids );
00061   
00062   mConfig->sync();
00063 }
00064 
00065 /*
00066  * loads one from file
00067  */
00068 Profile::List ProfileConfig::load()
00069 {
00070   mConfig->setGroup( "General" );
00071   QStringList keys = mConfig->readListEntry( "Keys" );
00072 
00073   Profile::List profiles;
00074   QStringList::Iterator it;
00075   for ( it = keys.begin(); it != keys.end(); ++it ) {
00076     mConfig->setGroup( *it );
00077     profiles.append( readProfile( mConfig ) );
00078   }
00079 
00080   if ( profiles.isEmpty() ) profiles = defaultProfiles();
00081 
00082   return profiles;
00083 }
00084 
00085 Profile::List ProfileConfig::defaultProfiles()
00086 {
00087   Profile::List profiles;
00088 
00089   Profile profSyncing;
00090   profSyncing.setName( i18n("Syncing") );
00091   ActionPartService::List partsSyncing;
00092   addPart( "overview", partsSyncing );
00093   addPart( "backup", partsSyncing );
00094   addPart( "syncerpart", partsSyncing );
00095   profSyncing.setActionParts( partsSyncing );
00096   profiles.append( profSyncing );
00097 
00098   Profile profRestore;
00099   profRestore.setName( i18n("Restore Backup") );
00100   ActionPartService::List partsRestore;
00101   addPart( "backup", partsRestore );
00102   profRestore.setActionParts( partsRestore );
00103   profiles.append( profRestore );
00104     
00105   return profiles;
00106 }
00107 
00108 void ProfileConfig::addPart( const QString &id, ActionPartService::List &parts )
00109 {
00110   ActionPartService overview = ActionPartService::partForId( id );
00111   if ( !overview.id().isEmpty() ) {
00112     parts.append( overview );
00113   }
00114 }
00115 
00116 void ProfileConfig::clear( KConfig *conf )
00117 {
00118   QStringList list = conf->groupList();
00119   QStringList::Iterator it;
00120   for ( it = list.begin(); it != list.end(); ++it ) {
00121     conf->deleteGroup( *it );
00122   }
00123 }
00124 
00125 void ProfileConfig::saveProfile( KConfig *conf, const Profile &prof )
00126 {
00127   conf->setGroup( prof.uid() );
00128   conf->writeEntry( "Name", prof.name() );
00129   conf->writeEntry( "Pixmap", prof.pixmap() );
00130   conf->writeEntry( "ConfirmDelete", prof.confirmDelete() );
00131   conf->writeEntry( "ConfirmSync", prof.confirmSync() );
00132 
00133   QMap<QString,QString> paths = prof.paths();
00134   QMap<QString,QString>::Iterator pathIt;
00135   QStringList pathlist;
00136   for ( pathIt = paths.begin(); pathIt != paths.end(); ++pathIt ) {
00137     pathlist << pathIt.key();
00138     conf->writePathEntry( "Path" + pathIt.key(), pathIt.data() );
00139   }
00140   conf->writeEntry("LocationPath", pathlist );
00141 
00142   ActionPartService::List parts = prof.actionParts();
00143   QStringList ids;
00144   ActionPartService::List::ConstIterator it;
00145   for( it = parts.begin(); it != parts.end(); ++it ) {
00146     ids.append( (*it).id() );
00147   }
00148   conf->writeEntry( "ActionParts", ids );
00149 }
00150 
00151 void ProfileConfig::saveActionPart( KConfig *conf,
00152                                     const ActionPartService &s )
00153 {
00154   conf->writeEntry( "Id", s.id() );
00155 }
00156 
00157 Profile ProfileConfig::readProfile( KConfig *conf )
00158 {
00159   Profile prof;
00160   prof.setUid( conf->group() );
00161   prof.setName( conf->readEntry( "Name" ) );
00162   prof.setPixmap( conf->readEntry( "Pixmap" ) );
00163   prof.setConfirmSync( conf->readBoolEntry( "ConfirmSync",  true ) );
00164   prof.setConfirmDelete( conf->readBoolEntry( "ConfirmDelete", true ) );
00165 
00166   QStringList locationPath = conf->readListEntry( "LocationPath" );
00167   QStringList::ConstIterator it;
00168   QMap<QString,QString> map;
00169   for ( it = locationPath.begin(); it != locationPath.end(); ++it ) {
00170     QString val = conf->readPathEntry( "Path" + (*it) );
00171     map.insert( (*it),  val );
00172   }
00173   prof.setPaths( map );
00174 
00175   ActionPartService::List parts;
00176 
00177   QStringList ids = conf->readListEntry( "ActionParts" );
00178   for( it = ids.begin(); it != ids.end(); ++it ) {
00179     addPart( *it, parts );
00180   }
00181 
00182   prof.setActionParts( parts );
00183 
00184   return prof;
00185 }
KDE Home | KDE Accessibility Home | Description of Access Keys