[OpenDocString] kdeconnect-kde (cpp)
indicatorhelper_win.cpp
IndicatorHelper::IndicatorHelper(const QUrl &indicatorUrl)
    : m_indicatorUrl(indicatorUrl)
{
}
This creates a helper object from an indicator url.
IndicatorHelper::~IndicatorHelper()
{
    this->terminateProcess(processes::dbus_daemon, m_indicatorUrl);
    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);
    this->terminateProcess(processes::kdeconnect_daemon, m_indicatorUrl);
}
This class sets all processes that are running in theIndicatorService. It terminates all DBus and kdeconnect processes, and sets all settings to their default values.
void IndicatorHelper::preInit()
{
}
This code is executed before the object is initialized. It is called before any other code that uses the IndicatorHelper class to initialize the Indicator helper.
void IndicatorHelper::postInit()
{
}
This code is executed after the init sequence of the Indicator helper object is initialized.
void IndicatorHelper::iconPathHook()
{
}
This implements the icon path hook.
int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
{
    kdeconnectd.start(processes::kdeconnect_daemon);
    return 0;
}
This starts a daemon process. It returns 0 on success.
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 {
        if (QString::fromWCharArray((wchar_t *)pe32.szExeFile) == processName) {
            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;
}
This function creates a handle to the process object of the given name, and checks if it exists. It first creates a handle, checks if the first process is the first process of the given name, and if it is it. Then it loops over the processes of the given name, and checks if the process exists and terminates. If the is the first process exists, the function returns TRUE. If the is the first process is found, the function returns FALSE.