33 static const char configKeyDefaultIdentity[] =
"Default Identity";
39 #include "identitymanager.h"
42 #include <libemailfunctions/email.h>
44 #include <tdeemailsettings.h>
45 #include <tdeapplication.h>
46 #include <tdelocale.h>
48 #include <tdeconfig.h>
50 #include <dcopclient.h>
58 static TQCString newDCOPObjectName()
60 static int s_count = 0;
61 TQCString name(
"KPIM::IdentityManager" );
64 name += TQCString().setNum( s_count );
69 IdentityManager::IdentityManager(
bool readonly, TQObject * parent,
const char * name )
70 : ConfigManager( parent, name ), DCOPObject( newDCOPObjectName() )
73 mConfig =
new TDEConfig(
"emailidentities", readonly );
75 if ( mIdentities.isEmpty() ) {
76 kdDebug(5006) <<
"emailidentities is empty -> convert from kmailrc" << endl;
79 TDEConfig kmailConf(
"kmailrc",
true );
80 readConfig( &kmailConf );
83 if ( mIdentities.isEmpty() ) {
84 kdDebug( 5006 ) <<
"IdentityManager: No identity found. Creating default." << endl;
85 createDefaultIdentity();
89 if ( KEMailSettings().getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
94 if ( !connectDCOPSignal( 0,
"KPIM::IdentityManager",
"identitiesChanged(TQCString,TQCString)",
95 "slotIdentitiesChanged(TQCString,TQCString)",
false ) )
96 kdError(5650) <<
"IdentityManager: connection to identitiesChanged failed" << endl;
99 IdentityManager::~IdentityManager()
101 kdWarning( hasPendingChanges(), 5006 )
102 <<
"IdentityManager: There were uncommitted changes!" << endl;
106 void IdentityManager::commit()
109 if ( !hasPendingChanges() || mReadOnly )
return;
111 TQValueList<uint> seenUOIDs;
112 for ( TQValueList<Identity>::ConstIterator it = mIdentities.begin() ;
113 it != mIdentities.end() ; ++it )
114 seenUOIDs << (*it).uoid();
116 TQValueList<uint> changedUOIDs;
118 for ( TQValueList<Identity>::ConstIterator it = mShadowIdentities.begin() ;
119 it != mShadowIdentities.end() ; ++it ) {
120 TQValueList<uint>::Iterator uoid = seenUOIDs.find( (*it).uoid() );
121 if ( uoid != seenUOIDs.end() ) {
122 const Identity & orig = identityForUoid( *uoid );
125 kdDebug( 5006 ) <<
"emitting changed() for identity " << *uoid << endl;
127 changedUOIDs << *uoid;
129 seenUOIDs.remove( uoid );
132 kdDebug( 5006 ) <<
"emitting added() for identity " << (*it).uoid() << endl;
138 for ( TQValueList<uint>::ConstIterator it = seenUOIDs.begin() ;
139 it != seenUOIDs.end() ; ++it ) {
140 kdDebug( 5006 ) <<
"emitting deleted() for identity " << (*it) << endl;
144 mIdentities = mShadowIdentities;
149 for ( TQValueList<uint>::ConstIterator it = changedUOIDs.begin() ;
150 it != changedUOIDs.end() ; ++it )
153 emit ConfigManager::changed();
158 TQByteArray data; TQDataStream arg( data, IO_WriteOnly );
159 arg << kapp->dcopClient()->appId();
160 arg << DCOPObject::objId();
161 kapp->dcopClient()->emitDCOPSignal(
"KPIM::IdentityManager",
"identitiesChanged(TQCString,TQCString)", data );
164 void IdentityManager::rollback()
166 mShadowIdentities = mIdentities;
169 bool IdentityManager::hasPendingChanges()
const
171 return mIdentities != mShadowIdentities;
174 TQStringList IdentityManager::identities()
const
177 for ( ConstIterator it = mIdentities.begin() ;
178 it != mIdentities.end() ; ++it )
179 result << (*it).identityName();
183 TQStringList IdentityManager::shadowIdentities()
const
186 for ( ConstIterator it = mShadowIdentities.begin() ;
187 it != mShadowIdentities.end() ; ++it )
188 result << (*it).identityName();
192 void IdentityManager::sort() {
193 qHeapSort( mShadowIdentities );
196 void IdentityManager::writeConfig()
const {
197 TQStringList identities = groupList(mConfig);
198 for ( TQStringList::Iterator group = identities.begin() ;
199 group != identities.end() ; ++group )
200 mConfig->deleteGroup( *group );
202 for ( ConstIterator it = mIdentities.begin() ;
203 it != mIdentities.end() ; ++it, ++i ) {
204 TDEConfigGroup cg( mConfig, TQString::fromLatin1(
"Identity #%1").arg(i) );
205 (*it).writeConfig( &cg );
206 if ( (*it).isDefault() ) {
208 TDEConfigGroup general( mConfig,
"General" );
209 general.writeEntry( configKeyDefaultIdentity, (*it).uoid() );
213 es.setSetting( KEMailSettings::RealName, (*it).fullName() );
214 es.setSetting( KEMailSettings::EmailAddress, (*it).primaryEmailAddress() );
215 es.setSetting( KEMailSettings::Organization, (*it).organization() );
216 es.setSetting( KEMailSettings::ReplyToAddress, (*it).replyToAddr() );
223 void IdentityManager::readConfig(TDEConfigBase* config) {
226 TQStringList identities = groupList(config);
227 if ( identities.isEmpty() )
return;
229 TDEConfigGroup general( config,
"General" );
230 uint defaultIdentity = general.readUnsignedNumEntry( configKeyDefaultIdentity );
231 bool haveDefault =
false;
233 for ( TQStringList::Iterator group = identities.begin() ;
234 group != identities.end() ; ++group ) {
235 TDEConfigGroup configGroup( config, *group );
237 mIdentities.last().readConfig( &configGroup );
238 if ( !haveDefault && mIdentities.last().uoid() == defaultIdentity ) {
240 mIdentities.last().setIsDefault(
true );
243 if ( !haveDefault ) {
244 kdWarning( 5006 ) <<
"IdentityManager: There was no default identity. Marking first one as default." << endl;
245 mIdentities.first().setIsDefault(
true );
247 qHeapSort( mIdentities );
249 mShadowIdentities = mIdentities;
252 TQStringList IdentityManager::groupList(TDEConfigBase* config)
const {
253 return config->groupList().grep( TQRegExp(
"^Identity #\\d+$") );
256 IdentityManager::ConstIterator IdentityManager::begin()
const {
257 return mIdentities.begin();
260 IdentityManager::ConstIterator IdentityManager::end()
const {
261 return mIdentities.end();
264 IdentityManager::Iterator IdentityManager::modifyBegin() {
265 return mShadowIdentities.begin();
268 IdentityManager::Iterator IdentityManager::modifyEnd() {
269 return mShadowIdentities.end();
272 const Identity & IdentityManager::identityForName(
const TQString & name )
const
275 <<
"deprecated method IdentityManager::identityForName() called!" << endl;
276 for ( ConstIterator it = begin() ; it != end() ; ++it )
277 if ( (*it).identityName() == name )
return (*it);
278 return Identity::null();
281 const Identity & IdentityManager::identityForUoid( uint uoid )
const {
282 for ( ConstIterator it = begin() ; it != end() ; ++it )
283 if ( (*it).uoid() == uoid )
return (*it);
284 return Identity::null();
287 const Identity & IdentityManager::identityForNameOrDefault(
const TQString & name )
const
289 const Identity & ident = identityForName( name );
290 if ( ident.isNull() )
291 return defaultIdentity();
296 const Identity & IdentityManager::identityForUoidOrDefault( uint uoid )
const
298 const Identity & ident = identityForUoid( uoid );
299 if ( ident.isNull() )
300 return defaultIdentity();
305 const Identity & IdentityManager::identityForAddress(
const TQString & addresses )
const
307 const TQStringList addressList = KPIM::splitEmailAddrList( addresses );
308 for( TQStringList::ConstIterator addrIt = addressList.begin();
309 addrIt != addressList.end(); ++addrIt ) {
310 const TQString addr = KPIM::getEmailAddress( *addrIt ).lower();
311 for ( ConstIterator it = begin() ; it != end() ; ++it ) {
313 if (
id.matchesEmailAddress( addr ) ) {
318 return Identity::null();
321 bool IdentityManager::thatIsMe(
const TQString & addressList )
const {
322 return !identityForAddress( addressList ).isNull();
325 Identity & IdentityManager::modifyIdentityForName(
const TQString & name )
327 for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
328 if ( (*it).identityName() == name )
return (*it);
329 kdWarning( 5006 ) <<
"IdentityManager::identityForName() used as newFromScratch() replacement!"
330 <<
"\n name == \"" << name <<
"\"" << endl;
331 return newFromScratch( name );
334 Identity & IdentityManager::modifyIdentityForUoid( uint uoid )
336 for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
337 if ( (*it).uoid() == uoid )
return (*it);
338 kdWarning( 5006 ) <<
"IdentityManager::identityForUoid() used as newFromScratch() replacement!"
339 <<
"\n uoid == \"" << uoid <<
"\"" << endl;
340 return newFromScratch( i18n(
"Unnamed") );
343 const Identity & IdentityManager::defaultIdentity()
const {
344 for ( ConstIterator it = begin() ; it != end() ; ++it )
345 if ( (*it).isDefault() )
return (*it);
346 (mIdentities.isEmpty() ? kdFatal( 5006 ) : kdWarning( 5006 ) )
347 <<
"IdentityManager: No default identity found!" << endl;
351 bool IdentityManager::setAsDefault(
const TQString & name ) {
353 TQStringList names = shadowIdentities();
354 if ( names.find( name ) == names.end() )
return false;
356 for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
357 (*it).setIsDefault( (*it).identityName() == name );
363 bool IdentityManager::setAsDefault( uint uoid ) {
366 for ( ConstIterator it = mShadowIdentities.begin() ;
367 it != mShadowIdentities.end() ; ++it )
368 if ( (*it).uoid() == uoid ) {
372 if ( !found )
return false;
375 for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
376 (*it).setIsDefault( (*it).uoid() == uoid );
382 bool IdentityManager::removeIdentity(
const TQString & name ) {
383 for ( Iterator it = modifyBegin() ; it != modifyEnd() ; ++it )
384 if ( (*it).identityName() == name ) {
385 bool removedWasDefault = (*it).isDefault();
386 mShadowIdentities.remove( it );
387 if ( removedWasDefault )
388 mShadowIdentities.first().setIsDefault(
true );
394 Identity & IdentityManager::newFromScratch(
const TQString & name ) {
395 return newFromExisting(
Identity( name ) );
398 Identity & IdentityManager::newFromControlCenter(
const TQString & name ) {
400 es.setProfile( es.defaultProfileName() );
402 return newFromExisting(
Identity( name,
403 es.getSetting( KEMailSettings::RealName ),
404 es.getSetting( KEMailSettings::EmailAddress ),
405 es.getSetting( KEMailSettings::Organization ),
406 es.getSetting( KEMailSettings::ReplyToAddress )
411 const TQString & name ) {
412 mShadowIdentities << other;
413 Identity & result = mShadowIdentities.last();
415 result.setUoid( newUoid() );
416 if ( !name.isNull() )
417 result.setIdentityName( name );
421 void IdentityManager::createDefaultIdentity() {
422 TQString fullName, emailAddress;
426 createDefaultIdentity( fullName, emailAddress );
429 if ( fullName.isEmpty() && emailAddress.isEmpty() ) {
430 KEMailSettings emailSettings;
431 fullName = emailSettings.getSetting( KEMailSettings::RealName );
432 emailAddress = emailSettings.getSetting( KEMailSettings::EmailAddress );
434 if ( !fullName.isEmpty() && !emailAddress.isEmpty() ) {
435 newFromControlCenter( i18n(
"Default") );
440 if ( fullName.isEmpty() )
441 fullName = user.fullName();
442 if ( emailAddress.isEmpty() ) {
443 emailAddress = user.loginName();
444 if ( !emailAddress.isEmpty() ) {
445 TDEConfigGroup general( mConfig,
"General" );
446 TQString defaultdomain = general.readEntry(
"Default domain" );
447 if( !defaultdomain.isEmpty() ) {
448 emailAddress +=
'@' + defaultdomain;
451 emailAddress = TQString();
459 mShadowIdentities <<
Identity( i18n(
"Default"), fullName, emailAddress );
461 mShadowIdentities.last().setIsDefault(
true );
462 mShadowIdentities.last().setUoid( newUoid() );
464 mIdentities = mShadowIdentities;
467 int IdentityManager::newUoid()
472 TQValueList<uint> usedUOIDs;
473 for ( TQValueList<Identity>::ConstIterator it = mIdentities.begin() ;
474 it != mIdentities.end() ; ++it )
475 usedUOIDs << (*it).uoid();
477 if ( hasPendingChanges() ) {
480 for ( TQValueList<Identity>::ConstIterator it = mShadowIdentities.begin() ;
481 it != mShadowIdentities.end() ; ++it ) {
482 usedUOIDs << (*it).uoid();
490 uoid = kapp->random();
491 }
while ( usedUOIDs.find( uoid ) != usedUOIDs.end() );
496 TQStringList KPIM::IdentityManager::allEmails()
const
499 for ( ConstIterator it = begin() ; it != end() ; ++it ) {
500 lst << (*it).primaryEmailAddress();
505 void KPIM::IdentityManager::slotIdentitiesChanged( TQCString appId, TQCString objId )
509 if ( kapp->dcopClient()->appId() != appId || DCOPObject::objId() != objId ) {
510 mConfig->reparseConfiguration();
511 Q_ASSERT( !hasPendingChanges() );
512 readConfig( mConfig );
516 #include "identitymanager.moc"
User identity information.
void setIsDefault(bool flag)
Set whether this identity is the default identity.