channelDestroyed hadling, TPSESSION_DEBUG env
[tpsession] / tpsession-0.1 / tpsession / tpsessionchannel.cpp
1 /*
2  * This file is part of TpSession
3  *
4  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
5  * Contact Kate Alhola  kate.alholanokia.com
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "tpsessionchannel.h"
22
23
24 /**
25  * \class TpSessionChannel
26  * \headerfile <tpsessionchannel.h>
27  *
28  *
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.
33  */
34 /**
35  * \fn void TpSessionChannel::channelReady(TpSessionChannel *);
36  *
37  * Emitted when the channel becomes ready
38  *
39  * \param  TpSessionChannel  pointer to channel become ready
40  */
41 /**
42  * \fn void TpSessionChannel::channelDestroyed(TpSessionChannel *);
43  *
44  * Emitted when the channel is destroyed
45  *
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
48  */
49 /**
50  * \fn void TpSessionChannel::messageReceived(const Tp::ReceivedMessage &,TpSessionConnection *);
51  *
52  * Emitted when any of Account Managers recived message
53  *
54  * \param  Tp::ReceivedMessage  Message received
55  * \param  TpSessionChannel  pointer to channel received message
56  */
57 /**
58   * \fn void TpSessionChannel::messageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &,TpSessionChannel *);
59   *
60   * \param Tp::Message message sent
61   */
62
63 /**
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
67  *
68  *
69  * \param conn connection where this channel is created
70  * \param contact  Contacto to your peer to establish channel
71  */
72
73
74 TpSessionChannel::TpSessionChannel(Tp::ConnectionPtr conn,const Tp::ContactPtr &contact)
75 {
76     QVariantMap request;
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]);
84
85     connect(conn->ensureChannel(request),
86             SIGNAL(finished(Tp::PendingOperation*)),
87             SLOT(onChannelCreated(Tp::PendingOperation*)));
88     peerContact=contact;
89 }
90
91 /**
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
95  *
96  *
97  * \param conn connection where this channel is created
98  */
99
100
101 TpSessionChannel::TpSessionChannel(Tp::TextChannelPtr ch)
102 {
103   if(TpSession::tpsDebug()) qDebug() << "TpSessionChannel::TpSessionChannel" <<"path " << ch->objectPath();
104      channel=ch;
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()));
111
112 }
113
114 void TpSessionChannel::onChannelCreated(Tp::PendingOperation *op)
115 {
116   if(TpSession::tpsDebug())qDebug() << "TpSessionChannel::onOutgoingChannelCreated" ;
117    if (op->isError()) {
118      qWarning() << "Connection cannot become connected" ;
119      return;
120    }
121    Tp::PendingChannel *pc = qobject_cast<Tp::PendingChannel *>(op);
122
123    channel = Tp::TextChannel::create(pc->connection(),pc->objectPath(), pc->immutableProperties());
124
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()));
131 }
132
133 QString TpSessionChannel::objectPath()
134 {
135         return channel->objectPath();
136 }
137 QString TpSessionChannel::type()
138 {
139         return channel->channelType();
140 }
141 void TpSessionChannel::onChannelReady(Tp::PendingOperation *op)
142 {
143   Q_UNUSED(op);
144   if(TpSession::tpsDebug()) qDebug() << "TpSessionChannel::onChannelReady type=" << channel->channelType() <<"path " << channel->objectPath() <<
145             "initiatorContact=" << (channel->initiatorContact() ? channel->initiatorContact()->id():"NULL");
146          ;
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);
160     }
161 }
162 /**
163  *  Send message to to ths channel
164  *
165  *
166  * \param message   message to send
167  */
168
169 void TpSessionChannel::sendMessage(QString message)
170 {
171     channel->send(message);
172 }
173 void TpSessionChannel::onMessageReceived(const Tp::ReceivedMessage &msg)
174 {
175   //    qDebug() << "TpSessionChannel::onMessageReceived " << msg.text();
176     emit messageReceived(msg,this);
177 };
178 void TpSessionChannel::onMessageSent(const Tp::Message &msg,Tp::MessageSendingFlags sflags, const QString &flags)
179 {
180  //   qDebug() << "TpSessionChannel::onMessageSen" << peerId() <<"txt:" << msg.text();;
181     emit messageSent(msg,sflags,flags,this);
182 };
183 /**
184  *  Get id ( address of your peer )
185  *
186  *
187  * \returns your peer id/address ir empty QString
188  */
189 QString TpSessionChannel::peerId()
190 {
191     return peerContact ? peerContact->id():QString();
192 }
193
194 void TpSessionChannel::onChannelDestroyed()
195 {
196    if(TpSession::tpsDebug()) qDebug() << "TpSessionChannel::onChannelDestroyed" << peerId() << objectPath();
197     //TpSessionChannel *call = (TpSessionChannel *) obj;
198      emit channelDestroyed(this);
199 }
200