-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqprocesswatcher.cpp
More file actions
254 lines (201 loc) · 7.14 KB
/
Copy pathqprocesswatcher.cpp
File metadata and controls
254 lines (201 loc) · 7.14 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*****************************************************************************
** **
** Copyright (C) 2019 WxMaper (https://wxmaper.ru/) **
** **
** This file is part of the QProcessWatcher class. **
** **
** BEGIN LICENSE: MPL 2.0 **
** **
** This Source Code Form is subject to the terms of the Mozilla Public **
** License, v. 2.0. If a copy of the MPL was not distributed with this **
** file, You can obtain one at http://mozilla.org/MPL/2.0/. **
** **
** END LICENSE **
** **
*****************************************************************************/
#include "qprocesswatcher.h"
#include "qprocesswatcher_p.h"
#include <QThread>
#include <QTimer>
#include <QCoreApplication>
#if defined(__MINGW32__) || defined(__MINGW64__)
#include <windows.h>
#include <tlhelp32.h>
#else
#include <Windows.h>
#include <TlHelp32.h>
#endif
#ifdef PROCESS_WATCHER_DEBUG
#include <QDebug>
#define PW_DEBUG(d) qDebug() << d
#else
#define PW_DEBUG(d)
#endif
QProcessWatcher::QProcessWatcher(quint32 timerInterval, QObject *parent)
: QObject(parent),
m_watcher(new QProcessWatcherPrivate(timerInterval)),
m_watcherThread(new QThread)
{
m_watcher->moveToThread(m_watcherThread);
connect(this, &QProcessWatcher::doWatchProcesses,
m_watcher, &QProcessWatcherPrivate::checkProcesses);
connect(m_watcher, &QProcessWatcherPrivate::processStarted,
this, &QProcessWatcher::processStarted);
connect(m_watcher, &QProcessWatcherPrivate::processFinished,
this, &QProcessWatcher::processFinished);
connect(m_watcherThread, &QThread::finished,
m_watcher, &QProcessWatcherPrivate::deleteLater);
connect(m_watcherThread, &QThread::started,
m_watcher, &QProcessWatcherPrivate::createTimer);
m_watcherThread->start();
}
QProcessWatcher::~QProcessWatcher()
{
PW_DEBUG(Q_FUNC_INFO);
m_watcherThread->quit();
if (!m_watcherThread->wait(2000)) {
m_watcherThread->terminate();
m_watcherThread->wait();
}
}
void QProcessWatcher::setWatchingInterval(quint32 msecs)
{
m_watcher->setTimerInterval(msecs);
}
void QProcessWatcher::addProcess(const QString &processName)
{
m_watcher->addProcess(processName);
}
bool QProcessWatcher::removeProcess(const QString &processName)
{
return m_watcher->removeProcess(processName);
}
void QProcessWatcher::watchProcesses()
{
emit doWatchProcesses();
}
// ===================== //
// ProcessWatcherPrivate //
// ===================== //
QProcessWatcherPrivate::QProcessWatcherPrivate(quint32 timerInterval)
: QObject(),
m_checkTimer(nullptr),
m_timerInterval(timerInterval)
{
}
QProcessWatcherPrivate::~QProcessWatcherPrivate()
{
PW_DEBUG(Q_FUNC_INFO);
if (m_checkTimer) {
m_checkTimer->stop();
}
}
void QProcessWatcherPrivate::setTimerInterval(quint32 msecs)
{
QMutexLocker locker(&m_mutex);
m_timerInterval = msecs;
if (m_checkTimer != nullptr && m_checkTimer->isActive()) {
stop();
start();
}
}
void QProcessWatcherPrivate::addProcess(const QString &processName)
{
PW_DEBUG(QString("add watched process `%1`")
.arg(processName));
QMutexLocker locker(&m_mutex);
stop();
const QString lowerProcessName = processName.toLower();
m_processesMap.insert(lowerProcessName, processName);
m_processesStatus.insert(lowerProcessName, false);
start();
}
bool QProcessWatcherPrivate::removeProcess(const QString &processName)
{
PW_DEBUG(QString("remove watched process `%1`")
.arg(processName));
QMutexLocker locker(&m_mutex);
stop();
const QString lowerProcessName = processName.toLower();
bool result = m_processesMap.contains(lowerProcessName);
if (result) {
m_processesMap.remove(lowerProcessName);
m_processesStatus.remove(lowerProcessName);
}
start();
return result;
}
void QProcessWatcherPrivate::checkProcesses()
{
QMutexLocker locker(&m_mutex);
QStringList processesList = m_processesMap.keys();
int size = processesList.size();
if (size == 0)
return;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (Process32First(snapshot, &entry)) {
QString originalProcessName;
QString lowerProcessName;
int index;
do {
originalProcessName = QString::fromWCharArray(entry.szExeFile);
lowerProcessName = originalProcessName.toLower();
if ((index = processesList.indexOf(lowerProcessName)) >= 0) {
processExists(lowerProcessName, originalProcessName);
processesList.removeAt(index);
size--;
}
} while (size > 0 && Process32Next(snapshot, &entry));
// We don't need check all running processes if processesList is empty
}
CloseHandle(snapshot);
for (int i = 0; i < size; i++) {
processNotExists(processesList.at(i));
}
}
void QProcessWatcherPrivate::createTimer()
{
QMutexLocker locker(&m_mutex);
m_checkTimer = new QTimer(this);
m_checkTimer->setInterval(m_timerInterval);
connect(m_checkTimer, &QTimer::timeout,
this, &QProcessWatcherPrivate::checkProcesses);
start();
}
void QProcessWatcherPrivate::start()
{
if (m_checkTimer == nullptr) {
return;
}
m_checkTimer->setInterval(m_timerInterval);
QTimer::singleShot(0, m_checkTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
}
void QProcessWatcherPrivate::stop()
{
if (m_checkTimer == nullptr) {
return;
}
QTimer::singleShot(0, m_checkTimer, &QTimer::stop);
}
void QProcessWatcherPrivate::processExists(const QString &lowerProcessName,
const QString &originalProcessName)
{
if (m_processesStatus.value(lowerProcessName) == false) {
m_processesStatus.insert(lowerProcessName, true);
emit processStarted(m_processesMap.value(lowerProcessName), originalProcessName);
PW_DEBUG(QString("process `%1` started with name `%2`")
.arg(m_processesMap.value(lowerProcessName))
.arg(originalProcessName));
}
}
void QProcessWatcherPrivate::processNotExists(const QString &lowerProcessName)
{
if (m_processesStatus.value(lowerProcessName) == true) {
m_processesStatus.insert(lowerProcessName, false);
emit processFinished(m_processesMap.value(lowerProcessName));
PW_DEBUG(QString("process `%1` finished")
.arg(m_processesMap.value(lowerProcessName)));
}
}