tpsession initial import
[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 TpSessionChannel::TpSessionChannel(Tp::ConnectionPtr conn,const Tp::ContactPtr &contact)
24 {
25     QVariantMap request;
26     qDebug() << "TpSessionChannel::TpSessionChannel" <<"contact.id() " << contact->id();
27     request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType"),
28                    TELEPATHY_INTERFACE_CHANNEL_TYPE_TEXT);
29     request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandleType"),
30                    (uint) Tp::HandleTypeContact);
31     request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandle"),
32                    contact->handle()[0]);
33
34     connect(conn->ensureChannel(request),
35             SIGNAL(finished(Tp::PendingOperation*)),
36             SLOT(onChannelCreated(Tp::PendingOperation*)));
37     peerContact=contact;
38 }
39
40
41
42 TpSessionChannel::TpSessionChannel(Tp::TextChannelPtr ch)
43 {
44      qDebug() << "TpSessionChannel::TpSessionChannel" <<"path " << ch->objectPath();
45      channel=ch;
46      connect(channel->becomeReady(Tp::TextChannel::FeatureMessageQueue|Tp::TextChannel::FeatureMessageSentSignal),
47                     SIGNAL(finished(Tp::PendingOperation *)),
48                     SLOT(onChannelReady(Tp::PendingOperation *)));
49
50 }
51
52 void TpSessionChannel::onChannelCreated(Tp::PendingOperation *op)
53 {
54    qDebug() << "TpSessionChannel::onOutgoingChannelCreated" ;
55    if (op->isError()) {
56      qWarning() << "Connection cannot become connected" ;
57      return;
58    }
59    Tp::PendingChannel *pc = qobject_cast<Tp::PendingChannel *>(op);
60
61    channel = Tp::TextChannel::create(pc->connection(),pc->objectPath(), pc->immutableProperties());
62
63     connect(channel->becomeReady(Tp::TextChannel::FeatureMessageQueue | Tp::TextChannel::FeatureMessageSentSignal),
64             SIGNAL(finished(Tp::PendingOperation*)),
65             SLOT(onChannelReady(Tp::PendingOperation*)));
66 }
67
68 void TpSessionChannel::onChannelReady(Tp::PendingOperation *op)
69 {
70  qDebug() << "TpSessionChannel::onChannelReady type=" << channel->channelType() <<"path " << channel->objectPath() <<
71             "initiatorContact=" << (channel->initiatorContact() ? channel->initiatorContact()->id():"NULL") ;
72          ;
73  connect(channel.data(),
74          SIGNAL(messageReceived(const Tp::ReceivedMessage &)),
75          SLOT(onMessageReceived(const Tp::ReceivedMessage &)));
76  connect(channel.data(),
77          SIGNAL(messageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &)),
78          SLOT(onMessageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &)));
79  emit channelReady(this);
80  peerContact=channel->initiatorContact();
81  QList<Tp::ReceivedMessage> queuedMessages = channel->messageQueue();
82  foreach(Tp::ReceivedMessage message, queuedMessages) {
83       qDebug()  << "received " << message.text();
84            emit messageReceived(message,this);
85     }
86 }
87 void TpSessionChannel::sendMessage(QString message)
88 {
89     channel->send(message);
90 }
91 void TpSessionChannel::onMessageReceived(const Tp::ReceivedMessage &msg)
92 {
93     qDebug() << "TpSessionChannel::onMessageReceived " << msg.text();
94     emit messageReceived(msg,this);
95 };
96 void TpSessionChannel::onMessageSent(const Tp::Message &msg,Tp::MessageSendingFlags sflags, const QString &flags)
97 {
98     qDebug() << "TpSessionChannel::onMessageSent";
99     emit messageSent(msg,sflags,flags,this);
100 };
101
102 QString TpSessionChannel::peerId()
103 {
104     return peerContact ? peerContact->id():"";
105 }