Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F140041
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
12 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b2372b5..637a346 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,50 +1,51 @@
cmake_minimum_required(VERSION 3.13)
project(kazv)
include(FeatureSummary)
set(kazv_VERSION_MAJOR 0)
set(kazv_VERSION_MINOR 0)
set(kazv_VERSION_PATCH 1)
set(kazv_VERSION_STRING ${kazv_VERSION_MAJOR}.${kazv_VERSION_MINOR}.${kazv_VERSION_PATCH})
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
set(CMAKE_CXX_STANDARD 17)
set(QT5_MIN_VERSION 5.15)
set(KF5_MIN_VERSION 5.78)
find_package(ECM ${KF5_MIN_VERSION} REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH})
include(KDEInstallDirs)
include(KDECMakeSettings)
include(KDECompilerSettings NO_POLICY_SCOPE)
include(ECMSetupVersion)
include(ECMGenerateHeaders)
include(ECMPoQmTools)
+include(ECMAddTests)
find_package(Qt5 ${QT5_MIN_VERSION} REQUIRED COMPONENTS
Core Gui Qml QuickControls2 Svg Concurrent Widgets
- Multimedia
+ Multimedia Test
)
# Sql Quick QuickControls2 Network DBus Xml)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS Kirigami2 Config KIO)
# Notifications Config Service KIO)
#find_package(MauiKit REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(libkazv REQUIRED COMPONENTS job)
kde_enable_exceptions()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -foperator-names -DQT_NO_EMIT")
add_subdirectory(src)
install(PROGRAMS moe.kazv.mxc.kazv.desktop DESTINATION ${KDE_INSTALL_APPDIR})
install(FILES moe.kazv.mxc.kazv.appdata.xml DESTINATION ${KDE_INSTALL_METAINFODIR})
feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 1ffea9c..aa821e1 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,45 +1,49 @@
set(KAZV_DATA_DIR ${KDE_INSTALL_DATADIR})
set(KAZV_L10N_DIR ${KAZV_DATA_DIR}/l10n)
configure_file(kazv-path-config.hpp.in kazv-path-config.hpp)
configure_file(kazv-version.cpp.in kazv-version.cpp)
set(kazv_SRCS
${CMAKE_CURRENT_BINARY_DIR}/kazv-version.cpp
main.cpp
matrix-sdk.cpp
matrix-room.cpp
matrix-room-list.cpp
matrix-room-timeline.cpp
matrix-room-member.cpp
matrix-event.cpp
meta-types.cpp
l10n-provider.cpp
qt-rand-adapter.cpp
+ qt-job-handler.cpp
+ qt-job.cpp
kazv-config.cpp
kazv-io-manager.cpp
kazv-io-job.cpp
upload-job-model.cpp
shortcuts/shortcut-util.cpp
resources.qrc
)
add_executable(kazv ${kazv_SRCS})
target_include_directories(kazv PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(kazv
libkazv::kazvall
libkazv::kazvbase
libkazv::kazvjob
Threads::Threads
Qt5::Core Qt5::Gui Qt5::Qml Qt5::Quick Qt5::QuickControls2 Qt5::Svg Qt5::Concurrent Qt5::Widgets
KF5::ConfigCore KF5::KIOCore)
install(TARGETS kazv ${KF5_INSTALL_TARGETS_DEFAULT_ARGS})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/l10n/
DESTINATION ${KAZV_L10N_DIR}
FILES_MATCHING PATTERN "*.ftl"
PATTERN "*.json"
)
+
+add_subdirectory(tests)
diff --git a/src/qt-job-handler.cpp b/src/qt-job-handler.cpp
new file mode 100644
index 0000000..e7f9473
--- /dev/null
+++ b/src/qt-job-handler.cpp
@@ -0,0 +1,84 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2022 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libkazv-config.hpp>
+
+#include <unordered_map>
+
+#include <QPointer>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QNetworkAccessManager>
+#include <QByteArray>
+#include <QUrlQuery>
+#include <QUrl>
+
+#include <zug/transducer/filter.hpp>
+#include <zug/into.hpp>
+
+#include "qt-job-handler.hpp"
+
+struct QtJobHandler::Private
+{
+ struct JobDesc
+ {
+ BaseJob kazvJob;
+ QPointer<QNetworkReply> reply;
+ QByteArray data{};
+ };
+
+ std::unordered_map<QNetworkReply *, JobDesc> jobs;
+ QNetworkAccessManager *manager;
+
+ Private(QtJobHandler *q)
+ : jobs()
+ , manager(new QNetworkAccessManager(q))
+ {
+ }
+
+ // void collectJobs()
+ // {
+ // jobs = zug::into(
+ // std::unordered_map<QNetworkReply *, JobDesc>(),
+ // zug::filter([](const auto &, const auto &desc) { return !desc.reply.isNull(); }),
+ // jobs
+ // );
+ // }
+};
+
+QtJobHandler::QtJobHandler()
+ : m_d(new Private(this))
+{
+}
+
+QtJobHandler::~QtJobHandler() = default;
+
+void QtJobHandler::async(std::function<void()> func)
+{
+}
+
+void QtJobHandler::setTimeout(std::function<void()> func, int ms,
+ std::optional<std::string> timerId)
+{
+}
+
+void QtJobHandler::setInterval(std::function<void()> func, int ms,
+ std::optional<std::string> timerId)
+{
+}
+
+void QtJobHandler::cancel(std::string timerId)
+{
+}
+
+void QtJobHandler::submit(BaseJob job,
+ std::function<void(Response)> callback)
+{
+}
+
+void QtJobHandler::stop()
+{
+}
diff --git a/src/qt-job-handler.hpp b/src/qt-job-handler.hpp
new file mode 100644
index 0000000..0f91a56
--- /dev/null
+++ b/src/qt-job-handler.hpp
@@ -0,0 +1,41 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2022 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#pragma once
+#include <libkazv-config.hpp>
+
+#include <memory>
+
+#include <QObject>
+
+#include <jobinterface.hpp>
+
+using namespace Kazv;
+
+class QtJobHandler : public QObject, public JobInterface
+{
+ Q_OBJECT
+
+public:
+ QtJobHandler();
+ ~QtJobHandler() override;
+
+ void async(std::function<void()> func) override;
+ void setTimeout(std::function<void()> func, int ms,
+ std::optional<std::string> timerId = std::nullopt) override;
+ void setInterval(std::function<void()> func, int ms,
+ std::optional<std::string> timerId = std::nullopt) override;
+ void cancel(std::string timerId) override;
+
+ void submit(BaseJob job,
+ std::function<void(Response)> callback) override;
+
+ void stop();
+
+private:
+ struct Private;
+ std::unique_ptr<Private> m_d;
+};
diff --git a/src/qt-job.cpp b/src/qt-job.cpp
new file mode 100644
index 0000000..b153310
--- /dev/null
+++ b/src/qt-job.cpp
@@ -0,0 +1,120 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2022-2023 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libkazv-config.hpp>
+
+#include <QNetworkAccessManager>
+#include <QPointer>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QUrlQuery>
+
+#include "qt-job.hpp"
+
+struct QtJob::Private
+{
+ QPointer<QNetworkAccessManager> manager;
+ BaseJob job;
+ std::function<void(Response)> callback;
+ QByteArray receivedData{};
+ QPointer<QNetworkReply> reply{};
+
+ QNetworkRequest createRequest() const
+ {
+ auto query = job.requestQuery();
+ auto qQuery = QUrlQuery();
+ for (auto &&[k, v] : query) {
+ qQuery.addQueryItem(QByteArray::fromStdString(k), QByteArray::fromStdString(v));
+ }
+
+ auto url = QUrl(QString::fromStdString(job.url()));
+ url.setQuery(qQuery);
+
+ auto request = QNetworkRequest(url);
+
+ auto header = job.requestHeader();
+ for (auto &&[k, v] : *header) {
+ request.setRawHeader(QByteArray::fromStdString(k), QByteArray::fromStdString(v));
+ }
+
+ return request;
+ }
+
+ Header getHeaders(QList<QNetworkReply::RawHeaderPair> headers)
+ {
+ typename Header::value_type res;
+ for (auto &&[name, value] : headers) {
+ res[name.toStdString()] = value.toStdString();
+ }
+ return res;
+ }
+};
+
+static QByteArray methodFor(BaseJob::Get)
+{
+ return "GET";
+}
+
+static QByteArray methodFor(BaseJob::Post)
+{
+ return "POST";
+}
+
+static QByteArray methodFor(BaseJob::Put)
+{
+ return "PUT";
+}
+
+static QByteArray methodFor(BaseJob::Delete)
+{
+ return "DELETE";
+}
+
+static QByteArray methodFor(BaseJob::Method m)
+{
+ return std::visit(
+ [](auto &&m) { return methodFor(m); },
+ m
+ );
+}
+
+QtJob::QtJob(QNetworkAccessManager *manager, BaseJob job, std::function<void(Response)> callback)
+ : m_d(new Private{manager, std::move(job), std::move(callback)})
+{
+ auto request = m_d->createRequest();
+
+ auto body = QByteArray::fromStdString(std::get<BytesBody>(m_d->job.requestBody()));
+
+ m_d->reply = m_d->manager->sendCustomRequest(request, methodFor(m_d->job.requestMethod()), body);
+
+ connect(m_d->reply.data(), &QNetworkReply::readyRead, this, &QtJob::injectData);
+ connect(m_d->reply.data(), &QNetworkReply::finished, this, &QtJob::callCallback);
+ connect(m_d->reply.data(), &QNetworkReply::finished, this, &QtJob::finished);
+}
+
+QtJob::~QtJob() = default;
+
+void QtJob::injectData()
+{
+ auto size = m_d->reply->bytesAvailable();
+ auto thisRead = m_d->reply->read(size);
+ m_d->receivedData += thisRead;
+}
+
+void QtJob::callCallback()
+{
+ auto statusCode = m_d->reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
+ auto header = m_d->getHeaders(m_d->reply->rawHeaderPairs());
+
+ auto response = Response{
+ static_cast<Response::StatusCode>(statusCode),
+ std::move(m_d->receivedData).toStdString(),
+ header,
+ {} // extraData, will be added in genResponse
+ };
+
+ std::move(m_d->callback)(std::move(m_d->job).genResponse(response));
+}
diff --git a/src/qt-job.hpp b/src/qt-job.hpp
new file mode 100644
index 0000000..6a99649
--- /dev/null
+++ b/src/qt-job.hpp
@@ -0,0 +1,36 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2022 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#pragma once
+#include <libkazv-config.hpp>
+
+#include <memory>
+
+#include <QObject>
+
+#include <jobinterface.hpp>
+
+using namespace Kazv;
+
+class QtJob : public QObject
+{
+ Q_OBJECT
+
+public:
+ QtJob(QNetworkAccessManager *manager, BaseJob job, std::function<void(Response)> callback);
+ ~QtJob() override;
+
+Q_SIGNALS:
+ void finished();
+
+public Q_SLOTS:
+ void injectData();
+ void callCallback();
+
+private:
+ struct Private;
+ std::unique_ptr<Private> m_d;
+};
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
new file mode 100644
index 0000000..57a75e6
--- /dev/null
+++ b/src/tests/CMakeLists.txt
@@ -0,0 +1,4 @@
+ecm_add_tests(
+ qt-job-handler-test.cpp
+ LINK_LIBRARIES Qt5::Test libkazv::kazvall
+)
diff --git a/src/tests/qt-job-handler-test.cpp b/src/tests/qt-job-handler-test.cpp
new file mode 100644
index 0000000..b98b79a
--- /dev/null
+++ b/src/tests/qt-job-handler-test.cpp
@@ -0,0 +1,40 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2023 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libkazv-config.hpp>
+
+#include <basejob.hpp>
+
+#include "tests.hpp"
+
+#include "qt-job-handler-test.hpp"
+
+using namespace Kazv;
+
+void QtJobHandlerTest::testBaseJob()
+{
+ // BaseJob job(TEST_SERVER_URL, "/.well-known/matrix/client", BaseJob::Get{}, "TestJob");
+
+ // CprJobHandler h(ioContext.get_executor());
+ // h.submit(
+ // job.withData(json{{"test", "bar"}}),
+ // [&h](auto r) {
+ // if (r.statusCode == 200) {
+ // REQUIRE( isBodyJson(r.body) );
+
+ // json j = r.jsonBody().get();
+
+ // REQUIRE_NOTHROW( (j["m.homeserver"]["base_url"]) );
+ // REQUIRE( (j["m.homeserver"]["base_url"].size() > 0) );
+
+ // REQUIRE( r.dataStr("test") == "bar" );
+ // }
+ // h.stop();
+ // });
+ // ioContext.run();
+}
+
+QTEST_MAIN(QtJobHandlerTest)
diff --git a/src/tests/qt-job-handler-test.hpp b/src/tests/qt-job-handler-test.hpp
new file mode 100644
index 0000000..8fc28f1
--- /dev/null
+++ b/src/tests/qt-job-handler-test.hpp
@@ -0,0 +1,19 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2023 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <QtTest>
+
+class QtJobHandlerTest : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void testBaseJob();
+ // void testSetTimeout();
+ // void testSetInterval();
+ // void testSetIntervalCancel();
+ // void testJobQueue();
+};
diff --git a/src/tests/tests.hpp b/src/tests/tests.hpp
new file mode 100644
index 0000000..1209130
--- /dev/null
+++ b/src/tests/tests.hpp
@@ -0,0 +1,14 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2023 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#pragma once
+#include <string>
+
+#ifndef TEST_SERVER
+#define TEST_SERVER "https://matrix.org"
+#endif
+
+const std::string TEST_SERVER_URL{TEST_SERVER};
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Jan 19, 11:00 AM (3 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55116
Default Alt Text
(12 KB)
Attached To
Mode
rK kazv
Attached
Detach File
Event Timeline
Log In to Comment