From b5bf22a8eef9a9836c62d57f9dfb8f45463831d1 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Fri, 28 Jan 2022 01:18:49 +0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20=20update=20the=20behaviour=20of=20?= =?UTF-8?q?execute=20button?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit update thread logic --- src/kernel.cpp | 6 +++--- src/km-window.cpp | 27 +++++++++++++-------------- src/km-window.hpp | 6 +++--- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/kernel.cpp b/src/kernel.cpp index 85f3732..53c44bc 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -29,6 +29,7 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wuseless-cast" #pragma GCC diagnostic ignored "-Wsign-conversion" +#pragma GCC diagnostic ignored "-Wsuggest-attribute=pure" #endif #include @@ -246,9 +247,6 @@ int runCmdTerminal(QString cmd, bool escalate) { paramlist << cmd; proc.start("/usr/lib/cachyos-kernel-manager/terminal-helper", paramlist); - proc.waitForStarted(-1); - const auto data = proc.readAllStandardError(); - fprintf(stderr, "%s\n", data.toStdString().c_str()); proc.waitForFinished(-1); return proc.exitCode(); } @@ -257,11 +255,13 @@ void Kernel::commit_transaction() noexcept { if (!g_kernel_install_list.empty()) { const auto& packages_install = utils::make_multiline(g_kernel_install_list, false, " "); runCmdTerminal(fmt::format("pacman -S --needed {}", packages_install).c_str(), true); + g_kernel_install_list.clear(); } if (!g_kernel_removal_list.empty()) { const auto& packages_remove = utils::make_multiline(g_kernel_removal_list, false, " "); runCmdTerminal(fmt::format("pacman -Rsn {}", packages_remove).c_str(), true); + g_kernel_removal_list.clear(); } } #endif diff --git a/src/km-window.cpp b/src/km-window.cpp index b55f2b7..ba68660 100644 --- a/src/km-window.cpp +++ b/src/km-window.cpp @@ -453,20 +453,26 @@ MainWindow::MainWindow(QWidget* parent) // Create worker thread m_worker = new Work([&]() { while (m_running) { + m_ui->ok->setEnabled(false); install_packages(m_handle, m_kernels, m_ui->list->model()); remove_packages(m_handle, m_kernels, m_ui->list->model()); #ifdef PKG_DUMMY_IMPL Kernel::commit_transaction(); -#endif +#else m_last_percent = 100; m_last_text = "Done"; +#endif m_running = false; m_ui->ok->setDisabled(false); } }); + m_worker->moveToThread(m_worker_th); + // name to appear in ps, task manager, etc. + m_worker_th->setObjectName("WorkerThread"); + static constexpr auto TIMEOUT_MS = 50; // Set update timer @@ -532,6 +538,10 @@ MainWindow::MainWindow(QWidget* parent) connect(m_ui->cancel, SIGNAL(clicked()), this, SLOT(on_cancel())); connect(m_ui->ok, SIGNAL(clicked()), this, SLOT(on_execute())); + // Connect worker thread signals + connect(m_worker_th, SIGNAL(finished()), m_worker, SLOT(deleteLater())); + connect(m_worker_th, SIGNAL(started()), m_worker, SLOT(doHeavyCaclulations()), Qt::QueuedConnection); + // Wait for async function to finish a2.wait(); } @@ -564,19 +574,8 @@ void Work::doHeavyCaclulations() { } void MainWindow::on_execute() noexcept { - m_running = false; - if (m_worker_th->isRunning()) { - m_worker_th->terminate(); - m_worker_th->wait(1500); - } + if (m_running) + return; m_running = true; - m_ui->ok->setEnabled(false); - m_worker->moveToThread(m_worker_th); - // name to appear in ps, task manager, etc. - m_worker_th->setObjectName("WorkerThread"); - - connect(m_worker_th, SIGNAL(finished()), m_worker, SLOT(deleteLater())); - connect(m_worker_th, SIGNAL(started()), m_worker, SLOT(doHeavyCaclulations()), Qt::QueuedConnection); - m_worker_th->start(); } diff --git a/src/km-window.hpp b/src/km-window.hpp index 02a9d7a..53d9ea8 100644 --- a/src/km-window.hpp +++ b/src/km-window.hpp @@ -74,7 +74,7 @@ class MainWindow final : public QMainWindow { Q_DISABLE_COPY_MOVE(MainWindow) public: explicit MainWindow(QWidget* parent = nullptr); - ~MainWindow() override = default; + virtual ~MainWindow() = default; private slots: void on_cancel() noexcept; @@ -90,9 +90,9 @@ class MainWindow final : public QMainWindow { std::mutex m_mutex{}; QThread* m_worker_th = new QThread(this); - Work* m_worker; + Work* m_worker{nullptr}; - alpm_errno_t m_err; + alpm_errno_t m_err{}; alpm_handle_t* m_handle = alpm_initialize("/", "/var/lib/pacman/", &m_err); std::vector m_kernels = Kernel::get_kernels(m_handle); std::unique_ptr m_ui = std::make_unique();