• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kopete/libkopete

  • sources
  • kde-4.14
  • kdenetwork
  • kopete
  • libkopete
kopeteidentitymanager.cpp
Go to the documentation of this file.
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 <KGlobal>
26 #include <KSharedConfig>
27 #include <KSharedConfigPtr>
28 #include <KLocale>
29 
30 
31 namespace Kopete {
32 
33 class IdentityManager::Private
34 {
35 public:
36  Private()
37  {
38  defaultIdentity = 0;
39  }
40  Identity::List identities;
41  Identity * defaultIdentity;
42 };
43 
44 IdentityManager * IdentityManager::s_self = 0L;
45 
46 IdentityManager * IdentityManager::self()
47 {
48  if ( !s_self )
49  s_self = new IdentityManager;
50 
51  return s_self;
52 }
53 
54 
55 IdentityManager::IdentityManager()
56 : QObject( qApp ), d(new Private())
57 {
58  setObjectName( "KopeteIdentityManager" );
59 }
60 
61 
62 IdentityManager::~IdentityManager()
63 {
64  s_self = 0L;
65 
66  delete d;
67 }
68 
69 void IdentityManager::setOnlineStatus( uint category , const Kopete::StatusMessage &statusMessage, uint flags )
70 {
71  Q_UNUSED(flags);
72  foreach( Identity *identity , d->identities )
73  {
74  if ( !identity->excludeConnect() )
75  identity->setOnlineStatus( category , statusMessage );
76  }
77 }
78 
79 Identity* IdentityManager::registerIdentity( Identity *identity )
80 {
81  if( !identity || d->identities.contains( identity ) )
82  return identity;
83 
84  // If this identity already exists, do nothing
85  foreach( Identity *currident, d->identities )
86  {
87  if ( identity->id() == currident->id() )
88  {
89  identity->deleteLater();
90  return 0L;
91  }
92  }
93 
94  d->identities.append( identity );
95 
96  // Connect to the identity's status changed signal
97  connect(identity, SIGNAL(onlineStatusChanged(Kopete::Identity*)),
98  this, SLOT(slotIdentityOnlineStatusChanged(Kopete::Identity*)));
99 
100  connect(identity, SIGNAL(identityDestroyed(const Kopete::Identity*)) , this, SLOT(unregisterIdentity(const Kopete::Identity*)));
101 
102  emit identityRegistered( identity );
103 
104  return identity;
105 }
106 
107 void IdentityManager::unregisterIdentity( const Identity *identity )
108 {
109  kDebug( 14010 ) << "Unregistering identity " << identity->id();
110  d->identities.removeAll( const_cast<Identity*>(identity) );
111 
112  emit identityUnregistered( identity );
113 }
114 
115 const Identity::List& IdentityManager::identities() const
116 {
117  return d->identities;
118 }
119 
120 Identity *IdentityManager::findIdentity( const QString &id )
121 {
122  foreach( Identity *identity , d->identities )
123  {
124  if ( identity->id() == id )
125  return identity;
126  }
127  return 0L;
128 }
129 
130 Identity *IdentityManager::defaultIdentity()
131 {
132  Identity *ident = 0;
133 
134  if (d->defaultIdentity)
135  ident = findIdentity(d->defaultIdentity->id());
136 
137  if (ident)
138  return ident;
139 
140  // if the identity set as the default identity does not exist, try using another one
141 
142  // if there is no identity registered, create a default identity
143  if (!d->defaultIdentity)
144  {
145  ident = new Identity(i18nc("Label for the default identity, used by users to group their instant messaging accounts", "Default Identity"));
146  ident = registerIdentity(ident);
147  emit defaultIdentityChanged( ident );
148  setDefaultIdentity( ident );
149  if (ident)
150  return ident;
151  }
152  else
153  {
154  // use any identity available
155  ident = d->identities.first();
156  setDefaultIdentity( ident );
157  return ident;
158  }
159  return 0;
160 }
161 
162 void IdentityManager::setDefaultIdentity( Identity *identity )
163 {
164  Q_ASSERT(identity);
165 
166  // if the default identity didn't change, just return
167  if (identity == d->defaultIdentity)
168  return;
169 
170  // if the given identity is not registered, does nothing
171  if (d->identities.indexOf( identity ) == -1)
172  return;
173 
174  d->defaultIdentity = identity;
175  save();
176  emit defaultIdentityChanged( identity );
177 }
178 
179 void IdentityManager::removeIdentity( Identity *identity )
180 {
181  KConfigGroup *configgroup = identity->configGroup();
182 
183  // Clean up the identity list
184  d->identities.removeAll( identity );
185 
186  // Clean up configuration
187  configgroup->deleteGroup();
188  configgroup->sync();
189  if (d->defaultIdentity == identity) {
190  d->defaultIdentity = 0;
191  }
192  delete identity;
193 }
194 
195 void IdentityManager::save()
196 {
197  // save the default identity
198  KConfigGroup group = KGlobal::config()->group("IdentityManager");
199  group.writeEntry("DefaultIdentity", d->defaultIdentity->id());
200 
201  //kDebug( 14010 );
202  foreach( Identity *identity, d->identities )
203  {
204  KConfigGroup *config = identity->configGroup();
205 
206  config->writeEntry( "Id", identity->id() );
207  config->writeEntry( "Label", identity->label() );
208  identity->save();
209  }
210 
211  KGlobal::config()->sync();
212 }
213 
214 void IdentityManager::load()
215 {
216  // Iterate over all groups that start with "Identity_" as those are identities.
217  KSharedConfig::Ptr config = KGlobal::config();
218 
219  QStringList identityGroups = config->groupList().filter( QRegExp( QString::fromLatin1( "^Identity_" ) ) );
220  for ( QStringList::Iterator it = identityGroups.begin(); it != identityGroups.end(); ++it )
221  {
222  KConfigGroup cg( config, *it );
223 
224  QString identityId = cg.readEntry( "Id" );
225  QString label = cg.readEntry( "Label" );
226 
227  Identity *identity = registerIdentity( new Identity( identityId, label ) );
228  if ( !identity )
229  {
230  kWarning( 14010 ) <<
231  "Failed to create identity for '" << identityId << "'" << endl;
232  continue;
233  }
234  kDebug() << "Created identity " << identityId;
235  }
236 
237  // get the default identity
238  KConfigGroup group = config->group("IdentityManager");
239  Identity * storedDefault = findIdentity( group.readEntry("DefaultIdentity", QString()) );
240  if ( storedDefault ) {
241  d->defaultIdentity = storedDefault;
242  }
243 
244  // just to make sure the default identity gets created when there is no identity registered
245  defaultIdentity();
246 }
247 
248 void IdentityManager::slotIdentityOnlineStatusChanged(Identity *i)
249 {
250  //TODO: check if we need to do something more on status changes
251  //kDebug(14010);
252  emit identityOnlineStatusChanged(i);
253 }
254 
255 } //END namespace Kopete
256 
257 #include "kopeteidentitymanager.moc"
258 // vim: set noet ts=4 sts=4 sw=4:
259 // kate: tab-width 4; indent-mode csands;
Kopete::Identity::List
QList< Identity * > List
Definition: kopeteidentity.h:45
Kopete::Identity::excludeConnect
bool excludeConnect() const
This identity should be connected when connect all is called?
Definition: kopeteidentity.cpp:107
Kopete::Identity::label
QString label() const
The label is used to identify the identity in the UI.
Definition: kopeteidentity.cpp:96
Kopete::IdentityManager::defaultIdentity
Identity * defaultIdentity()
Returs the default identity to be used.
Definition: kopeteidentitymanager.cpp:130
kopeteidentitymanager.h
Kopete::StatusMessage
This class encapsulate a status message.
Definition: kopetestatusmessage.h:48
Kopete::IdentityManager::~IdentityManager
~IdentityManager()
Definition: kopeteidentitymanager.cpp:62
Kopete::IdentityManager::registerIdentity
Identity * registerIdentity(Identity *identity)
Register the identity.
Definition: kopeteidentitymanager.cpp:79
Kopete::Identity::save
void save()
Save the identity information.
Definition: kopeteidentity.cpp:242
Kopete::IdentityManager::setOnlineStatus
void setOnlineStatus(uint category, const Kopete::StatusMessage &statusMessage=Kopete::StatusMessage(), uint flags=0)
Set all identities a status in the specified category.
Definition: kopeteidentitymanager.cpp:69
Kopete::Identity::configGroup
KConfigGroup * configGroup() const
Returns the KConfigGroup that should be used to read/write settings of this identity.
Definition: kopeteidentity.cpp:229
Kopete::IdentityManager::setDefaultIdentity
void setDefaultIdentity(Identity *ident)
Sets a new default identity.
Definition: kopeteidentitymanager.cpp:162
QRegExp
Kopete::IdentityManager::defaultIdentityChanged
void defaultIdentityChanged(Kopete::Identity *identity)
Signals when the default identity has changed.
Kopete::IdentityManager::load
void load()
Definition: kopeteidentitymanager.cpp:214
Kopete::IdentityManager::removeIdentity
void removeIdentity(Identity *identity)
Delete the identity and clean the config data.
Definition: kopeteidentitymanager.cpp:179
Kopete::IdentityManager::identities
const Identity::List & identities() const
Retrieve the list of identities.
Definition: kopeteidentitymanager.cpp:115
QObject
Kopete::IdentityManager
IdentityManager manages all defined identities in Kopete.
Definition: kopeteidentitymanager.h:39
QObject::setObjectName
void setObjectName(const QString &name)
QList::Iterator
typedef Iterator
QObject::deleteLater
void deleteLater()
QString
QList< Identity * >
QStringList
QList::end
iterator end()
Kopete::IdentityManager::identityUnregistered
void identityUnregistered(const Kopete::Identity *identity)
Signals when an identity has been unregistered.
Kopete::IdentityManager::identityOnlineStatusChanged
void identityOnlineStatusChanged(Kopete::Identity *identity)
Kopete::Identity::id
QString id() const
The id is a unique internal handle and should not be exposed in the UI.
Definition: kopeteidentity.cpp:91
Kopete::IdentityManager::self
static IdentityManager * self()
Retrieve the instance of IdentityManager.
Definition: kopeteidentitymanager.cpp:46
Kopete::IdentityManager::identityRegistered
void identityRegistered(Kopete::Identity *identity)
Signals when an identity is ready for use.
Kopete::IdentityManager::findIdentity
Identity * findIdentity(const QString &identityId)
Return the identity asked.
Definition: kopeteidentitymanager.cpp:120
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QStringList::filter
QStringList filter(const QString &str, Qt::CaseSensitivity cs) const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Kopete::Identity
Definition: kopeteidentity.h:41
Kopete::IdentityManager::save
void save()
Definition: kopeteidentitymanager.cpp:195
kopeteidentity.h
QList::begin
iterator begin()
Kopete::Identity::setOnlineStatus
void setOnlineStatus(uint category, const Kopete::StatusMessage &statusMessage)
Sets the online status for this identity Sets the online status for each account in this identity...
Definition: kopeteidentity.cpp:113
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/libkopete

Skip menu "kopete/libkopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal