File indexing completed on 2023-10-01 08:41:45

0001 /*
0002     Copyright (C) 2013 David Edmundson <davidedmundson@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Lesser General Public License for more details.
0013 
0014     You should have received a copy of the GNU Lesser General Public
0015     License along with this library; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0017 */
0018 
0019 #include "persistent-contact.h"
0020 
0021 #include <TelepathyQt/Account>
0022 #include <TelepathyQt/AccountManager>
0023 #include <TelepathyQt/PendingContacts>
0024 #include <TelepathyQt/PendingReady>
0025 #include <TelepathyQt/ContactManager>
0026 
0027 
0028 #include <KTp/core.h>
0029 
0030 #include "ktp-debug.h"
0031 
0032 namespace KTp
0033 {
0034 class PersistentContact::Private
0035 {
0036 public:
0037     QString contactId;
0038     QString accountId;
0039     KTp::ContactPtr contact;
0040     Tp::AccountPtr account;
0041 };
0042 }
0043 
0044 KTp::PersistentContactPtr KTp::PersistentContact::create(const QString &accountId, const QString &contactId)
0045 {
0046     return KTp::PersistentContactPtr(new KTp::PersistentContact(accountId, contactId));
0047 }
0048 
0049 KTp::PersistentContact::PersistentContact(const QString &accountId, const QString &contactId)
0050     : QObject(),
0051       d(new PersistentContact::Private())
0052 {
0053     d->contactId = contactId;
0054     d->accountId = accountId;
0055 
0056     //FIXME there must be a const for this?
0057     QString objectPath = TP_QT_ACCOUNT_OBJECT_PATH_BASE + QLatin1Char('/') + accountId;
0058 
0059     Tp::PendingReady *op = KTp::accountFactory()->proxy(TP_QT_ACCOUNT_MANAGER_BUS_NAME, objectPath, KTp::connectionFactory(), KTp::channelFactory(), KTp::contactFactory());
0060     connect(op, &Tp::PendingReady::finished, this, &KTp::PersistentContact::onAccountReady);
0061 }
0062 
0063 KTp::PersistentContact::~PersistentContact()
0064 {
0065     delete d;
0066 }
0067 
0068 QString KTp::PersistentContact::contactId() const
0069 {
0070     return d->contactId;
0071 }
0072 
0073 QString KTp::PersistentContact::accountId() const
0074 {
0075     return d->accountId;
0076 }
0077 
0078 void KTp::PersistentContact::setAccountManager(const Tp::AccountManagerPtr &accountManager)
0079 {
0080 }
0081 
0082 KTp::ContactPtr KTp::PersistentContact::contact() const
0083 {
0084     return d->contact;
0085 }
0086 
0087 Tp::AccountPtr KTp::PersistentContact::account() const
0088 {
0089     return d->account;
0090 }
0091 
0092 void KTp::PersistentContact::onAccountReady(Tp::PendingOperation *op)
0093 {
0094     if (op->isError()) {
0095         qCWarning(KTP_COMMONINTERNALS) << "could not load account " << d->accountId;
0096     }
0097     Tp::PendingReady *pendingReady = qobject_cast<Tp::PendingReady*>(op);
0098     Q_ASSERT(pendingReady);
0099     Tp::AccountPtr account = Tp::AccountPtr::qObjectCast(pendingReady->proxy());
0100     d->account = account;
0101     connect(account.data(), SIGNAL(connectionChanged(Tp::ConnectionPtr)), SLOT(onAccountConnectionChanged(Tp::ConnectionPtr)));
0102     onAccountConnectionChanged(account->connection());
0103 }
0104 
0105 
0106 void KTp::PersistentContact::onAccountConnectionChanged(const Tp::ConnectionPtr &connection)
0107 {
0108     if (connection) {
0109         Tp::ContactManagerPtr manager = connection->contactManager();
0110         connect(manager->contactsForIdentifiers(QStringList() << d->contactId), SIGNAL(finished(Tp::PendingOperation*)), SLOT(onPendingContactsFinished(Tp::PendingOperation*)));
0111     }
0112 }
0113 
0114 void KTp::PersistentContact::onPendingContactsFinished(Tp::PendingOperation *op)
0115 {
0116     Tp::PendingContacts *pendingContactsOp = qobject_cast<Tp::PendingContacts*>(op);
0117     Q_ASSERT(pendingContactsOp);
0118 
0119     if (pendingContactsOp->contacts().size() == 1) {
0120         d->contact = KTp::ContactPtr::qObjectCast(pendingContactsOp->contacts()[0]);
0121         Q_EMIT contactChanged(d->contact);
0122         connect(d->contact.data(), SIGNAL(invalidated()), SLOT(onContactInvalid()));
0123     }
0124 }
0125 
0126 void KTp::PersistentContact::onContactInvalid()
0127 {
0128     d->contact = KTp::ContactPtr();
0129     Q_EMIT contactChanged(d->contact);
0130 }