Commit 0614dbac authored by Marzanna's avatar Marzanna Committed by Linus Jahn

Disconnect on exit, Improve UI, Clean up coding style

* Disconnect account on exit
* Minor UI changes
* Fix indent
parent a7ebfda7
# path to local compiled swift 3 lib
SWIFT3PATH = ../../../swift-3.0
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++
......@@ -17,16 +17,16 @@ LIBS += -L$${SWIFT3PATH}/Swiften -L$${SWIFT3PATH}/3rdParty/Boost $${SWIFTLIB}
DEFINES += BOOST_SIGNALS_NO_DEPRECATION_WARNING
SOURCES += source/main.cpp \
source/Kaidan.cpp \
source/RosterContoller.cpp \
source/RosterItem.cpp
source/Kaidan.cpp \
source/RosterContoller.cpp \
source/RosterItem.cpp
HEADERS += source/Kaidan.h \
source/EchoPayload.h \
source/EchoPayloadParserFactory.h \
source/EchoPayloadSerializer.h \
source/RosterContoller.h \
source/RosterItem.h
source/EchoPayload.h \
source/EchoPayloadParserFactory.h \
source/EchoPayloadSerializer.h \
source/RosterContoller.h \
source/RosterItem.h
RESOURCES += resources/qml.qrc
......
android-no-sdk {
target.path = /data/user/qt
export(target.path)
INSTALLS += target
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
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
isEmpty(target.path) {
qnx {
target.path = /tmp/$${TARGET}/bin
} else {
target.path = /opt/$${TARGET}/bin
}
export(target.path)
}
INSTALLS += target
}
export(INSTALLS)
import QtQuick 2.1
import QtQuick.Controls 1.4
import harbour.kaidan 1.0
Rectangle {
visible: true
width: 360
height: 360
ApplicationWindow{
visible: true
width: 360
height: 720
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Flickable{
anchors.fill: parent
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
ListView {
width: 100; height: 100
model: kaidan.rosterController.rosterList
delegate: Rectangle {
height: 25
width: 100
Text { text: model.modelData.jid}
}
}
ListView {
anchors.fill: parent
model: kaidan.rosterController.rosterList
delegate: Rectangle {
height: 25
width: parent.width
Text { text: model.modelData.jid}
}
}
}
onClosing: kaidan.mainQuit()
}
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
......@@ -14,13 +14,13 @@ class EchoPayload : public Swift::Payload
public:
EchoPayload() {}
const std::string& getMessage() const
{
const std::string& getMessage() const
{
return message;
}
void setMessage(const std::string& message)
{
void setMessage(const std::string& message)
{
this->message = message;
}
......
......@@ -17,21 +17,21 @@ class EchoPayloadParser : public Swift::GenericPayloadParser<EchoPayload>
EchoPayloadParser() : currentDepth(0) {}
void handleStartElement(
const std::string& /* element */, const std::string& /* ns */, const AttributeMap&)
{
const std::string& /* element */, const std::string& /* ns */, const AttributeMap&)
{
currentDepth++;
}
void handleEndElement(const std::string& /* element */, const std::string& /* ns */)
{
void handleEndElement(const std::string& /* element */, const std::string& /* ns */)
{
currentDepth--;
if (currentDepth == 0) {
getPayloadInternal()->setMessage(currentText);
}
}
void handleCharacterData(const std::string& data)
{
void handleCharacterData(const std::string& data)
{
currentText += data;
}
......
......@@ -12,8 +12,8 @@
class EchoPayloadSerializer : public Swift::GenericPayloadSerializer<EchoPayload>
{
public:
std::string serializePayload(boost::shared_ptr<EchoPayload> payload) const
{
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();
......
......@@ -8,76 +8,80 @@
#include "RosterContoller.h"
Kaidan::Kaidan(NetworkFactories* networkFactories, QObject *parent) :
rosterController_(new RosterController()), QObject(parent)
rosterController_(new RosterController()), QObject(parent)
{
client = new Swift::Client("jid@...", "pass", networkFactories);
client->setAlwaysTrustCertificates();
client->onConnected.connect(boost::bind(&Kaidan::handleConnected, this));
client->onMessageReceived.connect(
boost::bind(&Kaidan::handleMessageReceived, this, _1));
client->onPresenceReceived.connect(
boost::bind(&Kaidan::handlePresenceReceived, this, _1));
tracer = new Swift::ClientXMLTracer(client);
client = new Swift::Client("jid@...", "pass", networkFactories);
client->setAlwaysTrustCertificates();
client->onConnected.connect(boost::bind(&Kaidan::handleConnected, this));
client->onMessageReceived.connect(
boost::bind(&Kaidan::handleMessageReceived, this, _1));
client->onPresenceReceived.connect(
boost::bind(&Kaidan::handlePresenceReceived, this, _1));
tracer = new Swift::ClientXMLTracer(client);
softwareVersionResponder = new Swift::SoftwareVersionResponder(client->getIQRouter());
softwareVersionResponder->setVersion("Kaidan", "0.1");
softwareVersionResponder->start();
softwareVersionResponder = new Swift::SoftwareVersionResponder(client->getIQRouter());
softwareVersionResponder->setVersion("Kaidan", "0.1");
softwareVersionResponder->start();
client->addPayloadParserFactory(&echoPayloadParserFactory);
client->addPayloadSerializer(&echoPayloadSerializer);
client->addPayloadParserFactory(&echoPayloadParserFactory);
client->addPayloadSerializer(&echoPayloadSerializer);
client->connect();
client->connect();
}
Kaidan::~Kaidan()
{
client->removePayloadSerializer(&echoPayloadSerializer);
client->removePayloadParserFactory(&echoPayloadParserFactory);
client->removePayloadSerializer(&echoPayloadSerializer);
client->removePayloadParserFactory(&echoPayloadParserFactory);
softwareVersionResponder->stop();
delete softwareVersionResponder;
delete tracer;
delete client;
softwareVersionResponder->stop();
delete softwareVersionResponder;
delete tracer;
delete client;
delete rosterController_;
delete rosterController_;
}
//we don't want to close client without disconnection
void Kaidan::mainQuit(){
client->disconnect();
}
void Kaidan::handlePresenceReceived(Presence::ref presence)
{
// Automatically approve subscription requests
if (presence->getType() == Swift::Presence::Subscribe)
{
Swift::Presence::ref response = Swift::Presence::create();
response->setTo(presence->getFrom());
response->setType(Swift::Presence::Subscribed);
client->sendPresence(response);
}
// Automatically approve subscription requests
if (presence->getType() == Swift::Presence::Subscribe)
{
Swift::Presence::ref response = Swift::Presence::create();
response->setTo(presence->getFrom());
response->setType(Swift::Presence::Subscribed);
client->sendPresence(response);
}
}
void Kaidan::handleConnected()
{
client->sendPresence(Presence::create("Send me a message"));
client->sendPresence(Presence::create("Send me a message"));
// Request the roster
rosterController_->requestRosterFromClient(client);
// Request the roster
rosterController_->requestRosterFromClient(client);
}
void Kaidan::handleMessageReceived(Message::ref message)
{
// Echo back the incoming message
message->setTo(message->getFrom());
message->setFrom(JID());
// 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);
}
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);
}
}
RosterController* Kaidan::getRosterController()
{
return rosterController_;
return rosterController_;
}
......@@ -13,31 +13,32 @@ class RosterController;
class Kaidan : public QObject
{
Q_OBJECT
Q_OBJECT
Q_PROPERTY(RosterController* rosterController READ getRosterController NOTIFY rosterControllerChanged)
Q_PROPERTY(RosterController* rosterController READ getRosterController NOTIFY rosterControllerChanged)
public:
Kaidan(Swift::NetworkFactories* networkFactories, QObject *parent = 0);
~Kaidan();
Kaidan(Swift::NetworkFactories* networkFactories, QObject *parent = 0);
~Kaidan();
Q_INVOKABLE void mainQuit();
RosterController* getRosterController();
RosterController* getRosterController();
signals:
void rosterControllerChanged();
void rosterControllerChanged();
private:
void handlePresenceReceived(Swift::Presence::ref presence);
void handleConnected();
void handleMessageReceived(Swift::Message::ref message);
void handlePresenceReceived(Swift::Presence::ref presence);
void handleConnected();
void handleMessageReceived(Swift::Message::ref message);
Swift::Client* client;
Swift::ClientXMLTracer* tracer;
Swift::SoftwareVersionResponder* softwareVersionResponder;
EchoPayloadParserFactory echoPayloadParserFactory;
EchoPayloadSerializer echoPayloadSerializer;
Swift::Client* client;
Swift::ClientXMLTracer* tracer;
Swift::SoftwareVersionResponder* softwareVersionResponder;
EchoPayloadParserFactory echoPayloadParserFactory;
EchoPayloadSerializer echoPayloadSerializer;
RosterController* rosterController_;
RosterController* rosterController_;
};
#endif
......@@ -4,54 +4,54 @@
RosterController::RosterController(QObject *parent) : QObject(parent), client_(NULL), rosterList_()
{
//rosterList_.append(new RosterItem(QString("lala@lala.de"), QString("lala"), None));
//rosterList_.append(new RosterItem(QString("lala@lala.de"), QString("lala"), None));
}
void RosterController::requestRosterFromClient(Swift::Client *client)
{
client_ = client;
client_ = client;
client_->requestRoster();
client_->requestRoster();
Swift::GetRosterRequest::ref rosterRequest = Swift::GetRosterRequest::create(client->getIQRouter());
rosterRequest->onResponse.connect(bind(&RosterController::handleRosterReceived, this, _2));
rosterRequest->send();
Swift::GetRosterRequest::ref rosterRequest = Swift::GetRosterRequest::create(client->getIQRouter());
rosterRequest->onResponse.connect(bind(&RosterController::handleRosterReceived, this, _2));
rosterRequest->send();
}
void RosterController::handleRosterReceived(Swift::ErrorPayload::ref error)
{
if (error)
{
std::cerr << "Error receiving roster. Continuing anyway.";
}
else
{
std::cout << "handleRosterReceived!!!" << std::endl;
Swift::XMPPRoster* roster = client_->getRoster();
std::vector<Swift::XMPPRosterItem> rosterItems = roster->getItems();
std::vector<Swift::XMPPRosterItem>::iterator it;
std::cout << "size: " << rosterItems.size() << std::endl;
for(it = rosterItems.begin(); it < rosterItems.end(); it++ )
{
if (error)
{
std::cerr << "Error receiving roster. Continuing anyway.";
}
else
{
std::cout << "handleRosterReceived!!!" << std::endl;
Swift::XMPPRoster* roster = client_->getRoster();
std::vector<Swift::XMPPRosterItem> rosterItems = roster->getItems();
std::vector<Swift::XMPPRosterItem>::iterator it;
std::cout << "size: " << rosterItems.size() << std::endl;
for(it = rosterItems.begin(); it < rosterItems.end(); it++ )
{
#if 0
std::cout << "jid: " << (*it).getJID().toString() <<
", Name: " << (*it).getName() <<
", Subscription: " << (*it).getSubscription() << std::endl;
std::cout << "jid: " << (*it).getJID().toString() <<
", Name: " << (*it).getName() <<
", Subscription: " << (*it).getSubscription() << std::endl;
#endif
rosterList_.append(new RosterItem(QString::fromStdString((*it).getJID().toString()),
QString::fromStdString((*it).getName()),
(Subscription)(*it).getSubscription()));
rosterList_.append(new RosterItem(QString::fromStdString((*it).getJID().toString()),
QString::fromStdString((*it).getName()),
(Subscription)(*it).getSubscription()));
emit rosterListChanged();
}
}
emit rosterListChanged();
}
}
}
QQmlListProperty<RosterItem> RosterController::getRosterList()
{
return QQmlListProperty<RosterItem>(this, rosterList_);
return QQmlListProperty<RosterItem>(this, rosterList_);
}
......@@ -9,31 +9,31 @@
class RosterController : public QObject
{
Q_OBJECT
Q_OBJECT
//Q_PROPERTY(QList<QObject*> rosterList READ getRosterList NOTIFY rosterListChanged)
Q_PROPERTY(QQmlListProperty<RosterItem> rosterList READ getRosterList NOTIFY rosterListChanged)
//Q_PROPERTY(QList<QObject*> rosterList READ getRosterList NOTIFY rosterListChanged)
Q_PROPERTY(QQmlListProperty<RosterItem> rosterList READ getRosterList NOTIFY rosterListChanged)
public:
RosterController(QObject *parent = 0);
RosterController(QObject *parent = 0);
void requestRosterFromClient(Swift::Client *client);
void requestRosterFromClient(Swift::Client *client);
//QList<QObject*> getRosterList();
QQmlListProperty<RosterItem> getRosterList();
//QList<QObject*> getRosterList();
QQmlListProperty<RosterItem> getRosterList();
signals:
void rosterListChanged();
void rosterListChanged();
public slots:
private:
void handleRosterReceived(Swift::ErrorPayload::ref error);
void handleRosterReceived(Swift::ErrorPayload::ref error);
Swift::Client* client_;
Swift::Client* client_;
//QList<QObject*> rosterList_;
QList<RosterItem*> rosterList_;
//QList<QObject*> rosterList_;
QList<RosterItem*> rosterList_;
};
......
......@@ -5,42 +5,42 @@ RosterItem::RosterItem(QObject *parent) : QObject(parent), subscription_(None),
}
RosterItem::RosterItem(const QString &jid, const QString &name, const Subscription &subscription, QObject* parent) :
QObject(parent), jid_(jid), name_(name), subscription_(subscription)
QObject(parent), jid_(jid), name_(name), subscription_(subscription)
{
}
QString RosterItem::getName()
{
return name_;
return name_;
}
void RosterItem::setName(const QString &name)
{
name_ = name;
name_ = name;
emit nameChanged();
emit nameChanged();
}
QString RosterItem::getJid()
{
return jid_;
return jid_;
}
void RosterItem::setJid(const QString &jid)
{
jid_ = jid;
jid_ = jid;
emit jidChanged();
emit jidChanged();
}
Subscription RosterItem::getSubscription()
{
return subscription_;
return subscription_;
}
void RosterItem::setSubscription(const Subscription &subscription)
{
subscription_ = subscription;
subscription_ = subscription;
emit subscriptionChanged();
emit subscriptionChanged();
}
......@@ -5,44 +5,44 @@
enum Subscription
{ None,
To,
From,
Both,
Remove
To,
From,
Both,
Remove
};
class RosterItem : public QObject
{
Q_OBJECT
Q_OBJECT
Q_PROPERTY(QString name READ getName WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString jid READ getJid WRITE setJid NOTIFY jidChanged)
Q_PROPERTY(Subscription subscription READ getSubscription WRITE setSubscription NOTIFY subscriptionChanged)
Q_PROPERTY(QString name READ getName WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString jid READ getJid WRITE setJid NOTIFY jidChanged)
Q_PROPERTY(Subscription subscription READ getSubscription WRITE setSubscription NOTIFY subscriptionChanged)
public:
explicit RosterItem(QObject *parent = 0);
RosterItem(const QString& jid, const QString& name, const Subscription& subscription, QObject* parent = 0);
explicit RosterItem(QObject *parent = 0);
RosterItem(const QString& jid, const QString& name, const Subscription& subscription, QObject* parent = 0);
QString getName();
void setName(const QString& name);
QString getName();
void setName(const QString& name);
QString getJid();
void setJid(const QString& jid);
QString getJid();
void setJid(const QString& jid);
Subscription getSubscription();
void setSubscription(const Subscription& subscription);
Subscription getSubscription();
void setSubscription(const Subscription& subscription);
signals:
void nameChanged();
void jidChanged();
void subscriptionChanged();
void nameChanged();