1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "polkit.h"
6#include "processutil.h"
7
8#include <QProcess>
9
10#define POLKIT_NAME "pkexec"
11
12PolKit::PolKit()
13{
14
15}
16
17PolKit &PolKit::instance()
18{
19 static PolKit ins;
20 return ins;
21}
22
23qint64 PolKit::execute(const QString &program, const QStringList &arguments)
24{
25 if (!ProcessUtil::exists(program)) {
26 emit failed(QString("Failed, not found %0").arg(program).toLatin1());
27 return -1;
28 }
29
30 QProcess process;
31 process.setProgram(POLKIT_NAME);
32 QStringList argsTemp = arguments;
33 argsTemp.push_front(program);
34 process.setArguments(argsTemp);
35 qint64 polkidID;
36 process.startDetached(&polkidID);
37 return polkidID;
38}
39
40void PolKit::cancel(qint64 executeID)
41{
42 QProcess killProcess;
43 killProcess.setProgram("kill");
44 killProcess.setArguments({"-9", QString("%0").arg(executeID)});
45 killProcess.start();
46
47 emit canceled();
48}
49