Return process IDs in Utils::findRunningProcess().

Rework to return mapping between (filtered) process names and their pid values
(if running). Note that this functionality is not implemented for Linux (but as
it's mostly intended to detect Itunes currently not needed on that platform).

Change-Id: Ie348bfed46bda390f469c37635a96c75e5782616
This commit is contained in:
Dominik Riebeling 2015-12-30 18:27:13 +01:00
parent 3b089b439f
commit 537f9a4ab6
2 changed files with 36 additions and 24 deletions

View file

@ -670,13 +670,13 @@ QStringList Utils::mountpoints(enum MountpointsFilter type)
/** Check if a process with a given name is running /** Check if a process with a given name is running
* @param names list of names to check * @param names list of names to filter on. All processes if empty list.
* @return list of detected processes. * @return list of processname, process ID pairs.
*/ */
QStringList Utils::findRunningProcess(QStringList names) QMap<QString, QList<int> > Utils::findRunningProcess(QStringList names)
{ {
QStringList processlist; QMap<QString, QList<int> > processlist;
QStringList found; QMap<QString, QList<int> > found;
#if defined(Q_OS_WIN32) #if defined(Q_OS_WIN32)
HANDLE hdl; HANDLE hdl;
PROCESSENTRY32 entry; PROCESSENTRY32 entry;
@ -694,14 +694,16 @@ QStringList Utils::findRunningProcess(QStringList names)
return found; return found;
} }
processlist.append(QString::fromWCharArray(entry.szExeFile));
do { do {
int pid = entry.th32ProcessID; // FIXME: DWORD vs int!
QString name = QString::fromWCharArray(entry.szExeFile);
if(processlist.find(name) == processlist.end()) {
processlist.insert(name, QList<int>());
}
processlist[name].append(pid);
entry.dwSize = sizeof(PROCESSENTRY32); entry.dwSize = sizeof(PROCESSENTRY32);
entry.szExeFile[0] = '\0'; entry.szExeFile[0] = '\0';
result = Process32Next(hdl, &entry); result = Process32Next(hdl, &entry);
if(result) {
processlist.append(QString::fromWCharArray(entry.szExeFile));
}
} while(result); } while(result);
CloseHandle(hdl); CloseHandle(hdl);
#endif #endif
@ -726,28 +728,38 @@ QStringList Utils::findRunningProcess(QStringList names)
if(isprint(buf[i])) break; if(isprint(buf[i])) break;
} }
// avoid adding duplicates. // avoid adding duplicates.
QString process = QString::fromUtf8(&buf[i]); QString name = QString::fromUtf8(&buf[i]);
if(!processlist.contains(process)) { if(processlist.find(name) == processlist.end()) {
processlist.append(process); processlist.insert(name, QList<int>());
} }
processlist[name].append(pid);
} }
} }
} while(err == noErr); } while(err == noErr);
#endif #endif
// check for given names in list of processes #if defined(Q_OS_LINUX)
// not implemented for Linux!
#endif
// Filter for names (unless empty)
if(names.size() > 0) {
for(int i = 0; i < names.size(); ++i) { for(int i = 0; i < names.size(); ++i) {
QStringList k(processlist.keys());
#if defined(Q_OS_WIN32) #if defined(Q_OS_WIN32)
// the process name might be truncated. Allow the extension to be partial. // the process name might be truncated. Allow the extension to be partial.
int index = processlist.indexOf(QRegExp(names.at(i) + "(\\.(e(x(e?)?)?)?)?")); int index = k.indexOf(QRegExp(names.at(i) + "(\\.(e(x(e?)?)?)?)?",
Qt::CaseInsensitive));
#else #else
int index = processlist.indexOf(names.at(i)); int index = k.indexOf(names.at(i));
#endif #endif
if(index != -1) { if(index != -1) {
found.append(processlist.at(index)); found.insert(k[index], processlist[k[index]]);
} }
} }
LOG_INFO() << "Found listed processes running:" << found; }
else {
found = processlist;
}
LOG_INFO() << "Looking for processes" << names << "found" << found;
return found; return found;
} }

View file

@ -54,7 +54,7 @@ public:
static QStringList mountpoints(enum MountpointsFilter type = MountpointsAll); static QStringList mountpoints(enum MountpointsFilter type = MountpointsAll);
static QString resolveDevicename(QString path); static QString resolveDevicename(QString path);
static QString resolveMountPoint(QString device); static QString resolveMountPoint(QString device);
static QStringList findRunningProcess(QStringList names); static QMap<QString, QList<int> > findRunningProcess(QStringList names);
static bool ejectDevice(QString device); static bool ejectDevice(QString device);
}; };