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

kopete/kopete

  • sources
  • kde-4.14
  • kdenetwork
  • kopete
  • kopete
kopetedbusinterface.cpp
Go to the documentation of this file.
1 /*
2  kopetedbusinterface.h - Kopete D-Bus interface
3 
4  Copyright (c) 2007 by MichaĆ«l Larouche <larouche@kde.org>
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 #include "kopetedbusinterface.h"
19 #include "kopetedbusinterface_p.h"
20 
21 // Qt includes
22 #include <QtCore/QStringList>
23 #include <QtCore/QList>
24 #include <QtCore/QLatin1String>
25 
26 // KDE includes
27 #include <kurl.h>
28 #include <kplugininfo.h>
29 
30 // Kopete includes
31 #include <kopetecontactlist.h>
32 #include <kopetemetacontact.h>
33 #include <kopetecontact.h>
34 #include <kopeteidentity.h>
35 #include <kopeteidentitymanager.h>
36 #include <kopeteavatarmanager.h>
37 #include <kopeteaccount.h>
38 #include <kopeteaccountmanager.h>
39 #include <kopeteonlinestatus.h>
40 #include <kopetemessage.h>
41 #include <kopetechatsession.h>
42 #include <kopetegroup.h>
43 #include <kopetepluginmanager.h>
44 #include <kopetepicture.h>
45 #include <kopeteviewmanager.h>
46 #include <kopetemessageevent.h>
47 
48 // Local includes
49 #include "kopeteadaptor.h"
50 
51 KopeteDBusInterface::KopeteDBusInterface(QObject *parent) :
52  QObject(parent), d(new KopeteDBusInterfacePrivate())
53 {
54  setObjectName("KopeteDBusInterface");
55  new KopeteAdaptor(this);
56  QDBusConnection::sessionBus().registerObject("/Kopete", this);
57 
58  QObject::connect(d, SIGNAL(contactChanged(QString)), this, SIGNAL(contactChanged(QString)));
59 }
60 
61 KopeteDBusInterface::~KopeteDBusInterface()
62 {
63 }
64 
65 QStringList KopeteDBusInterface::protocols() const
66 {
67  QStringList list;
68  foreach(KPluginInfo p, Kopete::PluginManager::self()->availablePlugins("Protocols"))
69  {
70  list << p.name();
71  }
72  return list;
73 }
74 
75 QStringList KopeteDBusInterface::contacts() const
76 {
77  return d->listContact(Kopete::ContactList::self()->metaContacts());
78 }
79 
80 QStringList KopeteDBusInterface::contactsByFilter(const QString &filter) const
81 {
82  QList<Kopete::MetaContact*> completeList =
83  Kopete::ContactList::self()->metaContacts();
84  QList<Kopete::MetaContact*> filteredList;
85 
86  Kopete::MetaContact *contact;
87 
88  if (filter.toLower() == QLatin1String("online"))
89  {
90  // "online" returns contacts that are not offline, which means that
91  // those being away, busy etc. are included as well.
92  foreach(contact, completeList)
93  {
94  if( contact->isOnline() )
95  filteredList << contact;
96  }
97  }
98  else if( filter.toLower() == QLatin1String("reachable") )
99  {
100  foreach(contact, completeList)
101  {
102  if( contact->isReachable() )
103  filteredList << contact;
104  }
105  }
106  else if( filter.toLower() == QLatin1String("filecapable") )
107  {
108  foreach(contact, completeList)
109  {
110  if( contact->canAcceptFiles() )
111  filteredList << contact;
112  }
113  }
114  else if ( filter.toLower() == QLatin1String("away") )
115  {
116  foreach(contact, completeList)
117  {
118  if( contact->status() == Kopete::OnlineStatus::Away )
119  {
120  filteredList << contact;
121  }
122  }
123  }
124  else if ( filter.toLower() == QLatin1String("busy") )
125  {
126  foreach(contact, completeList)
127  {
128  if( contact->status() == Kopete::OnlineStatus::Busy )
129  {
130  filteredList << contact;
131  }
132  }
133  }
134 
135  return d->listContact(filteredList);
136 }
137 
138 void KopeteDBusInterface::setIdentityNickName(const QString &nickName,
139  const QString &identityId)
140 {
141  Kopete::Identity *identity = 0;
142 
143  if (identityId.isEmpty())
144  {
145  identity = Kopete::IdentityManager::self()->defaultIdentity();
146  }
147  else
148  {
149  identity = Kopete::IdentityManager::self()->findIdentity(identityId);
150  }
151 
152  if (identity)
153  {
154  identity->setProperty(Kopete::Global::Properties::self()->nickName(),
155  nickName);
156  }
157 }
158 
159 void KopeteDBusInterface::setIdentityAvatar(const QString &avatarUrl,
160  const QString &identityId)
161 {
162  Kopete::Identity *identity = 0;
163 
164  if (identityId.isEmpty())
165  {
166  identity = Kopete::IdentityManager::self()->defaultIdentity();
167  }
168  else
169  {
170  identity = Kopete::IdentityManager::self()->findIdentity(identityId);
171  }
172 
173  if (identity)
174  {
175  // Add the avatar using AvatarManager
176  Kopete::AvatarManager::AvatarEntry avatarEntry;
177  avatarEntry.name = "D-Bus Avatar";
178  avatarEntry.path = KUrl(avatarUrl).path();
179  avatarEntry.category = Kopete::AvatarManager::User;
180 
181  avatarEntry = Kopete::AvatarManager::self()->add(avatarEntry);
182 
183  identity->setProperty(Kopete::Global::Properties::self()->photo(),
184  avatarEntry.path);
185  }
186 }
187 
188 void KopeteDBusInterface::setIdentityOnlineStatus(const QString &status,
189  const QString &message, const QString &identityId)
190 {
191  Kopete::Identity *identity = 0;
192 
193  if (identityId.isEmpty())
194  {
195  identity = Kopete::IdentityManager::self()->defaultIdentity();
196  }
197  else
198  {
199  identity = Kopete::IdentityManager::self()->findIdentity(identityId);
200  }
201 
202  if (identity)
203  {
204  identity->setOnlineStatus(d->status2Value(status), message);
205  }
206 }
207 
208 QStringList KopeteDBusInterface::identities() const
209 {
210  QStringList result;
211 
212  foreach(Kopete::Identity *identity, Kopete::IdentityManager::self()->identities())
213  {
214  result << identity->id();
215  }
216 
217  return result;
218 }
219 
220 QString KopeteDBusInterface::labelForIdentity(const QString & id) const
221 {
222  Kopete::Identity * identity =
223  Kopete::IdentityManager::self()->findIdentity(id);
224  if (identity)
225  {
226  return identity->label();
227  }
228  else
229  {
230  return QString();
231  }
232 }
233 
234 QStringList KopeteDBusInterface::accounts() const
235 {
236  QStringList result;
237 
238  foreach(Kopete::Account *account, Kopete::AccountManager::self()->accounts())
239  {
240  result << account->accountId();
241  }
242 
243  return result;
244 }
245 
246 void KopeteDBusInterface::connectAll()
247 {
248  Kopete::AccountManager::self()->setOnlineStatus(
249  Kopete::OnlineStatusManager::Online, QString(),
250  Kopete::AccountManager::ConnectIfOffline);
251 }
252 
253 void KopeteDBusInterface::disconnectAll()
254 {
255  Kopete::AccountManager::self()->setOnlineStatus(
256  Kopete::OnlineStatusManager::Offline);
257 }
258 
259 void KopeteDBusInterface::suspend()
260 {
261  Kopete::AccountManager::self()->suspend();
262 }
263 
264 void KopeteDBusInterface::resume()
265 {
266  Kopete::AccountManager::self()->resume();
267 }
268 
269 void KopeteDBusInterface::setOnlineStatus(const QString &status,
270  const QString &message)
271 {
272  Kopete::AccountManager::self()->setOnlineStatus(d->status2Value(status),
273  message);
274 }
275 
276 void KopeteDBusInterface::setStatusMessage(const QString &message)
277 {
278  Kopete::AccountManager::self()->setStatusMessage(message);
279 }
280 
281 void KopeteDBusInterface::sendMessage(const QString &contactId,
282  const QString &message)
283 {
284  Kopete::MetaContact *destMetaContact = d->findContact(contactId);
285  if (destMetaContact && destMetaContact->isReachable())
286  {
287  Kopete::Contact *destContact = destMetaContact->execute();
288  if (destContact)
289  {
290  Kopete::Message newMessage(destContact->account()->myself(),
291  destContact);
292  newMessage.setPlainBody(message);
293  newMessage.setDirection(Kopete::Message::Outbound);
294 
295  destContact->manager(Kopete::Contact::CanCreate)->sendMessage(
296  newMessage);
297  }
298  }
299 }
300 
301 void KopeteDBusInterface::openChat(const QString &contactId)
302 {
303  Kopete::MetaContact *contact = d->findContact(contactId);
304  if (contact && contact->isReachable())
305  {
306  Kopete::Contact *preferredContact = contact->preferredContact();
307  if (preferredContact && preferredContact->account()
308  && preferredContact != preferredContact->account()->myself())
309  {
310  contact->execute();
311  }
312  }
313 }
314 
315 QString KopeteDBusInterface::getDisplayName(const QString &contactId)
316 {
317  Kopete::MetaContact *contact = d->findContact(contactId);
318  if (contact)
319  return contact->displayName();
320  else
321  return "";
322 }
323 
324 bool KopeteDBusInterface::isContactOnline(const QString &contactId)
325 {
326  Kopete::MetaContact *contact = d->findContact(contactId);
327  if (contact)
328  return contact->isOnline();
329  else
330  return false;
331 }
332 
333 bool KopeteDBusInterface::addContact(const QString &protocolName,
334  const QString &accountId, const QString &contactId,
335  const QString &displayName, const QString &groupName)
336 {
337  QString protocolId = protocolName;
338  if (!protocolName.contains("Protocol"))
339  {
340  protocolId += QLatin1String("Protocol");
341  }
342 
343  // Find the account using the given parameters on D-Bus
344  Kopete::Account *account = Kopete::AccountManager::self()->findAccount(
345  protocolId, accountId);
346 
347  if (account)
348  {
349  QString contactName;
350  Kopete::Group *realGroup = 0;
351 
352  if (displayName.isEmpty())
353  {
354  contactName = contactId;
355  }
356  else
357  {
358  contactName = displayName;
359  }
360 
361  if (!groupName.isEmpty())
362  realGroup = Kopete::ContactList::self()->findGroup(groupName);
363 
364  account->addContact(contactId, contactName, realGroup,
365  Kopete::Account::DontChangeKABC);
366 
367  return true;
368  }
369 
370  return false;
371 }
372 
373 void KopeteDBusInterface::sendFile(const QString &contactId,
374  const QString &fileUrl)
375 {
376  Kopete::MetaContact *destMetaContact = d->findContact(contactId);
377  if (destMetaContact && destMetaContact->isReachable())
378  {
379  Kopete::Contact *destContact = destMetaContact->execute();
380  if (destContact && destContact->isFileCapable())
381  {
382  destContact->sendFile(KUrl(fileUrl));
383  }
384  }
385 }
386 
387 bool KopeteDBusInterface::isConnected(const QString &protocolName,
388  const QString &accountId)
389 {
390  QString protocolId = protocolName;
391  if (!protocolName.contains("Protocol"))
392  {
393  protocolId += QLatin1String("Protocol");
394  }
395 
396  Kopete::Account *account = Kopete::AccountManager::self()->findAccount(
397  protocolId, accountId);
398  return account ? account->isConnected() : false;
399 }
400 
401 void KopeteDBusInterface::connect(const QString &protocolName,
402  const QString &accountId)
403 {
404  QString protocolId = protocolName;
405  if (!protocolName.contains("Protocol"))
406  {
407  protocolId += QLatin1String("Protocol");
408  }
409 
410  Kopete::Account *account = Kopete::AccountManager::self()->findAccount(
411  protocolId, accountId);
412  if (account)
413  {
414  account->connect();
415  }
416 }
417 
418 void KopeteDBusInterface::disconnect(const QString &protocolName,
419  const QString &accountId)
420 {
421  QString protocolId = protocolName;
422  if (!protocolName.contains("Protocol"))
423  {
424  protocolId += QLatin1String("Protocol");
425  }
426 
427  Kopete::Account *account = Kopete::AccountManager::self()->findAccount(
428  protocolId, accountId);
429  if (account)
430  {
431  account->disconnect();
432  }
433 }
434 
435 QVariantMap KopeteDBusInterface::contactProperties(const QString &contactId)
436 {
437  QVariantMap properties;
438  Kopete::MetaContact *contact = d->findContact(contactId);
439 
440  if (contact)
441  {
442  properties["status"] = Kopete::OnlineStatus::statusTypeToString(
443  contact->status());
444  properties["message_reachable"] = contact->isReachable();
445  properties["file_reachable"] = contact->canAcceptFiles();
446  properties["display_name"] = contact->displayName();
447  properties["id"] = contact->metaContactId().toString();
448  if (contact->photoSource() == Kopete::MetaContact::SourceCustom)
449  {
450  properties["picture"] = contact->customPhoto().prettyUrl();
451  }
452  else
453  {
454  properties["picture"] = contact->picture().path();
455  }
456  properties["idle_time"] = qulonglong(contact->idleTime());
457  if (contact->preferredContact())
458  {
460  properties["status_message"]
461  = contact->preferredContact()->statusMessage().message();
462  }
463 
464  QStringList messages;
465  foreach(Kopete::Contact *subContact, contact->contacts())
466  {
467  QList<Kopete::MessageEvent*> pendingMessages = KopeteViewManager::viewManager()->pendingMessages(subContact);
468  foreach(Kopete::MessageEvent *event, pendingMessages)
469  {
470  messages << event->message().parsedBody();
471  }
472  }
473  properties["pending_messages"] = messages;
474  }
475 
476  return properties;
477 }
478 
479 #include "kopetedbusinterface.moc"
KopeteDBusInterface::disconnect
void disconnect(const QString &protocolName, const QString &accountId)
Disconnect a given account in the given protocol.
Definition: kopetedbusinterface.cpp:418
KopeteDBusInterfacePrivate::listContact
QStringList listContact(const QList< Kopete::MetaContact * > &contactList)
KopeteDBusInterface::openChat
void openChat(const QString &contactId)
Open a chat window for the given contact.
Definition: kopetedbusinterface.cpp:301
KopeteDBusInterface::isConnected
bool isConnected(const QString &protocolName, const QString &accountId)
Get information if we are connected to a given account in the given protocol.
Definition: kopetedbusinterface.cpp:387
KopeteDBusInterface::addContact
bool addContact(const QString &protocolName, const QString &accountId, const QString &contactId, const QString &displayName, const QString &groupName=QString())
Adds a contact with the specified params.
Definition: kopetedbusinterface.cpp:333
QDBusConnection::registerObject
bool registerObject(const QString &path, QObject *object, QFlags< QDBusConnection::RegisterOption > options)
KopeteDBusInterface::resume
void resume()
Sets the online status of all accounts to the status they had when suspend was called.
Definition: kopetedbusinterface.cpp:264
QDBusConnection::sessionBus
QDBusConnection sessionBus()
KopeteDBusInterface::setIdentityNickName
void setIdentityNickName(const QString &nickName, const QString &identityId=QString())
Set the nickname for the given identity.
Definition: kopetedbusinterface.cpp:138
KopeteDBusInterface::connectAll
void connectAll()
Connect all accounts.
Definition: kopetedbusinterface.cpp:246
KopeteDBusInterface::contactsByFilter
QStringList contactsByFilter(const QString &filter) const
Get a filered list of contacts based on the filter.
Definition: kopetedbusinterface.cpp:80
QObject::event
virtual bool event(QEvent *e)
Kopete::Items::MetaContact
Definition: kopeteitembase.h:57
Kopete::Items::Group
Definition: kopeteitembase.h:57
accountId
QString accountId
Definition: kopete-account-kconf_update.cpp:31
KopeteDBusInterface::suspend
void suspend()
Saves the online status of all accounts and disconnects them.
Definition: kopetedbusinterface.cpp:259
kopetedbusinterface_p.h
QObject
KopeteDBusInterface::identities
QStringList identities() const
Get a list of all identities' ID.
Definition: kopetedbusinterface.cpp:208
KopeteDBusInterface::setIdentityOnlineStatus
void setIdentityOnlineStatus(const QString &status, const QString &message, const QString &identityId=QString())
Set the online status for the given identity.
Definition: kopetedbusinterface.cpp:188
QObject::setObjectName
void setObjectName(const QString &name)
QString::isEmpty
bool isEmpty() const
KopeteDBusInterface::contacts
QStringList contacts() const
Return all contacts.
Definition: kopetedbusinterface.cpp:75
KopeteDBusInterface::setIdentityAvatar
void setIdentityAvatar(const QString &avatarUrl, const QString &identityId=QString())
Set the avatar for the given identity.
Definition: kopetedbusinterface.cpp:159
QString
QList< Kopete::MetaContact * >
QStringList
QString::toLower
QString toLower() const
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
KopeteDBusInterface::labelForIdentity
QString labelForIdentity(const QString &id) const
Get UI labels for identities.
Definition: kopetedbusinterface.cpp:220
KopeteDBusInterfacePrivate::findContact
Kopete::MetaContact * findContact(const QString &nameOrId)
Tries to locate a meta contact using first the protocol:account:contact triplet, if that fails the me...
KopeteDBusInterface::setOnlineStatus
void setOnlineStatus(const QString &status, const QString &message=QString())
Change the online status for all accounts.
Definition: kopetedbusinterface.cpp:269
KopeteDBusInterface::setStatusMessage
void setStatusMessage(const QString &message)
Change the status message for all accounts.
Definition: kopetedbusinterface.cpp:276
KopeteDBusInterface::KopeteDBusInterface
KopeteDBusInterface(QObject *parent)
Constructor.
Definition: kopetedbusinterface.cpp:51
KopeteDBusInterface::contactProperties
QVariantMap contactProperties(const QString &contactId)
Look up details for a specific contact.
Definition: kopetedbusinterface.cpp:435
QLatin1String
KopeteDBusInterface::accounts
QStringList accounts() const
Get a list of all account's ID.
Definition: kopetedbusinterface.cpp:234
KopeteDBusInterface::connect
void connect(const QString &protocolName, const QString &accountId)
Connect a given account in the given protocol.
Definition: kopetedbusinterface.cpp:401
KopeteDBusInterface::protocols
QStringList protocols() const
Get a list of all protocol names.
Definition: kopetedbusinterface.cpp:65
KopeteDBusInterface::sendFile
void sendFile(const QString &contactId, const QString &fileUrl)
Send a file to the given contact.
Definition: kopetedbusinterface.cpp:373
KopeteDBusInterface::disconnectAll
void disconnectAll()
Disconnect all accounts.
Definition: kopetedbusinterface.cpp:253
KopeteDBusInterface::getDisplayName
QString getDisplayName(const QString &contactId)
Retrieve the Display Name from the given contact ID.
Definition: kopetedbusinterface.cpp:315
KopeteDBusInterface::isContactOnline
bool isContactOnline(const QString &contactId)
Get the Online Status of the contact.
Definition: kopetedbusinterface.cpp:324
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KopeteDBusInterfacePrivate::status2Value
Kopete::OnlineStatusManager::Categories status2Value(const QString &status)
KopeteDBusInterfacePrivate
Tracks changes of all metacontacts and reports them via signals.
Definition: kopetedbusinterface_p.h:62
KopeteDBusInterface::~KopeteDBusInterface
~KopeteDBusInterface()
Destructor.
Definition: kopetedbusinterface.cpp:61
KopeteDBusInterface::sendMessage
void sendMessage(const QString &contactId, const QString &message)
Send a message to the given contact.
Definition: kopetedbusinterface.cpp:281
kopetedbusinterface.h
KopeteDBusInterface::contactChanged
void contactChanged(QString contactId)
Contact properties have changed: displayName, avatar, pending messages...
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/kopete

Skip menu "kopete/kopete"
  • 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