diff options
Diffstat (limited to 'src/core/thread')
-rw-r--r-- | src/core/thread/CtxCheckTask.cpp (renamed from src/core/thread/CtxCheckThread.cpp) | 8 | ||||
-rw-r--r-- | src/core/thread/CtxCheckTask.h (renamed from src/core/thread/CtxCheckThread.h) | 11 | ||||
-rw-r--r-- | src/core/thread/FileReadTask.cpp | 88 | ||||
-rw-r--r-- | src/core/thread/FileReadTask.h | 65 | ||||
-rw-r--r-- | src/core/thread/Task.cpp | 74 | ||||
-rw-r--r-- | src/core/thread/Task.h | 113 | ||||
-rw-r--r-- | src/core/thread/TaskRunner.cpp | 66 | ||||
-rw-r--r-- | src/core/thread/TaskRunner.h | 75 | ||||
-rw-r--r-- | src/core/thread/TaskRunnerGetter.cpp | 46 | ||||
-rw-r--r-- | src/core/thread/TaskRunnerGetter.h | 56 |
10 files changed, 593 insertions, 9 deletions
diff --git a/src/core/thread/CtxCheckThread.cpp b/src/core/thread/CtxCheckTask.cpp index edec8855..ee170fbc 100644 --- a/src/core/thread/CtxCheckThread.cpp +++ b/src/core/thread/CtxCheckTask.cpp @@ -24,20 +24,20 @@ * */ -#include "core/thread/CtxCheckThread.h" +#include "core/thread/CtxCheckTask.h" #include "core/GpgContext.h" #include "core/GpgCoreInit.h" #include "core/common/CoreCommonUtil.h" #include "core/function/gpg/GpgKeyGetter.h" -GpgFrontend::CtxCheckThread::CtxCheckThread() : QThread(nullptr) { - connect(this, &CtxCheckThread::SignalGnupgNotInstall, +GpgFrontend::Thread::CtxCheckTask::CtxCheckTask() { + connect(this, &CtxCheckTask::SignalGnupgNotInstall, CoreCommonUtil::GetInstance(), &CoreCommonUtil::SignalGnupgNotInstall); } -void GpgFrontend::CtxCheckThread::run() { +void GpgFrontend::Thread::CtxCheckTask::Run() { // init logging init_logging(); diff --git a/src/core/thread/CtxCheckThread.h b/src/core/thread/CtxCheckTask.h index c597141f..06ddfd82 100644 --- a/src/core/thread/CtxCheckThread.h +++ b/src/core/thread/CtxCheckTask.h @@ -28,20 +28,21 @@ #define GPGFRONTEND_CTXCHECKTRHEAD_H #include "core/GpgFrontendCore.h" +#include "core/thread/Task.h" -namespace GpgFrontend { +namespace GpgFrontend::Thread { /** * @brief * */ -class GPGFRONTEND_CORE_EXPORT CtxCheckThread : public QThread { +class GPGFRONTEND_CORE_EXPORT CtxCheckTask : public Task { Q_OBJECT public: /** * @brief Construct a new Ctx Check Thread object * */ - CtxCheckThread(); + CtxCheckTask(); signals: /** @@ -55,8 +56,8 @@ class GPGFRONTEND_CORE_EXPORT CtxCheckThread : public QThread { * @brief * */ - void run() override; + void Run() override; }; -} // namespace GpgFrontend +} // namespace GpgFrontend::Thread #endif // GPGFRONTEND_CTXCHECKTRHEAD_H diff --git a/src/core/thread/FileReadTask.cpp b/src/core/thread/FileReadTask.cpp new file mode 100644 index 00000000..3a235390 --- /dev/null +++ b/src/core/thread/FileReadTask.cpp @@ -0,0 +1,88 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#include "core/thread/FileReadTask.h" + +#include <utility> + +namespace GpgFrontend::UI { + +FileReadTask::FileReadTask(std::string path) { + connect(this, &FileReadTask::SignalFileBytesReadNext, this, + &FileReadTask::read_bytes); + +#ifdef WINDOWS + std::filesystem::path read_file_path( + QString::fromStdString(path).toStdU16String()); +#else + std::filesystem::path read_file_path( + QString::fromStdString(path).toStdString()); +#endif + read_file_path_ = read_file_path; +} + +void FileReadTask::Run() { + SetFinishAfterRun(false); + + if (is_regular_file(read_file_path_)) { + LOG(INFO) << "read open file" << read_file_path_; + + target_file_.setFileName( + QString::fromStdString(read_file_path_.u8string())); + target_file_.open(QIODevice::ReadOnly); + + if (!(target_file_.isOpen() && target_file_.isReadable())) { + LOG(ERROR) << "file not open or not readable"; + if (target_file_.isOpen()) target_file_.close(); + return; + } + LOG(INFO) << "started reading" << read_file_path_; + read_bytes(); + } else { + emit SignalFileBytesReadEnd(); + } +} + +void FileReadTask::read_bytes() { + QByteArray read_buffer; + if (!target_file_.atEnd() && + (read_buffer = target_file_.read(buffer_size_)).size() > 0) { + LOG(INFO) << "read bytes" << read_buffer.size(); + emit SignalFileBytesRead(std::move(read_buffer)); + } else { + LOG(INFO) << "read bytes end"; + emit SignalFileBytesReadEnd(); + // finish task + emit SignalTaskFinished(); + } +} + +FileReadTask::~FileReadTask() { + LOG(INFO) << "close file" << read_file_path_; + if (target_file_.isOpen()) target_file_.close(); +} + +} // namespace GpgFrontend::UI diff --git a/src/core/thread/FileReadTask.h b/src/core/thread/FileReadTask.h new file mode 100644 index 00000000..d4e61cbe --- /dev/null +++ b/src/core/thread/FileReadTask.h @@ -0,0 +1,65 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#ifndef GPGFRONTEND_FILEREADTHREAD_H +#define GPGFRONTEND_FILEREADTHREAD_H + +#include "core/GpgFrontendCore.h" +#include "core/thread/Task.h" + +namespace GpgFrontend::UI { + +/** + * @brief + * + */ +class GPGFRONTEND_CORE_EXPORT FileReadTask : public GpgFrontend::Thread::Task { + Q_OBJECT + public: + explicit FileReadTask(std::string path); + + virtual ~FileReadTask() override; + + void Run() override; + + signals: + void SignalFileBytesRead(QByteArray bytes); + void SignalFileBytesReadEnd(); + void SignalFileBytesReadNext(); + + private: + std::filesystem::path read_file_path_; + QFile target_file_; + const size_t buffer_size_ = 4096; + QEventLoop looper; + + private slots: + void read_bytes(); +}; + +} // namespace GpgFrontend::UI + +#endif // GPGFRONTEND_FILEREADTHREAD_H diff --git a/src/core/thread/Task.cpp b/src/core/thread/Task.cpp new file mode 100644 index 00000000..9626ba69 --- /dev/null +++ b/src/core/thread/Task.cpp @@ -0,0 +1,74 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#include "core/thread/Task.h" + +#include <functional> + +#include "core/thread/TaskRunner.h" + +GpgFrontend::Thread::Task::Task() { init(); } + +GpgFrontend::Thread::Task::Task(TaskCallback callback) + : callback_(std::move(callback)) { + init(); +} + +GpgFrontend::Thread::Task::Task(TaskRunnable runnable, TaskCallback callback) + : runnable_(runnable), callback_(std::move(callback)) { + init(); +} + +GpgFrontend::Thread::Task::~Task() = default; + +void GpgFrontend::Thread::Task::SetFinishAfterRun(bool finish_after_run) { + this->finish_after_run_ = finish_after_run; +} + +void GpgFrontend::Thread::Task::SetRTN(int rtn) { this->rtn_ = rtn; } + +void GpgFrontend::Thread::Task::init() { + LOG(INFO) << "called"; + connect(this, &Task::SignalTaskFinished, this, &Task::before_finish_task); + connect(this, &Task::SignalTaskFinished, this, &Task::deleteLater); +} + +void GpgFrontend::Thread::Task::before_finish_task() { + LOG(INFO) << "called"; + if (callback_) callback_(rtn_); +} + +void GpgFrontend::Thread::Task::run() { + LOG(INFO) << "called"; + Run(); + if (finish_after_run_) emit SignalTaskFinished(); +} + +void GpgFrontend::Thread::Task::Run() { + if (runnable_) { + rtn_ = runnable_(); + } +} diff --git a/src/core/thread/Task.h b/src/core/thread/Task.h new file mode 100644 index 00000000..4b536176 --- /dev/null +++ b/src/core/thread/Task.h @@ -0,0 +1,113 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#ifndef GPGFRONTEND_TASK_H +#define GPGFRONTEND_TASK_H + +#include <functional> + +#include "core/GpgFrontendCore.h" + +namespace GpgFrontend::Thread { + +class TaskRunner; + +class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable { + Q_OBJECT + public: + using TaskRunnable = std::function<int()>; ///< + using TaskCallback = std::function<void(int)>; ///< + friend class TaskRunner; + + /** + * @brief Construct a new Task object + * + */ + Task(); + + /** + * @brief Construct a new Task object + * + * @param callback The callback function to be executed. + * callback must not be nullptr, and not tp opreate UI object. + */ + Task(TaskCallback callback); + + /** + * @brief Construct a new Task object + * + * @param runnable + */ + Task( + TaskRunnable runnable, TaskCallback callback = [](int) {}); + + /** + * @brief Destroy the Task object + * + */ + virtual ~Task() override; + + /** + * @brief Run - run the task + * + */ + virtual void Run(); + + signals: + void SignalTaskFinished(); + + protected: + void SetFinishAfterRun(bool finish_after_run); + + void SetRTN(int rtn); + + private: + TaskCallback callback_; ///< + TaskRunnable runnable_; ///< + bool finish_after_run_ = true; ///< + int rtn_ = 0; ///< + + /** + * @brief + * + */ + void before_finish_task(); + + /** + * @brief + * + */ + void init(); + + /** + * @brief + * + */ + virtual void run() override; +}; +} // namespace GpgFrontend::Thread + +#endif // GPGFRONTEND_TASK_H
\ No newline at end of file diff --git a/src/core/thread/TaskRunner.cpp b/src/core/thread/TaskRunner.cpp new file mode 100644 index 00000000..2223bdda --- /dev/null +++ b/src/core/thread/TaskRunner.cpp @@ -0,0 +1,66 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#include "core/thread/TaskRunner.h" + +#include "core/thread/Task.h" +#include "easylogging++.h" + +GpgFrontend::Thread::TaskRunner::TaskRunner() = default; + +GpgFrontend::Thread::TaskRunner::~TaskRunner() = default; + +void GpgFrontend::Thread::TaskRunner::PostTask(Task* task) { + LOG(INFO) << "called"; + if (task == nullptr) return; + task->setParent(nullptr); + task->moveToThread(this); + { + std::lock_guard<std::mutex> lock(tasks_mutex_); + tasks.push(task); + } + quit(); +} + +void GpgFrontend::Thread::TaskRunner::run() { + LOG(INFO) << "called"; + while (true) { + if (tasks.empty()) { + LOG(INFO) << "TaskRunner: No tasks to run"; + exec(); + } else { + LOG(INFO) << "TaskRunner: Running task, queue size:" << tasks.size(); + + Task* task = nullptr; + { + std::lock_guard<std::mutex> lock(tasks_mutex_); + task = std::move(tasks.front()); + tasks.pop(); + } + if (task != nullptr) task->run(); + } + } +} diff --git a/src/core/thread/TaskRunner.h b/src/core/thread/TaskRunner.h new file mode 100644 index 00000000..14eaeae7 --- /dev/null +++ b/src/core/thread/TaskRunner.h @@ -0,0 +1,75 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#ifndef GPGFRONTEND_TASKRUNNER_H +#define GPGFRONTEND_TASKRUNNER_H + +#include <mutex> +#include <queue> + +#include "core/GpgFrontendCore.h" + +namespace GpgFrontend::Thread { + +class Task; + +class GPGFRONTEND_CORE_EXPORT TaskRunner : public QThread { + Q_OBJECT + public: + /** + * @brief Construct a new Task Runner object + * + */ + TaskRunner(); + + /** + * @brief Destroy the Task Runner object + * + */ + virtual ~TaskRunner() override; + + /** + * @brief + * + */ + void run() override; + + public slots: + + /** + * @brief + * + * @param task + */ + void PostTask(Task* task); + + private: + std::queue<Task*> tasks; ///< The task queue + std::mutex tasks_mutex_; ///< The task queue mutex +}; +} // namespace GpgFrontend::Thread + +#endif // GPGFRONTEND_TASKRUNNER_H
\ No newline at end of file diff --git a/src/core/thread/TaskRunnerGetter.cpp b/src/core/thread/TaskRunnerGetter.cpp new file mode 100644 index 00000000..186483ec --- /dev/null +++ b/src/core/thread/TaskRunnerGetter.cpp @@ -0,0 +1,46 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#include "core/thread/TaskRunnerGetter.h" + +GpgFrontend::Thread::TaskRunnerGetter::TaskRunnerGetter(int channel) + : SingletonFunctionObject<TaskRunnerGetter>(channel) {} + +GpgFrontend::Thread::TaskRunner* +GpgFrontend::Thread::TaskRunnerGetter::GetTaskRunner( + TaskRunnerType runner_type) { + while (true) { + auto it = task_runners_.find(runner_type); + if (it != task_runners_.end()) { + return it->second; + } else { + auto runner = new TaskRunner(); + task_runners_[runner_type] = runner; + runner->start(); + continue; + } + } +} diff --git a/src/core/thread/TaskRunnerGetter.h b/src/core/thread/TaskRunnerGetter.h new file mode 100644 index 00000000..722484b5 --- /dev/null +++ b/src/core/thread/TaskRunnerGetter.h @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#ifndef GPGFRONTEND_TASKRUNNERGETTER_H +#define GPGFRONTEND_TASKRUNNERGETTER_H + +#include "core/GpgFrontendCore.h" +#include "core/GpgFunctionObject.h" +#include "core/thread/TaskRunner.h" + +namespace GpgFrontend::Thread { + +class GPGFRONTEND_CORE_EXPORT TaskRunnerGetter + : public GpgFrontend::SingletonFunctionObject<TaskRunnerGetter> { + public: + enum TaskRunnerType { + kTaskRunnerType_Default, + kTaskRunnerType_GPG, + kTaskRunnerType_IO, + }; + + TaskRunnerGetter(int channel = SingletonFunctionObject::GetDefaultChannel()); + + TaskRunner *GetTaskRunner( + TaskRunnerType runner_type = kTaskRunnerType_Default); + + private: + std::map<TaskRunnerType, TaskRunner *> task_runners_; +}; + +} // namespace GpgFrontend::Thread + +#endif // GPGFRONTEND_TASKRUNNERGETTER_H
\ No newline at end of file |