-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pause when specific programs are running
Closes #9
- Loading branch information
1 parent
cd97abc
commit c5549fe
Showing
8 changed files
with
140 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Sane Break is a polite and sane break reminder preventing mindless skips. | ||
// Copyright (C) 2024 Allan Chain | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#include "program-monitor.h" | ||
|
||
#include <qglobal.h> | ||
|
||
#include <QProcess> | ||
#include <utility> | ||
|
||
RunningProgramsMonitor::RunningProgramsMonitor(QObject *parent) | ||
: QObject(parent), monitorTimer(new QTimer(this)) { | ||
connect(monitorTimer, &QTimer::timeout, this, &RunningProgramsMonitor::tick); | ||
} | ||
|
||
void RunningProgramsMonitor::startMonitoring() { monitorTimer->start(5000); } | ||
void RunningProgramsMonitor::stopMonitoring() { monitorTimer->stop(); } | ||
|
||
void RunningProgramsMonitor::setPrograms(const QStringList &programs) { | ||
programsToMonitor = programs.filter(validProgramFilter); | ||
if (programsToMonitor.isEmpty() && previouslySeen) { | ||
// Clean up: if all programs are removed from the list, stop pausing | ||
emit programStopped(); | ||
} | ||
tick(); | ||
} | ||
|
||
void RunningProgramsMonitor::tick() { | ||
if (programsToMonitor.isEmpty()) return; | ||
QStringList runningPrograms; | ||
|
||
QProcess process; | ||
#ifdef Q_OS_LINUX | ||
process.start("ps", QStringList() << "-e" << "-o" << "comm="); | ||
#elif defined Q_OS_MACOS | ||
process.start("ps", QStringList() << "-ax" << "-o" << "comm="); | ||
#elif defined Q_OS_WIN | ||
process.start("tasklist", QStringList() << "/fo" << "csv" << "/nh"); | ||
#endif | ||
|
||
process.waitForFinished(); | ||
QString output = process.readAllStandardOutput(); | ||
if (output.isEmpty()) return; | ||
|
||
#ifdef Q_OS_WIN | ||
QStringList lines = output.split('\n', Qt::SkipEmptyParts); | ||
for (const QString &line : lines) { | ||
QStringList parts = line.split(',', Qt::SkipEmptyParts); | ||
if (parts.size() > 0) { | ||
QString program = parts[0].trimmed(); | ||
runningPrograms.append(program); | ||
} | ||
} | ||
#else | ||
runningPrograms = output.split('\n', Qt::SkipEmptyParts); | ||
#endif | ||
|
||
bool currentlySeen = false; | ||
for (const QString &program : std::as_const(runningPrograms)) { | ||
for (const QString &entry : std::as_const(programsToMonitor)) { | ||
if (program.contains(entry)) { | ||
currentlySeen = true; | ||
break; | ||
} | ||
} | ||
} | ||
if (!previouslySeen && currentlySeen) emit programStarted(); | ||
if (previouslySeen && !currentlySeen) emit programStopped(); | ||
previouslySeen = currentlySeen; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Sane Break is a polite and sane break reminder preventing mindless skips. | ||
// Copyright (C) 2024 Allan Chain | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#ifndef RUNNING_PROGRAMS_MONITOR_H | ||
#define RUNNING_PROGRAMS_MONITOR_H | ||
|
||
#include <QObject> | ||
#include <QRegularExpression> | ||
#include <QStringList> | ||
#include <QTimer> | ||
|
||
class RunningProgramsMonitor : public QObject { | ||
Q_OBJECT | ||
|
||
public: | ||
RunningProgramsMonitor(QObject *parent = nullptr); | ||
void startMonitoring(); | ||
void stopMonitoring(); | ||
void setPrograms(const QStringList &programs); | ||
|
||
signals: | ||
void programStarted(); | ||
void programStopped(); | ||
|
||
private slots: | ||
void tick(); | ||
|
||
private: | ||
const QRegularExpression validProgramFilter = QRegularExpression("."); | ||
QTimer *monitorTimer; | ||
QStringList programsToMonitor; | ||
bool previouslySeen = false; | ||
}; | ||
|
||
#endif // RUNNING_PROGRAMS_MONITOR_H |