diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-08-10 16:22:18 +0000 |
|---|---|---|
| committer | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-08-10 16:22:18 +0000 |
| commit | 70c376063aa3a30db16facd8c103b2c6ef3b3e36 (patch) | |
| tree | 5b28dd41176dba2d11eb45c3a6ed84fb7f263498 /examples | |
| parent | 1b16399a98d3f659d349e3e13e12d91dab954a22 (diff) | |
| download | qxmpp-70c376063aa3a30db16facd8c103b2c6ef3b3e36.tar.gz | |
rename "example" to "examples"
Diffstat (limited to 'examples')
41 files changed, 2027 insertions, 0 deletions
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 <QtCore/QCoreApplication> + +#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 <QtCore/QCoreApplication> +#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 <QtCore/QCoreApplication> +#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 <iostream> + +#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"<<std::endl; +} + +void xmppClient::rosterReceived() +{ + std::cout<<"example_2_rosterHandling:: Roster Received"<<std::endl; + QStringList list = rosterManager().getRosterBareJids(); + for(int i = 0; i < list.size(); ++i) + { + std::cout<<"Roster Received:: "<<qPrintable(list.at(i))<<std::endl; + } +} + +void xmppClient::presenceChanged(const QString& bareJid, + const QString& resource) +{ + std::cout<<"Presence changed:: "<< qPrintable(bareJid) + << qPrintable(resource)<<std::endl; +} diff --git a/examples/example_2_rosterHandling/xmppClient.h b/examples/example_2_rosterHandling/xmppClient.h new file mode 100644 index 00000000..90bcf9d2 --- /dev/null +++ b/examples/example_2_rosterHandling/xmppClient.h @@ -0,0 +1,44 @@ +/* + * 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 XMPPCLIENT_H +#define XMPPCLIENT_H + +#include "QXmppClient.h" + +class xmppClient : public QXmppClient +{ + Q_OBJECT + +public: + xmppClient(QObject *parent = 0); + ~xmppClient(); + +public slots: + void clientConnected(); + void rosterReceived(); + void presenceChanged(const QString& bareJid, const QString& resource); +}; + +#endif // XMPPCLIENT_H diff --git a/examples/example_3_transferHandling/example_3_transferHandling.pro b/examples/example_3_transferHandling/example_3_transferHandling.pro new file mode 100644 index 00000000..9ac85e59 --- /dev/null +++ b/examples/example_3_transferHandling/example_3_transferHandling.pro @@ -0,0 +1,8 @@ +include(../example.pri) + +TARGET = example_3_transferHandling + +SOURCES += main.cpp \ + xmppClient.cpp + +HEADERS += xmppClient.h diff --git a/examples/example_3_transferHandling/main.cpp b/examples/example_3_transferHandling/main.cpp new file mode 100644 index 00000000..1d6b49b2 --- /dev/null +++ b/examples/example_3_transferHandling/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 <cstdlib> +#include <cstdio> + +#include <QtCore/QCoreApplication> + +#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 <QBuffer> +#include <QDebug> + +#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 <cstdlib> +#include <cstdio> + +#include <QCoreApplication> + +#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 <QAudioInput> +#include <QAudioOutput> +#include <QDebug> + +#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<QXmppCall*>(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 <QtCore/QCoreApplication> +#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 @@ +<stream:stream + to='geiseri.com' + xmlns='jabber:client' + xmlns:stream='http://etherx.jabber.org/streams'> + +<iq id='A3' type='set'> + <query xmlns='jabber:iq:auth'> + <username>client</username> + <resource>telnet</resource> + <password>Passw0rd</password> + </query> +</iq> + +<presence/> + +<iq type='set' + from='client@geiseri.com/telnet' + to='server@geiseri.com/QXmpp' + id='rpc1'> + <query xmlns='jabber:iq:rpc'> + <methodCall> + <methodName>RemoteInterface.echoString</methodName> + <params> + <param> + <value><string>Test string</string></value> + </param> + </params> + </methodCall> + </query> +</iq> + +<iq type='set' + from='client@geiseri.com/telnet' + to='server@geiseri.com/QXmpp' + id='rpc1'> + <query xmlns='jabber:iq:rpc'> + <methodCall> + <methodName>RemoteInterface.badMethod</methodName> + <params> + <param> + <value><string>Test string</string></value> + </param> + </params> + </methodCall> + </query> +</iq> + +<iq type='set' + from='client@geiseri.com/telnet' + to='server@geiseri.com/QXmpp' + id='rpc1'> + <query xmlns='jabber:iq:rpc'> + <methodCall> + <methodName>BadInterface.echoString</methodName> + <params> + <param> + <value><string>Test string</string></value> + </param> + </params> + </methodCall> + </query> +</iq> + 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 <QtCore/QCoreApplication> +#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 <qdebug.h> +#include <QTimer> + +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 <QtCore/QCoreApplication> +#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 <QDateTime> +#include <iostream> + +#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<QXmppArchiveChat> &)), + SLOT(archiveListReceived(const QList<QXmppArchiveChat> &))); + 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<QXmppArchiveChat> &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<QXmppArchiveChat> &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 <cstdlib> + +#include <QCoreApplication> +#include <QDomDocument> +#include <QVariant> +#include <QtTest/QtTest> + +#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 <class T> +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 <class T> +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( + "<iq id=\"bind_1\" type=\"set\">" + "<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>" + "</iq>"); + + 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( + "<iq id=\"bind_2\" type=\"set\">" + "<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">" + "<resource>someresource</resource>" + "</bind>" + "</iq>"); + + 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( + "<iq id=\"bind_2\" type=\"result\">" + "<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">" + "<jid>somenode@example.com/someresource</jid>" + "</bind>" + "</iq>"); + + 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( + "<message to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\" type=\"normal\"/>"); + + 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( + "<message to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\" type=\"normal\">" + "<subject>test subject</subject>" + "<body>test body</body>" + "<thread>test thread</thread>" + "<composing xmlns=\"http://jabber.org/protocol/chatstates\"/>" + "</message>"); + + 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( + "<message to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\" type=\"normal\">" + "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-06-29T08:23:06Z\"/>" + "</message>"); + + 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( + "<message to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\" type=\"normal\">" + "<x xmlns=\"jabber:x:delay\" stamp=\"20100629T08:23:06\"/>" + "</message>"); + + 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( + "<presence to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\"/>"); + + 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( + "<presence to=\"foo@example.com/QXmpp\" from=\"bar@example.com/QXmpp\">" + "<show>away</show>" + "<status>In a meeting</status>" + "<priority>5</priority>" + "</presence>"); + + 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( + "<iq id=\"session_1\" to=\"example.com\" type=\"set\">" + "<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>" + "</iq>"); + + 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( + "<iq" + " id=\"zid615d9\"" + " to=\"juliet@capulet.lit/balcony\"" + " from=\"romeo@montague.lit/orchard\"" + " type=\"set\">" + "<jingle xmlns=\"urn:xmpp:jingle:1\"" + " action=\"session-initiate\"" + " initiator=\"romeo@montague.lit/orchard\"" + " sid=\"a73sjjvkla37jfea\">" + "<content creator=\"initiator\" name=\"this-is-a-stub\">" + "<description xmlns=\"urn:xmpp:jingle:apps:stub:0\"/>" + "<transport xmlns=\"urn:xmpp:jingle:transports:stub:0\"/>" + "</content>" + "</jingle>" + "</iq>"); + + 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( + "<iq" + " id=\"le71fa63\"" + " to=\"romeo@montague.lit/orchard\"" + " from=\"juliet@capulet.lit/balcony\"" + " type=\"set\">" + "<jingle xmlns=\"urn:xmpp:jingle:1\"" + " action=\"session-terminate\"" + " sid=\"a73sjjvkla37jfea\">" + "<reason>" + "<success/>" + "</reason>" + "</jingle>" + "</iq>"); + + 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("<payload-type id=\"103\" name=\"L16\" channels=\"2\" clockrate=\"16000\"/>"); + QXmppJinglePayloadType payload; + parsePacket(payload, xml); + QCOMPARE(payload.id(), static_cast<unsigned char>(103)); + QCOMPARE(payload.name(), QLatin1String("L16")); + QCOMPARE(payload.channels(), static_cast<unsigned char>(2)); + QCOMPARE(payload.clockrate(), 16000u); + serializePacket(payload, xml); +} + +void TestJingle::testRinging() +{ + const QByteArray xml( + "<iq" + " id=\"tgr515bt\"" + " to=\"romeo@montague.lit/orchard\"" + " from=\"juliet@capulet.lit/balcony\"" + " type=\"set\">" + "<jingle xmlns=\"urn:xmpp:jingle:1\"" + " action=\"session-info\"" + " initiator=\"romeo@montague.lit/orchard\"" + " sid=\"a73sjjvkla37jfea\">" + "<ringing xmlns=\"urn:xmpp:jingle:apps:rtp:info:1\"/>" + "</jingle>" + "</iq>"); + + 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("<value><base64>AAECAw==</base64></value>")); +} + +void TestXmlRpc::testBool() +{ + checkVariant(false, + QByteArray("<value><boolean>0</boolean></value>")); + checkVariant(true, + QByteArray("<value><boolean>1</boolean></value>")); +} + +void TestXmlRpc::testDateTime() +{ + checkVariant(QDateTime(QDate(1998, 7, 17), QTime(14, 8, 55)), + QByteArray("<value><dateTime.iso8601>1998-07-17T14:08:55</dateTime.iso8601></value>")); +} + +void TestXmlRpc::testDouble() +{ + checkVariant(double(-12.214), + QByteArray("<value><double>-12.214</double></value>")); +} + +void TestXmlRpc::testInt() +{ + checkVariant(int(-12), + QByteArray("<value><i4>-12</i4></value>")); +} + +void TestXmlRpc::testNil() +{ + checkVariant(QVariant(), + QByteArray("<value><nil/></value>")); +} + +void TestXmlRpc::testString() +{ + checkVariant(QString("hello world"), + QByteArray("<value><string>hello world</string></value>")); +} + +void TestXmlRpc::testArray() +{ + checkVariant(QVariantList() << QString("hello world") << double(-12.214), + QByteArray("<value><array><data>" + "<value><string>hello world</string></value>" + "<value><double>-12.214</double></value>" + "</data></array></value>")); +} + +void TestXmlRpc::testStruct() +{ + QMap<QString, QVariant> map; + map["bar"] = QString("hello world"); + map["foo"] = double(-12.214); + checkVariant(map, + QByteArray("<value><struct>" + "<member>" + "<name>bar</name>" + "<value><string>hello world</string></value>" + "</member>" + "<member>" + "<name>foo</name>" + "<value><double>-12.214</double></value>" + "</member>" + "</struct></value>")); +} + +void TestXmlRpc::testInvoke() +{ + const QByteArray xml( + "<iq" + " id=\"rpc1\"" + " to=\"responder@company-a.com/jrpc-server\"" + " from=\"requester@company-b.com/jrpc-client\"" + " type=\"set\">" + "<query xmlns=\"jabber:iq:rpc\">" + "<methodCall>" + "<methodName>examples.getStateName</methodName>" + "<params>" + "<param>" + "<value><i4>6</i4></value>" + "</param>" + "</params>" + "</methodCall>" + "</query>" + "</iq>"); + + 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( + "<iq" + " id=\"rpc1\"" + " to=\"requester@company-b.com/jrpc-client\"" + " from=\"responder@company-a.com/jrpc-server\"" + " type=\"result\">" + "<query xmlns=\"jabber:iq:rpc\">" + "<methodResponse>" + "<params>" + "<param>" + "<value><string>Colorado</string></value>" + "</param>" + "</params>" + "</methodResponse>" + "</query>" + "</iq>"); + + 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( + "<iq" + " id=\"rpc1\"" + " to=\"requester@company-b.com/jrpc-client\"" + " from=\"responder@company-a.com/jrpc-server\"" + " type=\"result\">" + "<query xmlns=\"jabber:iq:rpc\">" + "<methodResponse>" + "<fault>" + "<value>" + "<struct>" + "<member>" + "<name>faultCode</name>" + "<value><i4>404</i4></value>" + "</member>" + "<member>" + "<name>faultString</name>" + "<value><string>Not found</string></value>" + "</member>" + "</struct>" + "</value>" + "</fault>" + "</methodResponse>" + "</query>" + "</iq>"); + + 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 <QObject> + +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 |
