Vers 0.1.3, extra qDebug printouts commented out
[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();
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   //     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
109 }
110
111 void TpSessionChannel::onChannelCreated(Tp::PendingOperation *op)
112 {
113   //   qDebug() << "TpSessionChannel::onOutgoingChannelCreated" ;
114    if (op->isError()) {
115      qWarning() << "Connection cannot become connected" ;
116      return;
117    }
118    Tp::PendingChannel *pc = qobject_cast<Tp::PendingChannel *>(op);
119
120    channel = Tp::TextChannel::create(pc->connection(),pc->objectPath(), pc->immutableProperties());
121
122     connect(channel->becomeReady(Tp::TextChannel::FeatureMessageQueue | Tp::TextChannel::FeatureMessageSentSignal),
123             SIGNAL(finished(Tp::PendingOperation*)),
124             SLOT(onChannelReady(Tp::PendingOperation*)));
125 }
126
127 void TpSessionChannel::onChannelReady(Tp::PendingOperation *op)
128 {
129   // qDebug() << "TpSessionChannel::onChannelReady type=" << channel->channelType() <<"path " << channel->objectPath() <<
130   //            "initiatorContact=" << (channel->initiatorContact() ? channel->initiatorContact()->id():"NULL") ;
131          ;
132  connect(channel.data(),
133          SIGNAL(messageReceived(const Tp::ReceivedMessage &)),
134          SLOT(onMessageReceived(const Tp::ReceivedMessage &)));
135  connect(channel.data(),
136          SIGNAL(messageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &)),
137          SLOT(onMessageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &)));
138  connect(channel.data(),SIGNAL(destroyed(QObject *)),SLOT(onChannelDestroyed(QObject *)));
139  emit channelReady(this);
140  peerContact=channel->initiatorContact();
141  QList<Tp::ReceivedMessage> queuedMessages = channel->messageQueue();
142  foreach(Tp::ReceivedMessage message, queuedMessages) {
143    //      qDebug()  << "received " << message.text();
144            emit messageReceived(message,this);
145     }
146 }
147 /**
148  *  Send message to to ths channel
149  *
150  *
151  * \param message   message to send
152  */
153
154 void TpSessionChannel::sendMessage(QString message)
155 {
156     channel->send(message);
157 }
158 void TpSessionChannel::onMessageReceived(const Tp::ReceivedMessage &msg)
159 {
160   //    qDebug() << "TpSessionChannel::onMessageReceived " << msg.text();
161     emit messageReceived(msg,this);
162 };
163 void TpSessionChannel::onMessageSent(const Tp::Message &msg,Tp::MessageSendingFlags sflags, const QString &flags)
164 {
165   //    qDebug() << "TpSessionChannel::onMessageSent";
166     emit messageSent(msg,sflags,flags,this);
167 };
168 /**
169  *  Get id ( address of your peer )
170  *
171  *
172  * \returns your peer id/address ir empty QString
173  */
174 QString TpSessionChannel::peerId()
175 {
176     return peerContact ? peerContact->id():"";
177 }
178
179 void TpSessionChannel::onChannelDestroyed(QObject *obj)
180 {
181   //    qDebug() << "TpSessionChannel::onChannelDestroyed";
182     //TpSessionChannel *call = (TpSessionChannel *) obj;
183      emit channelDestroyed(this);
184 }
185