tpsession initial import
[tpsession] / tpsession-0.1 / tpsession / tpsession.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 "tpsession.h"
22 #include <QDebug>
23
24 TpSession::TpSession(QString cmname,bool synchronous)
25 {
26     Tp::registerTypes();
27     Tp::enableDebug(false);
28     Tp::enableWarnings(false);
29
30     mAM = Tp::AccountManager::create();
31     reqCm=cmname;
32     connect(mAM->becomeReady(),
33             SIGNAL(finished(Tp::PendingOperation *)),
34             SLOT(onAMReady(Tp::PendingOperation *)));
35     connect(mAM.data(),
36             SIGNAL(accountCreated(const QString &)),
37             SLOT(onAccountCreated(const QString &)));
38
39    // createObserver();
40   if(synchronous) loop.exec(); // Loop locally untill accounts are initialized
41    reqCm=cmname;
42
43 }
44 TpSession* TpSession::instancePtr=NULL;
45 TpSession* TpSession::instance(bool synchronous)
46 {
47     if(instancePtr==NULL) instancePtr=new TpSession("ring",synchronous);
48     return instancePtr;
49 };
50
51 void TpSession::onAMReady(Tp::PendingOperation *op)
52 {
53  qDebug() << "TpSession::onAMReady";
54  TpSessionAccount *tpacc;
55
56    foreach (const QString &path, mAM->allAccountPaths()) {
57        accounts+=tpacc=new TpSessionAccount(mAM, path);
58        connect(tpacc,SIGNAL(accountReady(TpSessionAccount*)),
59                       SLOT(onAccountReady(TpSessionAccount *)));
60     }
61
62 }
63
64 void TpSession::onReady(Tp::PendingOperation *)
65 {
66 };
67
68 void TpSession::onAccountCreated(const QString &path)
69 {
70
71     accounts+=new TpSessionAccount(mAM, path);
72 }
73
74 void TpSession::onAccountReady(TpSessionAccount *tpacc)
75 {
76     qDebug() << "TpSession::onAccountReady:Account " << tpacc->acc->cmName() << "is Ready";
77     connect(tpacc,SIGNAL(messageReceived(const Tp::ReceivedMessage &,TpSessionAccount *)),
78                   SLOT(onMessageReceived(const Tp::ReceivedMessage &,TpSessionAccount *)));
79     if(!reqCm.isEmpty() && tpacc->acc->cmName()==reqCm) {
80     if(sync) {
81         sync=false;
82         loop.quit();
83         emit accountReady(tpacc);
84      if(!reqMsg.isEmpty()) tpacc->sendMessageToAddress(reqAddress,reqMsg);
85     }
86   }
87 }
88
89 void TpSession::onMessageReceived(const Tp::ReceivedMessage &msg,TpSessionAccount *acc)
90 {
91     qDebug() << "TestProg::onMessageReceived " << msg.text() << "from " << msg.sender()->id();
92     emit messageReceived(msg,acc);
93 }
94
95 void TpSession::sendMessageToAddress(QString connectionMgr,QString address,QString message)
96 {
97  TpSessionAccount *tpsa=getAccount(connectionMgr);
98  if(tpsa) tpsa->sendMessageToAddress(address,message);
99 }
100
101 TpSessionAccount* TpSession::getAccount(const  QString cm,QString protocol)
102 {
103  qDebug() << "TpSession::getAccount" << cm << " " << protocol;
104  foreach (TpSessionAccount *tpacc, accounts) {
105      if((!cm.isEmpty()  && tpacc->acc->cmName()==cm) || (!protocol.isEmpty() && tpacc->acc->protocol()==protocol)) {
106      qDebug() << "TpSession::getAccount found" << tpacc->acc->cmName() << " " << tpacc->acc->protocol();
107      return tpacc;
108      }
109  }
110  return NULL;
111 }
112
113 void TpSession::createObserver()
114 {
115
116     qDebug() << __PRETTY_FUNCTION__ ;
117
118     registrar = Tp::ClientRegistrar::create();
119
120     Tp::ChannelClassList channelFilters;
121     QMap<QString, QDBusVariant> textFilter, mediaFilter;
122     // Registering Text channel observer
123     textFilter.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType"),
124                   QDBusVariant(TELEPATHY_INTERFACE_CHANNEL_TYPE_TEXT));
125     textFilter.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandleType"),
126                   QDBusVariant(Tp::HandleTypeContact));
127     channelFilters.append(textFilter);
128
129     // Registering Media channel observer
130     mediaFilter.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".ChannelType"),
131                   QDBusVariant(TELEPATHY_INTERFACE_CHANNEL_TYPE_STREAMED_MEDIA));
132     mediaFilter.insert(QLatin1String(TELEPATHY_INTERFACE_CHANNEL ".TargetHandleType"),
133                   QDBusVariant(Tp::HandleTypeContact));
134     channelFilters.append(mediaFilter);
135
136     TpSessionObserver* observer = new TpSessionObserver( channelFilters, this );
137     bool registered = registrar->registerClient(
138       Tp::AbstractClientPtr::dynamicCast(Tp::SharedPtr<TpSessionObserver>(observer)),
139       "TpSessionChannelObserver");
140
141         qDebug() << "TpSession::createObserver" << (registered ? "started" : "failed");
142
143 }
144
145
146 void TpSession::createChannelListener(const QString &channelType,
147                                    const Tp::MethodInvocationContextPtr<> &context,
148                                    const Tp::AccountPtr &account,
149                                    const Tp::ChannelPtr &channel)
150 {
151     qDebug() << "TpSession::createChannelListener";
152
153     QString channelObjectPath = channel->objectPath();
154
155
156     if ( channels.contains( channelObjectPath ) &&
157          !channelType.isEmpty() &&
158          !channelObjectPath.isEmpty() ) {
159         qDebug() << "TELEPATHY_ERROR_INVALID_ARGUMENT";
160         return;
161     }
162     qDebug() << "creating listener for: " << channelObjectPath << " type " << channelType;
163 #if 0
164     ChannelListener* listener = 0;
165     if( channelType == TELEPATHY_INTERFACE_CHANNEL_TYPE_TEXT ) {
166         listener = new TextChannelListener(account, channel, context);
167     } else if ( channelType == TELEPATHY_INTERFACE_CHANNEL_TYPE_STREAMED_MEDIA ) {
168         listener = new StreamChannelListener(account, channel, context);
169     }
170
171     if(listener) {
172         connect(listener, SIGNAL(channelClosed(ChannelListener *)),
173                 this, SLOT(channelClosed(ChannelListener *)));
174         Channels.append( channelObjectPath );
175     }
176 #endif
177 }
178
179
180
181
182
183
184
185