2019-12-11 15:00:39 +00:00
|
|
|
/*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2019 Weixuan XIAO <veyx.shaw@gmail.com>
|
2019-12-11 15:00:39 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2019-12-11 15:00:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QStandardPaths>
|
2020-10-23 06:55:36 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <tlhelp32.h>
|
2019-12-11 15:00:39 +00:00
|
|
|
|
|
|
|
#include "indicatorhelper.h"
|
2020-10-23 06:55:36 +01:00
|
|
|
#include "indicator_debug.h"
|
2019-12-11 15:00:39 +00:00
|
|
|
|
|
|
|
IndicatorHelper::IndicatorHelper() {}
|
|
|
|
IndicatorHelper::~IndicatorHelper() {}
|
|
|
|
|
|
|
|
void IndicatorHelper::preInit() {}
|
|
|
|
|
|
|
|
void IndicatorHelper::postInit() {}
|
|
|
|
|
|
|
|
void IndicatorHelper::iconPathHook() {}
|
|
|
|
|
|
|
|
int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
|
|
|
|
{
|
2020-10-23 06:55:36 +01:00
|
|
|
kdeconnectd.start(processes::kdeconnect_daemon);
|
2019-12-11 15:00:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef QSYSTRAY
|
|
|
|
void IndicatorHelper::systrayIconHook(QSystemTrayIcon &systray)
|
|
|
|
{
|
2020-10-19 12:38:47 +01:00
|
|
|
systray.setIcon(QIcon::fromTheme(QStringLiteral("kdeconnect-tray")));
|
2019-12-11 15:00:39 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
|
|
|
|
{
|
|
|
|
Q_UNUSED(systray);
|
|
|
|
}
|
|
|
|
#endif
|
2020-10-23 06:55:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
bool IndicatorHelper::terminateProcess(const QString &processName, const QUrl &indicatorUrl) const
|
|
|
|
{
|
|
|
|
HANDLE hProcessSnap;
|
|
|
|
HANDLE hProcess;
|
|
|
|
|
|
|
|
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
if (hProcessSnap == INVALID_HANDLE_VALUE) {
|
|
|
|
qCWarning(KDECONNECT_INDICATOR) << "Failed to get snapshot of processes.";
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESSENTRY32 pe32;
|
|
|
|
pe32.dwSize = sizeof(PROCESSENTRY32);
|
|
|
|
|
|
|
|
if (!Process32First(hProcessSnap, &pe32)) {
|
|
|
|
qCWarning(KDECONNECT_INDICATOR) << "Failed to get handle for the first process.";
|
|
|
|
CloseHandle(hProcessSnap);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-05-27 07:30:20 +01:00
|
|
|
if (QString::fromWCharArray((wchar_t *)pe32.szExeFile) == processName) {
|
2020-10-23 06:55:36 +01:00
|
|
|
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
|
|
|
|
|
|
|
|
if (hProcess == NULL) {
|
|
|
|
qCWarning(KDECONNECT_INDICATOR) << "Failed to get handle for the process:" << processName;
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
const DWORD processPathSize = 4096;
|
|
|
|
CHAR processPathString[processPathSize];
|
|
|
|
|
|
|
|
BOOL gotProcessPath = QueryFullProcessImageNameA(
|
|
|
|
hProcess,
|
|
|
|
0,
|
|
|
|
(LPSTR)processPathString,
|
|
|
|
(PDWORD) &processPathSize
|
|
|
|
);
|
|
|
|
|
|
|
|
if (gotProcessPath) {
|
|
|
|
const QUrl processUrl = QUrl::fromLocalFile(QString::fromStdString(processPathString)); // to replace \\ with /
|
|
|
|
if (indicatorUrl.isParentOf(processUrl)) {
|
|
|
|
BOOL terminateSuccess = TerminateProcess(
|
|
|
|
hProcess,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
if (!terminateSuccess) {
|
|
|
|
qCWarning(KDECONNECT_INDICATOR) << "Failed to terminate process:" << processName;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (Process32Next(hProcessSnap, &pe32));
|
|
|
|
|
|
|
|
CloseHandle(hProcessSnap);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|