- fix idle settings processing + version 0.1.13

This commit is contained in:
Dmytro Bogovych 2022-12-11 08:42:19 +03:00
parent 6200504627
commit 447f33bf94
6 changed files with 73 additions and 29 deletions

View File

@ -4,10 +4,10 @@
// App version // App version
#define QBREAK_VERSION_MAJOR 0 #define QBREAK_VERSION_MAJOR 0
#define QBREAK_VERSION_MINOR 1 #define QBREAK_VERSION_MINOR 1
#define QBREAK_VERSION_SUFFIX 12 #define QBREAK_VERSION_SUFFIX 13
// How often UI is updated - interval in seconds // How often UI is updated - interval in seconds
#define INTERVAL_UPDATE_UI (60) #define INTERVAL_UPDATE_UI (15)
// How often progress bar is updated - interval in milliseconds // How often progress bar is updated - interval in milliseconds
#define INTERVAL_UPDATE_PROGRESS (1000) #define INTERVAL_UPDATE_PROGRESS (1000)

View File

@ -6,6 +6,53 @@
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QTranslator> #include <QTranslator>
#include <QFileInfo> #include <QFileInfo>
#include <QDateTime>
#if defined(TARGET_LINUX)
# include <syslog.h>
#endif
// Handler for Qt log messages that sends output to syslog as well as standard error.
void SyslogMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Q_UNUSED(context)
std::string timePrefix = QDateTime::currentDateTime().toString().toStdString();
QByteArray localMsg = msg.toLocal8Bit();
switch (type) {
case QtDebugMsg:
fprintf(stderr, "debug: %s %s\n", timePrefix.c_str(), localMsg.constData());
#if defined(TARGET_LINUX)
syslog(LOG_DEBUG, "debug: %s", localMsg.constData());
#endif
break;
case QtInfoMsg:
fprintf(stderr, "info: %s %s\n", timePrefix.c_str(), localMsg.constData());
#if defined(TARGET_LINUX)
syslog(LOG_INFO, "info: %s", localMsg.constData());
#endif
break;
case QtWarningMsg:
fprintf(stderr, "warning: %s %s\n", timePrefix.c_str(), localMsg.constData());
#if defined(TARGET_LINUX)
syslog(LOG_WARNING, "warning: %s", localMsg.constData());
#endif
break;
case QtCriticalMsg:
fprintf(stderr, "critical: %s %s\n", timePrefix.c_str(), localMsg.constData());
#if defined(TARGET_LINUX)
syslog(LOG_CRIT, "critical: %s", localMsg.constData());
#endif
break;
case QtFatalMsg:
fprintf(stderr, "fatal: %s %s\n", timePrefix.c_str(), localMsg.constData());
#if defined(TARGET_LINUX)
syslog(LOG_ALERT, "fatal: %s", localMsg.constData());
#endif
abort();
}
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -13,6 +60,9 @@ int main(int argc, char *argv[])
if ( !guard.tryToRun() ) if ( !guard.tryToRun() )
return 0; return 0;
// Needed to enable logging to syslog or journald.
qInstallMessageHandler(SyslogMessageHandler);
QApplication app(argc, argv); QApplication app(argc, argv);
QCoreApplication::setOrganizationName("voipobjects.com"); QCoreApplication::setOrganizationName("voipobjects.com");
@ -29,7 +79,7 @@ int main(int argc, char *argv[])
// Put itself into app menu // Put itself into app menu
auto exe_path = QCoreApplication::applicationFilePath();//QFileInfo(QCoreApplication::arguments().front()).absoluteFilePath(); auto exe_path = QCoreApplication::applicationFilePath();
const char* appimage = std::getenv("APPIMAGE"); const char* appimage = std::getenv("APPIMAGE");
if (appimage != nullptr) if (appimage != nullptr)
exe_path = appimage; exe_path = appimage;

View File

@ -122,16 +122,6 @@ void MainWindow::init()
shiftTo(AppState::Counting); shiftTo(AppState::Counting);
} }
static QString get_time()
{
return QDateTime::currentDateTime().toString();
}
static QString get_log_prefix()
{
return get_time();
}
static int str_to_seconds(const QString& s) static int str_to_seconds(const QString& s)
{ {
if (s.isEmpty()) if (s.isEmpty())
@ -225,7 +215,7 @@ void MainWindow::showMe()
} }
} }
// else // else
// qDebug() << get_log_prefix() << "Screen not found!"; // qDebug() << "Screen not found!";
#if defined(DEBUG) #if defined(DEBUG)
showMaximized(); showMaximized();
@ -306,9 +296,7 @@ void MainWindow::shiftTo(AppState newState)
{ {
if (newState == mState) if (newState == mState)
return; return;
#if defined(DEBUG) qDebug() << state2str(mState) << " -> " << state2str(newState);
qDebug() << get_log_prefix() << state2str(mState) << " -> " << state2str(newState);
#endif
switch (newState) switch (newState)
{ {
@ -339,7 +327,10 @@ void MainWindow::shiftTo(AppState newState)
onUpdateUI(); onUpdateUI();
} }
void MainWindow::onUpdateUI() { void MainWindow::onUpdateUI()
{
qDebug() << "UI timer fired.";
int idle_milliseconds = 0; int idle_milliseconds = 0;
switch (mState) { switch (mState) {
case AppState::None: case AppState::None:
@ -350,7 +341,7 @@ void MainWindow::onUpdateUI() {
// Detected idle, don't count this time as working // Detected idle, don't count this time as working
// But check - maybe idle is over // But check - maybe idle is over
idle_milliseconds = get_idle_time_dynamically(); idle_milliseconds = get_idle_time_dynamically();
qDebug() << get_log_prefix() << "Idle found: " << idle_milliseconds / 1000 << "s"; qDebug() << "Idle found: " << idle_milliseconds / 1000 << "s";
if (idle_milliseconds < mAppConfig.idle_timeout * 1000) if (idle_milliseconds < mAppConfig.idle_timeout * 1000)
{ {
@ -371,6 +362,8 @@ void MainWindow::onUpdateUI() {
if (!mIdleStart && mAppConfig.idle_timeout) if (!mIdleStart && mAppConfig.idle_timeout)
{ {
idle_milliseconds = get_idle_time_dynamically(); idle_milliseconds = get_idle_time_dynamically();
qDebug() << "Idle found: " << idle_milliseconds / 1000 << "s";
if (idle_milliseconds >= mAppConfig.idle_timeout * 1000) if (idle_milliseconds >= mAppConfig.idle_timeout * 1000)
{ {
shiftTo(AppState::Idle); shiftTo(AppState::Idle);
@ -408,7 +401,7 @@ void MainWindow::onLongBreakStart()
{ {
mBreakStartTimer->stop(); mBreakStartTimer->stop();
mBreakNotifyTimer->stop(); mBreakNotifyTimer->stop();
qDebug() << get_log_prefix() << "Stop main and notify timers."; qDebug() << "Stop main and notify timers.";
// Reset idle counter // Reset idle counter
mIdleStart.reset(); mIdleStart.reset();
@ -445,12 +438,12 @@ void MainWindow::onLongBreakEnd()
if (!mBreakStartTimer->isActive()) if (!mBreakStartTimer->isActive())
{ {
mBreakStartTimer->start(std::chrono::seconds(mAppConfig.longbreak_interval)); mBreakStartTimer->start(std::chrono::seconds(mAppConfig.longbreak_interval));
qDebug() << get_log_prefix() << "Start main timer for " << mAppConfig.longbreak_interval << " seconds."; qDebug() << "Start main timer for " << mAppConfig.longbreak_interval << " seconds.";
} }
if (!mBreakNotifyTimer->isActive()) if (!mBreakNotifyTimer->isActive())
{ {
mBreakNotifyTimer->start(std::chrono::seconds(mAppConfig.longbreak_interval - 30)); mBreakNotifyTimer->start(std::chrono::seconds(mAppConfig.longbreak_interval - 30));
qDebug() << get_log_prefix() << "Start notify timer for " << mAppConfig.longbreak_interval - 30 << " seconds."; qDebug() << "Start notify timer for " << mAppConfig.longbreak_interval - 30 << " seconds.";
} }
// Play selected audio. When break is postponed - audio is not played // Play selected audio. When break is postponed - audio is not played
@ -462,7 +455,7 @@ void MainWindow::onLongBreakEnd()
{ {
int retcode = system(mAppConfig.script_on_break_finish.toStdString().c_str()); int retcode = system(mAppConfig.script_on_break_finish.toStdString().c_str());
if (retcode != 0) if (retcode != 0)
qDebug() << get_log_prefix() << "User script exited with error code " << retcode; qDebug() << "User script exited with error code " << retcode;
} }
} }
@ -531,7 +524,7 @@ void MainWindow::onIdleStart()
// Stop main & notify timers // Stop main & notify timers
mBreakStartTimer->stop(); mBreakStartTimer->stop();
mBreakNotifyTimer->stop(); mBreakNotifyTimer->stop();
qDebug() << get_log_prefix() << "Stop main and notify timers. Remaining time is " << mRemainingWorkInterval.value() << "s"; qDebug() << "Stop main and notify timers. Remaining time is " << mRemainingWorkInterval.value() << "s";
} }
void MainWindow::onIdleEnd() void MainWindow::onIdleEnd()
@ -544,13 +537,13 @@ void MainWindow::onIdleEnd()
// Update timer(s) duration // Update timer(s) duration
if (mRemainingWorkInterval) if (mRemainingWorkInterval)
{ {
qDebug() << get_log_prefix() << "Reset main timer to " << *mRemainingWorkInterval << "s"; qDebug() << "Reset main timer to " << *mRemainingWorkInterval << "s";
mBreakStartTimer->setInterval(std::chrono::seconds(*mRemainingWorkInterval)); mBreakStartTimer->setInterval(std::chrono::seconds(*mRemainingWorkInterval));
if (mRemainingWorkInterval > INTERVAL_NOTIFICATION) if (mRemainingWorkInterval > INTERVAL_NOTIFICATION)
{ {
mBreakNotifyTimer->setInterval(std::chrono::seconds(*mRemainingWorkInterval - INTERVAL_NOTIFICATION)); mBreakNotifyTimer->setInterval(std::chrono::seconds(*mRemainingWorkInterval - INTERVAL_NOTIFICATION));
qDebug() << get_log_prefix() << "Reset notify timer to " << *mRemainingWorkInterval - INTERVAL_NOTIFICATION << "s"; qDebug() << "Reset notify timer to " << *mRemainingWorkInterval - INTERVAL_NOTIFICATION << "s";
} }
mRemainingWorkInterval.reset(); mRemainingWorkInterval.reset();
} }

View File

@ -55,6 +55,7 @@ unix:LIBS += -L/usr/X11R6/lib/ \
-lX11 -lXext -lXss -ldl -lX11 -lXext -lXss -ldl
Debug: DEFINES += DEBUG Debug: DEFINES += DEBUG
Release: DEFINES += QT_NO_DEBUG_OUTPUT
# When using wayland: # When using wayland:
# unix:LIBS += -L/usr/local/lib \ # unix:LIBS += -L/usr/local/lib \

View File

@ -71,7 +71,7 @@ public:
selected_audio play_audio; selected_audio play_audio;
QString script_on_break_finish; QString script_on_break_finish;
// Zero means "idle is not tracked" // Zero means "idle is not tracked". Value in seconds.
int idle_timeout = Default_Idle_Timeout; int idle_timeout = Default_Idle_Timeout;
}; };

View File

@ -70,7 +70,7 @@ void SettingsDialog::init()
connect(ui->mAudioComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onAudioIndexChanged(int))); connect(ui->mAudioComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onAudioIndexChanged(int)));
// Idle timeout // Idle timeout
ui->mIdleTimeoutSpinbox->setValue(c.idle_timeout); ui->mIdleTimeoutSpinbox->setValue(c.idle_timeout / 60);
mSkipAudioChangeEvent = false; mSkipAudioChangeEvent = false;
} }
@ -90,7 +90,7 @@ void SettingsDialog::accept()
if (c.play_audio.name == Audio_Custom) if (c.play_audio.name == Audio_Custom)
c.play_audio.path = mCustomAudioPath; c.play_audio.path = mCustomAudioPath;
c.idle_timeout = ui->mIdleTimeoutSpinbox->value(); c.idle_timeout = ui->mIdleTimeoutSpinbox->value() * 60;
app_settings::save(c); app_settings::save(c);
emit accepted(); emit accepted();