21 static const char configKeyDefaultIdentity[] =
"Default Identity";
23 #include "identitymanager.h"
26 #include <kpimutils/email.h>
28 #include <kemailsettings.h>
34 #include <kconfiggroup.h>
38 #include <QtDBus/QtDBus>
43 #include "identitymanageradaptor.h"
45 using namespace KPIMIdentities;
47 static QString newDBusObjectName()
49 static int s_count = 0;
50 QString name(
"/KPIMIDENTITIES_IdentityManager" );
53 name += QString::number( s_count );
62 setObjectName( name );
63 KGlobal::locale()->insertCatalog(
"libkpimidentities" );
64 new IdentityManagerAdaptor(
this );
65 QDBusConnection dbus = QDBusConnection::sessionBus();
66 const QString dbusPath = newDBusObjectName();
67 setProperty(
"uniqueDBusPath", dbusPath );
68 const QString dbusInterface =
"org.kde.pim.IdentityManager";
69 dbus.registerObject( dbusPath,
this );
70 dbus.connect( QString(), QString(), dbusInterface,
"identitiesChanged",
this,
71 SLOT(slotIdentitiesChanged(QString)) );
74 mConfig =
new KConfig(
"emailidentities" );
75 readConfig( mConfig );
77 kDebug( 5325 ) <<
"emailidentities is empty -> convert from kmailrc";
81 KConfig kmailConf(
"kmailrc" );
82 readConfig( &kmailConf );
86 kDebug( 5325 ) <<
"IdentityManager: No identity found. Creating default.";
91 if ( KEMailSettings().getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
96 IdentityManager::~IdentityManager()
99 <<
"IdentityManager: There were uncommitted changes!";
106 QString result = name;
108 result = i18nc(
"%1: name; %2: number appended to it to make it unique "
109 "among a list of names",
"%1 #%2",
128 QList<uint> seenUOIDs;
129 QList<Identity>::ConstIterator end =
mIdentities.constEnd();
130 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
132 seenUOIDs << ( *it ).uoid();
135 QList<uint> changedUOIDs;
139 int index = seenUOIDs.indexOf( ( *it ).uoid() );
141 uint uoid = seenUOIDs.at( index );
145 kDebug( 5325 ) <<
"emitting changed() for identity" << uoid;
147 changedUOIDs << uoid;
149 seenUOIDs.removeAll( uoid );
152 kDebug( 5325 ) <<
"emitting added() for identity" << ( *it ).uoid();
158 for ( QList<uint>::ConstIterator it = seenUOIDs.constBegin();
159 it != seenUOIDs.constEnd(); ++it ) {
160 kDebug( 5325 ) <<
"emitting deleted() for identity" << ( *it );
170 QList<uint>::ConstIterator changedEnd( changedUOIDs.constEnd() );
171 for ( QList<uint>::ConstIterator it = changedUOIDs.constBegin();
172 it != changedEnd; ++it ) {
179 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
180 arg( QDBusConnection::sessionBus().baseService() ).
181 arg( property(
"uniqueDBusPath" ).toString() );
182 emit identitiesChanged( ourIdentifier );
201 result << ( *it ).identityName();
212 result << ( *it ).identityName();
222 void IdentityManager::writeConfig()
const
224 const QStringList
identities = groupList( mConfig );
225 QStringList::const_iterator groupEnd = identities.constEnd();
226 for ( QStringList::const_iterator group = identities.constBegin();
227 group != groupEnd; ++group ) {
228 mConfig->deleteGroup( *group );
233 it != end; ++it, ++i ) {
234 KConfigGroup cg( mConfig, QString::fromLatin1(
"Identity #%1" ).arg( i ) );
235 ( *it ).writeConfig( cg );
236 if ( ( *it ).isDefault() ) {
238 KConfigGroup general( mConfig,
"General" );
239 general.writeEntry( configKeyDefaultIdentity, ( *it ).uoid() );
243 es.setSetting( KEMailSettings::RealName, ( *it ).fullName() );
244 es.setSetting( KEMailSettings::EmailAddress, ( *it ).primaryEmailAddress() );
245 es.setSetting( KEMailSettings::Organization, ( *it ).organization() );
246 es.setSetting( KEMailSettings::ReplyToAddress, ( *it ).replyToAddr() );
253 void IdentityManager::readConfig( KConfig *config )
257 const QStringList identities = groupList( config );
258 if ( identities.isEmpty() ) {
262 KConfigGroup general( config,
"General" );
263 uint
defaultIdentity = general.readEntry( configKeyDefaultIdentity, 0 );
264 bool haveDefault =
false;
265 QStringList::const_iterator groupEnd = identities.constEnd();
266 for ( QStringList::const_iterator group = identities.constBegin();
267 group != groupEnd; ++group ) {
268 KConfigGroup configGroup( config, *group );
277 if ( !haveDefault ) {
278 kWarning( 5325 ) <<
"IdentityManager: There was no default identity."
279 <<
"Marking first one as default.";
287 QStringList IdentityManager::groupList( KConfig *config )
const
289 return config->groupList().filter( QRegExp(
"^Identity #\\d+$" ) );
292 IdentityManager::ConstIterator IdentityManager::begin()
const
297 IdentityManager::ConstIterator IdentityManager::end()
const
307 IdentityManager::Iterator IdentityManager::modifyEnd()
314 for ( ConstIterator it = begin(); it != end(); ++it ) {
315 if ( ( *it ).uoid() == uoid ) {
319 return Identity::null();
333 const QString &addresses )
const
335 const QStringList addressList = KPIMUtils::splitAddressList( addresses );
336 foreach (
const QString &fullAddress, addressList ) {
337 const QString addrSpec = KPIMUtils::extractEmailAddress( fullAddress ).toLower();
338 for ( ConstIterator it = begin(); it != end(); ++it ) {
345 return Identity::null();
355 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
356 if ( ( *it ).identityName() == name ) {
361 kWarning( 5325 ) <<
"IdentityManager::modifyIdentityForName() used as"
362 <<
"newFromScratch() replacement!"
363 << endl <<
" name == \"" << name <<
"\"";
364 return newFromScratch( name );
369 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
370 if ( ( *it ).uoid() == uoid ) {
375 kWarning( 5325 ) <<
"IdentityManager::identityForUoid() used as"
376 <<
"newFromScratch() replacement!"
377 << endl <<
" uoid == \"" << uoid <<
"\"";
378 return newFromScratch( i18n(
"Unnamed" ) );
383 for ( ConstIterator it = begin(); it != end(); ++it ) {
384 if ( ( *it ).isDefault() ) {
390 kFatal( 5325 ) <<
"IdentityManager: No default identity found!";
392 kWarning( 5325 ) <<
"IdentityManager: No default identity found!";
403 if ( ( *it ).uoid() == uoid ) {
414 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
415 ( *it ).setIsDefault( ( *it ).uoid() == uoid );
429 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
430 if ( ( *it ).identityName() == name ) {
431 bool removedWasDefault = ( *it ).isDefault();
444 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
445 if ( ( *it ).identityName() == name ) {
446 bool removedWasDefault = ( *it ).isDefault();
457 Identity &IdentityManager::newFromScratch(
const QString &name )
459 return newFromExisting(
Identity( name ) );
462 Identity &IdentityManager::newFromControlCenter(
const QString &name )
465 es.setProfile( es.defaultProfileName() );
469 es.getSetting( KEMailSettings::RealName ),
470 es.getSetting( KEMailSettings::EmailAddress ),
471 es.getSetting( KEMailSettings::Organization ),
472 es.getSetting( KEMailSettings::ReplyToAddress ) ) );
475 Identity &IdentityManager::newFromExisting(
const Identity &other,
const QString &name )
481 if ( !name.isNull() ) {
487 void IdentityManager::createDefaultIdentity()
489 QString fullName, emailAddress;
496 if ( fullName.isEmpty() && emailAddress.isEmpty() ) {
497 KEMailSettings emailSettings;
498 fullName = emailSettings.getSetting( KEMailSettings::RealName );
499 emailAddress = emailSettings.getSetting( KEMailSettings::EmailAddress );
501 if ( !fullName.isEmpty() && !emailAddress.isEmpty() ) {
502 newFromControlCenter( i18nc(
"use default address from control center",
508 if ( fullName.isEmpty() ) {
509 fullName = user.property( KUser::FullName ).toString();
511 if ( emailAddress.isEmpty() ) {
512 emailAddress = user.loginName();
513 if ( !emailAddress.isEmpty() ) {
514 KConfigGroup general( mConfig,
"General" );
515 QString defaultdomain = general.readEntry(
"Default domain" );
516 if ( !defaultdomain.isEmpty() ) {
517 emailAddress +=
'@' + defaultdomain;
519 emailAddress.clear();
528 QString name( i18nc(
"Default name for new email accounts/identities.",
"Unnamed" ) );
530 if ( !emailAddress.isEmpty() ) {
532 QString idName = emailAddress;
533 int pos = idName.indexOf(
'@' );
535 name = idName.mid( pos + 1, -1 );
539 name.replace(
'.',
' ' );
540 pos = name.indexOf(
' ' );
542 name[pos + 1] = name[pos + 1].toUpper();
544 name[0] = name[0].toUpper();
545 }
else if ( !fullName.isEmpty() ) {
559 int IdentityManager::newUoid()
564 QList<uint> usedUOIDs;
565 QList<Identity>::ConstIterator end(
mIdentities.constEnd() );
566 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
568 usedUOIDs << ( *it ).uoid();
576 it != endShadow; ++it ) {
577 usedUOIDs << ( *it ).uoid();
585 uoid = KRandom::random();
586 }
while ( usedUOIDs.indexOf( uoid ) != -1 );
594 for ( ConstIterator it = begin(); it != end(); ++it ) {
595 lst << ( *it ).primaryEmailAddress();
596 if ( !( *it ).emailAliases().isEmpty() ) {
597 lst << ( *it ).emailAliases();
603 void KPIMIdentities::IdentityManager::slotRollback()
608 void KPIMIdentities::IdentityManager::slotIdentitiesChanged(
const QString &
id )
610 kDebug( 5325 ) <<
" KPIMIdentities::IdentityManager::slotIdentitiesChanged :" << id;
611 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
612 arg( QDBusConnection::sessionBus().baseService() ).
613 arg( property(
"uniqueDBusPath" ).toString() );
614 if (
id != ourIdentifier ) {
615 mConfig->reparseConfiguration();
616 Q_ASSERT( !hasPendingChanges() );
617 readConfig( mConfig );
QString makeUnique(const QString &name) const
void deleted(uint uoid)
Emitted on commit() for each deleted identity.
bool removeIdentityForced(const QString &identityName)
Removes the identity with name identityName Will return false if the identity is not found...
IdentityManager(bool readonly=false, QObject *parent=0, const char *name=0)
Create an identity manager, which loads the emailidentities file to create identities.
void changed()
Emitted whenever a commit changes any configure option.
void setIsDefault(bool flag)
Set whether this identity is the default identity.
void rollback()
Re-read the config from disk and forget changes.
const Identity & identityForUoidOrDefault(uint uoid) const
Convenience menthod.
bool isUnique(const QString &name) const
void setIdentityName(const QString &name)
Identity/nickname for this collection.
Identity & modifyIdentityForUoid(uint uoid)
virtual void createDefaultIdentity(QString &, QString &)
This is called when no identity has been defined, so we need to create a default one.
QStringList identities() const
bool thatIsMe(const QString &addressList) const
void added(const KPIMIdentities::Identity &ident)
Emitted on commit() for each new identity.
QList< Identity > mIdentities
The list that will be seen by everyone.
bool matchesEmailAddress(const QString &addr) const
User identity information.
void commit()
Commit changes to disk and emit changed() if necessary.
QStringList shadowIdentities() const
Convenience method.
const Identity & identityForAddress(const QString &addresses) const
bool isNull() const
Returns true when the identity contains no values, all null values or only empty values ...
bool setAsDefault(uint uoid)
Sets the identity with Unique Object Identifier (UOID) uoid to be new the default identity...
void setUoid(uint aUoid)
set the uiod
Identity & modifyIdentityForName(const QString &identityName)
void sort()
Sort the identities by name (the default is always first).
bool hasPendingChanges() const
Check whether there are any unsaved changes.
const Identity & identityForUoid(uint uoid) const
QList< Identity > mShadowIdentities
The list that will be seen by the config dialog.
const Identity & defaultIdentity() const
Iterator modifyBegin()
Iterator used by the configuration dialog, which works on a separate list of identities, for modification.
QStringList allEmails() const
Returns the list of all email addresses (only name) from all identities.
bool removeIdentity(const QString &identityName)
Removes the identity with name identityName Will return false if the identity is not found...