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
|
|
|
*/
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QDebug>
|
2019-12-11 15:00:39 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QIcon>
|
2023-06-12 14:34:43 +01:00
|
|
|
#include <QSettings>
|
2023-07-16 15:20:34 +01:00
|
|
|
#include <QStandardPaths>
|
2020-10-23 06:55:36 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <tlhelp32.h>
|
2019-12-11 15:00:39 +00:00
|
|
|
|
2023-06-12 14:34:43 +01:00
|
|
|
#include <winrt/Windows.Foundation.Collections.h>
|
2023-07-16 15:20:34 +01:00
|
|
|
#include <winrt/Windows.UI.ViewManagement.h>
|
2023-06-12 14:34:43 +01:00
|
|
|
|
2020-10-23 06:55:36 +01:00
|
|
|
#include "indicator_debug.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "indicatorhelper.h"
|
2019-12-11 15:00:39 +00:00
|
|
|
|
2023-06-12 14:34:43 +01:00
|
|
|
winrt::Windows::UI::ViewManagement::UISettings uiSettings;
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
IndicatorHelper::IndicatorHelper(const QUrl &indicatorUrl)
|
|
|
|
: m_indicatorUrl(indicatorUrl)
|
|
|
|
{
|
2023-06-12 14:34:43 +01:00
|
|
|
uiSettings = winrt::Windows::UI::ViewManagement::UISettings();
|
2022-09-10 22:23:52 +01:00
|
|
|
}
|
2021-05-28 19:42:26 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
IndicatorHelper::~IndicatorHelper()
|
|
|
|
{
|
2021-05-28 19:42:26 +01:00
|
|
|
this->terminateProcess(processes::dbus_daemon, m_indicatorUrl);
|
2021-06-15 09:03:51 +01:00
|
|
|
this->terminateProcess(processes::kdeconnect_app, m_indicatorUrl);
|
|
|
|
this->terminateProcess(processes::kdeconnect_handler, m_indicatorUrl);
|
|
|
|
this->terminateProcess(processes::kdeconnect_settings, m_indicatorUrl);
|
|
|
|
this->terminateProcess(processes::kdeconnect_sms, m_indicatorUrl);
|
2021-05-28 19:42:26 +01:00
|
|
|
this->terminateProcess(processes::kdeconnect_daemon, m_indicatorUrl);
|
|
|
|
}
|
2019-12-11 15:00:39 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void IndicatorHelper::preInit()
|
|
|
|
{
|
|
|
|
}
|
2019-12-11 15:00:39 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void IndicatorHelper::postInit()
|
|
|
|
{
|
|
|
|
}
|
2019-12-11 15:00:39 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void IndicatorHelper::iconPathHook()
|
|
|
|
{
|
2023-06-12 14:34:43 +01:00
|
|
|
// FIXME: This doesn't seem to be enough for QIcon::fromTheme to find the icons, so we still have to use the full path when setting the icon
|
|
|
|
const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icons"), QStandardPaths::LocateDirectory);
|
|
|
|
if (!iconPath.isNull()) {
|
|
|
|
QStringList themeSearchPaths = QIcon::themeSearchPaths();
|
|
|
|
themeSearchPaths << iconPath;
|
|
|
|
QIcon::setThemeSearchPaths(themeSearchPaths);
|
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
}
|
2019-12-11 15:00:39 +00:00
|
|
|
|
|
|
|
int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
|
|
|
|
{
|
2023-07-21 13:53:23 +01:00
|
|
|
kdeconnectd.start(processes::kdeconnect_daemon, QStringList());
|
2019-12-11 15:00:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-12 14:34:43 +01:00
|
|
|
void onThemeChanged(QSystemTrayIcon &systray)
|
2019-12-11 15:00:39 +00:00
|
|
|
{
|
2023-06-12 14:34:43 +01:00
|
|
|
// Since this is a system tray icon, we care about the system theme and not the app theme
|
|
|
|
QSettings registry(QStringLiteral("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"), QSettings::Registry64Format);
|
|
|
|
bool isLightTheme = registry.value(QStringLiteral("SystemUsesLightTheme")).toBool();
|
|
|
|
if (isLightTheme) {
|
2023-07-16 15:20:34 +01:00
|
|
|
systray.setIcon(
|
|
|
|
QIcon(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("icons/hicolor/scalable/apps/kdeconnectindicator.svg"))));
|
2023-06-12 14:34:43 +01:00
|
|
|
} else {
|
2023-07-16 15:20:34 +01:00
|
|
|
systray.setIcon(
|
|
|
|
QIcon(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("icons/hicolor/scalable/apps/kdeconnectindicatordark.svg"))));
|
2023-06-12 14:34:43 +01:00
|
|
|
}
|
2019-12-11 15:00:39 +00:00
|
|
|
}
|
2023-06-12 14:34:43 +01:00
|
|
|
|
|
|
|
void IndicatorHelper::systrayIconHook(QSystemTrayIcon &systray)
|
2019-12-11 15:00:39 +00:00
|
|
|
{
|
2023-06-12 14:34:43 +01:00
|
|
|
// Set a callback so we can detect changes to light/dark themes and manually call the callback once the first time
|
|
|
|
uiSettings.ColorValuesChanged([&systray](auto &&unused1, auto &&unused2) {
|
|
|
|
onThemeChanged(systray);
|
|
|
|
});
|
|
|
|
onThemeChanged(systray);
|
2019-12-11 15:00:39 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
do {
|
2021-05-27 07:30:20 +01:00
|
|
|
if (QString::fromWCharArray((wchar_t *)pe32.szExeFile) == processName) {
|
2022-09-10 22:23:52 +01:00
|
|
|
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
|
2020-10-23 06:55:36 +01:00
|
|
|
|
|
|
|
if (hProcess == NULL) {
|
|
|
|
qCWarning(KDECONNECT_INDICATOR) << "Failed to get handle for the process:" << processName;
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
const DWORD processPathSize = 4096;
|
|
|
|
CHAR processPathString[processPathSize];
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
BOOL gotProcessPath = QueryFullProcessImageNameA(hProcess, 0, (LPSTR)processPathString, (PDWORD)&processPathSize);
|
2020-10-23 06:55:36 +01:00
|
|
|
|
|
|
|
if (gotProcessPath) {
|
|
|
|
const QUrl processUrl = QUrl::fromLocalFile(QString::fromStdString(processPathString)); // to replace \\ with /
|
|
|
|
if (indicatorUrl.isParentOf(processUrl)) {
|
2022-09-10 22:23:52 +01:00
|
|
|
BOOL terminateSuccess = TerminateProcess(hProcess, 0);
|
2020-10-23 06:55:36 +01:00
|
|
|
if (!terminateSuccess) {
|
|
|
|
qCWarning(KDECONNECT_INDICATOR) << "Failed to terminate process:" << processName;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (Process32Next(hProcessSnap, &pe32));
|
|
|
|
|
|
|
|
CloseHandle(hProcessSnap);
|
|
|
|
return TRUE;
|
|
|
|
}
|