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 "tpsessionaccount.h"
22 #include <TelepathyQt4/Message>
25 * \class TpSessionAccount
26 * \headerfile <tpsessionaccount.h>
28 * TpSessionAccount class represents every account you have. As example account for “Ring” connection manager represents your cellular
29 * account and you may send and receive SMS with it. Gabble represents your GoogleTalk account if you have defined them.
30 * TpSessionAccounts are created by TpSession class,they are not intended to be created stand-alone
34 * \fn void TpSessionAccount::accountReady(TpSessionAccount *);
36 * Emitted when the account becomes ready
38 * \param TpSessionAccount pointer to account become ready
41 * \fn void TpSessionAccount::channelReady(TpSessionAccount *);
43 * Emitted when the account Manager becomes ready
45 * \param TpSession pointer to TpSession class
48 * \fn void TpSessionAccount::messageReceived(const Tp::ReceivedMessage &,TpSessionAccount *);
50 * Emitted when any of Account Managers recived message
52 * \param Tp::ReceivedMessage Message received
53 * \param TpSessionAccount pointer to account received message
57 * Construct a new TpSessionAccount object. This constructor is called by TpSession class when new account is created or fetched from account manager. It is not inended to be used stand alone
59 * \param am Telepathy-Qt4 account manager for this account
60 * \param objectPath Dbus object path tonew account
62 TpSessionAccount::TpSessionAccount(Tp::AccountManagerPtr am,const QString &objectPath):
63 mAcc(Tp::Account::create(am->dbusConnection(),am->busName(), objectPath))
66 connect(mAcc->becomeReady(),SIGNAL(finished(Tp::PendingOperation *)),SLOT(onReady(Tp::PendingOperation *)));
68 qDebug() << "TpSessionAccount::TpSessionAccount objectPath=" << objectPath;
72 void TpSessionAccount::onReady(Tp::PendingOperation *op)
76 qDebug() << "TpSessionAccount::onReady cmName=" << acc->cmName() << "haveConnection=" <<
77 (acc->haveConnection()? ( acc->connection()->isReady() ? "Ready":"notReady"):"no");
79 if(acc->haveConnection()) {
81 connect(acc->connection()->becomeReady(Tp::Connection::FeatureRoster | Tp::Connection::FeatureSelfContact ),
82 SIGNAL(finished(Tp::PendingOperation *)),
83 SLOT(onContactsConnectionReady(Tp::PendingOperation *)));
84 if (acc->connection()->isReady() && acc->connection()->interfaces().contains(TELEPATHY_INTERFACE_CONNECTION_INTERFACE_REQUESTS)) {
85 qDebug() << "TpSessionAccount::onReady: connecting to Connection.Interface.NewChannels";
86 connect(acc->connection()->requestsInterface(),
87 SIGNAL(NewChannels(const Tp::ChannelDetailsList&)),
88 SLOT(onNewChannels(const Tp::ChannelDetailsList&)));
91 else { // If there is no connection, we are ready now, else we are ready when contacts connection is ready
93 emit accountReady(this);
97 void TpSessionAccount::onContactsConnectionReady(Tp::PendingOperation *op)
100 qWarning() << "Connection cannot become ready" << acc->cmName();
104 if (acc->connection()->interfaces().contains(TELEPATHY_INTERFACE_CONNECTION_INTERFACE_REQUESTS)) {
105 qDebug() << "TpSessionAccount::onContactsConectionReady: connecting to Connection.Interface.NewChannels";
106 connect(acc->connection()->requestsInterface(),
107 SIGNAL(NewChannels(const Tp::ChannelDetailsList&)),
108 SLOT(onNewChannels(const Tp::ChannelDetailsList&)));
109 } else qDebug() << "TpSessionAccount::onContactsConnectionReady: does NO have CONNECTION_INTERFACE_REQUESTS";
110 Tp::PendingReady *pr = qobject_cast<Tp::PendingReady *>(op);
111 contactsConn = Tp::ConnectionPtr(qobject_cast<Tp::Connection *>(pr->object()));
113 connect(contactsConn->contactManager(),
114 SIGNAL(presencePublicationRequested(const Tp::Contacts &)),
115 SLOT(onPresencePublicationRequested(const Tp::Contacts &)));
117 qDebug() << "TpSessionAccount::onContactsConnectionReady "<< acc->cmName() ;
120 myContacts=contactsConn->contactManager()->allKnownContacts();
121 foreach (const Tp::ContactPtr &contact, myContacts) {
122 qDebug() << "id=" <<contact->id() << " alias=" << contact->alias() << " presence=" << contact->presenceStatus() ;
123 if(contact->id()==reqContact) {
124 addOutgoingChannel(contact);
128 if(!reqContact.isEmpty() ) makeContactFromAddress(reqContact);
130 emit accountReady(this);
135 * Fetch Tp::ContactPtr for contact with given address. Contact is searched among contacts returned by contact manager for ths account.
136 * All connecions managers does not return contacts, as example Ring telephony contact manager does not. Gabble for Googletalk or Spirit for Skype does
139 * \param id Contact address/id, as example email address, telephone number etc. Only exact matches
140 * \return TpContactPtr, if nontact is not returned TpContactPtr.isNull() is true
143 Tp::ContactPtr TpSessionAccount::getContactFromAddress(QString id)
146 foreach (const Tp::ContactPtr &contact, myContacts) {
147 if(contact->id()==reqContact) return p=contact;
152 * Fetch TpSessionChannel for with given address. Contact is searched among active channels for this account.
155 * \param id Contact address/id, as example email address, telephone number etc. Only exact matches
156 * \return Pointer to TpSessionChannel or NULL if nit found
159 TpSessionChannel* TpSessionAccount::getChannelFromPeerAddress(QString id)
161 TpSessionChannel* p=NULL;
162 foreach (TpSessionChannel* channel, myChannels) {
163 if(channel->peerId()==id) return p=channel;
168 * Creates new contact with given address. This function is Acynchronous, it sends request to contact manager for contact creation,
170 * \param address Contact address/id, as example email address, telephone number etc.
173 void TpSessionAccount::makeContactFromAddress(QString address)
175 reqContact=address; // When we get retrieved signal, we check if it is this one
176 Tp::PendingContacts *pc=contactsConn->contactManager()->contactsForIdentifiers(QStringList(address));
177 connect(pc,SIGNAL(finished(Tp::PendingOperation *)),SLOT(onNewContactRetrieved(Tp::PendingOperation *)));
180 void TpSessionAccount::onNewContactRetrieved(Tp::PendingOperation *op)
182 Tp::PendingContacts *pcontacts = qobject_cast<Tp::PendingContacts *>(op);
183 QList<Tp::ContactPtr> contacts = pcontacts->contacts();
184 QString username = pcontacts->identifiers().first();
185 if (contacts.size() != 1 || !contacts.first()) {
186 qDebug() << "Unable to add contact " <<reqContact;
190 Tp::ContactPtr contact = contacts.first();
191 qDebug() << "TpSessionAccount::onContactRetrieved" << reqContact;
192 if(!reqContact.isEmpty()) addOutgoingChannel(contacts.first());
195 * Send message to given address. This function is compled Acynchronous function that may produce multiple state transitions beforecomletion.
196 * If there is already existing TpSessionChannel for this contact, it simply queues message for sending and no forther transitions are needed
197 * If there are no hannel, it first check is there contact for this address, if is, it requests new channel to be created for ths channel and message
198 * is left waiting untill channel is created. If there is no contact, it sends request fr contact creation and when contact is created state machine
199 * proceeds to channel creation.
201 * MessageSent() signal is emitted when completed
203 * \param address Contact address/id, as example email address, telephone number etc.
204 * \param message Message string
207 void TpSessionAccount::sendMessageToAddress(QString address,QString message)
210 TpSessionChannel* channel=getChannelFromPeerAddress(address);
212 channel->sendMessage(message); // We have already channel
215 p=getContactFromAddress(address); // Do we have contact ready ?
216 if(p.isNull()) // If not, create it
217 makeContactFromAddress(address); // Create and after created, send
219 addOutgoingChannel(p); // Create channel and when ready, send
223 void TpSessionAccount::addOutgoingChannel(const Tp::ContactPtr &contact)
227 qDebug() << "TpSessionAccount::addOutgoingChannel";
229 TpSessionChannel* newChannel=new TpSessionChannel(contact->manager()->connection(),contact);
230 connect(newChannel,SIGNAL(messageReceived(const Tp::ReceivedMessage &,TpSessionChannel *)),
231 SLOT(onMessageReceived(const Tp::ReceivedMessage &,TpSessionChannel *)));
232 connect(newChannel,SIGNAL(channelReady(TpSessionChannel *)),
233 SLOT(onOutgoingChannelReady(TpSessionChannel*)));
234 myChannels+=newChannel;
238 void TpSessionAccount::onOutgoingChannelReady(TpSessionChannel *ch)
240 qDebug() << "TpSessionAccoiunt::onOutgoingChannelReady";
241 emit channelReady(this);
242 if(!reqMessage.isEmpty()) ch->sendMessage(reqMessage);
247 void TpSessionAccount::onMessageSent(const Tp::Message &msg,Tp::MessageSendingFlags, const QString &flags)
249 qDebug() << "TpSessionAccount::onMessageSent";
250 emit messageSent(msg,this);
253 void TpSessionAccount::onMessageReceived(const Tp::ReceivedMessage &msg,TpSessionChannel *ch)
255 qDebug() << "TpSessionAccount::onMessageReceived " << msg.text();
256 emit messageReceived(msg,this);
259 void TpSessionAccount::onNewChannels(const Tp::ChannelDetailsList &channels)
262 Tp::TextChannelPtr myIngoingTextChannel;
263 qDebug() << "TpSessionAccount::onNewChannels";
264 foreach (const Tp::ChannelDetails &details, channels) {
265 QString channelType = details.properties.value(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType")).toString();
266 QString targetId = details.properties.value(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetID")).toString();
267 bool requested = details.properties.value(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".Requested")).toBool();
268 qDebug() << " channelType:" << channelType <<" requested :" << requested << " targetId" << targetId;
270 emit newChannel(this,channelType,targetId,details);
271 if (channelType == TELEPATHY_INTERFACE_CHANNEL_TYPE_TEXT && !requested) {
273 myIngoingTextChannel = Tp::TextChannel::create(acc->connection(),details.channel.path(),details.properties);
274 qDebug() << "TpSessionAccount::onNewChannels path=" <<"path " << myIngoingTextChannel->objectPath();
276 TpSessionChannel* newChannel=new TpSessionChannel( myIngoingTextChannel);
277 connect(newChannel,SIGNAL(messageReceived(const Tp::ReceivedMessage &,TpSessionChannel *)),
278 SLOT(onMessageReceived(const Tp::ReceivedMessage &,TpSessionChannel *)));
279 myChannels+=newChannel;
281 if (channelType == TELEPATHY_INTERFACE_CHANNEL_TYPE_STREAMED_MEDIA && !requested) {
282 qDebug() << "Incoming call" ;