Add server and client files
[miniature:miniature.git] / src / telepathy / tpoutgoingtube.cc
1 /* Miniature - A chess board that goes always with you, ready to let
2  * you play and learn wherever you go.
3  *
4  * Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
5  *              Dariusz Mikulski <dariusz.mikulski@collabora.co.uk>
6  *
7  *
8  * Miniature is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Miniature is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Miniature. If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "tpoutgoingtube.h"
23 #include "tphelpers.h"
24
25 #include <TelepathyQt4/PendingReady>
26
27 #include <QHostAddress>
28 #include <QNetworkInterface>
29
30 namespace Miniature
31 {
32
33 TpOutgoingTube::TpOutgoingTube(const Tp::ChannelPtr &channel, QObject *parent)
34     : QObject(parent),
35     mChannel(channel)
36 {
37     qDebug() << "TpOutgoingTube::TpOutgoingTube()";
38
39     qDBusRegisterMetaType<StreamTubeAddress>();
40
41     Tp::Features features;
42     features << Tp::Channel::FeatureCore;
43     connect(mChannel->becomeReady(features),
44             SIGNAL(finished(Tp::PendingOperation *)),
45             SLOT(onChannelReady(Tp::PendingOperation *)));
46 }
47
48 TpOutgoingTube::~TpOutgoingTube()
49 {
50
51 }
52
53 void TpOutgoingTube::onChannelReady(Tp::PendingOperation *op)
54 {
55     qDebug() << "TpOutgoingTube::onChannelReady()";
56
57     if(op->isError())
58     {
59         qDebug() << "Channel ready error: " << op->errorName() << op->errorMessage();
60         return;
61     }
62
63     Tp::Contacts contacts = mChannel->groupContacts();
64
65     Q_FOREACH(Tp::ContactPtr contact, contacts)
66     {
67         qDebug() << contact->id();
68     }
69
70     // upgrade contacts from contact manager ?
71     connect(mChannel.data(),
72             SIGNAL(invalidated(Tp::DBusProxy *, const QString &, const QString &)),
73             SLOT(onChannelInvalidated(Tp::DBusProxy *, const QString &, const QString &)));
74
75     mTubeInterface = mChannel->tubeInterface();
76     mStreamTubeInterface = mChannel->streamTubeInterface();
77
78     if(mStreamTubeInterface && mTubeInterface)
79     {
80         qDebug() << "We have tube control interfaces. Offering tube.";
81
82         connect(mTubeInterface,
83                 SIGNAL(TubeChannelStateChanged(uint)),
84                 SLOT(onTubeStateChanged(uint)));
85
86         if(!server.listen())
87         {
88             qDebug() << "Error server listening";
89             return;
90         }
91
92         QVariantMap parameters;
93
94         QString ipAddress;
95         QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
96         // use the first non-localhost IPv4 address
97         for (int i = 0; i < ipAddressesList.size(); ++i) {
98             if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
99                 ipAddressesList.at(i).toIPv4Address()) {
100                 ipAddress = ipAddressesList.at(i).toString();
101                 break;
102             }
103         }
104         // if we did not find one, use IPv4 localhost
105         if (ipAddress.isEmpty())
106             ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
107
108         StreamTubeAddress streamTubeAddress;
109         streamTubeAddress.address = ipAddress;
110         streamTubeAddress.port = server.serverPort();
111
112         qDebug() << "Server address:" << streamTubeAddress.address << "port:" << streamTubeAddress.port;
113
114         QDBusVariant address;
115         address.setVariant(qVariantFromValue(streamTubeAddress));
116
117         qDebug() << "Address qdbusvariant:" << address.variant().toString();
118
119         QDBusPendingReply<> ret = mStreamTubeInterface->Offer(
120                 uint(Tp::SocketAddressTypeIPv4),
121                 address,
122                 uint(Tp::SocketAccessControlPort),
123                 parameters);
124
125         qDebug() << "Parameters:" << parameters;
126
127         connect(new QDBusPendingCallWatcher(ret, this),
128                 SIGNAL(finished(QDBusPendingCallWatcher *)),
129                 SLOT(onOfferTubeFinished(QDBusPendingCallWatcher *)));
130         connect(mStreamTubeInterface,
131                 SIGNAL(NewRemoteConnection(uint, QDBusVariant, uint)),
132                 SLOT(onNewRemoteConnection(uint, QDBusVariant, uint)));
133     }
134     else
135     {
136         qDebug() << "? ERROR ?";
137     }
138 }
139
140 void TpOutgoingTube::onChannelInvalidated(Tp::DBusProxy *proxy, const QString &errorName, const QString &errorMessage)
141 {
142     Q_UNUSED(proxy);
143
144     qDebug() << "Tube channel invalidated:" << errorName << errorMessage;
145
146     Q_EMIT readyToBeDeleted();
147 }
148
149 void TpOutgoingTube::onTubeStateChanged(uint state)
150 {
151     qDebug() << "Tube state changed:" << state;
152
153     Q_EMIT statusChanged(state);
154 }
155
156 void TpOutgoingTube::onOfferTubeFinished(QDBusPendingCallWatcher *watcher)
157 {
158     QDBusPendingReply<void> reply = *watcher;
159
160     if(reply.isError())
161     {
162         qDebug() << "Offer tube failed:" << reply.error().message() << "Error name:" << reply.error().name() << "Error string:" << reply.error().errorString(reply.error().type());
163         Q_EMIT readyToBeDeleted();
164         return;
165     }
166
167     qDebug() << "Offer tube succeeded !!!!";
168 }
169
170 void TpOutgoingTube::onNewRemoteConnection(uint handle, QDBusVariant connectionParam, uint connectionId)
171 {
172     QVariant v = connectionParam.variant();
173     qDebug() << "variant =" << v << "handle:" << handle << "connectionId:" << connectionId;
174
175     //Tp::SocketAddressIPv4 ipv4address = qdbus_cast<Tp::SocketAddressIPv4>(v);
176
177     //qDebug() << "NewRemoteConnection:" << ipv4address.port << ipv4address.address;
178
179     // \todo:
180     // Start server listening!
181 }
182
183
184 };