kdeconnect-kde/telepathy-cm/kdeconnecttelepathyprotocolfactory.cpp
David Edmundson e4cbf22519 Telepathy integration
This sends recieved text messages to any Telepathy client and allows the
user to respond from there.
This should work with both our clients and Empathy.

An account on telepathy is created on activation.

As Telepathy clients expect backends to be always running, this is
started by the daemon to
suppress client errors. The plugin system then talks to the same CM via
use of a singleton accessor.

Based on work by Alexandr Akulich then tidied up and rebased.
2015-09-10 20:36:46 +02:00

68 lines
2.7 KiB
C++

#include <TelepathyQt/Types>
#include <TelepathyQt/BaseConnectionManager>
#include <TelepathyQt/Constants>
#include <TelepathyQt/Debug>
#include <TelepathyQt/Types>
#include <TelepathyQt/ConnectionManager>
#include <TelepathyQt/AccountManager>
#include <TelepathyQt/Account>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/PendingAccount>
#include <TelepathyQt/AccountSet>
#include <QtDBus/QDBusConnection>
#include "protocol.h"
#include "kdeconnecttelepathyprotocolfactory.h"
Tp::WeakPtr<KDEConnectTelepathyProtocol> KDEConnectTelepathyProtocolFactory::s_interface;
ConnectProtocolPtr KDEConnectTelepathyProtocolFactory::interface() {
if (s_interface.isNull()) {
Tp::registerTypes();
Tp::enableDebug(true);
Tp::enableWarnings(true);
ConnectProtocolPtr protocol = Tp::BaseProtocol::create<KDEConnectTelepathyProtocol>(
QDBusConnection::sessionBus(),
QLatin1String("kdeconnect"));
s_interface = protocol;
static Tp::BaseConnectionManagerPtr cm = Tp::BaseConnectionManager::create(
QDBusConnection::sessionBus(), QLatin1String("kdeconnect"));
protocol->setConnectionManagerName(cm->name());
protocol->setEnglishName(QLatin1String("KDE Connect"));
protocol->setIconName(QLatin1String("kdeconnect"));
protocol->setVCardField(QLatin1String("phone_number"));
cm->addProtocol(protocol);
cm->registerObject();
//fake being a client and create an account to use this connection
//maybe this should be per device.. with a device ID as a parameter, but lets keep it connect for now
Tp::AccountManagerPtr am = Tp::AccountManager::create(QDBusConnection::sessionBus());
QObject::connect(am->becomeReady(), &Tp::PendingOperation::finished, [am]() {
Tp::AccountSetPtr accounts = am->accountsByProtocol("kdeconnect");
if (!accounts) {
return;
}
if (accounts->accounts().isEmpty()) {
Tp::PendingAccount* pa = am->createAccount("kdeconnect", "kdeconnect", "kdeconnect", QVariantMap(), QVariantMap());
QObject::connect(pa, &Tp::PendingOperation::finished, pa, [pa](){
if (!pa->account()) {
return;
}
pa->account()->setEnabled(true);
pa->account()->setRequestedPresence(Tp::Presence::available());
});
} else {
Tp::AccountPtr account = accounts->accounts().first();
account->setRequestedPresence(Tp::Presence::available());
}
});
}
return s_interface.toStrongRef();
}