diff --git a/src/client/kimaiClient.cpp b/src/client/kimaiClient.cpp index 931e7bb..6c11dfa 100644 --- a/src/client/kimaiClient.cpp +++ b/src/client/kimaiClient.cpp @@ -4,8 +4,6 @@ #include #include -#include - using namespace kemai; /* @@ -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()), mQ(c) {} QNetworkRequest KimaiClient::KimaiClientPrivate::prepareRequest(ApiMethod method, const std::map& parameters, const QByteArray& data, const QString& subPath) const @@ -156,18 +151,6 @@ QNetworkReply* KimaiClient::KimaiClientPrivate::sendPatchRequest(const QNetworkR return networkAccessManager->sendCustomRequest(networkRequest, "PATCH", data); } -void KimaiClient::KimaiClientPrivate::onNamSslErrors(QNetworkReply* /*reply*/, const QList& 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 */ @@ -175,6 +158,11 @@ KimaiClient::KimaiClient(QObject* parent) : QObject(parent), mD(new KimaiClientP KimaiClient::~KimaiClient() = default; +void KimaiClient::setNetworkAccessManager(const std::shared_ptr& nam) +{ + mD->networkAccessManager = nam; +} + void KimaiClient::setHost(const QString& host) { mD->host = host; diff --git a/src/client/kimaiClient.h b/src/client/kimaiClient.h index a620eb4..a0d2812 100644 --- a/src/client/kimaiClient.h +++ b/src/client/kimaiClient.h @@ -3,6 +3,7 @@ #include #include +#include #include #include "client/kimaiAPI.h" @@ -33,6 +34,8 @@ class KimaiClient : public QObject explicit KimaiClient(QObject* parent = nullptr); ~KimaiClient() override; + void setNetworkAccessManager(const std::shared_ptr& nam); + void setHost(const QString& host); void setLegacyAuth(const QString& username, const QString& token); diff --git a/src/client/kimaiClient_p.h b/src/client/kimaiClient_p.h index 1581d88..36d307f 100644 --- a/src/client/kimaiClient_p.h +++ b/src/client/kimaiClient_p.h @@ -113,10 +113,8 @@ class KimaiClient::KimaiClientPrivate : public QObject return result; } - void onNamSslErrors(QNetworkReply* reply, const QList& errors); - QString username, host, token, apiToken; - QScopedPointer networkAccessManager; + std::shared_ptr networkAccessManager; private: KimaiClient* const mQ; diff --git a/src/gui/mainWindow.cpp b/src/gui/mainWindow.cpp index a434d99..64d2da3 100644 --- a/src/gui/mainWindow.cpp +++ b/src/gui/mainWindow.cpp @@ -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" @@ -27,10 +28,12 @@ static const auto FirstRequestDelayMs = 100; /* * Class impl */ -MainWindow::MainWindow() : mUi(std::make_unique()) +MainWindow::MainWindow() : mUi(std::make_unique()), mNam(std::make_shared()) { mUi->setupUi(this); + mUpdater.setNetworkAccessManager(mNam); + auto settings = Settings::get(); /* @@ -140,6 +143,7 @@ MainWindow::MainWindow() : mUi(std::make_unique()) /* * 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); @@ -233,6 +237,7 @@ void MainWindow::createKemaiSession(const Settings::Profile& profile) { auto kimaiClient = std::make_shared(); + kimaiClient->setNetworkAccessManager(mNam); kimaiClient->setHost(profile.host); kimaiClient->setLegacyAuth(profile.username, profile.token); kimaiClient->setAPIToken(profile.apiToken); @@ -339,6 +344,18 @@ void MainWindow::processAutoConnect() createKemaiSession(profile.value()); } +void MainWindow::onNamSslErrors(QNetworkReply* /*reply*/, const QList& 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()) diff --git a/src/gui/mainWindow.h b/src/gui/mainWindow.h index 325ddea..0b8bf0d 100644 --- a/src/gui/mainWindow.h +++ b/src/gui/mainWindow.h @@ -4,7 +4,7 @@ #include #include #include - +#include #include #include "client/kimaiClient.h" @@ -44,6 +44,7 @@ class MainWindow : public QMainWindow void updateProfilesMenu(); void processAutoConnect(); + void onNamSslErrors(QNetworkReply* reply, const QList& errors); void onCurrentTimeSheetChanged(); void onPluginsChanged(); void onSessionVersionChanged(); @@ -63,6 +64,7 @@ class MainWindow : public QMainWindow KemaiUpdater mUpdater; std::shared_ptr mSession; std::shared_ptr mDesktopEventsMonitor; + std::shared_ptr mNam; // Stacked widget (ownership is transferred, don't try to delete them) ActivityWidget* mActivityWidget = nullptr; diff --git a/src/updater/kemaiUpdater.cpp b/src/updater/kemaiUpdater.cpp index da26d3a..bb9c15c 100644 --- a/src/updater/kemaiUpdater.cpp +++ b/src/updater/kemaiUpdater.cpp @@ -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()), mQ(c) {} QNetworkRequest KemaiUpdater::KemaiUpdaterPrivate::prepareGithubRequest(const QString& path) { @@ -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(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(); } } @@ -67,11 +69,17 @@ KemaiUpdater::KemaiUpdater(QObject* parent) : QObject(parent), mD(new KemaiUpdat KemaiUpdater::~KemaiUpdater() = default; +void KemaiUpdater::setNetworkAccessManager(const std::shared_ptr& 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); } diff --git a/src/updater/kemaiUpdater.h b/src/updater/kemaiUpdater.h index 971a788..acaf01c 100644 --- a/src/updater/kemaiUpdater.h +++ b/src/updater/kemaiUpdater.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -21,6 +22,8 @@ class KemaiUpdater : public QObject explicit KemaiUpdater(QObject* parent = nullptr); ~KemaiUpdater() override; + void setNetworkAccessManager(const std::shared_ptr& nam); + void checkAvailableNewVersion(const QVersionNumber& sinceVersion = QVersionNumber(0, 0, 0), bool silenceIfNoNew = false); signals: diff --git a/src/updater/kemaiUpdater_p.h b/src/updater/kemaiUpdater_p.h index 4a86433..796accb 100644 --- a/src/updater/kemaiUpdater_p.h +++ b/src/updater/kemaiUpdater_p.h @@ -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 networkAccessManager; + std::shared_ptr networkAccessManager; private: KemaiUpdater* const mQ;