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 if(TpSession::tpsDebug()) 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 *)));
108 connect(channel.data(),
109 SIGNAL(invalidated(Tp::DBusProxy *, const QString &, const QString &)),
110 SLOT(onChannelDestroyed()));
114 void TpSessionChannel::onChannelCreated(Tp::PendingOperation *op)
116 if(TpSession::tpsDebug())qDebug() << "TpSessionChannel::onOutgoingChannelCreated" ;
118 qWarning() << "Connection cannot become connected" ;
121 Tp::PendingChannel *pc = qobject_cast<Tp::PendingChannel *>(op);
123 channel = Tp::TextChannel::create(pc->connection(),pc->objectPath(), pc->immutableProperties());
125 connect(channel->becomeReady(Tp::TextChannel::FeatureMessageQueue | Tp::TextChannel::FeatureMessageSentSignal),
126 SIGNAL(finished(Tp::PendingOperation*)),
127 SLOT(onChannelReady(Tp::PendingOperation*)));
128 connect(channel.data(),
129 SIGNAL(invalidated(Tp::DBusProxy *, const QString &, const QString &)),
130 SLOT(onChannelDestroyed()));
133 QString TpSessionChannel::objectPath()
135 return channel->objectPath();
137 QString TpSessionChannel::type()
139 return channel->channelType();
141 void TpSessionChannel::onChannelReady(Tp::PendingOperation *op)
144 if(TpSession::tpsDebug()) qDebug() << "TpSessionChannel::onChannelReady type=" << channel->channelType() <<"path " << channel->objectPath() <<
145 "initiatorContact=" << (channel->initiatorContact() ? channel->initiatorContact()->id():"NULL");
147 connect(channel.data(),
148 SIGNAL(messageReceived(const Tp::ReceivedMessage &)),
149 SLOT(onMessageReceived(const Tp::ReceivedMessage &)));
150 connect(channel.data(),
151 SIGNAL(messageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &)),
152 SLOT(onMessageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &)));
153 connect(channel.data(),SIGNAL(destroyed(QObject *)),SLOT(onChannelDestroyed()));
154 if(peerId().isEmpty()) peerContact=channel->initiatorContact(); // If this is incoming channel
155 emit channelReady(this);
156 QList<Tp::ReceivedMessage> queuedMessages = channel->messageQueue();
157 foreach(Tp::ReceivedMessage message, queuedMessages) {
158 // qDebug() << "received " << message.text();
159 emit messageReceived(message,this);
163 * Send message to to ths channel
166 * \param message message to send
169 void TpSessionChannel::sendMessage(QString message)
171 channel->send(message);
173 void TpSessionChannel::onMessageReceived(const Tp::ReceivedMessage &msg)
175 // qDebug() << "TpSessionChannel::onMessageReceived " << msg.text();
176 emit messageReceived(msg,this);
178 void TpSessionChannel::onMessageSent(const Tp::Message &msg,Tp::MessageSendingFlags sflags, const QString &flags)
180 // qDebug() << "TpSessionChannel::onMessageSen" << peerId() <<"txt:" << msg.text();;
181 emit messageSent(msg,sflags,flags,this);
184 * Get id ( address of your peer )
187 * \returns your peer id/address ir empty QString
189 QString TpSessionChannel::peerId()
191 return peerContact ? peerContact->id():QString();
194 void TpSessionChannel::onChannelDestroyed()
196 if(TpSession::tpsDebug()) qDebug() << "TpSessionChannel::onChannelDestroyed" << peerId() << objectPath();
197 //TpSessionChannel *call = (TpSessionChannel *) obj;
198 emit channelDestroyed(this);