Skip to content

Commit

Permalink
Share QNetworkAccessManager accross app
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrePTJ committed Jun 21, 2024
1 parent 1f9357f commit c9852c5
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 51 deletions.
24 changes: 6 additions & 18 deletions src/client/kimaiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <QCoreApplication>
#include <QUrlQuery>

#include <spdlog/spdlog.h>

using namespace kemai;

/*
Expand Down Expand Up @@ -83,10 +81,7 @@ QString kemai::apiMethodToString(ApiMethod method)
/*
* Private impl
*/
KimaiClient::KimaiClientPrivate::KimaiClientPrivate(KimaiClient* c) : networkAccessManager(new QNetworkAccessManager), mQ(c)
{
connect(networkAccessManager.get(), &QNetworkAccessManager::sslErrors, this, &KimaiClientPrivate::onNamSslErrors);
}
KimaiClient::KimaiClientPrivate::KimaiClientPrivate(KimaiClient* c) : networkAccessManager(std::make_shared<QNetworkAccessManager>()), mQ(c) {}

QNetworkRequest KimaiClient::KimaiClientPrivate::prepareRequest(ApiMethod method, const std::map<QString, QString>& parameters, const QByteArray& data,
const QString& subPath) const
Expand Down Expand Up @@ -156,25 +151,18 @@ QNetworkReply* KimaiClient::KimaiClientPrivate::sendPatchRequest(const QNetworkR
return networkAccessManager->sendCustomRequest(networkRequest, "PATCH", data);
}

void KimaiClient::KimaiClientPrivate::onNamSslErrors(QNetworkReply* /*reply*/, const QList<QSslError>& errors)
{
for (const auto& error : errors)
{
spdlog::error("SSL Error: {}", error.errorString());
}

// Process certificate errors one by one.
const auto& crtError = errors.first();
emit mQ->sslError(crtError.errorString(), crtError.certificate().serialNumber(), crtError.certificate().toPem());
}

/*
* Public impl
*/
KimaiClient::KimaiClient(QObject* parent) : QObject(parent), mD(new KimaiClientPrivate(this)) {}

KimaiClient::~KimaiClient() = default;

void KimaiClient::setNetworkAccessManager(const std::shared_ptr<QNetworkAccessManager>& nam)
{
mD->networkAccessManager = nam;
}

void KimaiClient::setHost(const QString& host)
{
mD->host = host;
Expand Down
3 changes: 3 additions & 0 deletions src/client/kimaiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>
#include <optional>

#include <QNetworkAccessManager>
#include <QObject>

#include "client/kimaiAPI.h"
Expand Down Expand Up @@ -33,6 +34,8 @@ class KimaiClient : public QObject
explicit KimaiClient(QObject* parent = nullptr);
~KimaiClient() override;

void setNetworkAccessManager(const std::shared_ptr<QNetworkAccessManager>& nam);

void setHost(const QString& host);

void setLegacyAuth(const QString& username, const QString& token);
Expand Down
4 changes: 1 addition & 3 deletions src/client/kimaiClient_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ class KimaiClient::KimaiClientPrivate : public QObject
return result;
}

void onNamSslErrors(QNetworkReply* reply, const QList<QSslError>& errors);

QString username, host, token, apiToken;
QScopedPointer<QNetworkAccessManager> networkAccessManager;
std::shared_ptr<QNetworkAccessManager> networkAccessManager;

private:
KimaiClient* const mQ;
Expand Down
19 changes: 18 additions & 1 deletion src/gui/mainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "activityWidget.h"
#include "client/kimaiFeatures.h"
#include "kemaiConfig.h"
#include "misc/customFmt.h"
#include "settings/settings.h"
#include "settingsDialog.h"
#include "taskWidget.h"
Expand All @@ -27,10 +28,12 @@ static const auto FirstRequestDelayMs = 100;
/*
* Class impl
*/
MainWindow::MainWindow() : mUi(std::make_unique<Ui::MainWindow>())
MainWindow::MainWindow() : mUi(std::make_unique<Ui::MainWindow>()), mNam(std::make_shared<QNetworkAccessManager>())
{
mUi->setupUi(this);

mUpdater.setNetworkAccessManager(mNam);

auto settings = Settings::get();

/*
Expand Down Expand Up @@ -140,6 +143,7 @@ MainWindow::MainWindow() : mUi(std::make_unique<Ui::MainWindow>())
/*
* Connections
*/
connect(mNam.get(), &QNetworkAccessManager::sslErrors, this, &MainWindow::onNamSslErrors);
connect(mActSettings, &QAction::triggered, this, &MainWindow::onActionSettingsTriggered);
connect(mActQuit, &QAction::triggered, qApp, &QCoreApplication::quit);
connect(mActViewActivities, &QAction::triggered, this, &MainWindow::showSelectedView);
Expand Down Expand Up @@ -233,6 +237,7 @@ void MainWindow::createKemaiSession(const Settings::Profile& profile)
{
auto kimaiClient = std::make_shared<KimaiClient>();

kimaiClient->setNetworkAccessManager(mNam);
kimaiClient->setHost(profile.host);
kimaiClient->setLegacyAuth(profile.username, profile.token);
kimaiClient->setAPIToken(profile.apiToken);
Expand Down Expand Up @@ -339,6 +344,18 @@ void MainWindow::processAutoConnect()
createKemaiSession(profile.value());
}

void MainWindow::onNamSslErrors(QNetworkReply* /*reply*/, const QList<QSslError>& errors)
{
for (const auto& error : errors)
{
spdlog::error("SSL Error: {}", error.errorString());
}

// // Process certificate errors one by one.
// const auto& crtError = errors.first();
// emit mQ->sslError(crtError.errorString(), crtError.certificate().serialNumber(), crtError.certificate().toPem());
}

void MainWindow::onCurrentTimeSheetChanged()
{
if (mSession && mSession->hasCurrentTimeSheet())
Expand Down
4 changes: 3 additions & 1 deletion src/gui/mainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <QLabel>
#include <QMainWindow>
#include <QMenuBar>

#include <QNetworkAccessManager>
#include <QSystemTrayIcon>

#include "client/kimaiClient.h"
Expand Down Expand Up @@ -44,6 +44,7 @@ class MainWindow : public QMainWindow
void updateProfilesMenu();
void processAutoConnect();

void onNamSslErrors(QNetworkReply* reply, const QList<QSslError>& errors);
void onCurrentTimeSheetChanged();
void onPluginsChanged();
void onSessionVersionChanged();
Expand All @@ -63,6 +64,7 @@ class MainWindow : public QMainWindow
KemaiUpdater mUpdater;
std::shared_ptr<KemaiSession> mSession;
std::shared_ptr<DesktopEventsMonitor> mDesktopEventsMonitor;
std::shared_ptr<QNetworkAccessManager> mNam;

// Stacked widget (ownership is transferred, don't try to delete them)
ActivityWidget* mActivityWidget = nullptr;
Expand Down
60 changes: 34 additions & 26 deletions src/updater/kemaiUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ using namespace kemai;
/*
* Private impl
*/
KemaiUpdater::KemaiUpdaterPrivate::KemaiUpdaterPrivate(KemaiUpdater* c) : networkAccessManager(new QNetworkAccessManager), mQ(c)
{
connect(networkAccessManager.data(), &QNetworkAccessManager::finished, this, &KemaiUpdaterPrivate::onNamFinished);
}
KemaiUpdater::KemaiUpdaterPrivate::KemaiUpdaterPrivate(KemaiUpdater* c) : networkAccessManager(std::make_shared<QNetworkAccessManager>()), mQ(c) {}

QNetworkRequest KemaiUpdater::KemaiUpdaterPrivate::prepareGithubRequest(const QString& path)
{
Expand All @@ -31,32 +28,37 @@ QNetworkRequest KemaiUpdater::KemaiUpdaterPrivate::prepareGithubRequest(const QS
return r;
}

void KemaiUpdater::KemaiUpdaterPrivate::onNamFinished(QNetworkReply* reply)
void KemaiUpdater::KemaiUpdaterPrivate::onLatestReleaseReplyFinished()
{
if (reply->error() != QNetworkReply::NoError)
{
spdlog::error("Error on update check: {}", reply->errorString());
}
else
if (auto reply = qobject_cast<QNetworkReply*>(sender()))
{
// Parse json
auto jdoc = QJsonDocument::fromJson(reply->readAll());
auto jobj = jdoc.object();

auto newVersion = QVersionNumber::fromString(jobj.value("tag_name").toString());
if (newVersion > currentCheckSinceVersion)
if (reply->error() != QNetworkReply::NoError)
{
VersionDetails vd;
vd.vn = newVersion;
vd.description = jobj.value("body").toString();
vd.url = jobj.value("html_url").toString();

emit mQ->checkFinished(vd);
spdlog::error("Error on update check: {}", reply->errorString());
}
else if (!silenceIfNoNew)
else
{
emit mQ->checkFinished(VersionDetails());
// Parse json
auto jdoc = QJsonDocument::fromJson(reply->readAll());
auto jobj = jdoc.object();

auto newVersion = QVersionNumber::fromString(jobj.value("tag_name").toString());
if (newVersion > currentCheckSinceVersion)
{
VersionDetails vd;
vd.vn = newVersion;
vd.description = jobj.value("body").toString();
vd.url = jobj.value("html_url").toString();

emit mQ->checkFinished(vd);
}
else if (!silenceIfNoNew)
{
emit mQ->checkFinished(VersionDetails());
}
}

reply->deleteLater();
}
}

Expand All @@ -67,11 +69,17 @@ KemaiUpdater::KemaiUpdater(QObject* parent) : QObject(parent), mD(new KemaiUpdat

KemaiUpdater::~KemaiUpdater() = default;

void KemaiUpdater::setNetworkAccessManager(const std::shared_ptr<QNetworkAccessManager>& nam)
{
mD->networkAccessManager = nam;
}

void KemaiUpdater::checkAvailableNewVersion(const QVersionNumber& sinceVersion, bool silenceIfNoNew)
{
mD->currentCheckSinceVersion = sinceVersion;
mD->silenceIfNoNew = silenceIfNoNew;

auto rq = mD->prepareGithubRequest("/repos/AlexandrePTJ/kemai/releases/latest");
mD->networkAccessManager->get(rq);
auto request = mD->prepareGithubRequest("/repos/AlexandrePTJ/kemai/releases/latest");
auto reply = mD->networkAccessManager->get(request);
connect(reply, &QNetworkReply::finished, mD.get(), &KemaiUpdaterPrivate::onLatestReleaseReplyFinished);
}
3 changes: 3 additions & 0 deletions src/updater/kemaiUpdater.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <QNetworkAccessManager>
#include <QObject>
#include <QUrl>
#include <QVersionNumber>
Expand All @@ -21,6 +22,8 @@ class KemaiUpdater : public QObject
explicit KemaiUpdater(QObject* parent = nullptr);
~KemaiUpdater() override;

void setNetworkAccessManager(const std::shared_ptr<QNetworkAccessManager>& nam);

void checkAvailableNewVersion(const QVersionNumber& sinceVersion = QVersionNumber(0, 0, 0), bool silenceIfNoNew = false);

signals:
Expand Down
4 changes: 2 additions & 2 deletions src/updater/kemaiUpdater_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class KemaiUpdater::KemaiUpdaterPrivate : public QObject
explicit KemaiUpdaterPrivate(KemaiUpdater* c);

QNetworkRequest prepareGithubRequest(const QString& path);
void onNamFinished(QNetworkReply* reply);
void onLatestReleaseReplyFinished();

QVersionNumber currentCheckSinceVersion;
bool silenceIfNoNew;

QScopedPointer<QNetworkAccessManager> networkAccessManager;
std::shared_ptr<QNetworkAccessManager> networkAccessManager;

private:
KemaiUpdater* const mQ;
Expand Down

0 comments on commit c9852c5

Please sign in to comment.