Initial commit
[drdanz-telepathy-kde:libktelepathy-tests.git] / offer-local-streamtube / test-offer-local-streamtube.cpp
1 /*
2 * Copyright (C) 2010 Daniele E. Domenichelli <daniele.domenichelli@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "test-offer-local-streamtube.h"
20 #include <KTelepathy/OfferLocalStreamTubeJob>
21 #include <KTelepathy/TelepathyBridge>
22
23 #include <pimo.h>
24 #include <nao.h>
25 #include <nco.h>
26 #include <telepathy.h>
27 #include <person.h>
28
29 extern "C"
30 {
31 #include <signal.h>
32 }
33
34 #include <QTimer>
35 #include <QLocalServer>
36 #include <QLocalSocket>
37
38 #include <KAboutData>
39 #include <KCmdLineArgs>
40 #include <KDebug>
41 #include <KApplication>
42 #include <KMessageBox>
43 #include <KJob>
44 #include <KNotification>
45
46 #include <Nepomuk/ResourceManager>
47 #include <Nepomuk/Resource>
48 #include <Nepomuk/Variant>
49 #include <Nepomuk/Query/QueryServiceClient>
50 #include <Nepomuk/Query/ComparisonTerm>
51 #include <Nepomuk/Query/ResourceTerm>
52 #include <Nepomuk/Query/ResourceTypeTerm>
53 #include <Nepomuk/Query/LiteralTerm>
54 #include <Nepomuk/Query/AndTerm>
55 #include <Nepomuk/Query/Result>
56
57 #include <informationelement.h>
58 #include <imaccount.h>
59
60
61 namespace
62 {
63     static void signal_handler(int signal)
64     {
65         if ((signal == SIGTERM) || (signal == SIGINT)) {
66             QCoreApplication * const app(QCoreApplication::instance());
67             if (app != 0) {
68                 kDebug() << "Signal Handler Called. Exiting...";
69                 app->quit();
70             }
71         }
72     }
73 }
74
75
76 int main(int argc, char** argv)
77 {
78     KAboutData aboutData("telepathy_test_offer_local_streamtube", 0, ki18n("Test OfferLocalStreamTube"), "0.1");
79
80     KCmdLineArgs::init(argc, argv, &aboutData);
81     KApplication app;
82
83     Nepomuk::ResourceManager::instance()->init();
84
85     // Set up signal handlers.
86     if (signal(SIGINT, signal_handler) == SIG_ERR) {
87         kWarning() << "Setting up SIGINT signal handler failed.";
88     }
89
90     if (signal(SIGTERM, signal_handler) == SIG_ERR) {
91         kWarning() << "Setting up SIGTERM signal handler failed.";
92     }
93
94     Test::OfferLocalStreamTube* initiator = new Test::OfferLocalStreamTube(0);
95     initiator->show();
96
97     // Start event loop.
98     return app.exec();
99 }
100
101
102
103 namespace Test {
104
105 OfferLocalStreamTube::OfferLocalStreamTube(QWidget* parent)
106     : QDialog(parent)
107 {
108     kDebug();
109
110     // Check if Nepomuk Query service client is up and running
111     if (!Nepomuk::Query::QueryServiceClient::serviceAvailable()) {
112         // That's a failure
113         KMessageBox::error(this, i18n("The Nepomuk Query Service client is not available on your system. "
114                                       "Contactlist requires the query service client to be available: please "
115                                       "check your system settings"));
116     }
117
118     setupUi(this);
119
120     // Initialize Telepathy
121     TelepathyBridge::instance()->init();
122 //    connect(TelepathyBridge::instance(),
123 //            SIGNAL(ready(bool)),
124 //            SLOT(onHandlerReady(bool)));
125
126     // Get 'me' as soon as possible
127     // FIXME: Port to new OSCAF standard for accessing "me" as soon as it
128     // becomes available.
129     Nepomuk::Thing me(QUrl::fromEncoded("nepomuk:/myself"));
130
131
132     // Loop through all the grounding instances of this person
133     foreach (Nepomuk::InformationElement resource, me.groundingOccurrences()) {
134         // See if this grounding instance is of type nco:contact.
135         if (resource.hasType(Nepomuk::Vocabulary::NCO::PersonContact())) {
136             // FIXME: We are going to assume the first NCO::PersonContact is the
137             // right one. Can we improve this?
138             m_mePersonContact = resource;
139             break;
140         }
141     }
142
143     connect(m_runPushButton, SIGNAL(clicked()),
144             this,            SLOT(onRunPushButtonClicked()));
145
146     connect(m_modeComboBox, SIGNAL(currentIndexChanged(int)),
147             this,           SLOT(onModeChanged(int)));
148 }
149
150
151 OfferLocalStreamTube::~OfferLocalStreamTube()
152 {
153     kDebug();
154 }
155
156
157 void OfferLocalStreamTube::onModeChanged(int mode)
158 {
159     switch (mode)
160     {
161         case 0:
162             m_addressLineEdit->setEnabled(false);
163             m_contactComboBox->setDisplayMode(KTelepathyAccountsWidget::DisplayModeContacts);
164             break;
165         case 1:
166             m_addressLineEdit->setEnabled(true);
167             m_contactComboBox->setDisplayMode(KTelepathyAccountsWidget::DisplayModeContacts);
168             break;
169         case 2:
170             m_addressLineEdit->setEnabled(false);
171             m_contactComboBox->setDisplayMode(KTelepathyAccountsWidget::DisplayModeContacts);
172             break;
173         case 3:
174             m_addressLineEdit->setEnabled(false);
175             m_contactComboBox->setDisplayMode(KTelepathyAccountsWidget::DisplayModeMetaContacts);
176             break;
177         case 4:
178             m_addressLineEdit->setEnabled(true);
179             m_contactComboBox->setDisplayMode(KTelepathyAccountsWidget::DisplayModeMetaContacts);
180             break;
181         case 5:
182             m_addressLineEdit->setEnabled(false);
183             m_contactComboBox->setDisplayMode(KTelepathyAccountsWidget::DisplayModeMetaContacts);
184             break;
185         default:
186             break;
187     }
188 }
189
190
191 void OfferLocalStreamTube::onRunPushButtonClicked()
192 {
193     kDebug();
194
195     int mode = m_modeComboBox->currentIndex();
196     KJob* job;
197
198     switch (mode)
199     {
200         case 0:
201         {
202             Nepomuk::PersonContact targetContact = m_contactComboBox->currentContact();
203             job = TelepathyBridge::instance()->offerLocalStreamTube(targetContact);
204             m_server = dynamic_cast<OfferLocalStreamTubeJob*>(job)->localServer();
205             break;
206         }
207         case 1:
208         {
209             Nepomuk::PersonContact targetContact = m_contactComboBox->currentContact();
210             job = TelepathyBridge::instance()->offerLocalStreamTube(targetContact, m_addressLineEdit->text().toAscii());
211             m_server = dynamic_cast<OfferLocalStreamTubeJob*>(job)->localServer();
212             break;
213         }
214         case 2:
215         {
216             Nepomuk::PersonContact targetContact = m_contactComboBox->currentContact();
217             m_server = new QLocalServer;
218             m_server->listen("/tmp/foo");
219             job = TelepathyBridge::instance()->offerLocalStreamTube(targetContact, m_server);
220             break;
221         }
222         case 3:
223         {
224             Nepomuk::Person targetMetaContact = m_contactComboBox->currentMetaContact();
225             job = TelepathyBridge::instance()->offerLocalStreamTube(targetMetaContact);
226             m_server = dynamic_cast<OfferLocalStreamTubeJob*>(job)->localServer();
227             break;
228         }
229         case 4:
230         {
231             Nepomuk::Person targetMetaContact = m_contactComboBox->currentMetaContact();
232             job = TelepathyBridge::instance()->offerLocalStreamTube(targetMetaContact, m_addressLineEdit->text().toAscii());
233             m_server = dynamic_cast<OfferLocalStreamTubeJob*>(job)->localServer();
234             break;
235         }
236         case 5:
237         {
238             Nepomuk::Person targetMetaContact = m_contactComboBox->currentMetaContact();
239             m_server = new QLocalServer;
240             m_server->listen("/tmp/foo");
241             job = TelepathyBridge::instance()->offerLocalStreamTube(targetMetaContact, m_server);
242             break;
243         }
244         default:
245             break;
246     }
247
248     if (job) {
249         connect (m_server, SIGNAL(newConnection()),
250                  this,     SLOT(onLocalServerNewConnection()));
251         connect (job,  SIGNAL(infoMessage(KJob*,QString,QString)),
252                  this, SLOT(onInfoMessage(KJob*,QString,QString)));
253     }
254
255     if (m_runComboBox->currentIndex() == 0)
256         asyncCall(job);
257     else
258         syncCall(job);
259 }
260
261
262 void OfferLocalStreamTube::asyncCall(KJob* job)
263 {
264     connect( job,  SIGNAL(result(KJob*)),
265              this, SLOT  (handleResult(KJob*)));
266     job->start();
267 }
268
269
270 void OfferLocalStreamTube::handleResult(KJob * job)
271 {
272     if ( job->error() ) {
273         // TODO do something;
274     }
275
276 //     OfferLocalStreamTubeJob* offerLocalStreamTubeJob = dynamic_cast<OfferLocalStreamTubeJob*>(job);
277 //     if(!offerLocalStreamTubeJob) {
278 //         // Not an OfferLocalStreamTubeJob
279 //         return;
280 //     }
281 //     m_outgoingStreamTubeChannel = offerLocalStreamTubeJob->outgoingStreamTubeChannel();
282 }
283
284
285 void OfferLocalStreamTube::syncCall(KJob* job)
286 {
287     if ( !job->exec() ) {
288         // An error occurred
289     } else {
290 //         OfferLocalStreamTubeJob* offerLocalStreamTubeJob = dynamic_cast<OfferLocalStreamTubeJob*>(job);
291 //         if(!offerLocalStreamTubeJob) {
292 //             // Not an OfferLocalStreamTubeJob
293 //             return;
294 //         }
295 //         m_outgoingStreamTubeChannel = offerLocalStreamTubeJob->outgoingStreamTubeChannel();
296     }
297 }
298
299
300 void OfferLocalStreamTube::onLocalServerNewConnection()
301 {
302     kDebug() << "Pending connection found";
303     QLocalSocket* socket = m_server->nextPendingConnection();
304     connect (socket, SIGNAL(readyRead()),
305              this,   SLOT(onDataFromSocket()));
306 }
307
308
309 void OfferLocalStreamTube::onDataFromSocket()
310 {
311     QIODevice *source = qobject_cast<QIODevice*>(sender());
312     QString data = QLatin1String(source->readLine());
313     data.remove(QLatin1Char('\n'));
314     kDebug() << "New data from socket: " << data;
315     if (data == QLatin1String("Hi there!!")) {
316         source->write(QByteArray("Hey back mate.\n"));
317     } else {
318         source->write(QByteArray("Sorry, I have no time for you right now.\n"));
319     }
320 }
321
322
323 void OfferLocalStreamTube::onInfoMessage(KJob* job, const QString &plain, const QString &rich)
324 {
325     Q_UNUSED(job)
326     kDebug() << plain;
327
328     KNotification::event("newOfferLocalStreamTubeEvent", plain, rich);
329 }
330
331 } // end namespace Test