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(
"/KPIMIDENTITIES_IdentityManager" );
54 name += QString::number( s_count );
63 setObjectName( name );
64 KGlobal::locale()->insertCatalog(
"libkpimidentities" );
65 new IdentityManagerAdaptor(
this );
66 QDBusConnection dbus = QDBusConnection::sessionBus();
67 const QString dbusPath = newDBusObjectName();
68 setProperty(
"uniqueDBusPath", dbusPath );
69 const QString dbusInterface =
"org.kde.pim.IdentityManager";
70 dbus.registerObject( dbusPath,
this );
71 dbus.connect( QString(), QString(), dbusInterface,
"identitiesChanged",
this,
72 SLOT(slotIdentitiesChanged(QString)) );
75 mConfig =
new KConfig(
"emailidentities" );
76 readConfig( mConfig );
78 kDebug( 5325 ) <<
"emailidentities is empty -> convert from kmailrc";
82 KConfig kmailConf(
"kmailrc" );
83 readConfig( &kmailConf );
87 kDebug( 5325 ) <<
"IdentityManager: No identity found. Creating default.";
92 if ( KEMailSettings().getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
97 IdentityManager::~IdentityManager()
100 <<
"IdentityManager: There were uncommitted changes!";
107 QString result = name;
109 result = i18nc(
"%1: name; %2: number appended to it to make it unique "
110 "among a list of names",
"%1 #%2",
129 QList<uint> seenUOIDs;
130 QList<Identity>::ConstIterator end =
mIdentities.constEnd();
131 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
133 seenUOIDs << ( *it ).uoid();
136 QList<uint> changedUOIDs;
140 int index = seenUOIDs.indexOf( ( *it ).uoid() );
142 uint uoid = seenUOIDs.at( index );
146 kDebug( 5325 ) <<
"emitting changed() for identity" << uoid;
148 changedUOIDs << uoid;
150 seenUOIDs.removeAll( uoid );
153 kDebug( 5325 ) <<
"emitting added() for identity" << ( *it ).uoid();
159 for ( QList<uint>::ConstIterator it = seenUOIDs.constBegin();
160 it != seenUOIDs.constEnd(); ++it ) {
161 kDebug( 5325 ) <<
"emitting deleted() for identity" << ( *it );
171 QList<uint>::ConstIterator changedEnd( changedUOIDs.constEnd() );
172 for ( QList<uint>::ConstIterator it = changedUOIDs.constBegin();
173 it != changedEnd; ++it ) {
180 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
181 arg( QDBusConnection::sessionBus().baseService() ).
182 arg( property(
"uniqueDBusPath" ).toString() );
183 emit identitiesChanged( ourIdentifier );
202 result << ( *it ).identityName();
213 result << ( *it ).identityName();
223 void IdentityManager::writeConfig()
const
225 const QStringList
identities = groupList( mConfig );
226 QStringList::const_iterator groupEnd = identities.constEnd();
227 for ( QStringList::const_iterator group = identities.constBegin();
228 group != groupEnd; ++group ) {
229 mConfig->deleteGroup( *group );
234 it != end; ++it, ++i ) {
235 KConfigGroup cg( mConfig, QString::fromLatin1(
"Identity #%1" ).arg( i ) );
236 ( *it ).writeConfig( cg );
237 if ( ( *it ).isDefault() ) {
239 KConfigGroup general( mConfig,
"General" );
240 general.writeEntry( configKeyDefaultIdentity, ( *it ).uoid() );
244 es.setSetting( KEMailSettings::RealName, ( *it ).fullName() );
245 es.setSetting( KEMailSettings::EmailAddress, ( *it ).primaryEmailAddress() );
246 es.setSetting( KEMailSettings::Organization, ( *it ).organization() );
247 es.setSetting( KEMailSettings::ReplyToAddress, ( *it ).replyToAddr() );
254 void IdentityManager::readConfig( KConfig *config )
258 const QStringList identities = groupList( config );
259 if ( identities.isEmpty() ) {
263 KConfigGroup general( config,
"General" );
264 uint
defaultIdentity = general.readEntry( configKeyDefaultIdentity, 0 );
265 bool haveDefault =
false;
266 QStringList::const_iterator groupEnd = identities.constEnd();
267 for ( QStringList::const_iterator group = identities.constBegin();
268 group != groupEnd; ++group ) {
269 KConfigGroup configGroup( config, *group );
278 if ( !haveDefault ) {
279 kWarning( 5325 ) <<
"IdentityManager: There was no default identity."
280 <<
"Marking first one as default.";
288 QStringList IdentityManager::groupList( KConfig *config )
const
290 return config->groupList().filter( QRegExp(
"^Identity #\\d+$" ) );
293 IdentityManager::ConstIterator IdentityManager::begin()
const
298 IdentityManager::ConstIterator IdentityManager::end()
const
308 IdentityManager::Iterator IdentityManager::modifyEnd()
315 for ( ConstIterator it = begin(); it != end(); ++it ) {
316 if ( ( *it ).uoid() == uoid ) {
320 return Identity::null();
334 const QString &addresses )
const
336 const QStringList addressList = KPIMUtils::splitAddressList( addresses );
337 foreach (
const QString &fullAddress, addressList ) {
338 const QString addrSpec = KPIMUtils::extractEmailAddress( fullAddress ).toLower();
339 for ( ConstIterator it = begin(); it != end(); ++it ) {
346 return Identity::null();
356 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
357 if ( ( *it ).identityName() == name ) {
362 kWarning( 5325 ) <<
"IdentityManager::modifyIdentityForName() used as"
363 <<
"newFromScratch() replacement!"
364 << endl <<
" name == \"" << name <<
"\"";
365 return newFromScratch( name );
370 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
371 if ( ( *it ).uoid() == uoid ) {
376 kWarning( 5325 ) <<
"IdentityManager::identityForUoid() used as"
377 <<
"newFromScratch() replacement!"
378 << endl <<
" uoid == \"" << uoid <<
"\"";
379 return newFromScratch( i18n(
"Unnamed" ) );
384 for ( ConstIterator it = begin(); it != end(); ++it ) {
385 if ( ( *it ).isDefault() ) {
391 kFatal( 5325 ) <<
"IdentityManager: No default identity found!";
393 kWarning( 5325 ) <<
"IdentityManager: No default identity found!";
404 if ( ( *it ).uoid() == uoid ) {
415 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
416 ( *it ).setIsDefault( ( *it ).uoid() == uoid );
430 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
431 if ( ( *it ).identityName() == name ) {
432 bool removedWasDefault = ( *it ).isDefault();
445 for ( Iterator it =
modifyBegin(); it != modifyEnd(); ++it ) {
446 if ( ( *it ).identityName() == name ) {
447 bool removedWasDefault = ( *it ).isDefault();
458 Identity &IdentityManager::newFromScratch(
const QString &name )
460 return newFromExisting(
Identity( name ) );
463 Identity &IdentityManager::newFromControlCenter(
const QString &name )
466 es.setProfile( es.defaultProfileName() );
470 es.getSetting( KEMailSettings::RealName ),
471 es.getSetting( KEMailSettings::EmailAddress ),
472 es.getSetting( KEMailSettings::Organization ),
473 es.getSetting( KEMailSettings::ReplyToAddress ) ) );
476 Identity &IdentityManager::newFromExisting(
const Identity &other,
const QString &name )
482 if ( !name.isNull() ) {
488 void IdentityManager::createDefaultIdentity()
490 QString fullName, emailAddress;
497 if ( fullName.isEmpty() && emailAddress.isEmpty() ) {
498 KEMailSettings emailSettings;
499 fullName = emailSettings.getSetting( KEMailSettings::RealName );
500 emailAddress = emailSettings.getSetting( KEMailSettings::EmailAddress );
502 if ( !fullName.isEmpty() && !emailAddress.isEmpty() ) {
503 newFromControlCenter( i18nc(
"use default address from control center",
509 if ( fullName.isEmpty() ) {
510 fullName = user.property( KUser::FullName ).toString();
512 if ( emailAddress.isEmpty() ) {
513 emailAddress = user.loginName();
514 if ( !emailAddress.isEmpty() ) {
515 KConfigGroup general( mConfig,
"General" );
516 QString defaultdomain = general.readEntry(
"Default domain" );
517 if ( !defaultdomain.isEmpty() ) {
518 emailAddress +=
'@' + defaultdomain;
520 emailAddress.clear();
529 QString name( i18nc(
"Default name for new email accounts/identities.",
"Unnamed" ) );
531 if ( !emailAddress.isEmpty() ) {
533 QString idName = emailAddress;
534 int pos = idName.indexOf(
'@' );
536 name = idName.mid( pos + 1, -1 );
540 name.replace(
'.',
' ' );
541 pos = name.indexOf(
' ' );
543 name[pos + 1] = name[pos + 1].toUpper();
545 name[0] = name[0].toUpper();
546 }
else if ( !fullName.isEmpty() ) {
560 int IdentityManager::newUoid()
565 QList<uint> usedUOIDs;
566 QList<Identity>::ConstIterator end(
mIdentities.constEnd() );
567 for ( QList<Identity>::ConstIterator it =
mIdentities.constBegin();
569 usedUOIDs << ( *it ).uoid();
577 it != endShadow; ++it ) {
578 usedUOIDs << ( *it ).uoid();
586 uoid = KRandom::random();
587 }
while ( usedUOIDs.indexOf( uoid ) != -1 );
595 for ( ConstIterator it = begin(); it != end(); ++it ) {
596 lst << ( *it ).primaryEmailAddress();
597 if ( !( *it ).emailAliases().isEmpty() ) {
598 lst << ( *it ).emailAliases();
604 void KPIMIdentities::IdentityManager::slotRollback()
609 void KPIMIdentities::IdentityManager::slotIdentitiesChanged(
const QString &
id )
611 kDebug( 5325 ) <<
" KPIMIdentities::IdentityManager::slotIdentitiesChanged :" << id;
612 const QString ourIdentifier = QString::fromLatin1(
"%1/%2" ).
613 arg( QDBusConnection::sessionBus().baseService() ).
614 arg( property(
"uniqueDBusPath" ).toString() );
615 if (
id != ourIdentifier ) {
616 mConfig->reparseConfiguration();
617 Q_ASSERT( !hasPendingChanges() );
618 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...