2 kopeteidentitymanager.cpp - Kopete Identity Manager
4 Copyright (c) 2007 by Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
5 Copyright (c) 2007 Will Stephenson <wstephenson@kde.org>
7 Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
9 *************************************************************************
11 * This library is free software; you can redistribute it and/or *
12 * modify it under the terms of the GNU Lesser General Public *
13 * License as published by the Free Software Foundation; either *
14 * version 2 of the License, or (at your option) any later version. *
16 *************************************************************************
19 #include "kopeteidentitymanager.h"
20 #include "kopeteidentity.h"
22 #include <QApplication>
24 #include <KConfigGroup>
26 #include <KSharedConfig>
27 #include <KSharedConfigPtr>
33 class IdentityManager::Private
40 Identity::List identities
;
41 Identity
* defaultIdentity
;
44 IdentityManager
* IdentityManager::s_self
= 0L;
46 IdentityManager
* IdentityManager::self()
49 s_self
= new IdentityManager
;
55 IdentityManager::IdentityManager()
58 setObjectName( "KopeteIdentityManager" );
63 IdentityManager::~IdentityManager()
70 void IdentityManager::setOnlineStatus( uint category
, const Kopete::StatusMessage
&statusMessage
, uint flags
)
73 foreach( Identity
*identity
, d
->identities
)
75 if ( !identity
->excludeConnect() )
76 identity
->setOnlineStatus( category
, statusMessage
);
80 Identity
* IdentityManager::registerIdentity( Identity
*identity
)
82 if( !identity
|| d
->identities
.contains( identity
) )
85 // If this identity already exists, do nothing
86 foreach( Identity
*currident
, d
->identities
)
88 if ( identity
->id() == currident
->id() )
90 identity
->deleteLater();
95 d
->identities
.append( identity
);
97 // Connect to the identity's status changed signal
98 connect(identity
, SIGNAL(onlineStatusChanged(Kopete::Identity
*)),
99 this, SLOT(slotIdentityOnlineStatusChanged(Kopete::Identity
*)));
101 connect(identity
, SIGNAL(identityDestroyed(const Kopete::Identity
*)) , this, SLOT( unregisterIdentity(const Kopete::Identity
*) ));
103 emit
identityRegistered( identity
);
108 void IdentityManager::unregisterIdentity( const Identity
*identity
)
110 kDebug( 14010 ) << "Unregistering identity " << identity
->id();
111 d
->identities
.removeAll( const_cast<Identity
*>(identity
) );
113 emit
identityUnregistered( identity
);
116 const Identity::List
& IdentityManager::identities() const
118 return d
->identities
;
121 Identity
*IdentityManager::findIdentity( const QString
&id
)
123 foreach( Identity
*identity
, d
->identities
)
125 if ( identity
->id() == id
)
131 Identity
*IdentityManager::defaultIdentity()
135 if (d
->defaultIdentity
)
136 ident
= findIdentity(d
->defaultIdentity
->id());
141 // if the identity set as the default identity does not exist, try using another one
143 // if there is no identity registered, create a default identity
144 if (!d
->defaultIdentity
)
146 ident
= new Identity(i18nc("Label for the default identity, used by users to group their instant messaging accounts", "Default Identity"));
147 ident
= registerIdentity(ident
);
148 emit
defaultIdentityChanged( ident
);
149 setDefaultIdentity( ident
);
155 // use any identity available
156 ident
= d
->identities
.first();
157 setDefaultIdentity( ident
);
163 void IdentityManager::setDefaultIdentity( Identity
*identity
)
167 // if the default identity didn't change, just return
168 if (identity
== d
->defaultIdentity
)
171 // if the given identity is not registered, does nothing
172 if (d
->identities
.indexOf( identity
) == -1)
175 d
->defaultIdentity
= identity
;
177 emit
defaultIdentityChanged( identity
);
180 void IdentityManager::removeIdentity( Identity
*identity
)
182 KConfigGroup
*configgroup
= identity
->configGroup();
184 // Clean up the identity list
185 d
->identities
.removeAll( identity
);
187 // Clean up configuration
188 configgroup
->deleteGroup();
190 if (d
->defaultIdentity
== identity
) {
191 d
->defaultIdentity
= 0;
196 void IdentityManager::save()
198 // save the default identity
199 KConfigGroup group
= KGlobal::config()->group("IdentityManager");
200 group
.writeEntry("DefaultIdentity", d
->defaultIdentity
->id());
203 foreach( Identity
*identity
, d
->identities
)
205 KConfigGroup
*config
= identity
->configGroup();
207 config
->writeEntry( "Id", identity
->id() );
208 config
->writeEntry( "Label", identity
->label() );
212 KGlobal::config()->sync();
215 void IdentityManager::load()
217 // Iterate over all groups that start with "Identity_" as those are identities.
218 KSharedConfig::Ptr config
= KGlobal::config();
220 QStringList identityGroups
= config
->groupList().filter( QRegExp( QString::fromLatin1( "^Identity_" ) ) );
221 for ( QStringList::Iterator it
= identityGroups
.begin(); it
!= identityGroups
.end(); ++it
)
223 KConfigGroup
cg( config
, *it
);
225 QString identityId
= cg
.readEntry( "Id" );
226 QString label
= cg
.readEntry( "Label" );
228 Identity
*identity
= registerIdentity( new Identity( identityId
, label
) );
232 "Failed to create identity for '" << identityId
<< "'" << endl
;
235 kDebug() << "Created identity " << identityId
;
238 // get the default identity
239 KConfigGroup group
= config
->group("IdentityManager");
240 Identity
* storedDefault
= findIdentity( group
.readEntry("DefaultIdentity", QString()) );
241 if ( storedDefault
) {
242 d
->defaultIdentity
= storedDefault
;
245 // just to make sure the default identity gets created when there is no identity registered
249 void IdentityManager::slotIdentityOnlineStatusChanged(Identity
*i
)
251 //TODO: check if we need to do something more on status changes
253 emit
identityOnlineStatusChanged(i
);
256 } //END namespace Kopete
258 #include "kopeteidentitymanager.moc"
259 // vim: set noet ts=4 sts=4 sw=4:
260 // kate: tab-width 4; indent-mode csands;