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"
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 );
316 if ( !haveDefault ) {
317 kWarning( 5325 ) <<
"IdentityManager: There was no default identity."
318 <<
"Marking first one as default.";
326 QStringList IdentityManager::groupList( KConfig *config )
const
328 return config->groupList().filter( QRegExp( QLatin1String(
"^Identity #\\d+$") ) );
331 IdentityManager::ConstIterator IdentityManager::begin()
const
336 IdentityManager::ConstIterator IdentityManager::end()
const
346 IdentityManager::Iterator IdentityManager::modifyEnd()
353 for ( ConstIterator it = begin(); it != end(); ++it ) {
354 if ( ( *it ).uoid() == uoid ) {
358 return Identity::null();
372 const QString &addresses )
const
374 const QStringList addressList = KPIMUtils::splitAddressList( addresses );
375 foreach (
const QString &fullAddress, addressList ) {
376 const QString addrSpec = KPIMUtils::extractEmailAddress( fullAddress ).toLower();
377 for ( ConstIterator it = begin(); it != end(); ++it ) {
384 return Identity::null();
394 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
395 if ( ( *it ).identityName() == name ) {
400 kWarning( 5325 ) <<
"IdentityManager::modifyIdentityForName() used as"
401 <<
"newFromScratch() replacement!"
402 << endl <<
" name == \"" << name <<
"\"";
403 return newFromScratch( name );
408 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
409 if ( ( *it ).uoid() == uoid ) {
414 kWarning( 5325 ) <<
"IdentityManager::identityForUoid() used as"
415 <<
"newFromScratch() replacement!"
416 << endl <<
" uoid == \"" << uoid <<
"\"";
417 return newFromScratch( i18n(
"Unnamed" ) );
422 for ( ConstIterator it = begin(); it != end(); ++it ) {
423 if ( ( *it ).isDefault() ) {
429 kFatal( 5325 ) <<
"IdentityManager: No default identity found!";
431 kWarning( 5325 ) <<
"IdentityManager: No default identity found!";
442 if ( ( *it ).uoid() == uoid ) {
453 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
454 ( *it ).setIsDefault( ( *it ).uoid() == uoid );
468 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
469 if ( ( *it ).identityName() == name ) {
470 bool removedWasDefault = ( *it ).isDefault();
483 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
484 if ( ( *it ).identityName() == name ) {
485 bool removedWasDefault = ( *it ).isDefault();
496 Identity &IdentityManager::newFromScratch(
const QString &name )
498 return newFromExisting(
Identity( name ) );
501 Identity &IdentityManager::newFromControlCenter(
const QString &name )
504 es.setProfile( es.defaultProfileName() );
508 es.getSetting( KEMailSettings::RealName ),
509 es.getSetting( KEMailSettings::EmailAddress ),
510 es.getSetting( KEMailSettings::Organization ),
511 es.getSetting( KEMailSettings::ReplyToAddress ) ) );
514 Identity &IdentityManager::newFromExisting(
const Identity &other,
const QString &name )
520 if ( !name.isNull() ) {
526 void IdentityManager::createDefaultIdentity()
528 QString fullName, emailAddress;
535 if ( fullName.isEmpty() && emailAddress.isEmpty() ) {
536 KEMailSettings emailSettings;
537 fullName = emailSettings.getSetting( KEMailSettings::RealName );
538 emailAddress = emailSettings.getSetting( KEMailSettings::EmailAddress );
540 if ( !fullName.isEmpty() && !emailAddress.isEmpty() ) {
541 newFromControlCenter( i18nc(
"use default address from control center",
547 if ( fullName.isEmpty() ) {
548 fullName = user.property( KUser::FullName ).toString();
550 if ( emailAddress.isEmpty() ) {
551 emailAddress = user.loginName();
552 if ( !emailAddress.isEmpty() ) {
553 KConfigGroup general( mConfig,
"General" );
554 QString defaultdomain = general.readEntry(
"Default domain" );
555 if ( !defaultdomain.isEmpty() ) {
556 emailAddress += QLatin1Char(
'@') + defaultdomain;
558 emailAddress.clear();
567 QString name( i18nc(
"Default name for new email accounts/identities.",
"Unnamed" ) );
569 if ( !emailAddress.isEmpty() ) {
571 QString idName = emailAddress;
572 int pos = idName.indexOf( QLatin1Char(
'@') );
574 name = idName.mid( pos + 1, -1 );
578 name.replace( QLatin1Char(
'.'), QLatin1Char(
' ') );
579 pos = name.indexOf( QLatin1Char(
' ') );
581 name[pos + 1] = name[pos + 1].toUpper();
583 name[0] = name[0].toUpper();
584 }
else if ( !fullName.isEmpty() ) {
598 int IdentityManager::newUoid()
603 QList<uint> usedUOIDs;
604 QList<Identity>::ConstIterator end(
mIdentities.constEnd() );
605 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
607 usedUOIDs << ( *it ).uoid();
615 it != endShadow; ++it ) {
616 usedUOIDs << ( *it ).uoid();
624 uoid = KRandom::random();
625 }
while ( usedUOIDs.indexOf( uoid ) != -1 );
633 for ( ConstIterator it = begin(); it != end(); ++it ) {
634 lst << ( *it ).primaryEmailAddress();
635 if ( !( *it ).emailAliases().isEmpty() ) {
636 lst << ( *it ).emailAliases();
642 void KPIMIdentities::IdentityManager::slotRollback()
647 void KPIMIdentities::IdentityManager::slotIdentitiesChanged(
const QString &
id )
649 kDebug( 5325 ) <<
" KPIMIdentities::IdentityManager::slotIdentitiesChanged :" << id;
650 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
651 arg( QDBusConnection::sessionBus().baseService() ).
652 arg( property(
"uniqueDBusPath" ).toString() );
653 if (
id != ourIdentifier ) {
654 mConfig->reparseConfiguration();
655 Q_ASSERT( !hasPendingChanges() );
656 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...