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 "tpsessionchannel.h"
25 * \class TpSessionChannel
26 * \headerfile <tpsessionchannel.h>
29 * When you start chat session or call with your buddy, channel is established with your buddy.
30 * TpSessionChannel represents this connection. TpSession account makes automatically channel when
31 * you send message to your buddy's address. If you send successive messages to same buddy with
32 * TpSessionAccount, it automatically reuses existing connection.
35 * \fn void TpSessionChannel::channelReady(TpSessionChannel *);
37 * Emitted when the channel becomes ready
39 * \param TpSessionChannel pointer to channel become ready
42 * \fn void TpSessionChannel::channelDestroyed(TpSessionChannel *);
44 * Emitted when the channel is destroyed
46 * \param TpSessionChannel pointer to channel destroyed. The pointer is only for referenc to remove
47 * it from some possible places where it could be stored. It is not guaranteed to point any more valid TpSessionChannel object
50 * \fn void TpSessionChannel::messageReceived(const Tp::ReceivedMessage &,TpSessionConnection *);
52 * Emitted when any of Account Managers recived message
54 * \param Tp::ReceivedMessage Message received
55 * \param TpSessionChannel pointer to channel received message
58 * \fn void TpSessionChannel::messageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &,TpSessionChannel *);
60 * \param Tp::Message message sent
64 * Construct a new TpSessionChannel object. This constructor is called by TpSessionAccount class when
65 * new channel is created . It is not inended to be used stand alone
66 * This varient with connection and contact as parameter is intented for creationg new connection from origginator side to your peer
69 * \param conn connection where this channel is created
70 * \param contact Contacto to your peer to establish channel
74 TpSessionChannel::TpSessionChannel(Tp::ConnectionPtr conn,const Tp::ContactPtr &contact)
77 // qDebug() << "TpSessionChannel::TpSessionChannel" <<"contact.id() " << contact->id() << contact->id().toLocal8Bit().toHex();
78 request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType"),
79 TELEPATHY_INTERFACE_CHANNEL_TYPE_TEXT);
80 request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandleType"),
81 (uint) Tp::HandleTypeContact);
82 request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandle"),
83 contact->handle()[0]);
85 connect(conn->ensureChannel(request),
86 SIGNAL(finished(Tp::PendingOperation*)),
87 SLOT(onChannelCreated(Tp::PendingOperation*)));
92 * Construct a new TpSessionChannel object. This constructor is called by TpSessionAccount class when
93 * new channel is created . It is not inended to be used stand alone
94 * This varient with connection only parameter is intented for receiving new connection from your peer
97 * \param conn connection where this channel is created
101 TpSessionChannel::TpSessionChannel(Tp::TextChannelPtr ch)
103 // qDebug() << "TpSessionChannel::TpSessionChannel" <<"path " << ch->objectPath();
105 connect(channel->becomeReady(Tp::TextChannel::FeatureMessageQueue|Tp::TextChannel::FeatureMessageSentSignal),
106 SIGNAL(finished(Tp::PendingOperation *)),
107 SLOT(onChannelReady(Tp::PendingOperation *)));
111 void TpSessionChannel::onChannelCreated(Tp::PendingOperation *op)
113 // qDebug() << "TpSessionChannel::onOutgoingChannelCreated" ;
115 qWarning() << "Connection cannot become connected" ;
118 Tp::PendingChannel *pc = qobject_cast<Tp::PendingChannel *>(op);
120 channel = Tp::TextChannel::create(pc->connection(),pc->objectPath(), pc->immutableProperties());
122 connect(channel->becomeReady(Tp::TextChannel::FeatureMessageQueue | Tp::TextChannel::FeatureMessageSentSignal),
123 SIGNAL(finished(Tp::PendingOperation*)),
124 SLOT(onChannelReady(Tp::PendingOperation*)));
127 QString TpSessionChannel::objectPath()
129 return channel->objectPath();
131 QString TpSessionChannel::type()
133 return channel->channelType();
135 void TpSessionChannel::onChannelReady(Tp::PendingOperation *op)
138 //qDebug() << "TpSessionChannel::onChannelReady type=" << channel->channelType() <<"path " << channel->objectPath() <<
139 // "initiatorContact=" << (channel->initiatorContact() ? channel->initiatorContact()->id():"NULL");
141 connect(channel.data(),
142 SIGNAL(messageReceived(const Tp::ReceivedMessage &)),
143 SLOT(onMessageReceived(const Tp::ReceivedMessage &)));
144 connect(channel.data(),
145 SIGNAL(messageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &)),
146 SLOT(onMessageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &)));
147 connect(channel.data(),SIGNAL(destroyed(QObject *)),SLOT(onChannelDestroyed(QObject *)));
148 if(peerId().isEmpty()) peerContact=channel->initiatorContact(); // If this is incoming channel
149 emit channelReady(this);
150 QList<Tp::ReceivedMessage> queuedMessages = channel->messageQueue();
151 foreach(Tp::ReceivedMessage message, queuedMessages) {
152 // qDebug() << "received " << message.text();
153 emit messageReceived(message,this);
157 * Send message to to ths channel
160 * \param message message to send
163 void TpSessionChannel::sendMessage(QString message)
165 channel->send(message);
167 void TpSessionChannel::onMessageReceived(const Tp::ReceivedMessage &msg)
169 // qDebug() << "TpSessionChannel::onMessageReceived " << msg.text();
170 emit messageReceived(msg,this);
172 void TpSessionChannel::onMessageSent(const Tp::Message &msg,Tp::MessageSendingFlags sflags, const QString &flags)
174 // qDebug() << "TpSessionChannel::onMessageSen" << peerId() <<"txt:" << msg.text();;
175 emit messageSent(msg,sflags,flags,this);
178 * Get id ( address of your peer )
181 * \returns your peer id/address ir empty QString
183 QString TpSessionChannel::peerId()
185 return peerContact ? peerContact->id():QString();
188 void TpSessionChannel::onChannelDestroyed(QObject *obj)
191 qDebug() << "TpSessionChannel::onChannelDestroyed";
192 //TpSessionChannel *call = (TpSessionChannel *) obj;
193 emit channelDestroyed(this);