From 70c376063aa3a30db16facd8c103b2c6ef3b3e36 Mon Sep 17 00:00:00 2001 From: Jeremy Lainé Date: Tue, 10 Aug 2010 16:22:18 +0000 Subject: rename "example" to "examples" --- examples/example.pri | 19 + examples/example.pro | 13 + examples/example_0_connected/README | 5 + .../example_0_connected/example_0_connected.pro | 7 + examples/example_0_connected/main.cpp | 47 ++ examples/example_1_echoClient/README | 5 + examples/example_1_echoClient/echoClient.cpp | 48 ++ examples/example_1_echoClient/echoClient.h | 42 ++ .../example_1_echoClient/example_1_echoClient.pro | 10 + examples/example_1_echoClient/main.cpp | 38 ++ examples/example_2_rosterHandling/README | 1 + .../example_2_rosterHandling.pro | 10 + examples/example_2_rosterHandling/main.cpp | 38 ++ examples/example_2_rosterHandling/xmppClient.cpp | 75 +++ examples/example_2_rosterHandling/xmppClient.h | 44 ++ .../example_3_transferHandling.pro | 8 + examples/example_3_transferHandling/main.cpp | 49 ++ examples/example_3_transferHandling/xmppClient.cpp | 117 +++++ examples/example_3_transferHandling/xmppClient.h | 48 ++ .../example_4_callHandling.pro | 10 + examples/example_4_callHandling/main.cpp | 49 ++ examples/example_4_callHandling/xmppClient.cpp | 118 +++++ examples/example_4_callHandling/xmppClient.h | 46 ++ .../example_5_rpcInterface.pro | 12 + examples/example_5_rpcInterface/main.cpp | 39 ++ .../example_5_rpcInterface/remoteinterface.cpp | 15 + examples/example_5_rpcInterface/remoteinterface.h | 20 + examples/example_5_rpcInterface/rpcClient.cpp | 38 ++ examples/example_5_rpcInterface/rpcClient.h | 40 ++ examples/example_5_rpcInterface/xmlrpctest.txt | 63 +++ .../example_6_rpcClient/example_6_rpcClient.pro | 10 + examples/example_6_rpcClient/main.cpp | 39 ++ examples/example_6_rpcClient/rpcClient.cpp | 66 +++ examples/example_6_rpcClient/rpcClient.h | 46 ++ .../example_7_archiveHandling.pro | 8 + examples/example_7_archiveHandling/main.cpp | 38 ++ examples/example_7_archiveHandling/xmppClient.cpp | 76 +++ examples/example_7_archiveHandling/xmppClient.h | 46 ++ examples/tests/tests.cpp | 534 +++++++++++++++++++++ examples/tests/tests.h | 81 ++++ examples/tests/tests.pro | 9 + 41 files changed, 2027 insertions(+) create mode 100644 examples/example.pri create mode 100644 examples/example.pro create mode 100644 examples/example_0_connected/README create mode 100644 examples/example_0_connected/example_0_connected.pro create mode 100644 examples/example_0_connected/main.cpp create mode 100644 examples/example_1_echoClient/README create mode 100644 examples/example_1_echoClient/echoClient.cpp create mode 100644 examples/example_1_echoClient/echoClient.h create mode 100644 examples/example_1_echoClient/example_1_echoClient.pro create mode 100644 examples/example_1_echoClient/main.cpp create mode 100644 examples/example_2_rosterHandling/README create mode 100644 examples/example_2_rosterHandling/example_2_rosterHandling.pro create mode 100644 examples/example_2_rosterHandling/main.cpp create mode 100644 examples/example_2_rosterHandling/xmppClient.cpp create mode 100644 examples/example_2_rosterHandling/xmppClient.h create mode 100644 examples/example_3_transferHandling/example_3_transferHandling.pro create mode 100644 examples/example_3_transferHandling/main.cpp create mode 100644 examples/example_3_transferHandling/xmppClient.cpp create mode 100644 examples/example_3_transferHandling/xmppClient.h create mode 100644 examples/example_4_callHandling/example_4_callHandling.pro create mode 100644 examples/example_4_callHandling/main.cpp create mode 100644 examples/example_4_callHandling/xmppClient.cpp create mode 100644 examples/example_4_callHandling/xmppClient.h create mode 100644 examples/example_5_rpcInterface/example_5_rpcInterface.pro create mode 100644 examples/example_5_rpcInterface/main.cpp create mode 100644 examples/example_5_rpcInterface/remoteinterface.cpp create mode 100644 examples/example_5_rpcInterface/remoteinterface.h create mode 100644 examples/example_5_rpcInterface/rpcClient.cpp create mode 100644 examples/example_5_rpcInterface/rpcClient.h create mode 100644 examples/example_5_rpcInterface/xmlrpctest.txt create mode 100644 examples/example_6_rpcClient/example_6_rpcClient.pro create mode 100644 examples/example_6_rpcClient/main.cpp create mode 100644 examples/example_6_rpcClient/rpcClient.cpp create mode 100644 examples/example_6_rpcClient/rpcClient.h create mode 100644 examples/example_7_archiveHandling/example_7_archiveHandling.pro create mode 100644 examples/example_7_archiveHandling/main.cpp create mode 100644 examples/example_7_archiveHandling/xmppClient.cpp create mode 100644 examples/example_7_archiveHandling/xmppClient.h create mode 100644 examples/tests/tests.cpp create mode 100644 examples/tests/tests.h create mode 100644 examples/tests/tests.pro (limited to 'examples') diff --git a/examples/example.pri b/examples/example.pri new file mode 100644 index 00000000..0875ca16 --- /dev/null +++ b/examples/example.pri @@ -0,0 +1,19 @@ +TEMPLATE = app + +INCLUDEPATH += ../../source + +QT += network xml + +CONFIG += console debug_and_release + +CONFIG(debug, debug|release) { + QXMPP_LIB = qxmpp_d + QXMPP_DIR = ../../source/debug +} else { + QXMPP_LIB = qxmpp + QXMPP_DIR = ../../source/release +} + +LIBS += -L$$QXMPP_DIR -l$$QXMPP_LIB +PRE_TARGETDEPS += $${QXMPP_DIR}/lib$${QXMPP_LIB}.a + diff --git a/examples/example.pro b/examples/example.pro new file mode 100644 index 00000000..c19cfde3 --- /dev/null +++ b/examples/example.pro @@ -0,0 +1,13 @@ +TEMPLATE = subdirs + +SUBDIRS = example_0_connected\ + example_1_echoClient\ + example_2_rosterHandling\ + example_3_transferHandling\ + example_4_callHandling\ + example_5_rpcInterface\ + example_6_rpcClient\ + example_7_archiveHandling\ + tests + +CONFIG += debug_and_release diff --git a/examples/example_0_connected/README b/examples/example_0_connected/README new file mode 100644 index 00000000..fb84fea9 --- /dev/null +++ b/examples/example_0_connected/README @@ -0,0 +1,5 @@ +This example just connects to the xmpp server. And start receiving presences (updates) from the server. + +After running this example, you can see this user online, if it's added in your roster (friends list). + +Logging type has been set to stdout. You can see the progress on the command line. diff --git a/examples/example_0_connected/example_0_connected.pro b/examples/example_0_connected/example_0_connected.pro new file mode 100644 index 00000000..2e38510d --- /dev/null +++ b/examples/example_0_connected/example_0_connected.pro @@ -0,0 +1,7 @@ +include(../example.pri) + +TARGET = example_0_connected + +SOURCES += main.cpp + +OTHER_FILES += README diff --git a/examples/example_0_connected/main.cpp b/examples/example_0_connected/main.cpp new file mode 100644 index 00000000..bb34f177 --- /dev/null +++ b/examples/example_0_connected/main.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Manjeet Dahiya + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#include + +#include "QXmppClient.h" +#include "QXmppLogger.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QXmppLogger::getLogger()->setLoggingType(QXmppLogger::STDOUT); + + QXmppClient client; + +// For google talk +// client.connectToServer("talk.google.com", "usernamewithoutdomain", "passwd", "gmail.com"); + +// For jabber +// client.connectToServer("jabber.org", "usernamewithoutdomain", "passwd", "jabber.org"); + + client.connectToServer("talk.google.com", "qxmpp.test1", "qxmpp123", "gmail.com"); + + return a.exec(); +} diff --git a/examples/example_1_echoClient/README b/examples/example_1_echoClient/README new file mode 100644 index 00000000..4c3ac681 --- /dev/null +++ b/examples/example_1_echoClient/README @@ -0,0 +1,5 @@ +This is a very simple bot which echoes the message sent to it. + +Run this example, send it a message from a friend of this bot. You will recieve the message back. + +This example shows how to receive and send messages. diff --git a/examples/example_1_echoClient/echoClient.cpp b/examples/example_1_echoClient/echoClient.cpp new file mode 100644 index 00000000..a6780bcc --- /dev/null +++ b/examples/example_1_echoClient/echoClient.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Manjeet Dahiya + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#include "echoClient.h" +#include "QXmppMessage.h" + +echoClient::echoClient(QObject *parent) + : QXmppClient(parent) +{ + bool check = connect(this, SIGNAL(messageReceived(const QXmppMessage&)), + SLOT(messageReceived(const QXmppMessage&))); + Q_ASSERT(check); + Q_UNUSED(check); +} + +echoClient::~echoClient() +{ + +} + +void echoClient::messageReceived(const QXmppMessage& message) +{ + QString from = message.from(); + QString msg = message.body(); + + sendPacket(QXmppMessage("", from, "Your message: " + msg)); +} diff --git a/examples/example_1_echoClient/echoClient.h b/examples/example_1_echoClient/echoClient.h new file mode 100644 index 00000000..0412f10d --- /dev/null +++ b/examples/example_1_echoClient/echoClient.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Manjeet Dahiya + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#ifndef ECHOCLIENT_H +#define ECHOCLIENT_H + +#include "QXmppClient.h" + +class echoClient : public QXmppClient +{ + Q_OBJECT + +public: + echoClient(QObject *parent = 0); + ~echoClient(); + +public slots: + void messageReceived(const QXmppMessage&); +}; + +#endif // ECHOCLIENT_H diff --git a/examples/example_1_echoClient/example_1_echoClient.pro b/examples/example_1_echoClient/example_1_echoClient.pro new file mode 100644 index 00000000..ec5a3f05 --- /dev/null +++ b/examples/example_1_echoClient/example_1_echoClient.pro @@ -0,0 +1,10 @@ +include(../example.pri) + +TARGET = example_1_echoClient + +SOURCES += main.cpp \ + echoClient.cpp + +HEADERS += echoClient.h + +OTHER_FILES += README diff --git a/examples/example_1_echoClient/main.cpp b/examples/example_1_echoClient/main.cpp new file mode 100644 index 00000000..dc26371f --- /dev/null +++ b/examples/example_1_echoClient/main.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Manjeet Dahiya + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#include +#include "echoClient.h" +#include "QXmppLogger.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QXmppLogger::getLogger()->setLoggingType(QXmppLogger::StdoutLogging); + + echoClient client; + client.connectToServer("talk.google.com", "qxmpp.test1", "qxmpp123", "gmail.com"); + return a.exec(); +} diff --git a/examples/example_2_rosterHandling/README b/examples/example_2_rosterHandling/README new file mode 100644 index 00000000..83c75d05 --- /dev/null +++ b/examples/example_2_rosterHandling/README @@ -0,0 +1 @@ +This example demonstrates how to get the roster and presence updates. diff --git a/examples/example_2_rosterHandling/example_2_rosterHandling.pro b/examples/example_2_rosterHandling/example_2_rosterHandling.pro new file mode 100644 index 00000000..a7a9d250 --- /dev/null +++ b/examples/example_2_rosterHandling/example_2_rosterHandling.pro @@ -0,0 +1,10 @@ +include(../example.pri) + +TARGET = example_2_rosterHandling + +SOURCES += main.cpp \ + xmppClient.cpp + +HEADERS += xmppClient.h + +OTHER_FILES += README diff --git a/examples/example_2_rosterHandling/main.cpp b/examples/example_2_rosterHandling/main.cpp new file mode 100644 index 00000000..74f8bc62 --- /dev/null +++ b/examples/example_2_rosterHandling/main.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Manjeet Dahiya + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#include +#include "xmppClient.h" +#include "QXmppLogger.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QXmppLogger::getLogger()->setLoggingType(QXmppLogger::FileLogging); + + xmppClient client; + client.connectToServer("talk.google.com", "qxmpp.test1@gmail.com", "qxmpp123"); + return a.exec(); +} diff --git a/examples/example_2_rosterHandling/xmppClient.cpp b/examples/example_2_rosterHandling/xmppClient.cpp new file mode 100644 index 00000000..353090fc --- /dev/null +++ b/examples/example_2_rosterHandling/xmppClient.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Manjeet Dahiya + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + +#include + +#include "QXmppMessage.h" +#include "QXmppRosterManager.h" + +#include "xmppClient.h" + +xmppClient::xmppClient(QObject *parent) + : QXmppClient(parent) +{ + bool check = connect(this, SIGNAL(connected()), + SLOT(clientConnected())); + Q_ASSERT(check); + + check = connect(&this->rosterManager(), SIGNAL(rosterReceived()), + SLOT(rosterReceived())); + Q_ASSERT(check); + + /// Then QXmppRoster::presenceChanged() is emitted whenever presence of someone + /// in roster changes + check = connect(&this->rosterManager(), + SIGNAL(presenceChanged(const QString&, const QString&)), + SLOT(presenceChanged(const QString&, const QString&))); + Q_ASSERT(check); +} + +xmppClient::~xmppClient() +{ + +} + +void xmppClient::clientConnected() +{ + std::cout<<"example_2_rosterHandling:: CONNECTED"< +#include + +#include + +#include "QXmppLogger.h" +#include "xmppClient.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QXmppLogger::getLogger()->setLoggingType(QXmppLogger::StdoutLogging); + + // we want one argument : "send" or "receive" + if (argc != 2 || (strcmp(argv[1], "send") && strcmp(argv[1], "receive"))) + { + fprintf(stderr, "Usage: ibbClient send|receive\n"); + return EXIT_FAILURE; + } + const QString username = (!strcmp(argv[1], "send")) ? QLatin1String("qxmpp.test1") : QLatin1String("qxmpp.test2"); + + xmppClient client; + client.connectToServer("talk.google.com", username, "qxmpp123", "gmail.com"); + return a.exec(); +} diff --git a/examples/example_3_transferHandling/xmppClient.cpp b/examples/example_3_transferHandling/xmppClient.cpp new file mode 100644 index 00000000..4d092491 --- /dev/null +++ b/examples/example_3_transferHandling/xmppClient.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Authors: + * Ian Reinhart Geiser + * Jeremy Lainé + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + +#include +#include + +#include "QXmppMessage.h" +#include "QXmppUtils.h" + +#include "xmppClient.h" + +xmppClient::xmppClient(QObject *parent) + : QXmppClient(parent) +{ + // comment the following to use all available methods (highly recommended) + transferManager().setSupportedMethods(QXmppTransferJob::InBandMethod); + + bool check = connect(this, SIGNAL(presenceReceived(QXmppPresence)), + this, SLOT(slotPresenceReceived(QXmppPresence))); + Q_ASSERT(check); + + check = connect(&transferManager(), SIGNAL(fileReceived(QXmppTransferJob*)), + this, SLOT(slotFileReceived(QXmppTransferJob*))); + Q_ASSERT(check); +} + +/// A file transfer failed. + +void xmppClient::slotError(QXmppTransferJob::Error error) +{ + qDebug() << "Transmission failed:" << error; +} + +/// A file transfer request was received. + +void xmppClient::slotFileReceived(QXmppTransferJob *job) +{ + qDebug() << "Got transfer request from:" << job->jid(); + + bool check = connect(job, SIGNAL(error(QXmppTransferJob::Error)), this, SLOT(slotError(QXmppTransferJob::Error))); + Q_ASSERT(check); + + check = connect(job, SIGNAL(finished()), this, SLOT(slotFinished())); + Q_ASSERT(check); + + check = connect(job, SIGNAL(progress(qint64,qint64)), this, SLOT(slotProgress(qint64,qint64))); + Q_ASSERT(check); + + // allocate a buffer to receive the file + QBuffer *buffer = new QBuffer(this); + buffer->open(QIODevice::WriteOnly); + job->accept(buffer); +} + +/// A file transfer finished. + +void xmppClient::slotFinished() +{ + qDebug() << "Transmission finished"; +} + +/// A presence was received + +void xmppClient::slotPresenceReceived(const QXmppPresence &presence) +{ + const QLatin1String recipient("qxmpp.test2@gmail.com"); + + // if we are the recipient, or if the presence is not from the recipient, + // do nothing + if (configuration().jidBare() == recipient || + jidToBareJid(presence.from()) != recipient || + presence.type() != QXmppPresence::Available) + return; + + // send the file and connect to the job's signals + QXmppTransferJob *job = transferManager().sendFile(presence.from(), "xmppClient.cpp"); + + bool check = connect( job, SIGNAL(error(QXmppTransferJob::Error)), + this, SLOT(slotError(QXmppTransferJob::Error)) ); + Q_ASSERT(check); + + check = connect( job, SIGNAL(finished()), + this, SLOT(slotFinished()) ); + Q_ASSERT(check); + + check = connect( job, SIGNAL(progress(qint64,qint64)), + this, SLOT(slotProgress(qint64,qint64)) ); + Q_ASSERT(check); +} + +/// A file transfer has made progress. + +void xmppClient::slotProgress(qint64 done, qint64 total) +{ + qDebug() << "Transmission progress:" << done << "/" << total; +} diff --git a/examples/example_3_transferHandling/xmppClient.h b/examples/example_3_transferHandling/xmppClient.h new file mode 100644 index 00000000..f57168db --- /dev/null +++ b/examples/example_3_transferHandling/xmppClient.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Ian Reinhart Geiser + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#ifndef IBBCLIENT_H +#define IBBCLIENT_H + +#include "QXmppClient.h" +#include "QXmppTransferManager.h" + +class QBuffer; + +class xmppClient : public QXmppClient +{ + Q_OBJECT + +public: + xmppClient(QObject *parent = 0); + +private slots: + void slotError(QXmppTransferJob::Error error); + void slotFileReceived(QXmppTransferJob *job); + void slotFinished(); + void slotPresenceReceived(const QXmppPresence &presence); + void slotProgress(qint64 done, qint64 total); +}; + +#endif // IBBCLIENT_H diff --git a/examples/example_4_callHandling/example_4_callHandling.pro b/examples/example_4_callHandling/example_4_callHandling.pro new file mode 100644 index 00000000..5628f14f --- /dev/null +++ b/examples/example_4_callHandling/example_4_callHandling.pro @@ -0,0 +1,10 @@ +include(../example.pri) + +QT += multimedia + +TARGET = example_4_callHandling + +SOURCES += main.cpp \ + xmppClient.cpp + +HEADERS += xmppClient.h diff --git a/examples/example_4_callHandling/main.cpp b/examples/example_4_callHandling/main.cpp new file mode 100644 index 00000000..237da8ee --- /dev/null +++ b/examples/example_4_callHandling/main.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Ian Reinhart Geiser + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + +#include +#include + +#include + +#include "QXmppLogger.h" +#include "xmppClient.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QXmppLogger::getLogger()->setLoggingType(QXmppLogger::StdoutLogging); + + // we want one argument : "send" or "receive" + if (argc != 2 || (strcmp(argv[1], "send") && strcmp(argv[1], "receive"))) + { + fprintf(stderr, "Usage: callClient send|receive\n"); + return EXIT_FAILURE; + } + const QString username = (!strcmp(argv[1], "send")) ? QLatin1String("qxmpp.test1") : QLatin1String("qxmpp.test2"); + + xmppClient client; + client.connectToServer("talk.google.com", username, "qxmpp123", "gmail.com"); + return a.exec(); +} diff --git a/examples/example_4_callHandling/xmppClient.cpp b/examples/example_4_callHandling/xmppClient.cpp new file mode 100644 index 00000000..d4f040f3 --- /dev/null +++ b/examples/example_4_callHandling/xmppClient.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Authors: + * Ian Reinhart Geiser + * Jeremy Lainé + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + +#include +#include +#include + +#include "QXmppCallManager.h" +#include "QXmppUtils.h" + +#include "xmppClient.h" + +xmppClient::xmppClient(QObject *parent) + : QXmppClient(parent) +{ + bool check = connect(this, SIGNAL(presenceReceived(QXmppPresence)), + this, SLOT(slotPresenceReceived(QXmppPresence))); + Q_ASSERT(check); + + check = connect(&callManager(), SIGNAL(callReceived(QXmppCall*)), + this, SLOT(slotCallReceived(QXmppCall*))); + Q_ASSERT(check); +} + +/// A call was received. + +void xmppClient::slotCallReceived(QXmppCall *call) +{ + qDebug() << "Got call from:" << call->jid(); + + bool check = connect(call, SIGNAL(connected()), this, SLOT(slotConnected())); + Q_ASSERT(check); + + check = connect(call, SIGNAL(finished()), this, SLOT(slotFinished())); + Q_ASSERT(check); + + // accept call + call->accept(); +} + +/// A call connected. + +void xmppClient::slotConnected() +{ + QXmppCall *call = qobject_cast(sender()); + Q_ASSERT(call); + + qDebug() << "Call connected"; + + // prepare audio format + QAudioFormat format; + format.setFrequency(call->payloadType().clockrate()); + format.setChannels(call->payloadType().channels()); + format.setSampleSize(16); + format.setCodec("audio/pcm"); + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::SignedInt); + + // initialise audio output + QAudioOutput *audioOutput = new QAudioOutput(format, this); + audioOutput->start(call); + + // initialise audio input + QAudioInput *audioInput = new QAudioInput(format, this); + audioInput->start(call); +} + +/// A call finished. + +void xmppClient::slotFinished() +{ + qDebug() << "Call finished"; +} + +/// A presence was received. + +void xmppClient::slotPresenceReceived(const QXmppPresence &presence) +{ + const QLatin1String recipient("qxmpp.test2@gmail.com"); + + // if we are the recipient, or if the presence is not from the recipient, + // do nothing + if (configuration().jidBare() == recipient || + jidToBareJid(presence.from()) != recipient || + presence.type() != QXmppPresence::Available) + return; + + // start the call and connect to the its signals + QXmppCall *call = callManager().call(presence.from()); + + bool check = connect(call, SIGNAL(connected()), this, SLOT(slotConnected())); + Q_ASSERT(check); + + check = connect(call, SIGNAL(finished()), this, SLOT(slotFinished())); + Q_ASSERT(check); +} + diff --git a/examples/example_4_callHandling/xmppClient.h b/examples/example_4_callHandling/xmppClient.h new file mode 100644 index 00000000..75e14dcc --- /dev/null +++ b/examples/example_4_callHandling/xmppClient.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Ian Reinhart Geiser + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#ifndef XMPPCLIENT_H +#define XMPPCLIENT_H + +#include "QXmppClient.h" + +class QXmppCall; + +class xmppClient : public QXmppClient +{ + Q_OBJECT + +public: + xmppClient(QObject *parent = 0); + +private slots: + void slotCallReceived(QXmppCall *call); + void slotConnected(); + void slotFinished(); + void slotPresenceReceived(const QXmppPresence &presence); +}; + +#endif // IBBCLIENT_H diff --git a/examples/example_5_rpcInterface/example_5_rpcInterface.pro b/examples/example_5_rpcInterface/example_5_rpcInterface.pro new file mode 100644 index 00000000..eaa19e67 --- /dev/null +++ b/examples/example_5_rpcInterface/example_5_rpcInterface.pro @@ -0,0 +1,12 @@ +include(../example.pri) + +TARGET = example_5_rpcInterface + +SOURCES += main.cpp \ + rpcClient.cpp \ + remoteinterface.cpp + +HEADERS += rpcClient.h \ + remoteinterface.h + +OTHER_FILES += README diff --git a/examples/example_5_rpcInterface/main.cpp b/examples/example_5_rpcInterface/main.cpp new file mode 100644 index 00000000..5792cc21 --- /dev/null +++ b/examples/example_5_rpcInterface/main.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Manjeet Dahiya + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#include +#include "rpcClient.h" +#include "QXmppLogger.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QXmppLogger::getLogger()->setLoggingType(QXmppLogger::StdoutLogging); + + rpcClient client; + client.configuration().setUseSASLAuthentication( false ); + client.connectToServer("jabber.geiseri.com", "server", "Passw0rd", "geiseri.com"); + return a.exec(); +} diff --git a/examples/example_5_rpcInterface/remoteinterface.cpp b/examples/example_5_rpcInterface/remoteinterface.cpp new file mode 100644 index 00000000..77d52384 --- /dev/null +++ b/examples/example_5_rpcInterface/remoteinterface.cpp @@ -0,0 +1,15 @@ +#include "remoteinterface.h" + +RemoteInterface::RemoteInterface(QObject *parent) : QXmppInvokable(parent) +{ +} + +bool RemoteInterface::isAuthorized( const QString &jid ) const +{ + return true; +} + +QString RemoteInterface::echoString(const QString& message) +{ + return "Echo: " + message; +} diff --git a/examples/example_5_rpcInterface/remoteinterface.h b/examples/example_5_rpcInterface/remoteinterface.h new file mode 100644 index 00000000..02ce72d7 --- /dev/null +++ b/examples/example_5_rpcInterface/remoteinterface.h @@ -0,0 +1,20 @@ +#ifndef REMOTEINTERFACE_H +#define REMOTEINTERFACE_H + +#include "QXmppInvokable.h" + +class RemoteInterface : public QXmppInvokable +{ + Q_OBJECT +public: + RemoteInterface(QObject *parent = 0); + + bool isAuthorized( const QString &jid ) const; + +// RPC Interface +public slots: + QString echoString( const QString &message ); + +}; + +#endif // REMOTEINTERFACE_H diff --git a/examples/example_5_rpcInterface/rpcClient.cpp b/examples/example_5_rpcInterface/rpcClient.cpp new file mode 100644 index 00000000..60dd75ea --- /dev/null +++ b/examples/example_5_rpcInterface/rpcClient.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Ian Reinhart Geiser + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#include "rpcClient.h" +#include "remoteinterface.h" + +rpcClient::rpcClient(QObject *parent) + : QXmppClient(parent) +{ + addInvokableInterface( new RemoteInterface(this) ); +} + +rpcClient::~rpcClient() +{ + +} + diff --git a/examples/example_5_rpcInterface/rpcClient.h b/examples/example_5_rpcInterface/rpcClient.h new file mode 100644 index 00000000..22eda459 --- /dev/null +++ b/examples/example_5_rpcInterface/rpcClient.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Ian Reinhart Geiser + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#ifndef RPCCLIENT_H +#define RPCCLIENT_H + +#include "QXmppClient.h" + +class rpcClient : public QXmppClient +{ + Q_OBJECT + +public: + rpcClient(QObject *parent = 0); + ~rpcClient(); + +}; + +#endif // RPCCLIENT_H diff --git a/examples/example_5_rpcInterface/xmlrpctest.txt b/examples/example_5_rpcInterface/xmlrpctest.txt new file mode 100644 index 00000000..4796fdd3 --- /dev/null +++ b/examples/example_5_rpcInterface/xmlrpctest.txt @@ -0,0 +1,63 @@ + + + + + client + telnet + Passw0rd + + + + + + + + + RemoteInterface.echoString + + + Test string + + + + + + + + + + RemoteInterface.badMethod + + + Test string + + + + + + + + + + BadInterface.echoString + + + Test string + + + + + + diff --git a/examples/example_6_rpcClient/example_6_rpcClient.pro b/examples/example_6_rpcClient/example_6_rpcClient.pro new file mode 100644 index 00000000..0dbb806f --- /dev/null +++ b/examples/example_6_rpcClient/example_6_rpcClient.pro @@ -0,0 +1,10 @@ +include(../example.pri) + +TARGET = example_6_rpcClient + +SOURCES += main.cpp \ + rpcClient.cpp + +HEADERS += rpcClient.h + +OTHER_FILES += README diff --git a/examples/example_6_rpcClient/main.cpp b/examples/example_6_rpcClient/main.cpp new file mode 100644 index 00000000..08d43af4 --- /dev/null +++ b/examples/example_6_rpcClient/main.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Ian Reinhart Geiser + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#include +#include "rpcClient.h" +#include "QXmppLogger.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QXmppLogger::getLogger()->setLoggingType(QXmppLogger::StdoutLogging); + + rpcClient client; + client.configuration().setUseSASLAuthentication( false ); + client.connectToServer("jabber.geiseri.com", "client", "Passw0rd", "geiseri.com"); + return a.exec(); +} diff --git a/examples/example_6_rpcClient/rpcClient.cpp b/examples/example_6_rpcClient/rpcClient.cpp new file mode 100644 index 00000000..3f63d062 --- /dev/null +++ b/examples/example_6_rpcClient/rpcClient.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Ian Reinhart Geiser + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#include "rpcClient.h" +#include "QXmppRemoteMethod.h" +#include +#include + +rpcClient::rpcClient(QObject *parent) + : QXmppClient(parent) +{ + connect( this, SIGNAL(connected()), this, SLOT(isConnected())); +} + +rpcClient::~rpcClient() +{ + +} + +void rpcClient::isConnected() +{ + //We need to wait until we have sent the presense stuff, or for some + //reason the server ignores us... + QTimer::singleShot(5000, this, SLOT(invokeRemoteMethod())); +} + +void rpcClient::invokeRemoteMethod() +{ + QXmppRemoteMethodResult methodResult = callRemoteMethod( + "server@geiseri.com/QXmpp", "RemoteInterface.echoString", "This is a test" ); + if( methodResult.hasError ) + error( methodResult.code, methodResult.errorMessage ); + else + result( methodResult.result ); +} + +void rpcClient::result(const QVariant &value ) +{ + qDebug() << "Result:" << value; +} + +void rpcClient::error( int code, const QString &message ) +{ + qDebug() << "Error:" << code << message; +} diff --git a/examples/example_6_rpcClient/rpcClient.h b/examples/example_6_rpcClient/rpcClient.h new file mode 100644 index 00000000..aeb7d605 --- /dev/null +++ b/examples/example_6_rpcClient/rpcClient.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Ian Reinhart Geiser + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#ifndef RPCCLIENT_H +#define RPCCLIENT_H + +#include "QXmppClient.h" + +class rpcClient : public QXmppClient +{ + Q_OBJECT + +public: + rpcClient(QObject *parent = 0); + ~rpcClient(); + +public slots: + void isConnected(); + void invokeRemoteMethod(); + void result(const QVariant &value ); + void error( int code, const QString &message ); + +}; + +#endif // RPCCLIENT_H diff --git a/examples/example_7_archiveHandling/example_7_archiveHandling.pro b/examples/example_7_archiveHandling/example_7_archiveHandling.pro new file mode 100644 index 00000000..42b79c1d --- /dev/null +++ b/examples/example_7_archiveHandling/example_7_archiveHandling.pro @@ -0,0 +1,8 @@ +include(../example.pri) + +TARGET = example_7_archiveHandling + +SOURCES += main.cpp \ + xmppClient.cpp + +HEADERS += xmppClient.h diff --git a/examples/example_7_archiveHandling/main.cpp b/examples/example_7_archiveHandling/main.cpp new file mode 100644 index 00000000..74f8bc62 --- /dev/null +++ b/examples/example_7_archiveHandling/main.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Manjeet Dahiya + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#include +#include "xmppClient.h" +#include "QXmppLogger.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + QXmppLogger::getLogger()->setLoggingType(QXmppLogger::FileLogging); + + xmppClient client; + client.connectToServer("talk.google.com", "qxmpp.test1@gmail.com", "qxmpp123"); + return a.exec(); +} diff --git a/examples/example_7_archiveHandling/xmppClient.cpp b/examples/example_7_archiveHandling/xmppClient.cpp new file mode 100644 index 00000000..32959653 --- /dev/null +++ b/examples/example_7_archiveHandling/xmppClient.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Jeremy Lainé + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#include +#include + +#include "QXmppArchiveIq.h" +#include "QXmppArchiveManager.h" + +#include "xmppClient.h" + +xmppClient::xmppClient(QObject *parent) + : QXmppClient(parent) +{ + bool check = connect(this, SIGNAL(connected()), + SLOT(clientConnected())); + Q_ASSERT(check); + + check = connect(&archiveManager(), SIGNAL(archiveChatReceived(const QXmppArchiveChat &)), + SLOT(archiveChatReceived(const QXmppArchiveChat &))); + Q_ASSERT(check); + + check = connect(&archiveManager(), SIGNAL(archiveListReceived(const QList &)), + SLOT(archiveListReceived(const QList &))); + Q_ASSERT(check); +} + +xmppClient::~xmppClient() +{ + +} + +void xmppClient::clientConnected() +{ + std::cout << "example_7_archiveHandling:: CONNECTED" << std::endl; + archiveManager().listCollections("", + QDateTime::currentDateTime().addDays(-7)); +} + +void xmppClient::archiveListReceived(const QList &chats) +{ + std::cout << "example_7_archiveHandling:: LIST RECEIVED" << std::endl; + foreach (const QXmppArchiveChat &chat, chats) + archiveManager().retrieveCollection(chat.with(), chat.start()); +} + +void xmppClient::archiveChatReceived(const QXmppArchiveChat &chat) +{ + std::cout << "example_7_archiveHandling:: CHAT RECEIVED" << std::endl; + foreach (const QXmppArchiveMessage &msg, chat.messages()) + { + std::cout << "example_7_archiveHandling::" << msg.body().toStdString() << std::endl; + } +} + diff --git a/examples/example_7_archiveHandling/xmppClient.h b/examples/example_7_archiveHandling/xmppClient.h new file mode 100644 index 00000000..a672e98d --- /dev/null +++ b/examples/example_7_archiveHandling/xmppClient.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Jeremy Lainé + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + + +#ifndef XMPPCLIENT_H +#define XMPPCLIENT_H + +#include "QXmppClient.h" + +class QXmppArchiveChat; + +class xmppClient : public QXmppClient +{ + Q_OBJECT + +public: + xmppClient(QObject *parent = 0); + ~xmppClient(); + +public slots: + void clientConnected(); + void archiveListReceived(const QList &chats); + void archiveChatReceived(const QXmppArchiveChat &chat); +}; + +#endif // XMPPCLIENT_H diff --git a/examples/tests/tests.cpp b/examples/tests/tests.cpp new file mode 100644 index 00000000..a838020a --- /dev/null +++ b/examples/tests/tests.cpp @@ -0,0 +1,534 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Jeremy Lainé + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + +#include + +#include +#include +#include +#include + +#include "QXmppBind.h" +#include "QXmppJingleIq.h" +#include "QXmppMessage.h" +#include "QXmppPresence.h" +#include "QXmppRpcIq.h" +#include "QXmppSession.h" +#include "QXmppUtils.h" +#include "tests.h" + +void TestUtils::testHmac() +{ + QByteArray hmac = generateHmacMd5(QByteArray(16, 0x0b), QByteArray("Hi There")); + QCOMPARE(hmac, QByteArray::fromHex("9294727a3638bb1c13f48ef8158bfc9d")); + + hmac = generateHmacMd5(QByteArray("Jefe"), QByteArray("what do ya want for nothing?")); + QCOMPARE(hmac, QByteArray::fromHex("750c783e6ab0b503eaa86e310a5db738")); + + hmac = generateHmacMd5(QByteArray(16, 0xaa), QByteArray(50, 0xdd)); + QCOMPARE(hmac, QByteArray::fromHex("56be34521d144c88dbb8c733f0e8b3f6")); +} + +template +static void parsePacket(T &packet, const QByteArray &xml) +{ + //qDebug() << "parsing" << xml; + QDomDocument doc; + QCOMPARE(doc.setContent(xml, true), true); + QDomElement element = doc.documentElement(); + packet.parse(element); +} + +template +static void serializePacket(T &packet, const QByteArray &xml) +{ + QBuffer buffer; + buffer.open(QIODevice::ReadWrite); + QXmlStreamWriter writer(&buffer); + packet.toXml(&writer); + qDebug() << "expect " << xml; + qDebug() << "writing" << buffer.data(); + QCOMPARE(buffer.data(), xml); +} + +void TestPackets::testBindNoResource() +{ + const QByteArray xml( + "" + "" + ""); + + QXmppBind bind; + parsePacket(bind, xml); + QCOMPARE(bind.type(), QXmppIq::Set); + QCOMPARE(bind.id(), QString("bind_1")); + QCOMPARE(bind.jid(), QString()); + QCOMPARE(bind.resource(), QString()); + serializePacket(bind, xml); +} + +void TestPackets::testBindResource() +{ + const QByteArray xml( + "" + "" + "someresource" + "" + ""); + + QXmppBind bind; + parsePacket(bind, xml); + QCOMPARE(bind.type(), QXmppIq::Set); + QCOMPARE(bind.id(), QString("bind_2")); + QCOMPARE(bind.jid(), QString()); + QCOMPARE(bind.resource(), QString("someresource")); + serializePacket(bind, xml); +} + +void TestPackets::testBindResult() +{ + const QByteArray xml( + "" + "" + "somenode@example.com/someresource" + "" + ""); + + QXmppBind bind; + parsePacket(bind, xml); + QCOMPARE(bind.type(), QXmppIq::Result); + QCOMPARE(bind.id(), QString("bind_2")); + QCOMPARE(bind.jid(), QString("somenode@example.com/someresource")); + QCOMPARE(bind.resource(), QString()); + serializePacket(bind, xml); +} + +void TestPackets::testMessage() +{ + const QByteArray xml( + ""); + + QXmppMessage message; + parsePacket(message, xml); + QCOMPARE(message.to(), QString("foo@example.com/QXmpp")); + QCOMPARE(message.from(), QString("bar@example.com/QXmpp")); + serializePacket(message, xml); +} + +void TestPackets::testMessageFull() +{ + const QByteArray xml( + "" + "test subject" + "test body" + "test thread" + "" + ""); + + QXmppMessage message; + parsePacket(message, xml); + QCOMPARE(message.to(), QString("foo@example.com/QXmpp")); + QCOMPARE(message.from(), QString("bar@example.com/QXmpp")); + QCOMPARE(message.type(), QXmppMessage::Normal); + QCOMPARE(message.body(), QString("test body")); + QCOMPARE(message.subject(), QString("test subject")); + QCOMPARE(message.thread(), QString("test thread")); + QCOMPARE(message.state(), QXmppMessage::Composing); + serializePacket(message, xml); +} + +void TestPackets::testMessageDelay() +{ + const QByteArray xml( + "" + "" + ""); + + QXmppMessage message; + parsePacket(message, xml); + QCOMPARE(message.stamp(), QDateTime(QDate(2010, 06, 29), QTime(8, 23, 6), Qt::UTC)); + serializePacket(message, xml); +} + +void TestPackets::testMessageLegacyDelay() +{ + const QByteArray xml( + "" + "" + ""); + + QXmppMessage message; + parsePacket(message, xml); + QCOMPARE(message.stamp(), QDateTime(QDate(2010, 06, 29), QTime(8, 23, 6), Qt::UTC)); + serializePacket(message, xml); +} + +void TestPackets::testPresence() +{ + const QByteArray xml( + ""); + + QXmppPresence presence; + parsePacket(presence, xml); + QCOMPARE(presence.to(), QString("foo@example.com/QXmpp")); + QCOMPARE(presence.from(), QString("bar@example.com/QXmpp")); + serializePacket(presence, xml); +} + +void TestPackets::testPresenceFull() +{ + const QByteArray xml( + "" + "away" + "In a meeting" + "5" + ""); + + QXmppPresence presence; + parsePacket(presence, xml); + QCOMPARE(presence.to(), QString("foo@example.com/QXmpp")); + QCOMPARE(presence.from(), QString("bar@example.com/QXmpp")); + QCOMPARE(presence.status().type(), QXmppPresence::Status::Away); + QCOMPARE(presence.status().statusText(), QString("In a meeting")); + QCOMPARE(presence.status().priority(), 5); + serializePacket(presence, xml); +} + +void TestPackets::testSession() +{ + const QByteArray xml( + "" + "" + ""); + + QXmppSession session; + parsePacket(session, xml); + QCOMPARE(session.id(), QString("session_1")); + QCOMPARE(session.to(), QString("example.com")); + QCOMPARE(session.type(), QXmppIq::Set); + serializePacket(session, xml); +} + +void TestJingle::testSession() +{ + const QByteArray xml( + "" + "" + "" + "" + "" + "" + "" + ""); + + QXmppJingleIq session; + parsePacket(session, xml); + QCOMPARE(session.action(), QXmppJingleIq::SessionInitiate); + QCOMPARE(session.initiator(), QLatin1String("romeo@montague.lit/orchard")); + QCOMPARE(session.sid(), QLatin1String("a73sjjvkla37jfea")); + QCOMPARE(session.content().creator(), QLatin1String("initiator")); + QCOMPARE(session.content().name(), QLatin1String("this-is-a-stub")); + QCOMPARE(session.reason().text(), QString()); + QCOMPARE(session.reason().type(), QXmppJingleIq::Reason::None); + serializePacket(session, xml); +} + +void TestJingle::testTerminate() +{ + const QByteArray xml( + "" + "" + "" + "" + "" + "" + ""); + + QXmppJingleIq session; + parsePacket(session, xml); + QCOMPARE(session.action(), QXmppJingleIq::SessionTerminate); + QCOMPARE(session.initiator(), QString()); + QCOMPARE(session.sid(), QLatin1String("a73sjjvkla37jfea")); + QCOMPARE(session.reason().text(), QString()); + QCOMPARE(session.reason().type(), QXmppJingleIq::Reason::Success); + serializePacket(session, xml); +} + +void TestJingle::testPayloadType() +{ + const QByteArray xml(""); + QXmppJinglePayloadType payload; + parsePacket(payload, xml); + QCOMPARE(payload.id(), static_cast(103)); + QCOMPARE(payload.name(), QLatin1String("L16")); + QCOMPARE(payload.channels(), static_cast(2)); + QCOMPARE(payload.clockrate(), 16000u); + serializePacket(payload, xml); +} + +void TestJingle::testRinging() +{ + const QByteArray xml( + "" + "" + "" + "" + ""); + + QXmppJingleIq iq; + parsePacket(iq, xml); + QCOMPARE(iq.ringing(), true); + serializePacket(iq, xml); +} + +static void checkVariant(const QVariant &value, const QByteArray &xml) +{ + // serialise + QBuffer buffer; + buffer.open(QIODevice::ReadWrite); + QXmlStreamWriter writer(&buffer); + XMLRPC::marshall(&writer, value); + qDebug() << "expect " << xml; + qDebug() << "writing" << buffer.data(); + QCOMPARE(buffer.data(), xml); + + // parse + QDomDocument doc; + QCOMPARE(doc.setContent(xml, true), true); + QDomElement element = doc.documentElement(); + QStringList errors; + QVariant test = XMLRPC::demarshall(element, errors); + if (!errors.isEmpty()) + qDebug() << errors; + QCOMPARE(errors, QStringList()); + QCOMPARE(test, value); +} + +void TestXmlRpc::testBase64() +{ + checkVariant(QByteArray("\0\1\2\3", 4), + QByteArray("AAECAw==")); +} + +void TestXmlRpc::testBool() +{ + checkVariant(false, + QByteArray("0")); + checkVariant(true, + QByteArray("1")); +} + +void TestXmlRpc::testDateTime() +{ + checkVariant(QDateTime(QDate(1998, 7, 17), QTime(14, 8, 55)), + QByteArray("1998-07-17T14:08:55")); +} + +void TestXmlRpc::testDouble() +{ + checkVariant(double(-12.214), + QByteArray("-12.214")); +} + +void TestXmlRpc::testInt() +{ + checkVariant(int(-12), + QByteArray("-12")); +} + +void TestXmlRpc::testNil() +{ + checkVariant(QVariant(), + QByteArray("")); +} + +void TestXmlRpc::testString() +{ + checkVariant(QString("hello world"), + QByteArray("hello world")); +} + +void TestXmlRpc::testArray() +{ + checkVariant(QVariantList() << QString("hello world") << double(-12.214), + QByteArray("" + "hello world" + "-12.214" + "")); +} + +void TestXmlRpc::testStruct() +{ + QMap map; + map["bar"] = QString("hello world"); + map["foo"] = double(-12.214); + checkVariant(map, + QByteArray("" + "" + "bar" + "hello world" + "" + "" + "foo" + "-12.214" + "" + "")); +} + +void TestXmlRpc::testInvoke() +{ + const QByteArray xml( + "" + "" + "" + "examples.getStateName" + "" + "" + "6" + "" + "" + "" + "" + ""); + + QXmppRpcInvokeIq iq; + parsePacket(iq, xml); + QCOMPARE(iq.method(), QLatin1String("examples.getStateName")); + QCOMPARE(iq.arguments(), QVariantList() << int(6)); + serializePacket(iq, xml); +} + +void TestXmlRpc::testResponse() +{ + const QByteArray xml( + "" + "" + "" + "" + "" + "Colorado" + "" + "" + "" + "" + ""); + + QXmppRpcResponseIq iq; + parsePacket(iq, xml); + QCOMPARE(iq.faultCode(), 0); + QCOMPARE(iq.faultString(), QString()); + QCOMPARE(iq.values(), QVariantList() << QString("Colorado")); + serializePacket(iq, xml); +} + +void TestXmlRpc::testResponseFault() +{ + const QByteArray xml( + "" + "" + "" + "" + "" + "" + "" + "faultCode" + "404" + "" + "" + "faultString" + "Not found" + "" + "" + "" + "" + "" + "" + ""); + + QXmppRpcResponseIq iq; + parsePacket(iq, xml); + QCOMPARE(iq.faultCode(), 404); + QCOMPARE(iq.faultString(), QLatin1String("Not found")); + QCOMPARE(iq.values(), QVariantList()); + serializePacket(iq, xml); +} + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + + // run tests + int errors = 0; + + TestUtils testUtils; + errors += QTest::qExec(&testUtils); + + TestPackets testPackets; + errors += QTest::qExec(&testPackets); + + TestJingle testJingle; + errors += QTest::qExec(&testJingle); + + TestXmlRpc testXmlRpc; + errors += QTest::qExec(&testXmlRpc); + + if (errors) + { + qWarning() << "Total failed tests:" << errors; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +}; + diff --git a/examples/tests/tests.h b/examples/tests/tests.h new file mode 100644 index 00000000..33536883 --- /dev/null +++ b/examples/tests/tests.h @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Jeremy Lainé + * + * Source: + * http://code.google.com/p/qxmpp + * + * This file is a part of QXmpp library. + * + * 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. + * + */ + +#include + +class TestUtils : public QObject +{ + Q_OBJECT + +private slots: + void testHmac(); +}; + +class TestPackets : public QObject +{ + Q_OBJECT + +private slots: + void testBindNoResource(); + void testBindResource(); + void testBindResult(); + void testMessage(); + void testMessageFull(); + void testMessageDelay(); + void testMessageLegacyDelay(); + void testPresence(); + void testPresenceFull(); + void testSession(); +}; + +class TestJingle : public QObject +{ + Q_OBJECT + +private slots: + void testSession(); + void testTerminate(); + void testPayloadType(); + void testRinging(); +}; + +class TestXmlRpc : public QObject +{ + Q_OBJECT + +private slots: + void testBase64(); + void testBool(); + void testDateTime(); + void testDouble(); + void testInt(); + void testNil(); + void testString(); + + void testArray(); + void testStruct(); + + void testInvoke(); + void testResponse(); + void testResponseFault(); +}; diff --git a/examples/tests/tests.pro b/examples/tests/tests.pro new file mode 100644 index 00000000..b51cda23 --- /dev/null +++ b/examples/tests/tests.pro @@ -0,0 +1,9 @@ +include(../example.pri) + +QT += testlib + +TARGET = tests + +SOURCES += tests.cpp + +HEADERS += tests.h -- cgit v1.2.3