diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,6 +26,7 @@ plasmaeffect.cpp breakcontrol.cpp rsiidletime.cpp +notificator.cpp ) QT5_ADD_DBUS_ADAPTOR( rsibreak_sources diff --git a/src/notificator.h b/src/notificator.h new file mode 100644 --- /dev/null +++ b/src/notificator.h @@ -0,0 +1,42 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef RSIBREAK_NOTIFICATOR_H +#define RSIBREAK_NOTIFICATOR_H + +#include +#include + +class Notificator : public QObject +{ + Q_OBJECT + +public slots: + + void onShortTimerReset(); + + void onTimersReset(); + + void onStartLongBreak(); + + void onEndLongBreak(); + + void onStartShortBreak(); + + void onEndShortBreak(); +}; + +#endif //RSIBREAK_NOTIFICATOR_H diff --git a/src/notificator.cpp b/src/notificator.cpp new file mode 100644 --- /dev/null +++ b/src/notificator.cpp @@ -0,0 +1,56 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "notificator.h" + +#include +#include +#include + +void Notificator::onShortTimerReset() +{ + KNotification::event( "short timer reset", + i18n( "Timer for the short break has now been reset" ), + KIconLoader::global()->loadIcon( "rsibreak0", KIconLoader::Desktop ) ); + +} + +void Notificator::onTimersReset() +{ + KNotification::event( "timers reset", + i18n( "The timers have now been reset" ), + KIconLoader::global()->loadIcon( "rsibreak0", KIconLoader::Desktop ) ); +} + +void Notificator::onStartLongBreak() +{ + KNotification::event( "start long break", i18n( "Start of a long break" ) ); +} + +void Notificator::onEndLongBreak() +{ + KNotification::event( "end long break", i18n( "End of a long break" ) ); +} + +void Notificator::onStartShortBreak() +{ + KNotification::event( "start short break", i18n( "Start of a short break" ) ); +} + +void Notificator::onEndShortBreak() +{ + KNotification::event( "end short break", i18n( "End of a short break" ) ); +} diff --git a/src/rsiglobals.cpp b/src/rsiglobals.cpp --- a/src/rsiglobals.cpp +++ b/src/rsiglobals.cpp @@ -110,17 +110,3 @@ { m_usageArray.fill( false, 60 * 60 * 24 ); } - -void RSIGlobals::NotifyBreak( bool start, bool big ) -{ - if ( start ) - big ? KNotification::event( "start long break", - i18n( "Start of a long break" ) ) - : KNotification::event( "start short break", - i18n( "Start of a short break" ) ); - else - big ? KNotification::event( "end long break", - i18n( "End of a long break" ) ) - : KNotification::event( "end short break", - i18n( "End of a short break" ) ); -} diff --git a/src/rsitimer.h b/src/rsitimer.h --- a/src/rsitimer.h +++ b/src/rsitimer.h @@ -23,9 +23,9 @@ #define RSITimer_H #include +#include #include -#include "rsiglobals.h" #include "rsitimercounter.h" #include "rsiidletime.h" @@ -56,6 +56,7 @@ int bigLeft() const { return m_bigBreakCounter->counterLeft(); }; public slots: + /** Reads the configuration and restarts the timer with slotRestart. */ @@ -162,6 +163,11 @@ */ void bigBreakSkipped(); + void startLongBreak(); + void endLongBreak(); + void startShortBreak(); + void endShortBreak(); + private: std::unique_ptr m_idleTimeInstance; diff --git a/src/rsitimer.cpp b/src/rsitimer.cpp --- a/src/rsitimer.cpp +++ b/src/rsitimer.cpp @@ -30,6 +30,7 @@ #include #include +#include "rsiglobals.h" #include "rsistats.h" RSITimer::RSITimer( QObject *parent ) : QThread( parent ) @@ -106,7 +107,11 @@ m_state = TimerState::Resting; m_pauseCounter = std::unique_ptr { new RSITimerCounter( breakTime, breakTime, INT_MAX ) }; m_popupCounter = nullptr; - RSIGlobals::instance()->NotifyBreak( true, nextBreakIsBig ); + if ( nextBreakIsBig ) { + emit startLongBreak(); + } else { + emit startShortBreak(); + } emit updateWidget( breakTime ); emit breakNow(); } @@ -120,7 +125,11 @@ emit updateIdleAvg( 0.0 ); emit relax( -1, false ); emit minimize(); - RSIGlobals::instance()->NotifyBreak( false, m_bigBreakCounter->isReset() ); + if ( m_bigBreakCounter->isReset() ) { + emit endLongBreak(); + } else { + emit endShortBreak(); + } } // -------------------------- SLOTS ------------------------// diff --git a/src/rsiwidget.h b/src/rsiwidget.h --- a/src/rsiwidget.h +++ b/src/rsiwidget.h @@ -22,13 +22,12 @@ #define RSIWIDGET_H #include "rsitimer.h" +#include "notificator.h" class RSIDock; class RSIRelaxPopup; class BreakBase; -class QLabel; - /** * @class RSIObject * This controls all RSIBreak components @@ -95,6 +94,7 @@ QString m_currentIcon; + Notificator m_notificator; /* Available through D-Bus */ public Q_SLOTS: diff --git a/src/rsiwidget.cpp b/src/rsiwidget.cpp --- a/src/rsiwidget.cpp +++ b/src/rsiwidget.cpp @@ -160,16 +160,12 @@ void RSIObject::tinyBreakSkipped() { - KNotification::event( "short timer reset", - i18n( "Timer for the short break has now been reset" ), - KIconLoader::global()->loadIcon( "rsibreak0", KIconLoader::Desktop ) ); + m_notificator.onShortTimerReset(); } void RSIObject::bigBreakSkipped() { - KNotification::event( "timers reset", - i18n( "The timers have now been reset" ), - KIconLoader::global()->loadIcon( "rsibreak0", KIconLoader::Desktop ) ); + m_notificator.onTimersReset(); } //--------------------------- CONFIG ----------------------------// @@ -190,6 +186,10 @@ connect(m_timer, &RSITimer::relax, m_relaxpopup, &RSIRelaxPopup::relax, Qt::QueuedConnection ); connect(m_timer, &RSITimer::tinyBreakSkipped, this, &RSIObject::tinyBreakSkipped, Qt::QueuedConnection ); connect(m_timer, &RSITimer::bigBreakSkipped, this, &RSIObject::bigBreakSkipped, Qt::QueuedConnection ); + connect(m_timer, &RSITimer::startLongBreak, &m_notificator, &Notificator::onStartLongBreak ); + connect(m_timer, &RSITimer::endLongBreak, &m_notificator, &Notificator::onEndLongBreak ); + connect(m_timer, &RSITimer::startShortBreak, &m_notificator, &Notificator::onStartShortBreak ); + connect(m_timer, &RSITimer::endShortBreak, &m_notificator, &Notificator::onEndShortBreak ); connect(m_tray, &RSIDock::configChanged, m_timer, &RSITimer::updateConfig); connect(m_tray, &RSIDock::dialogEntered, m_timer, &RSITimer::slotStop); diff --git a/test/rsitimer_test.cpp b/test/rsitimer_test.cpp --- a/test/rsitimer_test.cpp +++ b/test/rsitimer_test.cpp @@ -16,6 +16,7 @@ #include "rsitimer_test.h" +#include "rsiglobals.h" #include "rsitimer.h" static constexpr int RELAX_ENDED_MAGIC_VALUE = -1; @@ -39,6 +40,8 @@ RSIIdleTimeFake* idle_time_ptr = idle_time.get(); RSITimer timer( std::move( idle_time ), m_intervals, true, true ); + QSignalSpy spyEndShortBreak( &timer, SIGNAL( endShortBreak( void ) ) ); + // Part one, no idleness till small break. QSignalSpy spy1Relax( &timer, SIGNAL(relax(int,bool)) ); QSignalSpy spy1UpdateIdleAvg( &timer, SIGNAL(updateIdleAvg(double)) ); @@ -85,6 +88,7 @@ } QList spy2RelaxSignals = spy2Relax.takeFirst(); // The last one is special. QCOMPARE( spy2RelaxSignals.at( 0 ).toInt(), RELAX_ENDED_MAGIC_VALUE ); + QCOMPARE( spyEndShortBreak.count(), 1 ); } void RSITimerTest::triggerComplexTinyBreak() @@ -166,6 +170,8 @@ // We don't tick big pause timer during tiny breaks and patience, so it will actually happen later. int ticks = m_intervals[BIG_BREAK_INTERVAL] + tinyBreaks * ( m_intervals[PATIENCE_INTERVAL] + m_intervals[TINY_BREAK_DURATION] ); + QSignalSpy spyEndLongBreak( &timer, SIGNAL( endLongBreak( void ) ) ); + // Part one, no idleness till big break. QSignalSpy spy1Relax( &timer, SIGNAL(relax(int,bool)) ); QSignalSpy spy1UpdateIdleAvg( &timer, SIGNAL(updateIdleAvg(double)) ); @@ -189,6 +195,7 @@ } QCOMPARE( timer.m_state, RSITimer::TimerState::Monitoring ); QCOMPARE( spy2Relax.count(), m_intervals[BIG_BREAK_DURATION] ); + QCOMPARE( spyEndLongBreak.count(), 1 ); } void RSITimerTest::postponeBreak() @@ -279,6 +286,9 @@ RSIIdleTimeFake* idle_time_ptr = idle_time.get(); RSITimer timer( std::move( idle_time ), m_intervals, false, true ); + QSignalSpy spyStartShortBreak( &timer, SIGNAL( startShortBreak( void ) ) ); + QSignalSpy spyEndShortBreak( &timer, SIGNAL( endShortBreak( void ) ) ); + // Part one, no idleness till small break. QSignalSpy spy1BreakNow( &timer, SIGNAL(breakNow()) ); QSignalSpy spy1UpdateWidget( &timer, SIGNAL(updateWidget(int)) ); @@ -290,6 +300,7 @@ } // Popup is disabled so straight to breaking. + QCOMPARE( spyStartShortBreak.count(), 1 ); QCOMPARE( timer.m_state, RSITimer::TimerState::Resting ); QCOMPARE( spy1BreakNow.count(), 1 ); @@ -312,14 +323,18 @@ QList spy2UpdateWidgetSignals = spy2UpdateWidget.takeFirst(); QCOMPARE( spy2UpdateWidgetSignals.at( 0 ).toInt(), m_intervals[TINY_BREAK_DURATION] - i ); } + QCOMPARE( spyEndShortBreak.count(), 1 ); } void RSITimerTest::regularBreaks() { std::unique_ptr idle_time( new RSIIdleTimeFake() ); RSIIdleTimeFake* idle_time_ptr = idle_time.get(); RSITimer timer( std::move( idle_time ), m_intervals, true, false ); + QSignalSpy spyEndShortBreak( &timer, SIGNAL( endShortBreak( void ) ) ); + QSignalSpy spyEndLongBreak( &timer, SIGNAL( endLongBreak( void ) ) ); + int tinyBreaks = m_intervals[BIG_BREAK_INTERVAL] / ( m_intervals[TINY_BREAK_INTERVAL] + m_intervals[PATIENCE_INTERVAL] + m_intervals[TINY_BREAK_DURATION] ); int tick = 0; @@ -365,6 +380,9 @@ timer.timeout(); } QCOMPARE( timer.m_state, RSITimer::TimerState::Monitoring ); + + QCOMPARE( spyEndShortBreak.count(), tinyBreaks ); + QCOMPARE( spyEndLongBreak.count(), 1 ); } #include "rsitimer_test.moc"