From 6af94a3a6f8885f55ba88143eafaa23eda873eee Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Sun, 27 Nov 2022 19:37:46 +0300 Subject: [PATCH] - fixes in idle handling + notifying is fixed + version increased to 0.1.11 --- app/config.h | 2 +- app/main.cpp | 22 +---- app/mainwindow.cpp | 208 ++++++++++++++++++++++++++------------------- app/mainwindow.h | 1 - 4 files changed, 124 insertions(+), 109 deletions(-) diff --git a/app/config.h b/app/config.h index e617767..dfd3eb7 100644 --- a/app/config.h +++ b/app/config.h @@ -4,7 +4,7 @@ // App version #define QBREAK_VERSION_MAJOR 0 #define QBREAK_VERSION_MINOR 1 -#define QBREAK_VERSION_SUFFIX 10 +#define QBREAK_VERSION_SUFFIX 11 // How often UI is updated - interval in seconds #define INTERVAL_UPDATE_UI (60) diff --git a/app/main.cpp b/app/main.cpp index 494354f..a992b80 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -15,8 +15,8 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); - QCoreApplication::setOrganizationName("qbreak.com"); - QCoreApplication::setOrganizationDomain("qbreak.com"); + QCoreApplication::setOrganizationName("voipobjects.com"); + QCoreApplication::setOrganizationDomain("voipobjects.com"); QCoreApplication::setApplicationName("QBreak"); QTranslator translator; @@ -27,19 +27,9 @@ int main(int argc, char *argv[]) app.setQuitOnLastWindowClosed(false); - QCommandLineParser parser; - QCommandLineOption test_1(QStringList() << "t1" << "test_1"); - parser.addOption(test_1); - - // Run app with break intervals & duration 60 seconds. - // This should trigger the notification in 30 seconds before breaks. - QCommandLineOption test_2(QStringList() << "t2" << "test_2"); - parser.addOption(test_2); - - parser.process(app); // Put itself into app menu - auto exe_path = QFileInfo(QCoreApplication::arguments().front()).absoluteFilePath(); + auto exe_path = QCoreApplication::applicationFilePath();//QFileInfo(QCoreApplication::arguments().front()).absoluteFilePath(); const char* appimage = std::getenv("APPIMAGE"); if (appimage != nullptr) exe_path = appimage; @@ -50,12 +40,6 @@ int main(int argc, char *argv[]) MainWindow w; w.hide(); - if (parser.isSet(test_1)) - w.test_1(); - else - if (parser.isSet(test_2)) - w.test_2(); - int retcode = app.exec(); return retcode; diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index 67daf92..e49dbec 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -19,6 +19,11 @@ #include #include #include +#include +#include +#include + +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -117,10 +122,53 @@ void MainWindow::init() 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) +{ + if (s.isEmpty()) + throw std::runtime_error("Bad parameter value."); + + if (s.back() == 'h') + return s.leftRef(s.size()-1).toInt() * 3600; + if (s.back() == 'm') + return s.leftRef(s.size()-1).toInt() * 60; + if (s.back() == 's') + return s.leftRef(s.size()-1).toInt(); + + if (s.back().isDigit()) + return s.toInt(); + + throw std::runtime_error("Bad parameter value"); +} + void MainWindow::loadConfig() { - app_settings settings; - mAppConfig = settings.load(); + mAppConfig = app_settings::load(); + + // Check for command line options + QCommandLineParser parser; + QCommandLineOption + param_break("break-duration", "Long break duration.", "break-duration"), + param_work("work-duration", "Work time duration.", "work-duration"), + param_idle("idle-timeout", "Idle timeout.", "idle-timeout"); + parser.addOptions({param_break, param_work, param_idle}); + + parser.process(*QApplication::instance()); + if (parser.isSet(param_break)) + mAppConfig.longbreak_length = str_to_seconds(parser.value(param_break)); + if (parser.isSet(param_work)) + mAppConfig.longbreak_interval = str_to_seconds(parser.value(param_work)); + if (parser.isSet(param_idle)) + mAppConfig.idle_timeout = str_to_seconds(parser.value(param_idle)); } void MainWindow::applyConfig() @@ -153,39 +201,6 @@ void MainWindow::applyConfig() autostart::disable(); } -void MainWindow::schedule() -{ - ; -} - -void MainWindow::test_1() -{ - // 10 seconds test break - mAppConfig.longbreak_length = 10; - mAppConfig.longbreak_postpone_interval = 10; - mAppConfig.longbreak_interval = 10; - - mAppConfig.window_on_top = true; - mAppConfig.verbose = true; - applyConfig(); - onUpdateUI(); - shiftTo(AppState::Break); -} - -void MainWindow::test_2() -{ - // 60 seconds test break - mAppConfig.longbreak_length = 60; - mAppConfig.longbreak_postpone_interval = 60; - mAppConfig.longbreak_interval = 60; - - mAppConfig.window_on_top = true; - mAppConfig.verbose = true; - - applyConfig(); - onUpdateUI(); -} - void MainWindow::showMe() { QScreen* screen = nullptr; @@ -209,8 +224,12 @@ void MainWindow::showMe() // qDebug() << "Window moved to screen " << screen->name() + " / " + screen->model() + " " + screen->manufacturer(); } } + // else + // qDebug() << get_log_prefix() << "Screen not found!"; -#if !defined(DEBUG) +#if defined(DEBUG) + showMaximized(); +#else showFullScreen(); #endif } @@ -288,7 +307,7 @@ void MainWindow::shiftTo(AppState newState) if (newState == mState) return; #if defined(DEBUG) - qDebug() << state2str(mState) << " -> " << state2str(newState); + qDebug() << get_log_prefix() << state2str(mState) << " -> " << state2str(newState); #endif switch (newState) @@ -321,55 +340,60 @@ void MainWindow::shiftTo(AppState newState) } void MainWindow::onUpdateUI() { - int idle_milliseconds = 0; - switch (mState) { - case AppState::None: - // Do nothing, app is not started - break; + int idle_milliseconds = 0; + switch (mState) { + case AppState::None: + // Do nothing, app is not started + break; - case AppState::Idle: - // Detected idle, don't count this time as working - // But check - maybe idle is over - idle_milliseconds = get_idle_time_dynamically(); - if (idle_milliseconds < mAppConfig.idle_timeout * 60 * 1000) { - shiftTo(AppState::Counting); - return; - } - break; + case AppState::Idle: + // Detected idle, don't count this time as working + // But check - maybe idle is over + idle_milliseconds = get_idle_time_dynamically(); + qDebug() << get_log_prefix() << "Idle found: " << idle_milliseconds / 1000 << "s"; - case AppState::Break: - // Break is active - if (mTrayIcon) - mTrayIcon->setToolTip(QString()); - break; + if (idle_milliseconds < mAppConfig.idle_timeout * 1000) + { + shiftTo(AppState::Counting); + return; + } + break; - case AppState::Counting: - // Working, break is closing - // Check maybe it is idle ? - if (!mIdleStart && mAppConfig.idle_timeout) { - idle_milliseconds = get_idle_time_dynamically(); - if (idle_milliseconds >= mAppConfig.idle_timeout * 60 * 1000) { - shiftTo(AppState::Idle); - return; - } + case AppState::Break: + // Break is active + if (mTrayIcon) + mTrayIcon->setToolTip(QString()); + break; + + case AppState::Counting: + // Working, break is closing + // Check maybe it is idle ? + if (!mIdleStart && mAppConfig.idle_timeout) + { + idle_milliseconds = get_idle_time_dynamically(); + if (idle_milliseconds >= mAppConfig.idle_timeout * 1000) + { + shiftTo(AppState::Idle); + return; + } + } + + // Update tray icon + if (mTrayIcon) { + auto remaining_milliseconds = mBreakStartTimer->remainingTime(); + if (remaining_milliseconds < 60000) + mTrayIcon->setToolTip( + tr("Less than a minute left until the next break.")); + else + mTrayIcon->setToolTip( + tr("There are %1 minutes left until the next break.") + .arg(msec2min(remaining_milliseconds))); + } + + break; } - // Update tray icon - if (mTrayIcon) { - auto remaining_milliseconds = mBreakStartTimer->remainingTime(); - if (remaining_milliseconds < 60000) - mTrayIcon->setToolTip( - tr("Less than a minute left until the next break.")); - else - mTrayIcon->setToolTip( - tr("There are %1 minutes left until the next break.") - .arg(msec2min(remaining_milliseconds))); - } - - break; - } - - ui->mSkipButton->setVisible(mPostponeCount > 0); + ui->mSkipButton->setVisible(mPostponeCount > 0); } void MainWindow::onLongBreakNotify() @@ -377,14 +401,14 @@ void MainWindow::onLongBreakNotify() mTrayIcon->showMessage(tr("New break"), tr("New break will start in %1 secs").arg(Default_Notify_Length), getAppIcon(), - INTERVAL_NOTIFICATION); + INTERVAL_NOTIFICATION * 1000); } void MainWindow::onLongBreakStart() { mBreakStartTimer->stop(); mBreakNotifyTimer->stop(); - qDebug() << "Stop main and notify timers."; + qDebug() << get_log_prefix() << "Stop main and notify timers."; // Reset idle counter mIdleStart.reset(); @@ -421,12 +445,12 @@ void MainWindow::onLongBreakEnd() if (!mBreakStartTimer->isActive()) { mBreakStartTimer->start(std::chrono::seconds(mAppConfig.longbreak_interval)); - qDebug() << "Start main timer for " << mAppConfig.longbreak_interval << " seconds."; + qDebug() << get_log_prefix() << "Start main timer for " << mAppConfig.longbreak_interval << " seconds."; } if (!mBreakNotifyTimer->isActive()) { mBreakNotifyTimer->start(std::chrono::seconds(mAppConfig.longbreak_interval - 30)); - qDebug() << "Start notify timer for " << mAppConfig.longbreak_interval - 30 << " seconds."; + qDebug() << get_log_prefix() << "Start notify timer for " << mAppConfig.longbreak_interval - 30 << " seconds."; } // Play selected audio. When break is postponed - audio is not played @@ -438,7 +462,7 @@ void MainWindow::onLongBreakEnd() { int retcode = system(mAppConfig.script_on_break_finish.toStdString().c_str()); if (retcode != 0) - qDebug() << "User script exited with error code " << retcode; + qDebug() << get_log_prefix() << "User script exited with error code " << retcode; } } @@ -472,7 +496,10 @@ void MainWindow::onProgress() if (remaining < 0) remaining = 0; - ui->mRemainingLabel->setText(QString("Remaining: ") + secondsToText(remaining)); + auto text = QString("Remaining: ") + secondsToText(remaining); + ui->mRemainingLabel->setText(text); + if (mTrayIcon) + mTrayIcon->setToolTip(text); if (percents > 100) { @@ -504,7 +531,7 @@ void MainWindow::onIdleStart() // Stop main & notify timers mBreakStartTimer->stop(); mBreakNotifyTimer->stop(); - qDebug() << "Stop main and notify timers."; + qDebug() << get_log_prefix() << "Stop main and notify timers. Remaining time is " << mRemainingWorkInterval.value() << "s"; } void MainWindow::onIdleEnd() @@ -517,9 +544,14 @@ void MainWindow::onIdleEnd() // Update timer(s) duration if (mRemainingWorkInterval) { + qDebug() << get_log_prefix() << "Reset main timer to " << *mRemainingWorkInterval << "s"; mBreakStartTimer->setInterval(std::chrono::seconds(*mRemainingWorkInterval)); + if (mRemainingWorkInterval > INTERVAL_NOTIFICATION) + { mBreakNotifyTimer->setInterval(std::chrono::seconds(*mRemainingWorkInterval - INTERVAL_NOTIFICATION)); + qDebug() << get_log_prefix() << "Reset notify timer to " << *mRemainingWorkInterval - INTERVAL_NOTIFICATION << "s"; + } mRemainingWorkInterval.reset(); } else diff --git a/app/mainwindow.h b/app/mainwindow.h index 9c81b84..7c31d6c 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -65,7 +65,6 @@ private: void init(); void loadConfig(); void applyConfig(); - void schedule(); void createTrayIcon(); void showMe(); void hideMe();