Commit a3b63a73 authored by Georg's avatar Georg

first commit

parents
#include "EchoBot.h"
#include <iostream>
#include <boost/bind.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include "EchoPayload.h"
using namespace boost;
// FIXME rewrite all binds to qt connects
EchoBot::EchoBot(NetworkFactories* networkFactories)
{
client = new Client("schorsch@jabber-germany.de", "jwillrein", networkFactories);
client->setAlwaysTrustCertificates();
client->onConnected.connect(bind(&EchoBot::handleConnected, this));
client->onMessageReceived.connect(
bind(&EchoBot::handleMessageReceived, this, _1));
client->onPresenceReceived.connect(
bind(&EchoBot::handlePresenceReceived, this, _1));
tracer = new ClientXMLTracer(client);
softwareVersionResponder = new SoftwareVersionResponder(client->getIQRouter());
softwareVersionResponder->setVersion("EchoBot", "1.0");
softwareVersionResponder->start();
//...
client->addPayloadParserFactory(&echoPayloadParserFactory);
client->addPayloadSerializer(&echoPayloadSerializer);
//...
client->connect();
//...
}
EchoBot::~EchoBot()
{
client->removePayloadSerializer(&echoPayloadSerializer);
client->removePayloadParserFactory(&echoPayloadParserFactory);
//...
softwareVersionResponder->stop();
delete softwareVersionResponder;
delete tracer;
delete client;
//...
}
//...
void EchoBot::handlePresenceReceived(Presence::ref presence)
{
// Automatically approve subscription requests
if (presence->getType() == Presence::Subscribe)
{
Presence::ref response = Presence::create();
response->setTo(presence->getFrom());
response->setType(Presence::Subscribed);
client->sendPresence(response);
}
}
void EchoBot::handleConnected()
{
// Request the roster
GetRosterRequest::ref rosterRequest =
GetRosterRequest::create(client->getIQRouter());
rosterRequest->onResponse.connect(
bind(&EchoBot::handleRosterReceived, this, _2));
rosterRequest->send();
}
void EchoBot::handleRosterReceived(ErrorPayload::ref error)
{
if (error)
{
std::cerr << "Error receiving roster. Continuing anyway.";
}
// Send initial available presence
client->sendPresence(Presence::create("Send me a message"));
}
//...
void EchoBot::handleMessageReceived(Message::ref message)
{
//...
// Echo back the incoming message
message->setTo(message->getFrom());
message->setFrom(JID());
//...
if (!message->getPayload<EchoPayload>())
{
boost::shared_ptr<EchoPayload> echoPayload = boost::make_shared<EchoPayload>();
echoPayload->setMessage("This is an echoed message");
message->addPayload(echoPayload);
client->sendMessage(message);
}
}
#ifndef ECHOBOT_H
#define ECHPBOT_H
#include <Swiften/Swiften.h>
#include "EchoPayloadParserFactory.h"
#include "EchoPayloadSerializer.h"
using namespace Swift;
using namespace boost;
class EchoBot
{
public:
EchoBot(NetworkFactories* networkFactories);
~EchoBot();
private:
void handlePresenceReceived(Presence::ref presence);
void handleConnected();
void handleRosterReceived(ErrorPayload::ref error);
void handleMessageReceived(Message::ref message);
Client* client;
ClientXMLTracer* tracer;
SoftwareVersionResponder* softwareVersionResponder;
EchoPayloadParserFactory echoPayloadParserFactory;
EchoPayloadSerializer echoPayloadSerializer;
};
#endif
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
//...
#pragma once
#include <Swiften/Swiften.h>
//...
class EchoPayload : public Swift::Payload
{
public:
EchoPayload() {}
const std::string& getMessage() const
{
return message;
}
void setMessage(const std::string& message)
{
this->message = message;
}
private:
std::string message;
};
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#pragma once
#include <Swiften/Swiften.h>
#include "EchoPayload.h"
using namespace Swift;
class EchoPayloadParser : public Swift::GenericPayloadParser<EchoPayload>
{
public:
EchoPayloadParser() : currentDepth(0) {}
void handleStartElement(
const std::string& /* element */, const std::string& /* ns */, const AttributeMap&)
{
currentDepth++;
}
void handleEndElement(const std::string& /* element */, const std::string& /* ns */)
{
currentDepth--;
if (currentDepth == 0) {
getPayloadInternal()->setMessage(currentText);
}
}
void handleCharacterData(const std::string& data)
{
currentText += data;
}
private:
int currentDepth;
std::string currentText;
};
class EchoPayloadParserFactory : public Swift::GenericPayloadParserFactory<EchoPayloadParser>
{
public:
EchoPayloadParserFactory() :
GenericPayloadParserFactory<EchoPayloadParser>("echo", "http://swift.im/echo") {}
};
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#pragma once
#include <Swiften/Swiften.h>
#include "EchoPayload.h"
class EchoPayloadSerializer : public Swift::GenericPayloadSerializer<EchoPayload>
{
public:
std::string serializePayload(boost::shared_ptr<EchoPayload> payload) const
{
XMLElement element("echo", "http://swift.im/protocol/echo");
element.addNode(XMLTextNode::ref(new XMLTextNode(payload->getMessage())));
return element.serialize();
}
};
# Kaidan
android-no-sdk {
target.path = /data/user/qt
export(target.path)
INSTALLS += target
} else:android {
x86 {
target.path = /libs/x86
} else: armeabi-v7a {
target.path = /libs/armeabi-v7a
} else {
target.path = /libs/armeabi
}
export(target.path)
INSTALLS += target
} else:unix {
isEmpty(target.path) {
qnx {
target.path = /tmp/$${TARGET}/bin
} else {
target.path = /opt/$${TARGET}/bin
}
export(target.path)
}
INSTALLS += target
}
export(INSTALLS)
# path to local compiled swift 3 lib
SWIFT3PATH = ../swift-3.0
# from swift-config
SWIFTCXX = -DSWIFTEN_STATIC -DBOOST_ALL_NO_LIB -DBOOST_SYSTEM_NO_DEPRECATED -DBOOST_SIGNALS_NO_DEPRECATION_WARNING -DSWIFT_EXPERIMENTAL_FT
SWIFTLIB = -lSwiften -lSwiften_Boost -lrt -lz -lssl -lcrypto -lxml2 -lresolv -lpthread -ldl -lm -lc -lstdc++
TEMPLATE = app
QT += qml quick
INCLUDEPATH += $${SWIFT3PATH}/3rdParty/Boost/src
INCLUDEPATH += $${SWIFT3PATH}/
CXXFLAGS += $${SWIFTCXX}
LIBS += -L$${SWIFT3PATH}/Swiften -L$${SWIFT3PATH}/3rdParty/Boost $${SWIFTLIB}
SOURCES += main.cpp \
EchoBot.cpp
HEADERS += EchoBot.h \
EchoPayload.h \
EchoPayloadParserFactory.h \
EchoPayloadSerializer.h
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "Swiften/EventLoop/Qt/QtEventLoop.h"
#include "EchoBot.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// FIXME rewrite binds to qt specific versions
// FIXME test connects from QtEventLoop (Swiften) to QGuiApplication
QtEventLoop eventLoop;
BoostNetworkFactories networkFactories(&eventLoop);
EchoBot bot(&networkFactories);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
import QtQuick 2.1
import QtQuick.Window 2.0
Window {
visible: true
width: 360
height: 360
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
}
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment