make webinterface translatable. there are around 20 short strings, all with context...
[kdenetwork.git] / kopete / libkopete / kopeteidentitymanager.cpp
blob0b5b549b654d07a31be1aea135bd59436e96f587
1 /*
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 *************************************************************************
10 * *
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. *
15 * *
16 *************************************************************************
19 #include "kopeteidentitymanager.h"
20 #include "kopeteidentity.h"
22 #include <QApplication>
23 #include <KDebug>
24 #include <KConfigGroup>
25 #include <KGlobal>
26 #include <KSharedConfig>
27 #include <KSharedConfigPtr>
28 #include <KLocale>
31 namespace Kopete {
33 class IdentityManager::Private
35 public:
36 Private()
38 defaultIdentity = 0;
40 Identity::List identities;
41 Identity * defaultIdentity;
44 IdentityManager * IdentityManager::s_self = 0L;
46 IdentityManager * IdentityManager::self()
48 if ( !s_self )
49 s_self = new IdentityManager;
51 return s_self;
55 IdentityManager::IdentityManager()
56 : QObject( qApp )
58 setObjectName( "KopeteIdentityManager" );
59 d = new Private;
63 IdentityManager::~IdentityManager()
65 s_self = 0L;
67 delete d;
70 void IdentityManager::setOnlineStatus( uint category , const Kopete::StatusMessage &statusMessage, uint flags )
72 Q_UNUSED(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 ) )
83 return identity;
85 // If this identity already exists, do nothing
86 foreach( Identity *currident, d->identities )
88 if ( identity->id() == currident->id() )
90 identity->deleteLater();
91 return 0L;
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 );
105 return 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 )
126 return identity;
128 return 0L;
131 Identity *IdentityManager::defaultIdentity()
133 Identity *ident = 0;
135 if (d->defaultIdentity)
136 ident = findIdentity(d->defaultIdentity->id());
138 if (ident)
139 return ident;
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 );
150 if (ident)
151 return ident;
153 else
155 // use any identity available
156 ident = d->identities.first();
157 setDefaultIdentity( ident );
158 return ident;
160 return 0;
163 void IdentityManager::setDefaultIdentity( Identity *identity )
165 Q_ASSERT(identity);
167 // if the default identity didn't change, just return
168 if (identity == d->defaultIdentity)
169 return;
171 // if the given identity is not registered, does nothing
172 if (d->identities.indexOf( identity ) == -1)
173 return;
175 d->defaultIdentity = identity;
176 save();
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();
189 configgroup->sync();
190 if (d->defaultIdentity == identity) {
191 d->defaultIdentity = 0;
193 delete identity;
196 void IdentityManager::save()
198 // save the default identity
199 KConfigGroup group = KGlobal::config()->group("IdentityManager");
200 group.writeEntry("DefaultIdentity", d->defaultIdentity->id());
202 //kDebug( 14010 );
203 foreach( Identity *identity, d->identities )
205 KConfigGroup *config = identity->configGroup();
207 config->writeEntry( "Id", identity->id() );
208 config->writeEntry( "Label", identity->label() );
209 identity->save();
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 ) );
229 if ( !identity )
231 kWarning( 14010 ) <<
232 "Failed to create identity for '" << identityId << "'" << endl;
233 continue;
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
246 defaultIdentity();
249 void IdentityManager::slotIdentityOnlineStatusChanged(Identity *i)
251 //TODO: check if we need to do something more on status changes
252 //kDebug(14010);
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;