File indexing completed on 2024-05-12 16:25:35

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "awaymanager.h"
0008 #include "rocketchataccount.h"
0009 #include "ruqola_away_debug.h"
0010 #include <KIdleTime>
0011 // #define DEBUG_IDLETIME 1
0012 AwayManager::AwayManager(RocketChatAccount *const account, QObject *parent)
0013     : QObject{parent}
0014     , mRocketChatAccount(account)
0015 {
0016     connect(KIdleTime::instance(), &KIdleTime::resumingFromIdle, this, &AwayManager::slotResumeFromIdle);
0017     connect(KIdleTime::instance(), &KIdleTime::timeoutReached, this, &AwayManager::slotIdleTimeoutReached);
0018 
0019     KIdleTime::instance()->catchNextResumeEvent();
0020 }
0021 
0022 AwayManager::~AwayManager() = default;
0023 
0024 void AwayManager::slotResumeFromIdle()
0025 {
0026     if (!mEnabled) {
0027         return;
0028     }
0029     Q_EMIT awayChanged(false); // Not away now
0030     qCDebug(RUQOLA_AWAY_LOG) << " void AwayManager::slotResumeFromIdle() : name : " << mRocketChatAccount->accountName();
0031     const QHash<int, int> idleTimeouts = KIdleTime::instance()->idleTimeouts();
0032 
0033     int remainingTime = calculateRemainingTime();
0034     // Check if the user should be away right now.
0035     if (remainingTime <= 0) {
0036         Q_EMIT awayChanged(true);
0037         // As at least one identity is away we have to catch the next
0038         // resume event.
0039         KIdleTime::instance()->catchNextResumeEvent();
0040 
0041         // Since the user is away right now the next auto-away should occur
0042         // in X minutes (where X is the timeout which the user has
0043         // configured for the identity).
0044         remainingTime = mIdleTiming;
0045     }
0046     if (idleTimeouts[mTimerId] != remainingTime) {
0047         // Remove the idle timeout.
0048         if (mTimerId != -1) {
0049             KIdleTime::instance()->removeIdleTimeout(mTimerId);
0050         }
0051 
0052         // Then also reset the timer ID (as the timer does not exist anymore).
0053         mTimerId = -1;
0054     }
0055     // Check if we already have a timer.
0056     if (mTimerId == -1) {
0057         mTimerId = KIdleTime::instance()->addIdleTimeout(timeValue());
0058     }
0059 }
0060 
0061 int AwayManager::calculateRemainingTime() const
0062 {
0063     // The remaining time until the user will be marked as "auto-away".
0064     const int remainingTime = mIdleTiming - KIdleTime::instance()->idleTime();
0065 
0066     return remainingTime;
0067 }
0068 
0069 void AwayManager::slotIdleTimeoutReached(int timerId)
0070 {
0071     if (!mEnabled) {
0072         return;
0073     }
0074     if (mTimerId == timerId) {
0075         qDebug() << " slotIdleTimeoutReached " << KIdleTime::instance()->idleTimeouts();
0076         qCDebug(RUQOLA_AWAY_LOG) << " void AwayManager::slotIdleTimeoutReached() mTimerId :" << mTimerId << "name : " << mRocketChatAccount->accountName();
0077         Q_EMIT awayChanged(true); // Away now
0078         // Account is away => we need to catch next resume event.
0079         KIdleTime::instance()->catchNextResumeEvent();
0080     }
0081 }
0082 
0083 int AwayManager::idleTiming() const
0084 {
0085     return mIdleTiming;
0086 }
0087 
0088 void AwayManager::setIdleTiming(int newIdleTiming)
0089 {
0090     mIdleTiming = newIdleTiming;
0091 }
0092 
0093 void AwayManager::updateSettings()
0094 {
0095     const auto &ownerUserPref = mRocketChatAccount->ownUserPreferences();
0096     setIdleTiming(ownerUserPref.idleTimeLimit());
0097     setEnabled(ownerUserPref.enableAutoAway());
0098 }
0099 
0100 bool AwayManager::enabled() const
0101 {
0102     return mEnabled;
0103 }
0104 
0105 void AwayManager::setEnabled(bool newEnabled)
0106 {
0107     qCDebug(RUQOLA_AWAY_LOG) << " void AwayManager::setEnabled()" << newEnabled << "name : " << mRocketChatAccount->accountName();
0108     if (mEnabled != newEnabled) {
0109         mEnabled = newEnabled;
0110         if (!mEnabled && (mTimerId != -1)) {
0111             KIdleTime::instance()->removeIdleTimeout(mTimerId);
0112             qCDebug(RUQOLA_AWAY_LOG) << " Remove Idle Timeout " << newEnabled;
0113             mTimerId = -1;
0114         } else if (mEnabled && (mTimerId == -1)) {
0115             mTimerId = KIdleTime::instance()->addIdleTimeout(timeValue());
0116             qCDebug(RUQOLA_AWAY_LOG) << " Catch Next Resume Event " << newEnabled << " mIdleTiming (s)" << mIdleTiming << " mTimerId " << mTimerId;
0117             KIdleTime::instance()->catchNextResumeEvent();
0118         }
0119     }
0120 }
0121 
0122 int AwayManager::timeValue() const
0123 {
0124     constexpr int minute = 60 * 1000;
0125 #ifdef DEBUG_IDLETIME
0126     const int timerValue = minute; // 1 minutes
0127 #else
0128     const int timerValue = mIdleTiming * minute;
0129 #endif
0130     return timerValue;
0131 }
0132 
0133 QDebug operator<<(QDebug d, const AwayManager &t)
0134 {
0135     d.space() << "mEnabled" << t.enabled();
0136     d.space() << "mIdleTiming" << t.idleTiming();
0137     return d;
0138 }
0139 
0140 #include "moc_awaymanager.cpp"