From 71b54169e4dd7e5b063832ee06efa1fc336fd267 Mon Sep 17 00:00:00 2001 From: Frank Osterfeld Date: Tue, 19 Apr 2016 10:13:43 +0200 Subject: [PATCH] Fix idle detection logic Do not assume that there is only one idle event at a time, the OS X idle detector can hold multiple events before the dialog becomes visible (as it can receive multiple wake-up events in a row). The old logic didn't show the dialog in that case at all. Fixes #220 Change-Id: I36b016634023ebe3132debd6ca78992a62fb0b64 Reviewed-on: https://codereview.kdab.com/32376 Tested-by: Continuous Integration Reviewed-by: Hannah von Reth --- Charm/ApplicationCore.cpp | 5 +-- Charm/{Uniquifier.h => TemporaryValue.h} | 41 +++++++++++------------- Charm/Widgets/TimeTrackingWindow.cpp | 15 +++------ Charm/Widgets/TimeTrackingWindow.h | 1 + 4 files changed, 25 insertions(+), 37 deletions(-) rename Charm/{Uniquifier.h => TemporaryValue.h} (63%) diff --git a/Charm/ApplicationCore.cpp b/Charm/ApplicationCore.cpp index f16f9174..79162579 100644 --- a/Charm/ApplicationCore.cpp +++ b/Charm/ApplicationCore.cpp @@ -26,7 +26,6 @@ #include "CharmCMake.h" #include "Data.h" #include "ViewHelpers.h" -#include "Uniquifier.h" #include "Core/CharmConstants.h" #include "Core/CharmExceptions.h" @@ -803,9 +802,7 @@ void ApplicationCore::setHttpActionsVisible( bool visible ) void ApplicationCore::slotMaybeIdle() { if ( DATAMODEL->activeEventCount() > 0 ) { - if ( idleDetector()->idlePeriods().count() == 1 ) { - m_timeTracker.maybeIdle( idleDetector() ); - } // otherwise, the dialog will be showing already + m_timeTracker.maybeIdle( idleDetector() ); } // there are four parameters to the idle property: // - the initial start time of the currently active event(s) diff --git a/Charm/Uniquifier.h b/Charm/TemporaryValue.h similarity index 63% rename from Charm/Uniquifier.h rename to Charm/TemporaryValue.h index aadfa71d..90654c2c 100644 --- a/Charm/Uniquifier.h +++ b/Charm/TemporaryValue.h @@ -1,11 +1,9 @@ /* - Uniquifier.h - This file is part of Charm, a task-based time tracking application. Copyright (C) 2009-2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com - Author: Mirko Boehm + Author: Frank Osterfeld 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 @@ -21,28 +19,25 @@ along with this program. If not, see . */ -#ifndef UNIQUIFIER_H -#define UNIQUIFIER_H - -// Usage: -// void blurb() { -// static bool inProgress = false; -// if ( inProgress == true ) return; -// Uniquifier u( &inProgress ); -// // ... this code will be called only once -// } - -class Uniquifier { -public: - explicit Uniquifier( bool* guard ) { - m_guard = guard; - *m_guard = true; +#ifndef TEMPORARYVALUE_H +#define TEMPORARYVALUE_H + +template +struct TemporaryValue { + explicit TemporaryValue(T& x, const T& value) + : m_oldValue(x) + , m_x(x) + { + m_x = value; } - ~Uniquifier() { - *m_guard = false; + + ~TemporaryValue() { + m_x = m_oldValue; } -private: - bool *m_guard; + + T& m_x; + const T m_oldValue; }; #endif + diff --git a/Charm/Widgets/TimeTrackingWindow.cpp b/Charm/Widgets/TimeTrackingWindow.cpp index f2496b36..90dec0c7 100644 --- a/Charm/Widgets/TimeTrackingWindow.cpp +++ b/Charm/Widgets/TimeTrackingWindow.cpp @@ -36,8 +36,8 @@ #include "MessageBox.h" #include "MonthlyTimesheet.h" #include "MonthlyTimesheetConfigurationDialog.h" +#include "TemporaryValue.h" #include "TimeTrackingView.h" -#include "Uniquifier.h" #include "ViewHelpers.h" #include "WeeklyTimesheet.h" @@ -621,16 +621,12 @@ void TimeTrackingWindow::informUserAboutNewRelease( const QString& releaseVersio void TimeTrackingWindow::maybeIdle( IdleDetector* detector ) { Q_ASSERT( detector ); - static bool inProgress = false; + Q_ASSERT( !detector->idlePeriods().isEmpty() ); - if ( inProgress == true ) return; - Uniquifier u( &inProgress ); + if ( m_idleCorrectionDialogVisible ) + return; - Q_FOREACH( const IdleDetector::IdlePeriod& p, detector->idlePeriods() ) { - qDebug() << "ApplicationCore::slotMaybeIdle: computer might be have been idle from" - << p.first - << "to" << p.second; - } + const TemporaryValue tempValue( m_idleCorrectionDialogVisible, true ); // handle idle merging: IdleCorrectionDialog dialog( this ); @@ -646,7 +642,6 @@ void TimeTrackingWindow::maybeIdle( IdleDetector* detector ) // FIXME with this option, we can only change the events to // the start time of one idle period, I chose to use the last // one: - Q_ASSERT( !detector->idlePeriods().isEmpty() ); const IdleDetector::IdlePeriod period = detector->idlePeriods().last(); Q_FOREACH ( EventId eventId, activeEvents ) { diff --git a/Charm/Widgets/TimeTrackingWindow.h b/Charm/Widgets/TimeTrackingWindow.h index d73aa016..d1008ecf 100644 --- a/Charm/Widgets/TimeTrackingWindow.h +++ b/Charm/Widgets/TimeTrackingWindow.h @@ -142,6 +142,7 @@ private slots: QTimer m_checkCharmReleaseVersionTimer; QTimer m_updateUserInfoAndTasksDefinitionsTimer; BillDialog *m_billDialog; + bool m_idleCorrectionDialogVisible = false; }; #endif