21 static const char configKeyDefaultIdentity[] =
"Default Identity";
23 #include "identitymanager.h"
26 #include <kpimutils/email.h>
28 #include <kemailsettings.h>
30 #include <klocalizedstring.h>
35 #include <kconfiggroup.h>
39 #include <QtDBus/QtDBus>
45 #include "identitymanageradaptor.h"
47 using namespace KPIMIdentities;
49 static QString newDBusObjectName()
51 static int s_count = 0;
52 QString name( QLatin1String(
"/KPIMIDENTITIES_IdentityManager") );
54 name += QLatin1Char(
'_');
55 name += QString::number( s_count );
64 setObjectName( QLatin1String(name) );
65 KGlobal::locale()->insertCatalog( QLatin1String(
"libkpimidentities") );
66 new IdentityManagerAdaptor(
this );
67 QDBusConnection dbus = QDBusConnection::sessionBus();
68 const QString dbusPath = newDBusObjectName();
69 setProperty(
"uniqueDBusPath", dbusPath );
70 const QString dbusInterface = QLatin1String(
"org.kde.pim.IdentityManager");
71 dbus.registerObject( dbusPath,
this );
72 dbus.connect( QString(), QString(), dbusInterface, QLatin1String(
"identitiesChanged"),
this,
73 SLOT(slotIdentitiesChanged(QString)) );
76 mConfig =
new KConfig( QLatin1String(
"emailidentities") );
77 readConfig( mConfig );
79 kDebug( 5325 ) <<
"emailidentities is empty -> convert from kmailrc";
83 KConfig kmailConf( QLatin1String(
"kmailrc") );
84 readConfig( &kmailConf );
88 kDebug( 5325 ) <<
"IdentityManager: No identity found. Creating default.";
93 KConfig kmailConf( QLatin1String(
"kmail2rc") );
95 bool needCommit =
false;
96 if (kmailConf.hasGroup(QLatin1String(
"Composer"))) {
97 KConfigGroup composerGroup = kmailConf.group(QLatin1String(
"Composer"));
98 if (composerGroup.hasKey(QLatin1String(
"pgp-auto-sign"))) {
99 const bool pgpAutoSign = composerGroup.readEntry(QLatin1String(
"pgp-auto-sign"),
false);
101 for ( QList<Identity>::iterator it =
mIdentities.begin(); it != end; ++it ) {
102 it->setPgpAutoSign(pgpAutoSign);
104 composerGroup.deleteEntry(QLatin1String(
"pgp-auto-sign"));
105 composerGroup.sync();
109 if (kmailConf.hasGroup(QLatin1String(
"General"))) {
110 KConfigGroup generalGroup = kmailConf.group(QLatin1String(
"General"));
111 if (generalGroup.hasKey(QLatin1String(
"Default domain"))) {
112 QString defaultDomain = generalGroup.readEntry(QLatin1String(
"Default domain"));
113 if (defaultDomain.isEmpty()) {
114 defaultDomain = QHostInfo::localHostName();
117 for ( QList<Identity>::iterator it =
mIdentities.begin(); it != end; ++it ) {
118 it->setDefaultDomainName(defaultDomain);
120 generalGroup.deleteEntry(QLatin1String(
"Default domain"));
130 if ( KEMailSettings().getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
135 IdentityManager::~IdentityManager()
138 <<
"IdentityManager: There were uncommitted changes!";
145 QString result = name;
147 result = i18nc(
"%1: name; %2: number appended to it to make it unique "
148 "among a list of names",
"%1 #%2",
167 QList<uint> seenUOIDs;
168 QList<Identity>::ConstIterator end =
mIdentities.constEnd();
169 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
171 seenUOIDs << ( *it ).uoid();
174 QList<uint> changedUOIDs;
178 int index = seenUOIDs.indexOf( ( *it ).uoid() );
180 uint uoid = seenUOIDs.at( index );
184 kDebug( 5325 ) <<
"emitting changed() for identity" << uoid;
186 changedUOIDs << uoid;
188 seenUOIDs.removeAll( uoid );
191 kDebug( 5325 ) <<
"emitting added() for identity" << ( *it ).uoid();
197 for ( QList<uint>::ConstIterator it = seenUOIDs.constBegin();
198 it != seenUOIDs.constEnd(); ++it ) {
199 kDebug( 5325 ) <<
"emitting deleted() for identity" << ( *it );
209 QList<uint>::ConstIterator changedEnd( changedUOIDs.constEnd() );
210 for ( QList<uint>::ConstIterator it = changedUOIDs.constBegin();
211 it != changedEnd; ++it ) {
218 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
219 arg( QDBusConnection::sessionBus().baseService() ).
220 arg( property(
"uniqueDBusPath" ).toString() );
221 emit identitiesChanged( ourIdentifier );
240 result << ( *it ).identityName();
251 result << ( *it ).identityName();
261 void IdentityManager::writeConfig()
const
263 const QStringList
identities = groupList( mConfig );
264 QStringList::const_iterator groupEnd = identities.constEnd();
265 for ( QStringList::const_iterator group = identities.constBegin();
266 group != groupEnd; ++group ) {
267 mConfig->deleteGroup( *group );
272 it != end; ++it, ++i ) {
273 KConfigGroup cg( mConfig, QString::fromLatin1(
"Identity #%1" ).arg( i ) );
274 ( *it ).writeConfig( cg );
275 if ( ( *it ).isDefault() ) {
277 KConfigGroup general( mConfig,
"General" );
278 general.writeEntry( configKeyDefaultIdentity, ( *it ).uoid() );
282 es.setSetting( KEMailSettings::RealName, ( *it ).fullName() );
283 es.setSetting( KEMailSettings::EmailAddress, ( *it ).primaryEmailAddress() );
284 es.setSetting( KEMailSettings::Organization, ( *it ).organization() );
285 es.setSetting( KEMailSettings::ReplyToAddress, ( *it ).replyToAddr() );
292 void IdentityManager::readConfig( KConfig *config )
296 const QStringList identities = groupList( config );
297 if ( identities.isEmpty() ) {
301 KConfigGroup general( config,
"General" );
302 uint
defaultIdentity = general.readEntry( configKeyDefaultIdentity, 0 );
303 bool haveDefault =
false;
304 QStringList::const_iterator groupEnd = identities.constEnd();
305 for ( QStringList::const_iterator group = identities.constBegin();
306 group != groupEnd; ++group ) {
307 KConfigGroup configGroup( config, *group );
320 if ( !haveDefault ) {
324 kWarning( 5325 ) <<
"IdentityManager: There was no default identity."
325 <<
"Marking first one as default.";
333 QStringList IdentityManager::groupList( KConfig *config )
const
335 return config->groupList().filter( QRegExp( QLatin1String(
"^Identity #\\d+$") ) );
338 IdentityManager::ConstIterator IdentityManager::begin()
const
343 IdentityManager::ConstIterator IdentityManager::end()
const
353 IdentityManager::Iterator IdentityManager::modifyEnd()
360 for ( ConstIterator it = begin(); it != end(); ++it ) {
361 if ( ( *it ).uoid() == uoid ) {
365 return Identity::null();
379 const QString &addresses )
const
381 const QStringList addressList = KPIMUtils::splitAddressList( addresses );
382 foreach (
const QString &fullAddress, addressList ) {
383 const QString addrSpec = KPIMUtils::extractEmailAddress( fullAddress ).toLower();
384 for ( ConstIterator it = begin(); it != end(); ++it ) {
391 return Identity::null();
401 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
402 if ( ( *it ).identityName() == name ) {
407 kWarning( 5325 ) <<
"IdentityManager::modifyIdentityForName() used as"
408 <<
"newFromScratch() replacement!"
409 << endl <<
" name == \"" << name <<
"\"";
410 return newFromScratch( name );
415 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
416 if ( ( *it ).uoid() == uoid ) {
421 kWarning( 5325 ) <<
"IdentityManager::identityForUoid() used as"
422 <<
"newFromScratch() replacement!"
423 << endl <<
" uoid == \"" << uoid <<
"\"";
424 return newFromScratch( i18n(
"Unnamed" ) );
429 for ( ConstIterator it = begin(); it != end(); ++it ) {
430 if ( ( *it ).isDefault() ) {
436 kFatal( 5325 ) <<
"IdentityManager: No default identity found!";
438 kWarning( 5325 ) <<
"IdentityManager: No default identity found!";
449 if ( ( *it ).uoid() == uoid ) {
460 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
461 ( *it ).setIsDefault( ( *it ).uoid() == uoid );
475 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
476 if ( ( *it ).identityName() == name ) {
477 bool removedWasDefault = ( *it ).isDefault();
490 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
491 if ( ( *it ).identityName() == name ) {
492 bool removedWasDefault = ( *it ).isDefault();
503 Identity &IdentityManager::newFromScratch(
const QString &name )
505 return newFromExisting(
Identity( name ) );
508 Identity &IdentityManager::newFromControlCenter(
const QString &name )
511 es.setProfile( es.defaultProfileName() );
515 es.getSetting( KEMailSettings::RealName ),
516 es.getSetting( KEMailSettings::EmailAddress ),
517 es.getSetting( KEMailSettings::Organization ),
518 es.getSetting( KEMailSettings::ReplyToAddress ) ) );
521 Identity &IdentityManager::newFromExisting(
const Identity &other,
const QString &name )
527 if ( !name.isNull() ) {
533 void IdentityManager::createDefaultIdentity()
535 QString fullName, emailAddress;
542 if ( fullName.isEmpty() && emailAddress.isEmpty() ) {
543 KEMailSettings emailSettings;
544 fullName = emailSettings.getSetting( KEMailSettings::RealName );
545 emailAddress = emailSettings.getSetting( KEMailSettings::EmailAddress );
547 if ( !fullName.isEmpty() && !emailAddress.isEmpty() ) {
548 newFromControlCenter( i18nc(
"use default address from control center",
554 if ( fullName.isEmpty() ) {
555 fullName = user.property( KUser::FullName ).toString();
557 if ( emailAddress.isEmpty() ) {
558 emailAddress = user.loginName();
559 if ( !emailAddress.isEmpty() ) {
560 KConfigGroup general( mConfig,
"General" );
561 QString defaultdomain = general.readEntry(
"Default domain" );
562 if ( !defaultdomain.isEmpty() ) {
563 emailAddress += QLatin1Char(
'@') + defaultdomain;
565 emailAddress.clear();
574 QString name( i18nc(
"Default name for new email accounts/identities.",
"Unnamed" ) );
576 if ( !emailAddress.isEmpty() ) {
578 QString idName = emailAddress;
579 int pos = idName.indexOf( QLatin1Char(
'@') );
581 name = idName.mid( pos + 1, -1 );
585 name.replace( QLatin1Char(
'.'), QLatin1Char(
' ') );
586 pos = name.indexOf( QLatin1Char(
' ') );
588 name[pos + 1] = name[pos + 1].toUpper();
590 name[0] = name[0].toUpper();
591 }
else if ( !fullName.isEmpty() ) {
605 int IdentityManager::newUoid()
610 QList<uint> usedUOIDs;
611 QList<Identity>::ConstIterator end(
mIdentities.constEnd() );
612 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
614 usedUOIDs << ( *it ).uoid();
622 it != endShadow; ++it ) {
623 usedUOIDs << ( *it ).uoid();
631 uoid = KRandom::random();
632 }
while ( usedUOIDs.indexOf( uoid ) != -1 );
640 for ( ConstIterator it = begin(); it != end(); ++it ) {
641 lst << ( *it ).primaryEmailAddress();
642 if ( !( *it ).emailAliases().isEmpty() ) {
643 lst << ( *it ).emailAliases();
649 void KPIMIdentities::IdentityManager::slotRollback()
654 void KPIMIdentities::IdentityManager::slotIdentitiesChanged(
const QString &
id )
656 kDebug( 5325 ) <<
" KPIMIdentities::IdentityManager::slotIdentitiesChanged :" << id;
657 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
658 arg( QDBusConnection::sessionBus().baseService() ).
659 arg( property(
"uniqueDBusPath" ).toString() );
660 if (
id != ourIdentifier ) {
661 mConfig->reparseConfiguration();
662 Q_ASSERT( !hasPendingChanges() );
663 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.
QString primaryEmailAddress() const
primary email address (without the user name - only name@host).
uint uoid() const
Unique Object Identifier for this identity.
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.
void readConfig(const KConfigGroup &)
Read configuration from config.
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...