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>
44 #include "identitymanageradaptor.h"
46 using namespace KPIMIdentities;
48 static QString newDBusObjectName()
50 static int s_count = 0;
51 QString name( QLatin1String(
"/KPIMIDENTITIES_IdentityManager") );
53 name += QLatin1Char(
'_');
54 name += QString::number( s_count );
63 setObjectName( QLatin1String(name) );
64 KGlobal::locale()->insertCatalog( QLatin1String(
"libkpimidentities") );
65 new IdentityManagerAdaptor(
this );
66 QDBusConnection dbus = QDBusConnection::sessionBus();
67 const QString dbusPath = newDBusObjectName();
68 setProperty(
"uniqueDBusPath", dbusPath );
69 const QString dbusInterface = QLatin1String(
"org.kde.pim.IdentityManager");
70 dbus.registerObject( dbusPath,
this );
71 dbus.connect( QString(), QString(), dbusInterface, QLatin1String(
"identitiesChanged"),
this,
72 SLOT(slotIdentitiesChanged(QString)) );
75 mConfig =
new KConfig( QLatin1String(
"emailidentities") );
76 readConfig( mConfig );
78 kDebug( 5325 ) <<
"emailidentities is empty -> convert from kmailrc";
82 KConfig kmailConf( QLatin1String(
"kmailrc") );
83 readConfig( &kmailConf );
87 kDebug( 5325 ) <<
"IdentityManager: No identity found. Creating default.";
92 KConfig kmailConf( QLatin1String(
"kmail2rc") );
93 if (!mReadOnly && kmailConf.hasGroup(QLatin1String(
"Composer"))) {
94 KConfigGroup composerGroup = kmailConf.group(QLatin1String(
"Composer"));
95 if (composerGroup.hasKey(QLatin1String(
"pgp-auto-sign"))) {
96 composerGroup.deleteEntry(QLatin1String(
"pgp-auto-sign"));
98 const bool pgpAutoSign = composerGroup.readEntry(QLatin1String(
"pgp-auto-sign"),
false);
100 for ( QList<Identity>::iterator it =
mIdentities.begin(); it != end; ++it ) {
101 it->setPgpAutoSign(pgpAutoSign);
108 if ( KEMailSettings().getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
113 IdentityManager::~IdentityManager()
116 <<
"IdentityManager: There were uncommitted changes!";
123 QString result = name;
125 result = i18nc(
"%1: name; %2: number appended to it to make it unique "
126 "among a list of names",
"%1 #%2",
145 QList<uint> seenUOIDs;
146 QList<Identity>::ConstIterator end =
mIdentities.constEnd();
147 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
149 seenUOIDs << ( *it ).uoid();
152 QList<uint> changedUOIDs;
156 int index = seenUOIDs.indexOf( ( *it ).uoid() );
158 uint uoid = seenUOIDs.at( index );
162 kDebug( 5325 ) <<
"emitting changed() for identity" << uoid;
164 changedUOIDs << uoid;
166 seenUOIDs.removeAll( uoid );
169 kDebug( 5325 ) <<
"emitting added() for identity" << ( *it ).uoid();
175 for ( QList<uint>::ConstIterator it = seenUOIDs.constBegin();
176 it != seenUOIDs.constEnd(); ++it ) {
177 kDebug( 5325 ) <<
"emitting deleted() for identity" << ( *it );
187 QList<uint>::ConstIterator changedEnd( changedUOIDs.constEnd() );
188 for ( QList<uint>::ConstIterator it = changedUOIDs.constBegin();
189 it != changedEnd; ++it ) {
196 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
197 arg( QDBusConnection::sessionBus().baseService() ).
198 arg( property(
"uniqueDBusPath" ).toString() );
199 emit identitiesChanged( ourIdentifier );
218 result << ( *it ).identityName();
229 result << ( *it ).identityName();
239 void IdentityManager::writeConfig()
const
241 const QStringList
identities = groupList( mConfig );
242 QStringList::const_iterator groupEnd = identities.constEnd();
243 for ( QStringList::const_iterator group = identities.constBegin();
244 group != groupEnd; ++group ) {
245 mConfig->deleteGroup( *group );
250 it != end; ++it, ++i ) {
251 KConfigGroup cg( mConfig, QString::fromLatin1(
"Identity #%1" ).arg( i ) );
252 ( *it ).writeConfig( cg );
253 if ( ( *it ).isDefault() ) {
255 KConfigGroup general( mConfig,
"General" );
256 general.writeEntry( configKeyDefaultIdentity, ( *it ).uoid() );
260 es.setSetting( KEMailSettings::RealName, ( *it ).fullName() );
261 es.setSetting( KEMailSettings::EmailAddress, ( *it ).primaryEmailAddress() );
262 es.setSetting( KEMailSettings::Organization, ( *it ).organization() );
263 es.setSetting( KEMailSettings::ReplyToAddress, ( *it ).replyToAddr() );
270 void IdentityManager::readConfig( KConfig *config )
274 const QStringList identities = groupList( config );
275 if ( identities.isEmpty() ) {
279 KConfigGroup general( config,
"General" );
280 uint
defaultIdentity = general.readEntry( configKeyDefaultIdentity, 0 );
281 bool haveDefault =
false;
282 QStringList::const_iterator groupEnd = identities.constEnd();
283 for ( QStringList::const_iterator group = identities.constBegin();
284 group != groupEnd; ++group ) {
285 KConfigGroup configGroup( config, *group );
294 if ( !haveDefault ) {
295 kWarning( 5325 ) <<
"IdentityManager: There was no default identity."
296 <<
"Marking first one as default.";
304 QStringList IdentityManager::groupList( KConfig *config )
const
306 return config->groupList().filter( QRegExp( QLatin1String(
"^Identity #\\d+$") ) );
309 IdentityManager::ConstIterator IdentityManager::begin()
const
314 IdentityManager::ConstIterator IdentityManager::end()
const
324 IdentityManager::Iterator IdentityManager::modifyEnd()
331 for ( ConstIterator it = begin(); it != end(); ++it ) {
332 if ( ( *it ).uoid() == uoid ) {
336 return Identity::null();
350 const QString &addresses )
const
352 const QStringList addressList = KPIMUtils::splitAddressList( addresses );
353 foreach (
const QString &fullAddress, addressList ) {
354 const QString addrSpec = KPIMUtils::extractEmailAddress( fullAddress ).toLower();
355 for ( ConstIterator it = begin(); it != end(); ++it ) {
362 return Identity::null();
372 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
373 if ( ( *it ).identityName() == name ) {
378 kWarning( 5325 ) <<
"IdentityManager::modifyIdentityForName() used as"
379 <<
"newFromScratch() replacement!"
380 << endl <<
" name == \"" << name <<
"\"";
381 return newFromScratch( name );
386 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
387 if ( ( *it ).uoid() == uoid ) {
392 kWarning( 5325 ) <<
"IdentityManager::identityForUoid() used as"
393 <<
"newFromScratch() replacement!"
394 << endl <<
" uoid == \"" << uoid <<
"\"";
395 return newFromScratch( i18n(
"Unnamed" ) );
400 for ( ConstIterator it = begin(); it != end(); ++it ) {
401 if ( ( *it ).isDefault() ) {
407 kFatal( 5325 ) <<
"IdentityManager: No default identity found!";
409 kWarning( 5325 ) <<
"IdentityManager: No default identity found!";
420 if ( ( *it ).uoid() == uoid ) {
431 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
432 ( *it ).setIsDefault( ( *it ).uoid() == uoid );
446 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
447 if ( ( *it ).identityName() == name ) {
448 bool removedWasDefault = ( *it ).isDefault();
461 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
462 if ( ( *it ).identityName() == name ) {
463 bool removedWasDefault = ( *it ).isDefault();
474 Identity &IdentityManager::newFromScratch(
const QString &name )
476 return newFromExisting(
Identity( name ) );
479 Identity &IdentityManager::newFromControlCenter(
const QString &name )
482 es.setProfile( es.defaultProfileName() );
486 es.getSetting( KEMailSettings::RealName ),
487 es.getSetting( KEMailSettings::EmailAddress ),
488 es.getSetting( KEMailSettings::Organization ),
489 es.getSetting( KEMailSettings::ReplyToAddress ) ) );
492 Identity &IdentityManager::newFromExisting(
const Identity &other,
const QString &name )
498 if ( !name.isNull() ) {
504 void IdentityManager::createDefaultIdentity()
506 QString fullName, emailAddress;
513 if ( fullName.isEmpty() && emailAddress.isEmpty() ) {
514 KEMailSettings emailSettings;
515 fullName = emailSettings.getSetting( KEMailSettings::RealName );
516 emailAddress = emailSettings.getSetting( KEMailSettings::EmailAddress );
518 if ( !fullName.isEmpty() && !emailAddress.isEmpty() ) {
519 newFromControlCenter( i18nc(
"use default address from control center",
525 if ( fullName.isEmpty() ) {
526 fullName = user.property( KUser::FullName ).toString();
528 if ( emailAddress.isEmpty() ) {
529 emailAddress = user.loginName();
530 if ( !emailAddress.isEmpty() ) {
531 KConfigGroup general( mConfig,
"General" );
532 QString defaultdomain = general.readEntry(
"Default domain" );
533 if ( !defaultdomain.isEmpty() ) {
534 emailAddress += QLatin1Char(
'@') + defaultdomain;
536 emailAddress.clear();
545 QString name( i18nc(
"Default name for new email accounts/identities.",
"Unnamed" ) );
547 if ( !emailAddress.isEmpty() ) {
549 QString idName = emailAddress;
550 int pos = idName.indexOf( QLatin1Char(
'@') );
552 name = idName.mid( pos + 1, -1 );
556 name.replace( QLatin1Char(
'.'), QLatin1Char(
' ') );
557 pos = name.indexOf( QLatin1Char(
' ') );
559 name[pos + 1] = name[pos + 1].toUpper();
561 name[0] = name[0].toUpper();
562 }
else if ( !fullName.isEmpty() ) {
576 int IdentityManager::newUoid()
581 QList<uint> usedUOIDs;
582 QList<Identity>::ConstIterator end(
mIdentities.constEnd() );
583 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
585 usedUOIDs << ( *it ).uoid();
593 it != endShadow; ++it ) {
594 usedUOIDs << ( *it ).uoid();
602 uoid = KRandom::random();
603 }
while ( usedUOIDs.indexOf( uoid ) != -1 );
611 for ( ConstIterator it = begin(); it != end(); ++it ) {
612 lst << ( *it ).primaryEmailAddress();
613 if ( !( *it ).emailAliases().isEmpty() ) {
614 lst << ( *it ).emailAliases();
620 void KPIMIdentities::IdentityManager::slotRollback()
625 void KPIMIdentities::IdentityManager::slotIdentitiesChanged(
const QString &
id )
627 kDebug( 5325 ) <<
" KPIMIdentities::IdentityManager::slotIdentitiesChanged :" << id;
628 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
629 arg( QDBusConnection::sessionBus().baseService() ).
630 arg( property(
"uniqueDBusPath" ).toString() );
631 if (
id != ourIdentifier ) {
632 mConfig->reparseConfiguration();
633 Q_ASSERT( !hasPendingChanges() );
634 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...