Commit c31c6611 authored by Linus Jahn's avatar Linus Jahn
Browse files

Rewrite the Kaidan class using gloox

parent e2892d03
...@@ -28,15 +28,10 @@ ...@@ -28,15 +28,10 @@
#include <QSettings> #include <QSettings>
#include <QString> #include <QString>
#include <QStandardPaths> #include <QStandardPaths>
// Boost #include <QTimer>
#include <boost/bind.hpp>
// Swiften
#include <Swiften/Client/Client.h>
#include <Swiften/Client/MemoryStorages.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>
#include <Swiften/Queries/Responders/SoftwareVersionResponder.h>
// gloox // gloox
#include <gloox/rostermanager.h> #include <gloox/rostermanager.h>
#include <gloox/receipt.h>
// Kaidan // Kaidan
#include "RosterModel.h" #include "RosterModel.h"
#include "PresenceController.h" #include "PresenceController.h"
...@@ -45,11 +40,9 @@ ...@@ -45,11 +40,9 @@
#include "VCardController.h" #include "VCardController.h"
#include "ServiceDiscoveryManager.h" #include "ServiceDiscoveryManager.h"
Kaidan::Kaidan(Swift::NetworkFactories *networkFactories, QObject *parent) : Kaidan::Kaidan(QObject *parent) : QObject(parent)
QObject(parent)
{ {
connected = false; connected = false;
netFactories = networkFactories;
// setup database // setup database
database = new Database(); database = new Database();
...@@ -57,11 +50,8 @@ Kaidan::Kaidan(Swift::NetworkFactories *networkFactories, QObject *parent) : ...@@ -57,11 +50,8 @@ Kaidan::Kaidan(Swift::NetworkFactories *networkFactories, QObject *parent) :
if (database->needToConvert()) database->convertDatabase(); if (database->needToConvert()) database->convertDatabase();
// setup components // setup components
storages = new Swift::MemoryStorages(Swift::PlatformCryptoProvider::create());
messageModel = new MessageModel(database->getDatabase()); messageModel = new MessageModel(database->getDatabase());
presenceController = new PresenceController(); rosterModel = new RosterModel(database->getDatabase());
vCardController = new VCardController();
serviceDiscoveryManager = new ServiceDiscoveryManager();
// //
// Load settings data // Load settings data
...@@ -88,91 +78,80 @@ Kaidan::~Kaidan() ...@@ -88,91 +78,80 @@ Kaidan::~Kaidan()
{ {
if (connected) { if (connected) {
client->disconnect(); client->disconnect();
softwareVersionResponder->stop();
delete tracer;
delete softwareVersionResponder;
delete client; delete client;
} }
delete messageSessionHandler; delete messageSessionHandler;
delete rosterManager; delete rosterManager;
delete presenceController;
delete vCardController;
delete settings; delete settings;
delete serviceDiscoveryManager;
} }
void Kaidan::mainConnect() void Kaidan::mainConnect()
{ {
// Create a new XMPP client // Create a new XMPP client
client_ = new gloox::Client(gloox::JID(jid.toStdString()), password.toStdString()); client = new gloox::Client(gloox::JID(jid.toStdString()), password.toStdString());
// require encryption // require encryption
client_->setTls(gloox::TLSPolicy::TLSRequired); client->setTls(gloox::TLSPolicy::TLSRequired);
// set the JID resource // set the JID resource
client_->setResource(jidResource.toStdString()); client->setResource(jidResource.toStdString());
// Connection listener
client->registerConnectionListener(this);
// Message receiving/sending // Message receiving/sending
messageSessionHandler = new MessageSessionHandler(client_, messageModel); messageSessionHandler = new MessageSessionHandler(client, messageModel);
client_->registerMessageSessionHandler((gloox::MessageSessionHandler*) messageSessionHandler); client->registerMessageSessionHandler((gloox::MessageSessionHandler*) messageSessionHandler);
// Roster // Roster
rosterManager = new RosterManager(rosterModel, client_); rosterManager = new RosterManager(rosterModel, client);
//client_->connect();
client = new Swift::Client(jid.toStdString(), password.toStdString(), netFactories, storages);
// trust all certificates
client->setAlwaysTrustCertificates();
// event handling
client->onConnected.connect(boost::bind(&Kaidan::handleConnected, this));
client->onDisconnected.connect(boost::bind(&Kaidan::handleDisconnected, this));
// Create XML tracer (console output of xmpp data)
tracer = new Swift::ClientXMLTracer(client);
// share kaidan version and sytem info
QString systemInfo = QSysInfo::prettyProductName();
softwareVersionResponder = new Swift::SoftwareVersionResponder(client->getIQRouter());
softwareVersionResponder->setVersion(APPLICATION_DISPLAY_NAME, VERSION_STRING, systemInfo.toStdString());
softwareVersionResponder->start();
// set client in message, roster and presence controller // Register Stanza Extensions
presenceController->setClient(client); client->registerStanzaExtension(new gloox::Receipt(gloox::Receipt::Request));
vCardController->setClient(client); client->registerStanzaExtension(new gloox::Receipt(gloox::Receipt::Received));
serviceDiscoveryManager->setClient(client);
Swift::ClientOptions options; client->connect(false);
options.useStreamCompression = false;
// .. and connect! // every 100 ms: fetch new packages from the socket
client->connect(options); QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateClient()));
timer->start(100);
} }
// we don't want to close client without disconnection
void Kaidan::mainDisconnect() void Kaidan::mainDisconnect()
{ {
if (getConnectionState()) { if (connected) {
client->disconnect(); client->disconnect();
} }
} }
void Kaidan::handleConnected() void Kaidan::onConnect()
{ {
qDebug() << "[Connection] Connected successfully to server";
// emit connected signal // emit connected signal
connected = true; connected = true;
emit connectionStateConnected(); emit connectionStateConnected();
client->sendPresence(Swift::Presence::create("Send me a message"));
} }
void Kaidan::handleDisconnected() void Kaidan::onDisconnect(gloox::ConnectionError error)
{ {
qDebug() << "[Connection] Connection failed or disconnected" << error;
connected = false; connected = false;
emit connectionStateDisconnected(); emit connectionStateDisconnected();
} }
bool Kaidan::onTLSConnect(const gloox::CertInfo &info)
{
// accept certificate
qDebug() << "[Connection] Automatically accepting TLS certificate";
return true;
}
void Kaidan::updateClient()
{
// parse new incoming network packages; wait 0 ms for new ones
client->recv(0);
}
bool Kaidan::newLoginNeeded() bool Kaidan::newLoginNeeded()
{ {
return (jid == "") || (password == ""); return (jid == "") || (password == "");
......
...@@ -27,11 +27,9 @@ ...@@ -27,11 +27,9 @@
#include <QSettings> #include <QSettings>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
// Swiften
#include <Swiften/Client/Client.h>
#include <Swiften/Client/ClientXMLTracer.h>
// gloox // gloox
#include <gloox/client.h> #include <gloox/client.h>
#include <gloox/connectionlistener.h>
// Kaidan // Kaidan
#include "Database.h" #include "Database.h"
#include "RosterManager.h" #include "RosterManager.h"
...@@ -40,7 +38,7 @@ ...@@ -40,7 +38,7 @@
#include "VCardController.h" #include "VCardController.h"
#include "ServiceDiscoveryManager.h" #include "ServiceDiscoveryManager.h"
class Kaidan : public QObject class Kaidan : public QObject, public gloox::ConnectionListener
{ {
Q_OBJECT Q_OBJECT
...@@ -54,12 +52,17 @@ class Kaidan : public QObject ...@@ -54,12 +52,17 @@ class Kaidan : public QObject
Q_PROPERTY(QString chatPartner READ getChatPartner WRITE setChatPartner NOTIFY chatPartnerChanged) Q_PROPERTY(QString chatPartner READ getChatPartner WRITE setChatPartner NOTIFY chatPartnerChanged)
public: public:
Kaidan(Swift::NetworkFactories *networkFactories, QObject *parent = 0); Kaidan(QObject *parent = 0);
~Kaidan(); ~Kaidan();
Q_INVOKABLE void mainDisconnect();
Q_INVOKABLE void mainConnect(); Q_INVOKABLE void mainConnect();
Q_INVOKABLE void mainDisconnect();
Q_INVOKABLE bool newLoginNeeded(); Q_INVOKABLE bool newLoginNeeded();
Q_INVOKABLE void sendMessage(QString jid, QString message);
Q_INVOKABLE void addContact(QString jid, QString nick);
Q_INVOKABLE void removeContact(QString jid);
Q_INVOKABLE QString getResourcePath(QString);
Q_INVOKABLE QString getVersionString();
bool getConnectionState() const; bool getConnectionState() const;
QString getJid(); QString getJid();
...@@ -70,16 +73,13 @@ public: ...@@ -70,16 +73,13 @@ public:
void setPassword(QString); void setPassword(QString);
QString getChatPartner(); QString getChatPartner();
void setChatPartner(QString); void setChatPartner(QString);
RosterModel* getRosterModel(); RosterModel* getRosterModel();
MessageModel* getMessageModel(); MessageModel* getMessageModel();
VCardController* getVCardController(); VCardController* getVCardController();
Q_INVOKABLE void sendMessage(QString jid, QString message); virtual void onConnect();
Q_INVOKABLE void addContact(QString jid, QString nick); virtual void onDisconnect(gloox::ConnectionError error);
Q_INVOKABLE void removeContact(QString jid); virtual bool onTLSConnect(const gloox::CertInfo &info);
Q_INVOKABLE QString getResourcePath(QString);
Q_INVOKABLE QString getVersionString();
signals: signals:
void rosterModelChanged(); void rosterModelChanged();
...@@ -92,18 +92,11 @@ signals: ...@@ -92,18 +92,11 @@ signals:
void passwordChanged(); void passwordChanged();
void chatPartnerChanged(); void chatPartnerChanged();
private: private slots:
void handleConnected(); void updateClient();
void handleDisconnected();
bool connected; private:
gloox::Client *client;
gloox::Client *client_;
Swift::Client *client;
Swift::ClientXMLTracer *tracer;
Swift::SoftwareVersionResponder *softwareVersionResponder;
Swift::NetworkFactories *netFactories;
Swift::MemoryStorages *storages;
Database *database; Database *database;
RosterModel *rosterModel; RosterModel *rosterModel;
...@@ -113,9 +106,9 @@ private: ...@@ -113,9 +106,9 @@ private:
PresenceController *presenceController; PresenceController *presenceController;
VCardController *vCardController; VCardController *vCardController;
ServiceDiscoveryManager *serviceDiscoveryManager; ServiceDiscoveryManager *serviceDiscoveryManager;
QSettings *settings; QSettings *settings;
bool connected;
QString jid; QString jid;
QString jidResource; QString jidResource;
QString password; QString password;
......
...@@ -60,8 +60,8 @@ void MessageHandler::handleMessage(const gloox::Message &message, gloox::Message ...@@ -60,8 +60,8 @@ void MessageHandler::handleMessage(const gloox::Message &message, gloox::Message
const QString msgId = QString::fromStdString(message.id()); const QString msgId = QString::fromStdString(message.id());
messageModel->addMessage(&author, &author_resource, &recipient, messageModel->addMessage(&author, &recipient, &timestamp, &body, &msgId,
&recipient_resource, &timestamp, &body, &msgId, false); false, &author_resource, &recipient_resource);
// send a new notification | TODO: Resolve nickname from JID // send a new notification | TODO: Resolve nickname from JID
Notifications::sendMessageNotification(message.from().full(), body.toStdString()); Notifications::sendMessageNotification(message.from().full(), body.toStdString());
...@@ -104,8 +104,7 @@ void MessageHandler::sendMessage(QString *fromJid, QString *toJid, QString *body ...@@ -104,8 +104,7 @@ void MessageHandler::sendMessage(QString *fromJid, QString *toJid, QString *body
const QString timestamp = QDateTime::currentDateTime().toString(Qt::ISODate); const QString timestamp = QDateTime::currentDateTime().toString(Qt::ISODate);
const QString id = QString::fromStdString(message.id()); const QString id = QString::fromStdString(message.id());
messageModel->addMessage(fromJid, nullptr, toJid, nullptr, &timestamp, body, messageModel->addMessage(fromJid, toJid, &timestamp, body, &id, true);
&id, true);
// XEP-0184: Message Delivery Receipts // XEP-0184: Message Delivery Receipts
// request a delivery receipt from the other client // request a delivery receipt from the other client
......
...@@ -83,10 +83,10 @@ void MessageModel::setMessageAsDelivered(const QString msgId) ...@@ -83,10 +83,10 @@ void MessageModel::setMessageAsDelivered(const QString msgId)
submitAll(); submitAll();
} }
void MessageModel::addMessage(const QString *author, const QString *author_resource, void MessageModel::addMessage(const QString *author, const QString *recipient,
const QString *recipient, const QString *recipient_resource, const QString *timestamp, const QString *message,
const QString *timestamp, const QString *message, const QString *msgId, const QString *msgId, bool sentByMe,
bool sentByMe) const QString *author_resource, const QString *recipient_resource)
{ {
// //
// add the new message // add the new message
......
...@@ -33,10 +33,11 @@ public: ...@@ -33,10 +33,11 @@ public:
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE; QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
void applyRecipientFilter(QString *recipient_, QString *author_); void applyRecipientFilter(QString *recipient_, QString *author_);
void addMessage(const QString *author, const QString *author_resource, void addMessage(const QString *author, const QString *recipient,
const QString *recipient, const QString *recipient_resource, const QString *timestamp, const QString *message,
const QString *timestamp, const QString *message, const QString *msgId, bool sentByMe,
const QString *msgId, bool sentByMe); const QString *author_resource = new QString(),
const QString *recipient_resource = new QString());
void setMessageAsSent(const QString msgId); void setMessageAsSent(const QString msgId);
void setMessageAsDelivered(const QString msgId); void setMessageAsDelivered(const QString msgId);
......
...@@ -33,9 +33,6 @@ ...@@ -33,9 +33,6 @@
#include <QSqlError> #include <QSqlError>
#include <QTranslator> #include <QTranslator>
#include <QtQml> #include <QtQml>
// Swiften
#include <Swiften/EventLoop/Qt/QtEventLoop.h>
#include <Swiften/Network/BoostNetworkFactories.h>
// Kaidan // Kaidan
#include "Kaidan.h" #include "Kaidan.h"
#include "VCard.h" #include "VCard.h"
...@@ -108,11 +105,7 @@ int main(int argc, char *argv[]) ...@@ -108,11 +105,7 @@ int main(int argc, char *argv[])
// Kaidan back-end // Kaidan back-end
// //
Swift::QtEventLoop eventLoop; Kaidan kaidan;
Swift::BoostNetworkFactories networkFactories(&eventLoop);
Kaidan kaidan(&networkFactories);
// //
// Command line arguments // Command line arguments
......
...@@ -99,7 +99,9 @@ Kirigami.ScrollablePage { ...@@ -99,7 +99,9 @@ Kirigami.ScrollablePage {
} }
Component.onCompleted: { Component.onCompleted: {
function openAddContactSheet() { addContactSheet.open(); } function openAddContactSheet() {
addContactSheet.open();
}
function disconnectOpenAddContactSheet() { function disconnectOpenAddContactSheet() {
addContactDialogRequested.disconnect(openAddContactSheet); addContactDialogRequested.disconnect(openAddContactSheet);
} }
......
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