Skip to content

Commit

Permalink
♻ update the behaviour of execute button
Browse files Browse the repository at this point in the history
update thread logic
  • Loading branch information
vnepogodin committed Jan 27, 2022
1 parent 51f6760 commit b5bf22a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <QProcess>
Expand Down Expand Up @@ -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();
}
Expand All @@ -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
27 changes: 13 additions & 14 deletions src/km-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
6 changes: 3 additions & 3 deletions src/km-window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Kernel> m_kernels = Kernel::get_kernels(m_handle);
std::unique_ptr<Ui::MainWindow> m_ui = std::make_unique<Ui::MainWindow>();
Expand Down

0 comments on commit b5bf22a

Please sign in to comment.