kitchensync
profileconfig.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
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 }
|