21 static 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"
48 using namespace KPIMIdentities;
50 static QString newDBusObjectName()
52 static int s_count = 0;
66 KGlobal::locale()->insertCatalog(
QLatin1String(
"libkpimidentities") );
67 new IdentityManagerAdaptor(
this );
69 const QString dbusPath = newDBusObjectName();
74 SLOT(slotIdentitiesChanged(
QString)) );
78 readConfig( mConfig );
80 kDebug( 5325 ) <<
"emailidentities is empty -> convert from 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;
98 KConfigGroup composerGroup = kmailConf->group(
QLatin1String(
"Composer"));
100 const bool pgpAutoSign = composerGroup.readEntry(
QLatin1String(
"pgp-auto-sign"),
false);
103 it->setPgpAutoSign(pgpAutoSign);
106 composerGroup.sync();
111 KConfigGroup generalGroup = kmailConf->group(
QLatin1String(
"General"));
119 it->setDefaultDomainName(defaultDomain);
131 if ( KEMailSettings().getSetting( KEMailSettings::EmailAddress ).isEmpty() ) {
136 IdentityManager::~IdentityManager()
139 <<
"IdentityManager: There were uncommitted changes!";
148 result = i18nc(
"%1: name; %2: number appended to it to make it unique "
149 "among a list of names",
"%1 #%2",
172 seenUOIDs << ( *it ).uoid();
179 int index = seenUOIDs.
indexOf( ( *it ).uoid() );
181 uint uoid = seenUOIDs.
at( index );
185 kDebug( 5325 ) <<
"emitting changed() for identity" << uoid;
187 changedUOIDs << uoid;
192 kDebug( 5325 ) <<
"emitting added() for identity" << ( *it ).uoid();
199 it != seenUOIDs.
constEnd(); ++it ) {
200 kDebug( 5325 ) <<
"emitting deleted() for identity" << ( *it );
212 it != changedEnd; ++it ) {
221 arg(
property(
"uniqueDBusPath" ).toString() );
222 emit identitiesChanged( ourIdentifier );
241 result << ( *it ).identityName();
252 result << ( *it ).identityName();
262 void IdentityManager::writeConfig()
const
267 group != groupEnd; ++group ) {
268 mConfig->deleteGroup( *group );
273 it != end; ++it, ++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() );
293 void 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;
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.";
334 QStringList IdentityManager::groupList( KConfig *config )
const
339 IdentityManager::ConstIterator IdentityManager::begin()
const
344 IdentityManager::ConstIterator IdentityManager::end()
const
354 IdentityManager::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();
506 return newFromExisting(
Identity( 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 ) ) );
534 void IdentityManager::createDefaultIdentity()
536 QString fullName, emailAddress;
544 KEMailSettings emailSettings;
545 fullName = emailSettings.getSetting( KEMailSettings::RealName );
546 emailAddress = emailSettings.getSetting( KEMailSettings::EmailAddress );
549 newFromControlCenter( i18nc(
"use default address from control center",
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() ) {
566 emailAddress.
clear();
575 QString name( i18nc(
"Default name for new email accounts/identities.",
"Unnamed" ) );
577 if ( !emailAddress.
isEmpty() ) {
582 name = idName.
mid( pos + 1, -1 );
589 name[pos + 1] = name[pos + 1].
toUpper();
592 }
else if ( !fullName.
isEmpty() ) {
606 int IdentityManager::newUoid()
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();
650 void KPIMIdentities::IdentityManager::slotRollback()
655 void KPIMIdentities::IdentityManager::slotIdentitiesChanged(
const QString &
id )
657 kDebug( 5325 ) <<
" KPIMIdentities::IdentityManager::slotIdentitiesChanged :" << id;
660 arg( property(
"uniqueDBusPath" ).toString() );
661 if (
id != ourIdentifier ) {
662 mConfig->reparseConfiguration();
663 Q_ASSERT( !hasPendingChanges() );
664 readConfig( mConfig );
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
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.
const T & at(int i) const
bool registerObject(const QString &path, QObject *object, QFlags< QDBusConnection::RegisterOption > options)
bool contains(const QString &str, Qt::CaseSensitivity cs) const
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.
QDBusConnection sessionBus()
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)
int indexOf(const T &value, int from) const
const char * name() const
QString number(int n, int base)
virtual void createDefaultIdentity(QString &, QString &)
This is called when no identity has been defined, so we need to create a default one.
QVariant property(const char *name) const
QStringList identities() const
bool thatIsMe(const QString &addressList) const
void added(const KPIMIdentities::Identity &ident)
Emitted on commit() for each new identity.
void setObjectName(const QString &name)
int removeAll(const T &value)
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...
QString & replace(int position, int n, QChar after)
void setUoid(uint aUoid)
set the uiod
Identity & modifyIdentityForName(const QString &identityName)
QString mid(int position, int n) const
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.
QString fromLatin1(const char *str, int size)
const Identity & defaultIdentity() const
bool setProperty(const char *name, const QVariant &value)
QStringList filter(const QString &str, Qt::CaseSensitivity cs) const
Iterator modifyBegin()
Iterator used by the configuration dialog, which works on a separate list of identities, for modification.
const_iterator constEnd() const
const_iterator constBegin() const
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
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...