make webinterface translatable. there are around 20 short strings, all with context...
[kdenetwork.git] / kopete / libkopete / kopeteidentity.cpp
blob920bb5eae2c5059e256baf8fa27543399dbf48ec
1 /*
2 kopeteidentity.cpp - Kopete Identity
4 Copyright (c) 2007 by Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
5 (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 "kopetepropertycontainer.h"
20 #include "kopeteidentity.h"
21 #include "kopeteaccount.h"
22 #include "kopetecontact.h"
23 #include "kopeteprotocol.h"
25 #include <QStringList>
26 #include <KDebug>
27 #include <KConfigGroup>
28 #include <KGlobal>
29 #include <KSharedConfigPtr>
30 #include <KLocale>
31 #include <KRandom>
32 #include <KMenu>
34 #include <kdeversion.h>
36 namespace Kopete
39 class Identity::Private
41 public:
42 Private(const QString &i, const QString &l) : onlineStatus( OnlineStatus::Unknown )
44 id = i;
45 label = l;
46 configGroup = new KConfigGroup(KGlobal::config(), QString::fromLatin1( "Identity_%1" ).arg( id ));
48 QList<Kopete::Account*> accounts;
49 QString id;
50 QString label;
51 KConfigGroup *configGroup;
52 OnlineStatus::StatusType onlineStatus;
53 Kopete::StatusMessage statusMessage;
56 Identity::Identity( const QString &id, const QString &label )
57 : d( new Private(id, label) )
59 load();
60 connect(this, SIGNAL(propertyChanged(Kopete::PropertyContainer*, const QString&, const QVariant &, const QVariant &)),
61 this, SLOT(slotSaveProperty(Kopete::PropertyContainer*, const QString&, const QVariant &, const QVariant &)));
64 Identity::Identity(const QString &label)
65 : d( new Private(KRandom::randomString(10), label) )
67 connect(this, SIGNAL(propertyChanged(Kopete::PropertyContainer*, const QString&, const QVariant &, const QVariant &)),
68 this, SLOT(slotSaveProperty(Kopete::PropertyContainer*, const QString&, const QVariant &, const QVariant &)));
71 Identity * Identity::clone() const
73 Identity * id = new Identity( label() );
74 QMap<QString,QString> props;
75 serializeProperties( props );
76 id->deserializeProperties( props );
78 connect(id, SIGNAL(propertyChanged(Kopete::PropertyContainer*, const QString&, const QVariant &, const QVariant &)),
79 id, SLOT(slotSaveProperty(Kopete::PropertyContainer*, const QString&, const QVariant &, const QVariant &)));
80 return id;
83 Identity::~Identity()
85 emit identityDestroyed(this);
87 delete d->configGroup;
88 delete d;
91 QString Identity::id() const
93 return d->id;
96 QString Identity::label() const
98 return d->label;
101 void Identity::setLabel(const QString& label)
103 d->label = label;
106 bool Identity::excludeConnect() const
108 //TODO implement
109 return false;
112 void Identity::setOnlineStatus( uint category, const Kopete::StatusMessage &statusMessage )
114 OnlineStatusManager::Categories katgor=(OnlineStatusManager::Categories)category;
116 d->statusMessage = statusMessage;
117 foreach( Account *account , d->accounts )
119 Kopete::OnlineStatus status = OnlineStatusManager::self()->onlineStatus(account->protocol() , katgor);
120 if ( !account->excludeConnect() )
121 account->setOnlineStatus( status, statusMessage );
125 void Identity::setStatusMessage( const Kopete::StatusMessage &statusMessage )
127 d->statusMessage = statusMessage;
128 foreach( Account *account , d->accounts )
130 if ( !account->excludeConnect() )
132 Kopete::Contact *self = account->myself();
133 account->setOnlineStatus( self->onlineStatus(), statusMessage );
138 OnlineStatus::StatusType Identity::onlineStatus() const
140 return d->onlineStatus;
143 Kopete::StatusMessage Identity::statusMessage() const
145 return d->statusMessage;
148 QString Identity::toolTip() const
151 QString tt = QLatin1String("<qt>");
153 QString nick;
154 if ( hasProperty(Kopete::Global::Properties::self()->nickName().key()) )
155 nick = property(Kopete::Global::Properties::self()->nickName()).value().toString();
156 else
157 nick = d->label;
159 tt+= i18nc( "Identity tooltip information: <nobr>ICON <b>NAME</b></nobr><br /><br />",
160 "<nobr><img src=\"kopete-identity-icon:%1\"> <b>%2</b></nobr><br /><br />",
161 QString(QUrl::toPercentEncoding( d->label )), nick );
163 foreach(Account *a, d->accounts)
165 Kopete::Contact *self = a->myself();
166 QString onlineStatus = self ? self->onlineStatus().description() : i18n("Offline");
167 tt += i18nc( "Account tooltip information: <nobr>ICON <b>PROTOCOL:</b> NAME (<i>STATUS</i>)</nobr><br />",
168 "<nobr><img src=\"kopete-account-icon:%3:%4\"> <b>%1:</b> %2 (<i>%5</i>)</nobr><br />",
169 a->protocol()->displayName(), a->accountLabel(), QString(QUrl::toPercentEncoding( a->protocol()->pluginId() )),
170 QString(QUrl::toPercentEncoding( a->accountId() )), onlineStatus );
172 tt += QLatin1String("</qt>");
173 return tt;
176 QString Identity::customIcon() const
178 if (hasProperty( Kopete::Global::Properties::self()->photo().key() ))
179 return property(Kopete::Global::Properties::self()->photo()).value().toString();
180 else
181 return "user-identity";
185 QList<Account*> Identity::accounts() const
187 return d->accounts;
190 void Identity::addAccount( Kopete::Account *account )
192 // check if we already have this account
193 if ( d->accounts.indexOf( account ) != -1 )
194 return;
196 d->accounts.append( account );
198 connect( account->myself(),
199 SIGNAL(onlineStatusChanged(Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus &)),
200 this, SLOT(updateOnlineStatus()));
201 connect(account, SIGNAL(accountDestroyed(const Kopete::Account*)),
202 this, SLOT(removeAccount(const Kopete::Account*)));
204 updateOnlineStatus();
205 emit identityChanged( this );
206 emit toolTipChanged( this );
209 void Identity::removeAccount( const Kopete::Account *account )
211 Kopete::Account *a = const_cast<Kopete::Account*>(account);
212 if ( !d->accounts.contains( a ) )
213 return;
215 disconnect( account );
216 d->accounts.removeAll( a );
217 updateOnlineStatus();
218 emit identityChanged( this );
219 emit toolTipChanged( this );
222 KConfigGroup *Identity::configGroup() const
224 return d->configGroup;
227 void Identity::load()
229 QMap<QString,QString>::iterator it;
231 QMap<QString,QString> props = d->configGroup->entryMap();
232 deserializeProperties(props);
235 void Identity::save()
237 // FIXME: using kconfig for now, but I guess this is going to change
238 QMap<QString,QString> props;
239 serializeProperties(props);
240 QMap<QString,QString>::iterator it;
241 for (it = props.begin(); it != props.end(); ++it)
243 d->configGroup->writeEntry(it.key(), it.value());
247 void Identity::updateOnlineStatus()
249 Kopete::OnlineStatus mostSignificantStatus;
250 Kopete::OnlineStatus::StatusType newStatus = Kopete::OnlineStatus::Unknown;
252 foreach(Account *account, d->accounts)
254 // find most significant status
255 if ( account->myself() &&
256 account->myself()->onlineStatus() > mostSignificantStatus )
257 mostSignificantStatus = account->myself()->onlineStatus();
260 newStatus = mostSignificantStatus.status();
261 if( newStatus != d->onlineStatus )
263 d->onlineStatus = newStatus;
264 emit onlineStatusChanged( this );
266 emit toolTipChanged( this );
269 void Identity::slotSaveProperty( Kopete::PropertyContainer *container, const QString &key,
270 const QVariant &oldValue, const QVariant &newValue )
272 if ( !newValue.isValid() ) // the property was removed, remove the config entry also
274 QString cfgGrpKey = QString::fromLatin1("prop_%1_%2").arg(QString::fromLatin1(oldValue.typeName()), key );
275 d->configGroup->deleteEntry(cfgGrpKey);
277 save();
280 } //END namespace Kopete
282 #include "kopeteidentity.moc"
284 // vim: set noet ts=4 sts=4 sw=4: