Skip to content
Snippets Groups Projects
Commit ad388ea7 authored by Nicolas Werner's avatar Nicolas Werner
Browse files

Add sync loop on worker thread

parent 8c51dfeb
No related branches found
No related tags found
No related merge requests found
......@@ -47,9 +47,10 @@ int main(int argc, char *argv[]) {
RoomModel rooms;
Sync sync(rooms, &login);
QObject::connect(&login, &Login::loginSuccess, &sync, &Sync::startSync);
QObject::connect(&login, &Login::loginSuccess, &sync, &Sync::startSync, Qt::QueuedConnection);
QObject::connect(&sync, &Sync::newRoom, &rooms, &RoomModel::addRoom, Qt::QueuedConnection);
QObject::connect(&sync, &Sync::newInvite, &rooms, &RoomModel::addInvite, Qt::QueuedConnection);
QObject::connect(app.data(), &QGuiApplication::aboutToQuit, &sync, &Sync::stopSync, Qt::QueuedConnection);
QSharedPointer<Room> a = QSharedPointer<Room>(new Room);
a->tag = "group";
......
......@@ -6,10 +6,45 @@
#include "debug_out.h"
void Sync::startSync() {
qDebug() << "trying initial sync";
if (!isRunning()) {
start(LowPriority);
} else {
stop = false;
condition.wakeOne();
}
}
Sync::~Sync() {
stopSync();
}
void Sync::stopSync() {
this->exit = true;
this->stop = true;
http::client().shutdown();
condition.wakeOne();
wait();
}
void Sync::run() { emit nextSync(); }
void Sync::sync() {
qDebug() << "sync";
if (this->stop) {
if (this->exit) {
qDebug() << "sync exiting";
return;
} else {
qDebug() << "sync pausing";
mutex.lock();
this->condition.wait(&mutex);
mutex.unlock();
}
}
mtx::http::SyncOpts opts = {};
opts.timeout = 0;
opts.since = since;
http::client().sync(opts, [this](const mtx::responses::Sync &res, mtx::http::RequestErr err) {
if (err) {
qDebug() << "Failed sync";
......@@ -17,6 +52,8 @@ void Sync::startSync() {
return;
}
this->since = res.next_batch;
for (const std::pair<std::string, mtx::responses::JoinedRoom> &join : res.rooms.join) {
const mtx::responses::JoinedRoom &r = join.second;
......@@ -25,8 +62,6 @@ void Sync::startSync() {
room->name = join.first;
qDebug() << QString::fromStdString(room->name);
// room.picture = r.avatar();
// room.tag = r.account_data.events.back(;
room->unreadNotifications = r.unread_notifications.highlight_count;
emit newRoom(room);
}
......@@ -41,5 +76,7 @@ void Sync::startSync() {
qDebug() << QString::fromStdString(room->name);
emit newInvite(room);
}
emit nextSync();
});
}
#pragma once
#include <QObject>
#include <atomic>
#include <QMutex>
#include <QThread>
#include <QWaitCondition>
#include "login.h"
#include "models/roommodel.h"
class Sync : public QObject {
class Sync : public QThread {
Q_OBJECT
public:
Sync(RoomModel &rooms, Login *login) : rooms(rooms) {}
Sync(RoomModel &rooms, Login *login, QObject *parent = 0)
: QThread(parent), rooms(rooms), stop(false), exit(false) {
connect(this, &Sync::nextSync, this, &Sync::sync);
}
~Sync();
void run();
public slots:
void startSync();
void stopSync();
private slots:
void sync();
signals:
void newRoom(QSharedPointer<Room> room);
void newInvite(QSharedPointer<Room> room);
void nextSync();
private:
RoomModel &rooms;
QWaitCondition condition;
QMutex mutex;
std::atomic<bool> stop, exit;
std::string since;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment