File indexing completed on 2024-05-19 05:21:53
0001 /* 0002 * Copyright (C) 2003 by Scott Monachello <smonach@cox.net> 0003 * Copyright (C) 2019 Alexander Potashev <aspotashev@gmail.com> 0004 * 0005 * This program is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation; either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License along 0016 * with this program; if not, write to the 0017 * Free Software Foundation, Inc. 0018 * 51 Franklin Street, Fifth Floor 0019 * Boston, MA 02110-1301 USA. 0020 * 0021 */ 0022 0023 #include "idletimedetector.h" 0024 0025 0026 #include <KIdleTime> 0027 #include <KLocalizedString> 0028 #include <KMessageBox> 0029 0030 #include "ktimetrackerutility.h" // SecsPerMinute 0031 #include "ktt_debug.h" 0032 0033 IdleTimeDetector::IdleTimeDetector(int maxIdle) 0034 : m_overAllIdleDetect(false) 0035 , m_maxIdle(maxIdle) 0036 , m_timeoutId(0) 0037 { 0038 connect(KIdleTime::instance(), 0039 QOverload<int, int>::of(&KIdleTime::timeoutReached), 0040 this, 0041 &IdleTimeDetector::timeoutReached); 0042 } 0043 0044 // static 0045 bool IdleTimeDetector::isIdleDetectionPossible() 0046 { 0047 int id = KIdleTime::instance()->addIdleTimeout(5000); 0048 if (id) { 0049 KIdleTime::instance()->removeIdleTimeout(id); 0050 return true; 0051 } else { 0052 return false; 0053 } 0054 } 0055 0056 void IdleTimeDetector::timeoutReached(int /*unused*/, int timeout) 0057 { 0058 qCDebug(KTT_LOG) << "The desktop has been idle for " << timeout << " msec."; 0059 0060 if (!m_overAllIdleDetect) { 0061 // In the preferences the user has indicated that he does not want idle 0062 // detection. 0063 return; 0064 } 0065 0066 stopIdleDetection(); 0067 0068 // when the idletimedetectordialog started 0069 QDateTime dialogStart = QDateTime::currentDateTime(); 0070 0071 // when the idleness started 0072 QDateTime idleStart = dialogStart.addSecs(-secsPerMinute * m_maxIdle); 0073 QString backThen = idleStart.time().toString(); 0074 0075 // Create dialog 0076 QString hintYes = i18nc("@info:tooltip", 0077 "Apply the idle time since %1 to all " 0078 "active\ntimers and keep them running.", 0079 backThen); 0080 KGuiItem buttonYes(i18nc("@action:button", "Continue Timing"), QString(), hintYes, hintYes); 0081 0082 QString hintNo = i18nc("@info:tooltip", "Stop timing and revert back to the time at %1", backThen); 0083 KGuiItem buttonNo(i18nc("@action:button", "Revert Timing"), QString(), hintNo, hintNo); 0084 0085 const auto result = 0086 KMessageBox::questionTwoActions(nullptr, 0087 i18n("Desktop has been idle since %1. What do you want to do?", backThen), 0088 QString(), 0089 buttonYes, 0090 buttonNo); 0091 switch (result) { 0092 case KMessageBox::ButtonCode::PrimaryAction: 0093 startIdleDetection(); 0094 break; 0095 default: 0096 qCWarning(KTT_LOG) << "unexpected button clicked" << result; 0097 Q_FALLTHROUGH(); 0098 case KMessageBox::ButtonCode::SecondaryAction: 0099 revert(dialogStart, idleStart, timeout / 1000 / secsPerMinute); 0100 break; 0101 } 0102 } 0103 0104 void IdleTimeDetector::setMaxIdle(int maxIdle) 0105 { 0106 m_maxIdle = maxIdle; 0107 } 0108 0109 void IdleTimeDetector::revert(const QDateTime &dialogStart, const QDateTime &idleStart, int idleMinutes) 0110 { 0111 // revert and stop 0112 QDateTime end = QDateTime::currentDateTime(); 0113 const int64_t diff = dialogStart.secsTo(end) / secsPerMinute; 0114 Q_EMIT subtractTime(idleMinutes + diff); // subtract the time that has been added on the display 0115 Q_EMIT stopAllTimers(idleStart); 0116 } 0117 0118 void IdleTimeDetector::startIdleDetection() 0119 { 0120 if (!m_timeoutId) { 0121 m_timeoutId = KIdleTime::instance()->addIdleTimeout(m_maxIdle * secsPerMinute * 1000); 0122 } 0123 } 0124 0125 void IdleTimeDetector::stopIdleDetection() 0126 { 0127 if (m_timeoutId) { 0128 KIdleTime::instance()->removeIdleTimeout(m_timeoutId); 0129 m_timeoutId = 0; 0130 } 0131 } 0132 0133 void IdleTimeDetector::toggleOverAllIdleDetection(bool on) 0134 { 0135 m_overAllIdleDetect = on; 0136 } 0137 0138 #include "moc_idletimedetector.cpp"