some UI changes for usability
[makneto-zunavac1.git] / src / backend / telepathy-initializer.cpp
blob89e009f180d5f1fd5297a3f05516f5f886c4dc5a
1 /*
2 * Telepathy initializer initializes telepathy and is inspired by
3 * TelepathyQt4Yell project
5 * Copyright (C) 2011 Vojta Kulicka <vojtechkulicka@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <TelepathyQt4/Types>
23 #include <TelepathyQt4/Global>
24 #include <TelepathyQt4/Constants>
25 #include <TelepathyQt4/AccountManager>
26 #include <TelepathyQt4/Account>
27 #include <TelepathyQt4/ContactManager>
28 #include <TelepathyQt4/PendingOperation>
29 #include <TelepathyQt4/PendingReady>
30 #include <TelepathyQt4/TextChannel>
31 #include <TelepathyQt4/StreamedMediaChannel>
32 #include <TelepathyQt4/FileTransferChannel>
33 #include <TelepathyQt4/ChannelClassSpecList>
35 #include "telepathy-client.h"
36 #include "telepathy-initializer.h"
37 #include "accounts-model.h"
38 #include <TelepathyQt4/IncomingFileTransferChannel>
39 #include <TelepathyQt4/OutgoingFileTransferChannel>
41 namespace MaknetoBackend {
43 TelepathyInitializer::TelepathyInitializer(QObject *parent)
44 : QObject(parent),
45 m_connections(0) {
47 Tp::registerTypes();
48 Tp::enableDebug(false);
49 Tp::enableWarnings(true);
52 void TelepathyInitializer::createClient(QString name) {
54 m_clientName = name;
56 // check QDBus connection
57 if (!QDBusConnection::sessionBus().isConnected())
58 qWarning() << "TelepathyInitializer: session bus is not connected!" << QDBusConnection::sessionBus().lastError();
60 //create AccountManager
61 Tp::Features connFeatures;
62 Tp::Features accountFeatures;
64 //account factory
65 Tp::AccountFactoryPtr accountFactory = Tp::AccountFactory::create(QDBusConnection::sessionBus(),
66 Tp::Features()
67 << Tp::Account::FeatureCore
68 << Tp::Account::FeatureAvatar
69 << Tp::Account::FeatureCapabilities
72 //connection factory
73 Tp::ConnectionFactoryPtr connectionFactory = Tp::ConnectionFactory::create(
74 QDBusConnection::sessionBus(),
75 Tp::Features() << Tp::Connection::FeatureSelfContact
76 << Tp::Connection::FeatureCore
77 << Tp::Connection::FeatureSimplePresence
78 << Tp::Connection::FeatureRoster
79 << Tp::Connection::FeatureRosterGroups
82 //channel factory
83 Tp::ChannelFactoryPtr channelFactory = Tp::ChannelFactory::create(QDBusConnection::sessionBus());
85 Tp::Features textFeatures = Tp::Features() << Tp::TextChannel::FeatureCore
86 << Tp::TextChannel::FeatureMessageQueue
87 << Tp::TextChannel::FeatureMessageSentSignal
88 << Tp::TextChannel::FeatureChatState
89 << Tp::TextChannel::FeatureMessageCapabilities;
91 Tp::Features callFeatures = Tp::Features() << Tp::StreamedMediaChannel::FeatureCore
92 << Tp::StreamedMediaChannel::FeatureStreams;
94 Tp::Features incomingFileTransferFeatures = Tp::Features() << Tp::IncomingFileTransferChannel::FeatureCore;
95 Tp::Features outgoingFileTransferFeatures = Tp::Features() << Tp::OutgoingFileTransferChannel::FeatureCore;
97 channelFactory->addFeaturesForTextChats(textFeatures);
98 channelFactory->addFeaturesForTextChatrooms(textFeatures);
99 channelFactory->addFeaturesForStreamedMediaCalls(callFeatures);
100 channelFactory->addFeaturesForIncomingFileTransfers(incomingFileTransferFeatures);
101 channelFactory->addFeaturesForOutgoingFileTransfers(outgoingFileTransferFeatures);
103 //contact factory
104 Tp::ContactFactoryPtr contactFactory = Tp::ContactFactory::create(
105 Tp::Features() << Tp::Contact::FeatureAlias
106 << Tp::Contact::FeatureAvatarData
107 << Tp::Contact::FeatureCapabilities
108 << Tp::Contact::FeatureSimplePresence);
110 m_accountManager = Tp::AccountManager::create(QDBusConnection::sessionBus(),
111 accountFactory,
112 connectionFactory,
113 channelFactory,
114 contactFactory);
116 connect(m_accountManager->becomeReady(),
117 SIGNAL(finished(Tp::PendingOperation*)),
118 SLOT(onAccountManagerReady(Tp::PendingOperation*)));
121 void TelepathyInitializer::onAccountManagerReady(Tp::PendingOperation *op) {
122 if (op->isError()) {
123 qWarning() << "TelepathyInitializer: Error occured making AccountManager ready" << op->errorName() << op->errorMessage();
124 return;
127 foreach(Tp::AccountPtr account, m_accountManager->allAccounts()) {
129 if (!account->isEnabled())
130 continue;
132 connect(account->becomeReady(),
133 SIGNAL(finished(Tp::PendingOperation *)),
134 SLOT(onAccountReady(Tp::PendingOperation*)));
136 m_connections++;
141 void TelepathyInitializer::onAccountReady(Tp::PendingOperation *op) {
142 if (op->isError()) {
143 qWarning() << "TelepathyInitializer: Error occured making Account ready";
144 return;
147 Tp::PendingReady *pr = qobject_cast<Tp::PendingReady *>(op);
148 Tp::AccountPtr account = Tp::AccountPtr::dynamicCast(pr->object());
150 if (!account->connection().isNull()) {
152 connect(account->connection()->becomeReady(),
153 SIGNAL(finished(Tp::PendingOperation *)),
154 SLOT(onConnectionReady(Tp::PendingOperation *)));
155 } else {
156 m_connections--;
157 checkFinished();
159 qDebug() << "TelepathyInitializer: Account ready: " << account->uniqueIdentifier();
162 void TelepathyInitializer::onConnectionReady(Tp::PendingOperation *op) {
163 if (op->isError()) {
164 qWarning() << "TelepathyInitializer: Error occured making Connection ready";
165 return;
168 Tp::PendingReady *pr = qobject_cast<Tp::PendingReady *>(op);
169 Tp::ConnectionPtr connection = Tp::ConnectionPtr::dynamicCast(pr->object());
171 Q_ASSERT(connection);
173 m_connections--;
174 checkFinished();
177 void TelepathyInitializer::checkFinished() {
178 qDebug() << "TelepathyInitializer: Connections ready: " << m_connections;
179 if (m_connections > 0)
180 return;
182 //create accounts model
183 AccountsModel *accountsModel = new AccountsModel(m_accountManager);
185 //Create client
186 //ChannelClassSpec says what channel is the client interested in and is looked at by channel dispatcher
187 TelepathyClient *client = TelepathyClient::Instance(Tp::ChannelClassSpecList()
188 << Tp::ChannelClassSpec::textChat()
189 << Tp::ChannelClassSpec::unnamedTextChat()
190 << Tp::ChannelClassSpec::textChatroom()
191 << Tp::ChannelClassSpec::streamedMediaCall()
192 << Tp::ChannelClassSpec::streamedMediaAudioCall()
193 << Tp::ChannelClassSpec::incomingFileTransfer()
194 << Tp::ChannelClassSpec::outgoingFileTransfer()
195 << Tp::ChannelClassSpec::streamedMediaVideoCall()
196 << Tp::ChannelClassSpec::streamedMediaVideoCallWithAudio(),
197 m_accountManager,
198 accountsModel,
199 m_clientName,
200 this
203 //register client
204 m_cr = Tp::ClientRegistrar::create(m_accountManager);
205 Tp::AbstractClientPtr handler(client);
206 m_cr->registerClient(handler, m_clientName);
208 //create account model and set it to the client
209 qDebug() << "TelepathyInitializer: Finished";
210 emit finished(client);
214 #include "telepathy-initializer.moc"