Port things from MSN to WLM plugin:
[kdenetwork.git] / kopete / protocols / telepathy / telepathycontactmanager.cpp
blob519eb795b04834d4d6e3b8b6380129b529cf24cc
1 /*
2 * telepathycontactmanager.cpp - Telepathy Contact Manager
4 * Copyright (c) 2006 by Michaƫl Larouche <larouche@kde.org>
5 *
6 * Kopete (c) 2002-2006 by the Kopete developers <kopete-devel@kde.org>
8 *************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 *************************************************************************
17 #include "telepathycontactmanager.h"
19 // Qt includes
20 #include <QtCore/QPointer>
22 // KDE includes
23 #include <kdebug.h>
24 #include <klocale.h>
26 // QtTapioca includes
27 #include <QtTapioca/ContactList>
28 #include <QtTapioca/Contact>
29 #include <QtTapioca/ContactBase>
31 // Kopete includes
32 #include <kopetemetacontact.h>
33 #include <kopeteonlinestatus.h>
34 #include <kopetecontactlist.h>
36 // Local includes
37 #include "telepathyaccount.h"
38 #include "telepathycontact.h"
39 #include "telepathyprotocol.h"
40 #include "telepathyaddpendingcontactjob.h"
42 using namespace QtTapioca;
44 class TelepathyContactManager::Private
46 public:
47 Private()
50 QPointer<TelepathyAccount> account;
51 QPointer<ContactList> contactList;
54 TelepathyContactManager::TelepathyContactManager(TelepathyAccount *account)
55 : QObject(account), d(new Private)
57 d->account = account;
60 TelepathyContactManager::~TelepathyContactManager()
62 delete d;
65 TelepathyAccount *TelepathyContactManager::account()
67 Q_ASSERT_X( !d->account.isNull(), "TelepathyContactManager::account", "account is null" );
69 return d->account;
72 QtTapioca::ContactList *TelepathyContactManager::contactList()
74 Q_ASSERT_X( !d->contactList.isNull(), "TelepathyContactManager::contactList", "contactList is null" );
75 return d->contactList;
78 QtTapioca::Contact *TelepathyContactManager::addContact(const QString &contactId)
80 return const_cast<QtTapioca::Contact*>( contactList()->addContact(contactId) );
83 void TelepathyContactManager::removeContact(TelepathyContact *contact)
85 kDebug(TELEPATHY_DEBUG_AREA) ;
87 if( contact->internalContact() )
89 // First remove the contact from the contact list
90 contactList()->removeContact( contact->internalContact() );
92 else
94 kDebug(TELEPATHY_DEBUG_AREA) << "WARNING: Internal contact in " << contact->contactId() << " is null. Removing in Kopete only.";
97 // Then delete the contact from Kopete
98 contact->deleteLater();
101 void TelepathyContactManager::setContactList(QtTapioca::ContactList *contactList)
103 // Disconnect signals from previous instance.
104 if( !d->contactList.isNull() )
106 d->contactList->disconnect();
109 d->contactList = contactList;
111 // Connect signals/slot
112 connect(d->contactList, SIGNAL(authorizationRequested(QtTapioca::Contact*)), this, SLOT(telepathyAuthorizationRequired(QtTapioca::Contact*)));
113 connect(d->contactList, SIGNAL(subscriptionAccepted(QtTapioca::Contact *)), this, SLOT(telepathySubscriptionAccepted(QtTapioca::Contact*)));
116 void TelepathyContactManager::loadContacts()
118 kDebug(TELEPATHY_DEBUG_AREA) << "Loading contact list into Kopete.";
120 QList<Contact*> contacts = contactList()->knownContacts();
121 if( contacts.isEmpty() )
123 kDebug(TELEPATHY_DEBUG_AREA) << "WARNING: Contact list from Telepathy is empty !";
126 Contact *tempContact;
127 foreach(tempContact, contacts)
129 QString contactId = tempContact->uri();
131 kDebug(TELEPATHY_DEBUG_AREA) << "Subscription Status(" << contactId << "): " << int(tempContact->subscriptionStatus());
132 kDebug(TELEPATHY_DEBUG_AREA) << "Authorization Status(" << contactId << "): " << int(tempContact->authorizationStatus());
134 if( tempContact->authorizationStatus() == QtTapioca::Contact::LocalPending )
136 kDebug(TELEPATHY_DEBUG_AREA) << "Found a local pending contact. Adding it";
138 // Add the pending contact
139 TelepathyAddPendingContactJob *addPendingContactJob = new TelepathyAddPendingContactJob( account() );
140 addPendingContactJob->setPendingContact( tempContact );
141 addPendingContactJob->start();
143 else
145 // If the contact doesn't exist in Kopete, create it
146 if( !account()->contacts()[contactId] )
148 createContact(tempContact);
150 // else, set the internal telepathy object in the existing contact.
151 else
153 kDebug(TELEPATHY_DEBUG_AREA) << "Set internal information from Telepathy to " << contactId;
154 TelepathyContact *contact = static_cast<TelepathyContact*>( account()->contacts()[contactId] );
155 contact->setInternalContact(tempContact);
161 void TelepathyContactManager::telepathyAuthorizationRequired(QtTapioca::Contact *newContact)
163 kDebug(TELEPATHY_DEBUG_AREA) << "A contact want authorization";
165 QString contactUri = newContact->uri();
167 // Add and/or authorize the pending contact
168 TelepathyAddPendingContactJob *addPendingContactJob = new TelepathyAddPendingContactJob( account() );
169 addPendingContactJob->setPendingContact( newContact );
171 // Set to authorization only if the contact already exist in Kopete
172 addPendingContactJob->setAuthorizeOnly( account()->contacts()[contactUri] ? true : false );
174 addPendingContactJob->start();
177 void TelepathyContactManager::telepathySubscriptionAccepted(QtTapioca::Contact *contact)
179 kDebug(TELEPATHY_DEBUG_AREA) << "Contact " << contact->uri() << " has accepted your subscription request.";
182 void TelepathyContactManager::createContact(QtTapioca::Contact *telepathyContact)
184 QString contactId = telepathyContact->uri();
186 kDebug(TELEPATHY_DEBUG_AREA) << "Creating Telepathy contact \"" << contactId << "\" in Kopete.";
188 Kopete::MetaContact *metaContact = new Kopete::MetaContact();
190 // Create the TelepathyContact
191 TelepathyContact *newContact = new TelepathyContact( account(), contactId, metaContact );
192 newContact->setInternalContact(telepathyContact);
193 newContact->setMetaContact(metaContact);
195 Kopete::ContactList::self()->addMetaContact( metaContact );
198 #include "telepathycontactmanager.moc"