File: syncthingkiller.cpp

package info (click to toggle)
syncthingtray 1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,804 kB
  • sloc: cpp: 31,085; xml: 1,694; java: 570; sh: 81; javascript: 53; makefile: 25
file content (83 lines) | stat: -rw-r--r-- 2,993 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "./syncthingkiller.h"

#include <syncthingconnector/syncthingprocess.h>
#define SYNCTHINGTESTHELPER_FOR_CLI
#include "../../testhelper/helper.h"

#include <QCoreApplication>
#include <QMessageBox>

using namespace std;
using namespace Data;
using namespace CppUtilities;

namespace QtGui {

SyncthingKiller::SyncthingKiller(std::vector<ProcessWithConnection> &&processes)
    : m_processes(processes)
{
#ifndef LIB_SYNCTHING_CONNECTOR_NOOP_PROCESS
    for (const auto [process, connection] : m_processes) {
        process->stopSyncthing(connection);
        connect(process, &SyncthingProcess::confirmKill, this, &SyncthingKiller::confirmKill);
    }
#endif
}

void SyncthingKiller::waitForFinished()
{
#ifndef LIB_SYNCTHING_CONNECTOR_NOOP_PROCESS
    for (const auto [process, connection] : m_processes) {
        if (!process->isRunning()) {
            continue;
        }
        if (!waitForSignalsOrFail(noop, 0, signalInfo(this, &SyncthingKiller::ignored),
                signalInfo(process,
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0) || QT_DEPRECATED_SINCE(5, 13)
                    static_cast<void (SyncthingProcess::*)(int, QProcess::ExitStatus)>(
#endif
                        &SyncthingProcess::finished
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0) || QT_DEPRECATED_SINCE(5, 13)
                        )
#endif
                        ))) {
            return;
        }
    }
#endif
}

void SyncthingKiller::confirmKill() const
{
#ifndef LIB_SYNCTHING_CONNECTOR_NOOP_PROCESS
    auto *const process = static_cast<SyncthingProcess *>(sender());
    if (!process->isRunning()) {
        return;
    }

    const auto msg(tr("The process %1 (PID: %2) has been requested to terminate but hasn't reacted yet. "
                      "Kill the process?\n\n"
                      "This dialog closes automatically when the process finally terminates.")
            .arg(process->program(), QString::number(process->processId())));
    auto *const msgBox = new QMessageBox(QMessageBox::Critical, QCoreApplication::applicationName(), msg);
    msgBox->setAttribute(Qt::WA_DeleteOnClose);
    msgBox->addButton(tr("Keep running"), QMessageBox::RejectRole);
    msgBox->addButton(tr("Kill process"), QMessageBox::AcceptRole);
    connect(process,
#if !defined(LIB_SYNCTHING_CONNECTOR_BOOST_PROCESS) && (QT_VERSION < QT_VERSION_CHECK(5, 13, 0) || QT_DEPRECATED_SINCE(5, 13))
        static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(
#endif
            &SyncthingProcess::finished
#if !defined(LIB_SYNCTHING_CONNECTOR_BOOST_PROCESS) && (QT_VERSION < QT_VERSION_CHECK(5, 13, 0) || QT_DEPRECATED_SINCE(5, 13))
            )
#endif
            ,
        msgBox, &QMessageBox::close);
    connect(msgBox, &QMessageBox::accepted, process, &SyncthingProcess::killSyncthing);
    // FIXME: can not really ignore, just keep the process running
    //connect(msgBox, &QMessageBox::rejected, this, &SyncthingKiller::ignored);
    msgBox->show();
#endif
}

} // namespace QtGui