X-Git-Url: http://git.maemo.org/git/?p=groupsms;a=blobdiff_plain;f=sms%2Ftpsession%2Ftpsessionchannel.cpp;fp=sms%2Ftpsession%2Ftpsessionchannel.cpp;h=930cf3d36dad785d5c370fd1e27402dada95f717;hp=0000000000000000000000000000000000000000;hb=7f26d7b5e6ae5759bb11942a69a9ada134744e98;hpb=16b4a0a6faad751e05df97f44acef4990d87a39a diff --git a/sms/tpsession/tpsessionchannel.cpp b/sms/tpsession/tpsessionchannel.cpp new file mode 100755 index 0000000..930cf3d --- /dev/null +++ b/sms/tpsession/tpsessionchannel.cpp @@ -0,0 +1,185 @@ +/* + * This file is part of TpSession + * + * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). + * Contact Kate Alhola kate.alholanokia.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "tpsessionchannel.h" + + +/** + * \class TpSessionChannel + * \headerfile + * + * + * When you start chat session or call with your buddy, channel is established with your buddy. + * TpSessionChannel represents this connection. TpSession account makes automatically channel when + * you send message to your buddy's address. If you send successive messages to same buddy with + * TpSessionAccount, it automatically reuses existing connection. + */ +/** + * \fn void TpSessionChannel::channelReady(TpSessionChannel *); + * + * Emitted when the channel becomes ready + * + * \param TpSessionChannel pointer to channel become ready + */ +/** + * \fn void TpSessionChannel::channelDestroyed(TpSessionChannel *); + * + * Emitted when the channel is destroyed + * + * \param TpSessionChannel pointer to channel destroyed. The pointer is only for referenc to remove + * it from some possible places where it could be stored. It is not guaranteed to point any more valid TpSessionChannel object + */ +/** + * \fn void TpSessionChannel::messageReceived(const Tp::ReceivedMessage &,TpSessionConnection *); + * + * Emitted when any of Account Managers recived message + * + * \param Tp::ReceivedMessage Message received + * \param TpSessionChannel pointer to channel received message + */ +/** + * \fn void TpSessionChannel::messageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &,TpSessionChannel *); + * + * \param Tp::Message message sent + */ + +/** + * Construct a new TpSessionChannel object. This constructor is called by TpSessionAccount class when + * new channel is created . It is not inended to be used stand alone + * This varient with connection and contact as parameter is intented for creationg new connection from origginator side to your peer + * + * + * \param conn connection where this channel is created + * \param contact Contacto to your peer to establish channel + */ + + +TpSessionChannel::TpSessionChannel(Tp::ConnectionPtr conn,const Tp::ContactPtr &contact) +{ + QVariantMap request; + // qDebug() << "TpSessionChannel::TpSessionChannel" <<"contact.id() " << contact->id(); + request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType"), + TELEPATHY_INTERFACE_CHANNEL_TYPE_TEXT); + request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandleType"), + (uint) Tp::HandleTypeContact); + request.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandle"), + contact->handle()[0]); + + connect(conn->ensureChannel(request), + SIGNAL(finished(Tp::PendingOperation*)), + SLOT(onChannelCreated(Tp::PendingOperation*))); + peerContact=contact; +} + +/** + * Construct a new TpSessionChannel object. This constructor is called by TpSessionAccount class when + * new channel is created . It is not inended to be used stand alone + * This varient with connection only parameter is intented for receiving new connection from your peer + * + * + * \param conn connection where this channel is created + */ + + +TpSessionChannel::TpSessionChannel(Tp::TextChannelPtr ch) +{ + // qDebug() << "TpSessionChannel::TpSessionChannel" <<"path " << ch->objectPath(); + channel=ch; + connect(channel->becomeReady(Tp::TextChannel::FeatureMessageQueue|Tp::TextChannel::FeatureMessageSentSignal), + SIGNAL(finished(Tp::PendingOperation *)), + SLOT(onChannelReady(Tp::PendingOperation *))); + +} + +void TpSessionChannel::onChannelCreated(Tp::PendingOperation *op) +{ + // qDebug() << "TpSessionChannel::onOutgoingChannelCreated" ; + if (op->isError()) { + qWarning() << "Connection cannot become connected" ; + return; + } + Tp::PendingChannel *pc = qobject_cast(op); + + channel = Tp::TextChannel::create(pc->connection(),pc->objectPath(), pc->immutableProperties()); + + connect(channel->becomeReady(Tp::TextChannel::FeatureMessageQueue | Tp::TextChannel::FeatureMessageSentSignal), + SIGNAL(finished(Tp::PendingOperation*)), + SLOT(onChannelReady(Tp::PendingOperation*))); +} + +void TpSessionChannel::onChannelReady(Tp::PendingOperation *op) +{ + // qDebug() << "TpSessionChannel::onChannelReady type=" << channel->channelType() <<"path " << channel->objectPath() << + // "initiatorContact=" << (channel->initiatorContact() ? channel->initiatorContact()->id():"NULL") ; + ; + connect(channel.data(), + SIGNAL(messageReceived(const Tp::ReceivedMessage &)), + SLOT(onMessageReceived(const Tp::ReceivedMessage &))); + connect(channel.data(), + SIGNAL(messageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &)), + SLOT(onMessageSent(const Tp::Message &,Tp::MessageSendingFlags, const QString &))); + connect(channel.data(),SIGNAL(destroyed(QObject *)),SLOT(onChannelDestroyed(QObject *))); + Q_EMIT channelReady(this); + peerContact=channel->initiatorContact(); + QList queuedMessages = channel->messageQueue(); + Q_FOREACH(Tp::ReceivedMessage message, queuedMessages) { + // qDebug() << "received " << message.text(); + Q_EMIT messageReceived(message,this); + } +} +/** + * Send message to to ths channel + * + * + * \param message message to send + */ + +void TpSessionChannel::sendMessage(QString message) +{ + channel->send(message); +} +void TpSessionChannel::onMessageReceived(const Tp::ReceivedMessage &msg) +{ + // qDebug() << "TpSessionChannel::onMessageReceived " << msg.text(); + Q_EMIT messageReceived(msg,this); +}; +void TpSessionChannel::onMessageSent(const Tp::Message &msg,Tp::MessageSendingFlags sflags, const QString &flags) +{ + // qDebug() << "TpSessionChannel::onMessageSent"; + Q_EMIT messageSent(msg,sflags,flags,this); +}; +/** + * Get id ( address of your peer ) + * + * + * \returns your peer id/address ir empty QString + */ +QString TpSessionChannel::peerId() +{ + return peerContact ? peerContact->id():""; +} + +void TpSessionChannel::onChannelDestroyed(QObject *obj) +{ + // qDebug() << "TpSessionChannel::onChannelDestroyed"; + //TpSessionChannel *call = (TpSessionChannel *) obj; + Q_EMIT channelDestroyed(this); +} +