25 #include "profilemanager.h"
27 #include <tdeio/job.h>
29 #include <tdeapplication.h>
30 #include <tdeconfig.h>
31 #include <tdeglobal.h>
32 #include <kstandarddirs.h>
33 #include <kstaticdeleter.h>
37 #include <tqstringlist.h>
38 #include <tqvaluelist.h>
40 Kontact::Profile::Profile(
const TQString&
id,
bool isLocal ) : m_id( id ), m_local( isLocal )
44 Kontact::Profile::Profile() : m_local( false )
48 TQString Kontact::Profile::id()
const
53 TQString Kontact::Profile::name()
const
58 TQString Kontact::Profile::description()
const
63 bool Kontact::Profile::isNull()
const
68 void Kontact::Profile::setId(
const TQString&
id )
73 void Kontact::Profile::setDescription(
const TQString& description )
75 m_description = description;
78 void Kontact::Profile::setName(
const TQString& name )
83 void Kontact::Profile::setLocal( SetLocalMode mode )
88 if ( mode == CopyProfileFiles )
89 copyConfigFiles( m_originalLocation, localSaveLocation() );
94 bool Kontact::Profile::isLocal()
const
99 void Kontact::Profile::setOriginalLocation(
const TQString& path )
101 m_originalLocation = path;
104 TQString Kontact::Profile::localSaveLocation()
const
107 return m_id.isNull() ? TQString() : locateLocal(
"data",
"kontact/profiles/" + m_id, true );
110 TQString Kontact::Profile::saveLocation()
const
112 return m_local ? localSaveLocation() : m_originalLocation;
115 bool Kontact::Profile::operator==(
const Kontact::Profile& other )
const
117 return m_id == other.m_id && m_name == other.m_name && m_description == other.m_description;
120 Kontact::ProfileManager* Kontact::ProfileManager::m_self = 0;
122 static KStaticDeleter<Kontact::ProfileManager> profileManagerSD;
124 Kontact::ProfileManager* Kontact::ProfileManager::self()
128 profileManagerSD.setObject( m_self,
new Kontact::ProfileManager );
129 m_self->readConfig();
134 Kontact::ProfileManager::ProfileManager( TQObject* parent ) : TQObject( parent )
138 Kontact::ProfileManager::~ProfileManager()
143 void Kontact::ProfileManager::writeConfig()
const
145 const TQValueList<Kontact::Profile> profiles = m_profiles.values();
146 for ( TQValueList<Kontact::Profile>::ConstIterator it = profiles.begin(), end = profiles.end(); it != end; ++it )
148 writeProfileConfig( *it );
152 Kontact::Profile Kontact::ProfileManager::readFromConfiguration(
const TQString& configFile,
bool isLocal )
154 TDEConfig profileCfg( configFile,
true ,
false );
155 const TQString configDir = configFile.left( configFile.findRev( TQDir::separator(), -1 ) );
156 profileCfg.setGroup(
"Kontact Profile" );
157 const TQString
id = profileCfg.readEntry(
"Identifier" );
158 Kontact::Profile profile(
id );
159 profile.setName( profileCfg.readEntry(
"Name" ) );
160 profile.setDescription( profileCfg.readEntry(
"Description" ) );
161 profile.setOriginalLocation( configDir );
163 profile.setLocal( Kontact::Profile::DoNotCopyProfileFiles );
167 void Kontact::ProfileManager::writeProfileConfig(
const Kontact::Profile& profile )
const
169 const TQString profileDir = profile.saveLocation();
170 const TQString cfgPath = profileDir +
"/profile.cfg";
171 TDEConfig profileCfg( cfgPath,
false ,
false );
172 profileCfg.setGroup(
"Kontact Profile" );
173 profileCfg.writeEntry(
"Identifier", profile.id() );
174 profileCfg.writeEntry(
"Name", profile.name() );
175 profileCfg.writeEntry(
"Description", profile.description() );
178 void Kontact::ProfileManager::readConfig()
181 const TQStringList profilePaths = TDEGlobal::dirs()->findAllResources(
"data", TQString::fromLatin1(
"kontact/profiles/*/profile.cfg" ) );
183 typedef TQMap<TQString, Kontact::Profile> ProfileMap;
185 ProfileMap globalProfiles;
187 const TQString localPrefix = locateLocal(
"data",
"kontact/profiles/",
false );
188 for ( TQStringList::ConstIterator it = profilePaths.begin(), end = profilePaths.end(); it != end; ++it )
190 const bool isLocal = (*it).startsWith( localPrefix );
191 const Kontact::Profile profile = readFromConfiguration( *it, isLocal );
192 if ( profile.isNull() )
195 profiles[profile.id()] = profile;
197 globalProfiles[profile.id()] = profile;
200 for ( ProfileMap::ConstIterator it = globalProfiles.begin(), end = globalProfiles.end(); it != end; ++it )
202 if ( !profiles.contains( it.key() ) )
203 profiles[it.key()] = it.data();
206 for ( ProfileMap::ConstIterator it = profiles.begin(), end = profiles.end(); it != end; ++it )
208 addProfile( *it,
false );
212 TQValueList<Kontact::Profile> Kontact::ProfileManager::profiles()
const
214 return m_profiles.values();
217 Kontact::Profile Kontact::ProfileManager::profileById(
const TQString&
id )
const
219 return m_profiles[id];
222 void Kontact::ProfileManager::updateProfile(
const Kontact::Profile& profile_ )
224 const TQString
id = profile_.id();
225 if (
id.isNull() || m_profiles[
id] == profile_ )
227 Kontact::Profile profile( profile_ );
228 m_profiles[id] = profile;
229 profile.setLocal( Kontact::Profile::CopyProfileFiles );
230 writeProfileConfig( profile );
231 emit profileUpdated(
id );
234 void Kontact::Profile::copyConfigFiles(
const TQString& source_,
const TQString& dest_ )
236 const KURL source = KURL::fromPathOrURL( source_+
"/*rc" );
237 const KURL dest = KURL::fromPathOrURL( dest_ );
238 TDEIO::CopyJob* job = TDEIO::copy( source, dest,
false );
242 void Kontact::ProfileManager::saveToProfile(
const TQString&
id )
244 Kontact::Profile profile = profileById(
id );
245 if ( profile.isNull() )
247 profile.setLocal( Kontact::Profile::CopyProfileFiles );
248 writeProfileConfig( profile );
249 emit saveToProfileRequested(
id );
252 bool Kontact::ProfileManager::addProfile(
const Kontact::Profile& profile,
bool syncConfig )
254 const TQString
id = profile.id();
255 if ( m_profiles.contains(
id ) )
257 m_profiles[id] = profile;
258 emit profileAdded(
id );
259 emit saveToProfileRequested(
id );
261 writeProfileConfig( profile );
267 void Kontact::ProfileManager::loadProfile(
const TQString&
id )
269 if ( !m_profiles.contains(
id ) )
271 emit profileLoaded(
id );
274 void Kontact::ProfileManager::removeProfile(
const Kontact::Profile& profile )
276 removeProfile( profile.id() );
279 void Kontact::ProfileManager::removeProfile(
const TQString&
id )
281 if ( !m_profiles.contains(
id ) )
283 Kontact::Profile profile = profileById(
id );
284 if ( profile.isLocal() ) {
285 KURL location = KURL::fromPathOrURL( profile.saveLocation() );
286 TDEIO::DeleteJob* job = TDEIO::del( location,
false,
false );
289 m_profiles.remove(
id );
290 emit profileRemoved(
id );
293 Kontact::ProfileManager::ExportError Kontact::ProfileManager::exportProfileToDirectory(
const TQString&
id,
const TQString& path )
295 if ( !m_profiles.contains(
id ) )
296 return SuccessfulExport;
298 if ( !TQDir( path ).exists() )
299 return DirectoryDoesNotExist;
301 const Kontact::Profile profile = profileById(
id );
302 const KURL source = KURL::fromPathOrURL( profile.saveLocation() );
303 const KURL target = KURL::fromPathOrURL( path + TQDir::separator() + profile.name() );
305 TDEIO::CopyJob* job = TDEIO::copy( source, target,
false );
308 return SuccessfulExport;
311 Kontact::ProfileManager::ImportError Kontact::ProfileManager::importProfileFromDirectory(
const TQString& path )
313 Kontact::Profile profile = readFromConfiguration( path +
"/profile.cfg",
true );
314 if ( profile.isNull() )
315 return NoValidProfile;
317 profile.setId( generateNewId() );
319 const KURL source = KURL::fromPathOrURL( path );
320 const KURL target = KURL::fromPathOrURL( profile.saveLocation() );
322 TDEIO::CopyJob* job = TDEIO::copy( source, target,
false );
325 addProfile( profile );
327 return SuccessfulImport;
330 TQString Kontact::ProfileManager::generateNewId()
const
334 const TQString newId = TDEApplication::randomString( 10 );
335 if ( !m_profiles.contains( newId ) )
340 #include "profilemanager.moc"