1 /*
2 kopeteidentitymanager.cpp - Kopete Identity Manager
3
4 Copyright (c) 2007 by Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
5 Copyright (c) 2007 Will Stephenson <wstephenson@kde.org>
6
7 Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
8
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 *************************************************************************
17 */
18
19 #include "kopeteidentitymanager.h"
20 #include "kopeteidentity.h"
21
22 #include <QApplication>
23 #include <KDebug>
24 #include <KConfigGroup>
25 #include <KSharedConfig>
26 #include <KSharedConfigPtr>
27 #include <KLocalizedString>
28
29 namespace Kopete {
30 class IdentityManager::Private
31 {
32 public:
Private()33 Private()
34 {
35 defaultIdentity = 0;
36 }
37
38 Identity::List identities;
39 Identity *defaultIdentity;
40 };
41
42 IdentityManager *IdentityManager::s_self = nullptr;
43
self()44 IdentityManager *IdentityManager::self()
45 {
46 if (!s_self) {
47 s_self = new IdentityManager;
48 }
49
50 return s_self;
51 }
52
IdentityManager()53 IdentityManager::IdentityManager()
54 : QObject(qApp)
55 , d(new Private())
56 {
57 setObjectName(QStringLiteral("KopeteIdentityManager"));
58 }
59
~IdentityManager()60 IdentityManager::~IdentityManager()
61 {
62 s_self = nullptr;
63
64 delete d;
65 }
66
setOnlineStatus(uint category,const Kopete::StatusMessage & statusMessage,uint flags)67 void IdentityManager::setOnlineStatus(uint category, const Kopete::StatusMessage &statusMessage, uint flags)
68 {
69 Q_UNUSED(flags);
70 foreach (Identity *identity, d->identities) {
71 if (!identity->excludeConnect()) {
72 identity->setOnlineStatus(category, statusMessage);
73 }
74 }
75 }
76
registerIdentity(Identity * identity)77 Identity *IdentityManager::registerIdentity(Identity *identity)
78 {
79 if (!identity || d->identities.contains(identity)) {
80 return identity;
81 }
82
83 // If this identity already exists, do nothing
84 foreach (Identity *currident, d->identities) {
85 if (identity->id() == currident->id()) {
86 identity->deleteLater();
87 return nullptr;
88 }
89 }
90
91 d->identities.append(identity);
92
93 // Connect to the identity's status changed signal
94 connect(identity, SIGNAL(onlineStatusChanged(Kopete::Identity*)),
95 this, SLOT(slotIdentityOnlineStatusChanged(Kopete::Identity*)));
96
97 connect(identity, SIGNAL(identityDestroyed(const Kopete::Identity*)), this, SLOT(unregisterIdentity(const Kopete::Identity*)));
98
99 emit identityRegistered(identity);
100
101 return identity;
102 }
103
unregisterIdentity(const Identity * identity)104 void IdentityManager::unregisterIdentity(const Identity *identity)
105 {
106 qCDebug(LIBKOPETE_LOG) << "Unregistering identity " << identity->id();
107 d->identities.removeAll(const_cast<Identity *>(identity));
108
109 emit identityUnregistered(identity);
110 }
111
identities() const112 const Identity::List &IdentityManager::identities() const
113 {
114 return d->identities;
115 }
116
findIdentity(const QString & id)117 Identity *IdentityManager::findIdentity(const QString &id)
118 {
119 foreach (Identity *identity, d->identities) {
120 if (identity->id() == id) {
121 return identity;
122 }
123 }
124 return nullptr;
125 }
126
defaultIdentity()127 Identity *IdentityManager::defaultIdentity()
128 {
129 Identity *ident = nullptr;
130
131 if (d->defaultIdentity) {
132 ident = findIdentity(d->defaultIdentity->id());
133 }
134
135 if (ident) {
136 return ident;
137 }
138
139 // if the identity set as the default identity does not exist, try using another one
140
141 // if there is no identity registered, create a default identity
142 if (!d->defaultIdentity) {
143 ident = new Identity(i18nc("Label for the default identity, used by users to group their instant messaging accounts", "Default Identity"));
144 ident = registerIdentity(ident);
145 emit defaultIdentityChanged(ident);
146 setDefaultIdentity(ident);
147 if (ident) {
148 return ident;
149 }
150 } else {
151 // use any identity available
152 ident = d->identities.first();
153 setDefaultIdentity(ident);
154 return ident;
155 }
156 return 0;
157 }
158
setDefaultIdentity(Identity * identity)159 void IdentityManager::setDefaultIdentity(Identity *identity)
160 {
161 Q_ASSERT(identity);
162
163 // if the default identity didn't change, just return
164 if (identity == d->defaultIdentity) {
165 return;
166 }
167
168 // if the given identity is not registered, does nothing
169 if (d->identities.indexOf(identity) == -1) {
170 return;
171 }
172
173 d->defaultIdentity = identity;
174 save();
175 emit defaultIdentityChanged(identity);
176 }
177
removeIdentity(Identity * identity)178 void IdentityManager::removeIdentity(Identity *identity)
179 {
180 KConfigGroup *configgroup = identity->configGroup();
181
182 // Clean up the identity list
183 d->identities.removeAll(identity);
184
185 // Clean up configuration
186 configgroup->deleteGroup();
187 configgroup->sync();
188 if (d->defaultIdentity == identity) {
189 d->defaultIdentity = nullptr;
190 }
191 delete identity;
192 }
193
save()194 void IdentityManager::save()
195 {
196 // save the default identity
197 KConfigGroup group = KSharedConfig::openConfig()->group("IdentityManager");
198 group.writeEntry("DefaultIdentity", d->defaultIdentity->id());
199
200 //qCDebug(LIBKOPETE_LOG);
201 foreach (Identity *identity, d->identities) {
202 KConfigGroup *config = identity->configGroup();
203
204 config->writeEntry("Id", identity->id());
205 config->writeEntry("Label", identity->label());
206 identity->save();
207 }
208
209 KSharedConfig::openConfig()->sync();
210 }
211
load()212 void IdentityManager::load()
213 {
214 // Iterate over all groups that start with "Identity_" as those are identities.
215 KSharedConfig::Ptr config = KSharedConfig::openConfig();
216
217 QStringList identityGroups = config->groupList().filter(QRegExp(QLatin1String("^Identity_")));
218 for (QStringList::Iterator it = identityGroups.begin(); it != identityGroups.end(); ++it) {
219 KConfigGroup cg(config, *it);
220
221 QString identityId = cg.readEntry("Id");
222 QString label = cg.readEntry("Label");
223
224 Identity *identity = registerIdentity(new Identity(identityId, label));
225 if (!identity) {
226 qCWarning(LIBKOPETE_LOG)
227 <<"Failed to create identity for '" << identityId << "'" << endl;
228 continue;
229 }
230 qCDebug(LIBKOPETE_LOG) << "Created identity " << identityId;
231 }
232
233 // get the default identity
234 KConfigGroup group = config->group("IdentityManager");
235 Identity *storedDefault = findIdentity(group.readEntry("DefaultIdentity", QString()));
236 if (storedDefault) {
237 d->defaultIdentity = storedDefault;
238 }
239
240 // just to make sure the default identity gets created when there is no identity registered
241 defaultIdentity();
242 }
243
slotIdentityOnlineStatusChanged(Identity * i)244 void IdentityManager::slotIdentityOnlineStatusChanged(Identity *i)
245 {
246 //TODO: check if we need to do something more on status changes
247 //qCDebug(LIBKOPETE_LOG);
248 emit identityOnlineStatusChanged(i);
249 }
250 } //END namespace Kopete
251
252 // vim: set noet ts=4 sts=4 sw=4:
253 // kate: tab-width 4; indent-mode csands;
254