2 * This file is part of TpSession
4 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
5 * Contact Kate Alhola kate.alholanokia.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
21 #include "tpsession.h"
24 TpSession::TpSession(QString cmname,bool synchronous)
27 Tp::enableDebug(false);
28 Tp::enableWarnings(false);
30 mAM = Tp::AccountManager::create();
32 connect(mAM->becomeReady(),
33 SIGNAL(finished(Tp::PendingOperation *)),
34 SLOT(onAMReady(Tp::PendingOperation *)));
36 SIGNAL(accountCreated(const QString &)),
37 SLOT(onAccountCreated(const QString &)));
40 if(synchronous) loop.exec(); // Loop locally untill accounts are initialized
44 TpSession* TpSession::instancePtr=NULL;
45 TpSession* TpSession::instance(bool synchronous)
47 if(instancePtr==NULL) instancePtr=new TpSession("ring",synchronous);
51 void TpSession::onAMReady(Tp::PendingOperation *op)
53 qDebug() << "TpSession::onAMReady";
54 TpSessionAccount *tpacc;
56 foreach (const QString &path, mAM->allAccountPaths()) {
57 accounts+=tpacc=new TpSessionAccount(mAM, path);
58 connect(tpacc,SIGNAL(accountReady(TpSessionAccount*)),
59 SLOT(onAccountReady(TpSessionAccount *)));
64 void TpSession::onReady(Tp::PendingOperation *)
68 void TpSession::onAccountCreated(const QString &path)
71 accounts+=new TpSessionAccount(mAM, path);
74 void TpSession::onAccountReady(TpSessionAccount *tpacc)
76 qDebug() << "TpSession::onAccountReady:Account " << tpacc->acc->cmName() << "is Ready";
77 connect(tpacc,SIGNAL(messageReceived(const Tp::ReceivedMessage &,TpSessionAccount *)),
78 SLOT(onMessageReceived(const Tp::ReceivedMessage &,TpSessionAccount *)));
79 if(!reqCm.isEmpty() && tpacc->acc->cmName()==reqCm) {
83 emit accountReady(tpacc);
84 if(!reqMsg.isEmpty()) tpacc->sendMessageToAddress(reqAddress,reqMsg);
89 void TpSession::onMessageReceived(const Tp::ReceivedMessage &msg,TpSessionAccount *acc)
91 qDebug() << "TestProg::onMessageReceived " << msg.text() << "from " << msg.sender()->id();
92 emit messageReceived(msg,acc);
95 void TpSession::sendMessageToAddress(QString connectionMgr,QString address,QString message)
97 TpSessionAccount *tpsa=getAccount(connectionMgr);
98 if(tpsa) tpsa->sendMessageToAddress(address,message);
101 TpSessionAccount* TpSession::getAccount(const QString cm,QString protocol)
103 qDebug() << "TpSession::getAccount" << cm << " " << protocol;
104 foreach (TpSessionAccount *tpacc, accounts) {
105 if((!cm.isEmpty() && tpacc->acc->cmName()==cm) || (!protocol.isEmpty() && tpacc->acc->protocol()==protocol)) {
106 qDebug() << "TpSession::getAccount found" << tpacc->acc->cmName() << " " << tpacc->acc->protocol();
113 void TpSession::createObserver()
116 qDebug() << __PRETTY_FUNCTION__ ;
118 registrar = Tp::ClientRegistrar::create();
120 Tp::ChannelClassList channelFilters;
121 QMap<QString, QDBusVariant> textFilter, mediaFilter;
122 // Registering Text channel observer
123 textFilter.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType"),
124 QDBusVariant(TELEPATHY_INTERFACE_CHANNEL_TYPE_TEXT));
125 textFilter.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandleType"),
126 QDBusVariant(Tp::HandleTypeContact));
127 channelFilters.append(textFilter);
129 // Registering Media channel observer
130 mediaFilter.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType"),
131 QDBusVariant(TELEPATHY_INTERFACE_CHANNEL_TYPE_STREAMED_MEDIA));
132 mediaFilter.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandleType"),
133 QDBusVariant(Tp::HandleTypeContact));
134 channelFilters.append(mediaFilter);
136 TpSessionObserver* observer = new TpSessionObserver( channelFilters, this );
137 bool registered = registrar->registerClient(
138 Tp::AbstractClientPtr::dynamicCast(Tp::SharedPtr<TpSessionObserver>(observer)),
139 "TpSessionChannelObserver");
141 qDebug() << "TpSession::createObserver" << (registered ? "started" : "failed");
146 void TpSession::createChannelListener(const QString &channelType,
147 const Tp::MethodInvocationContextPtr<> &context,
148 const Tp::AccountPtr &account,
149 const Tp::ChannelPtr &channel)
151 qDebug() << "TpSession::createChannelListener";
153 QString channelObjectPath = channel->objectPath();
156 if ( channels.contains( channelObjectPath ) &&
157 !channelType.isEmpty() &&
158 !channelObjectPath.isEmpty() ) {
159 qDebug() << "TELEPATHY_ERROR_INVALID_ARGUMENT";
162 qDebug() << "creating listener for: " << channelObjectPath << " type " << channelType;
164 ChannelListener* listener = 0;
165 if( channelType == TELEPATHY_INTERFACE_CHANNEL_TYPE_TEXT ) {
166 listener = new TextChannelListener(account, channel, context);
167 } else if ( channelType == TELEPATHY_INTERFACE_CHANNEL_TYPE_STREAMED_MEDIA ) {
168 listener = new StreamChannelListener(account, channel, context);
172 connect(listener, SIGNAL(channelClosed(ChannelListener *)),
173 this, SLOT(channelClosed(ChannelListener *)));
174 Channels.append( channelObjectPath );