21static const char configKeyDefaultIdentity[] =
"Default Identity";
23#include "identitymanager.h"
26#include <kpimutils/email.h>
28#include <kemailsettings.h>
30#include <klocalizedstring.h>
34#include <ksharedconfig.h>
36#include <kconfiggroup.h>
40#include <QtDBus/QtDBus>
46#include "identitymanageradaptor.h"
48using namespace KPIMIdentities;
50static QString newDBusObjectName()
52 static int s_count = 0;
53 QString name( QLatin1String(
"/KPIMIDENTITIES_IdentityManager") );
55 name += QLatin1Char(
'_');
56 name += QString::number( s_count );
65 setObjectName( QLatin1String(name) );
66 KGlobal::locale()->insertCatalog( QLatin1String(
"libkpimidentities") );
67 new IdentityManagerAdaptor(
this );
68 QDBusConnection dbus = QDBusConnection::sessionBus();
69 const QString dbusPath = newDBusObjectName();
70 setProperty(
"uniqueDBusPath", dbusPath );
71 const QString dbusInterface = QLatin1String(
"org.kde.pim.IdentityManager");
72 dbus.registerObject( dbusPath,
this );
73 dbus.connect( QString(), QString(), dbusInterface, QLatin1String(
"identitiesChanged"),
this,
74 SLOT(slotIdentitiesChanged(QString)) );
77 mConfig =
new KConfig( QLatin1String(
"emailidentities") );
78 readConfig( mConfig );
80 kDebug( 5325 ) <<
"emailidentities is empty -> convert from kmailrc";
84 KConfig kmailConf( QLatin1String(
"kmailrc") );
85 readConfig( &kmailConf );
89 kDebug( 5325 ) <<
"IdentityManager: No identity found. Creating default.";
94 KSharedConfig::Ptr kmailConf(KSharedConfig::openConfig(QLatin1String(
"kmail2rc")));
96 bool needCommit =
false;
97 if (kmailConf->hasGroup(QLatin1String(
"Composer"))) {
98 KConfigGroup composerGroup = kmailConf->group(QLatin1String(
"Composer"));
99 if (composerGroup.hasKey(QLatin1String(
"pgp-auto-sign"))) {
100 const bool pgpAutoSign = composerGroup.readEntry(QLatin1String(
"pgp-auto-sign"),
false);
102 for ( QList<Identity>::iterator it =
mIdentities.begin(); it != end; ++it ) {
103 it->setPgpAutoSign(pgpAutoSign);
105 composerGroup.deleteEntry(QLatin1String(
"pgp-auto-sign"));
106 composerGroup.sync();
110 if (kmailConf->hasGroup(QLatin1String(
"General"))) {
111 KConfigGroup generalGroup = kmailConf->group(QLatin1String(
"General"));
112 if (generalGroup.hasKey(QLatin1String(
"Default domain"))) {
113 QString defaultDomain = generalGroup.readEntry(QLatin1String(
"Default domain"));
114 if (defaultDomain.isEmpty()) {
115 defaultDomain = QHostInfo::localHostName();
118 for ( QList<Identity>::iterator it =
mIdentities.begin(); it != end; ++it ) {
119 it->setDefaultDomainName(defaultDomain);
121 generalGroup.deleteEntry(QLatin1String(
"Default domain"));
131 if ( KEMailSettings().getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
136IdentityManager::~IdentityManager()
139 <<
"IdentityManager: There were uncommitted changes!";
146 QString result = name;
148 result = i18nc(
"%1: name; %2: number appended to it to make it unique "
149 "among a list of names",
"%1 #%2",
168 QList<uint> seenUOIDs;
169 QList<Identity>::ConstIterator end =
mIdentities.constEnd();
170 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
172 seenUOIDs << ( *it ).uoid();
175 QList<uint> changedUOIDs;
179 int index = seenUOIDs.indexOf( ( *it ).uoid() );
181 uint uoid = seenUOIDs.at( index );
185 kDebug( 5325 ) <<
"emitting changed() for identity" << uoid;
187 changedUOIDs << uoid;
189 seenUOIDs.removeAll( uoid );
192 kDebug( 5325 ) <<
"emitting added() for identity" << ( *it ).uoid();
198 for ( QList<uint>::ConstIterator it = seenUOIDs.constBegin();
199 it != seenUOIDs.constEnd(); ++it ) {
200 kDebug( 5325 ) <<
"emitting deleted() for identity" << ( *it );
210 QList<uint>::ConstIterator changedEnd( changedUOIDs.constEnd() );
211 for ( QList<uint>::ConstIterator it = changedUOIDs.constBegin();
212 it != changedEnd; ++it ) {
219 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
220 arg( QDBusConnection::sessionBus().baseService() ).
221 arg( property(
"uniqueDBusPath" ).toString() );
222 emit identitiesChanged( ourIdentifier );
241 result << ( *it ).identityName();
252 result << ( *it ).identityName();
262void IdentityManager::writeConfig()
const
264 const QStringList
identities = groupList( mConfig );
265 QStringList::const_iterator groupEnd =
identities.constEnd();
266 for ( QStringList::const_iterator group =
identities.constBegin();
267 group != groupEnd; ++group ) {
268 mConfig->deleteGroup( *group );
273 it != end; ++it, ++i ) {
274 KConfigGroup cg( mConfig, QString::fromLatin1(
"Identity #%1" ).arg( i ) );
275 ( *it ).writeConfig( cg );
276 if ( ( *it ).isDefault() ) {
278 KConfigGroup general( mConfig,
"General" );
279 general.writeEntry( configKeyDefaultIdentity, ( *it ).uoid() );
283 es.setSetting( KEMailSettings::RealName, ( *it ).fullName() );
284 es.setSetting( KEMailSettings::EmailAddress, ( *it ).primaryEmailAddress() );
285 es.setSetting( KEMailSettings::Organization, ( *it ).organization() );
286 es.setSetting( KEMailSettings::ReplyToAddress, ( *it ).replyToAddr() );
293void IdentityManager::readConfig( KConfig *config )
297 const QStringList
identities = groupList( config );
302 KConfigGroup general( config,
"General" );
303 uint
defaultIdentity = general.readEntry( configKeyDefaultIdentity, 0 );
304 bool haveDefault =
false;
305 QStringList::const_iterator groupEnd =
identities.constEnd();
306 for ( QStringList::const_iterator group =
identities.constBegin();
307 group != groupEnd; ++group ) {
308 KConfigGroup configGroup( config, *group );
321 if ( !haveDefault ) {
325 kWarning( 5325 ) <<
"IdentityManager: There was no default identity."
326 <<
"Marking first one as default.";
334QStringList IdentityManager::groupList( KConfig *config )
const
336 return config->groupList().filter( QRegExp( QLatin1String(
"^Identity #\\d+$") ) );
339IdentityManager::ConstIterator IdentityManager::begin()
const
344IdentityManager::ConstIterator IdentityManager::end()
const
354IdentityManager::Iterator IdentityManager::modifyEnd()
361 for ( ConstIterator it = begin(); it != end(); ++it ) {
362 if ( ( *it ).uoid() == uoid ) {
366 return Identity::null();
380 const QString &addresses )
const
382 const QStringList addressList = KPIMUtils::splitAddressList( addresses );
383 foreach (
const QString &fullAddress, addressList ) {
384 const QString addrSpec = KPIMUtils::extractEmailAddress( fullAddress ).toLower();
385 for ( ConstIterator it = begin(); it != end(); ++it ) {
392 return Identity::null();
402 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
403 if ( ( *it ).identityName() == name ) {
408 kWarning( 5325 ) <<
"IdentityManager::modifyIdentityForName() used as"
409 <<
"newFromScratch() replacement!"
410 << endl <<
" name == \"" << name <<
"\"";
411 return newFromScratch( name );
416 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
417 if ( ( *it ).uoid() == uoid ) {
422 kWarning( 5325 ) <<
"IdentityManager::identityForUoid() used as"
423 <<
"newFromScratch() replacement!"
424 << endl <<
" uoid == \"" << uoid <<
"\"";
425 return newFromScratch( i18n(
"Unnamed" ) );
430 for ( ConstIterator it = begin(); it != end(); ++it ) {
431 if ( ( *it ).isDefault() ) {
437 kFatal( 5325 ) <<
"IdentityManager: No default identity found!";
439 kWarning( 5325 ) <<
"IdentityManager: No default identity found!";
450 if ( ( *it ).uoid() == uoid ) {
461 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
462 ( *it ).setIsDefault( ( *it ).uoid() == uoid );
476 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
477 if ( ( *it ).identityName() == name ) {
478 bool removedWasDefault = ( *it ).isDefault();
491 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
492 if ( ( *it ).identityName() == name ) {
493 bool removedWasDefault = ( *it ).isDefault();
504Identity &IdentityManager::newFromScratch(
const QString &name )
506 return newFromExisting(
Identity( name ) );
509Identity &IdentityManager::newFromControlCenter(
const QString &name )
512 es.setProfile( es.defaultProfileName() );
516 es.getSetting( KEMailSettings::RealName ),
517 es.getSetting( KEMailSettings::EmailAddress ),
518 es.getSetting( KEMailSettings::Organization ),
519 es.getSetting( KEMailSettings::ReplyToAddress ) ) );
522Identity &IdentityManager::newFromExisting(
const Identity &other,
const QString &name )
528 if ( !name.isNull() ) {
534void IdentityManager::createDefaultIdentity()
536 QString fullName, emailAddress;
543 if ( fullName.isEmpty() && emailAddress.isEmpty() ) {
544 KEMailSettings emailSettings;
545 fullName = emailSettings.getSetting( KEMailSettings::RealName );
546 emailAddress = emailSettings.getSetting( KEMailSettings::EmailAddress );
548 if ( !fullName.isEmpty() && !emailAddress.isEmpty() ) {
549 newFromControlCenter( i18nc(
"use default address from control center",
555 if ( fullName.isEmpty() ) {
556 fullName = user.property( KUser::FullName ).toString();
558 if ( emailAddress.isEmpty() ) {
559 emailAddress = user.loginName();
560 if ( !emailAddress.isEmpty() ) {
561 KConfigGroup general( mConfig,
"General" );
562 QString defaultdomain = general.readEntry(
"Default domain" );
563 if ( !defaultdomain.isEmpty() ) {
564 emailAddress += QLatin1Char(
'@') + defaultdomain;
566 emailAddress.clear();
575 QString name( i18nc(
"Default name for new email accounts/identities.",
"Unnamed" ) );
577 if ( !emailAddress.isEmpty() ) {
579 QString idName = emailAddress;
580 int pos = idName.indexOf( QLatin1Char(
'@') );
582 name = idName.mid( pos + 1, -1 );
586 name.replace( QLatin1Char(
'.'), QLatin1Char(
' ') );
587 pos = name.indexOf( QLatin1Char(
' ') );
589 name[pos + 1] = name[pos + 1].toUpper();
591 name[0] = name[0].toUpper();
592 }
else if ( !fullName.isEmpty() ) {
606int IdentityManager::newUoid()
611 QList<uint> usedUOIDs;
612 QList<Identity>::ConstIterator end(
mIdentities.constEnd() );
613 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
615 usedUOIDs << ( *it ).uoid();
623 it != endShadow; ++it ) {
624 usedUOIDs << ( *it ).uoid();
632 uoid = KRandom::random();
633 }
while ( usedUOIDs.indexOf( uoid ) != -1 );
641 for ( ConstIterator it = begin(); it != end(); ++it ) {
642 lst << ( *it ).primaryEmailAddress();
643 if ( !( *it ).emailAliases().isEmpty() ) {
644 lst << ( *it ).emailAliases();
650void KPIMIdentities::IdentityManager::slotRollback()
655void KPIMIdentities::IdentityManager::slotIdentitiesChanged(
const QString &
id )
657 kDebug( 5325 ) <<
" KPIMIdentities::IdentityManager::slotIdentitiesChanged :" << id;
658 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
659 arg( QDBusConnection::sessionBus().baseService() ).
660 arg( property(
"uniqueDBusPath" ).toString() );
661 if (
id != ourIdentifier ) {
662 mConfig->reparseConfiguration();
663 Q_ASSERT( !hasPendingChanges() );
664 readConfig( mConfig );
const Identity & identityForUoid(uint uoid) const
void rollback()
Re-read the config from disk and forget changes.
void added(const KPIMIdentities::Identity &ident)
Emitted on commit() for each new identity.
void sort()
Sort the identities by name (the default is always first).
bool removeIdentityForced(const QString &identityName)
Removes the identity with name identityName Will return false if the identity is not found,...
QStringList allEmails() const
Returns the list of all email addresses (only name@host) from all identities.
const Identity & defaultIdentity() const
Identity & modifyIdentityForName(const QString &identityName)
bool isUnique(const QString &name) const
QList< Identity > mShadowIdentities
The list that will be seen by the config dialog.
Iterator modifyBegin()
Iterator used by the configuration dialog, which works on a separate list of identities,...
QStringList shadowIdentities() const
Convenience method.
Identity & modifyIdentityForUoid(uint uoid)
const Identity & identityForAddress(const QString &addresses) const
const Identity & identityForUoidOrDefault(uint uoid) const
Convenience menthod.
virtual void createDefaultIdentity(QString &, QString &)
This is called when no identity has been defined, so we need to create a default one.
bool hasPendingChanges() const
Check whether there are any unsaved changes.
QStringList identities() const
bool thatIsMe(const QString &addressList) const
bool setAsDefault(uint uoid)
Sets the identity with Unique Object Identifier (UOID) uoid to be new the default identity.
void changed()
Emitted whenever a commit changes any configure option.
void deleted(uint uoid)
Emitted on commit() for each deleted identity.
bool removeIdentity(const QString &identityName)
Removes the identity with name identityName Will return false if the identity is not found,...
void commit()
Commit changes to disk and emit changed() if necessary.
IdentityManager(bool readonly=false, QObject *parent=0, const char *name=0)
Create an identity manager, which loads the emailidentities file to create identities.
QList< Identity > mIdentities
The list that will be seen by everyone.
QString makeUnique(const QString &name) const
User identity information.
QString primaryEmailAddress() const
primary email address (without the user name - only name@host).
bool matchesEmailAddress(const QString &addr) const
void setUoid(uint aUoid)
set the uiod
void setIsDefault(bool flag)
Set whether this identity is the default identity.
bool isNull() const
Returns true when the identity contains no values, all null values or only empty values.
void readConfig(const KConfigGroup &)
Read configuration from config.
void setIdentityName(const QString &name)
Identity/nickname for this collection.
uint uoid() const
Unique Object Identifier for this identity.