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

Rewrite the Kaidan class using gloox

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