File indexing completed on 2024-04-21 16:35:01

0001 /*
0002     SPDX-FileCopyrightText: 2006, 2010 Tom Albers <toma@kde.org>
0003     SPDX-FileCopyrightText: 2010 Juan Luis Baptiste <juan.baptiste@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "rsiglobals.h"
0009 
0010 #include <kconfig.h>
0011 #include <kconfiggroup.h>
0012 #include <klocalizedstring.h>
0013 #include <knotification.h>
0014 #include <ksharedconfig.h>
0015 #include <qdebug.h>
0016 
0017 #include <math.h>
0018 
0019 #include "rsistats.h"
0020 
0021 RSIGlobals *RSIGlobals::m_instance = nullptr;
0022 RSIStats *RSIGlobals::m_stats = nullptr;
0023 
0024 RSIGlobals::RSIGlobals(QObject *parent)
0025     : QObject(parent)
0026 {
0027     resetUsage();
0028     slotReadConfig();
0029 }
0030 
0031 RSIGlobals::~RSIGlobals()
0032 {
0033     delete m_stats;
0034     m_stats = nullptr;
0035 }
0036 
0037 RSIGlobals *RSIGlobals::instance()
0038 {
0039     if (!m_instance) {
0040         m_instance = new RSIGlobals();
0041         m_stats = new RSIStats();
0042     }
0043 
0044     return m_instance;
0045 }
0046 
0047 QString RSIGlobals::formatSeconds(const int seconds)
0048 {
0049     return m_format.formatSpelloutDuration(seconds * 1000);
0050 }
0051 
0052 void RSIGlobals::slotReadConfig()
0053 {
0054     KConfigGroup config = KSharedConfig::openConfig()->group("General Settings");
0055 
0056     int mult = 60;
0057     if (config.readEntry("DEBUG", 0) > 0) {
0058         qDebug() << "Debug mode activated";
0059         mult = 1;
0060     }
0061 
0062     m_intervals.resize(INTERVAL_COUNT);
0063     m_intervals[TINY_BREAK_INTERVAL] = config.readEntry("TinyEnabled", true) ? config.readEntry("TinyInterval", 10) * mult : 0;
0064     m_intervals[TINY_BREAK_DURATION] = config.readEntry("TinyDuration", 20);
0065     m_intervals[TINY_BREAK_THRESHOLD] = config.readEntry("TinyThreshold", 20);
0066     m_intervals[BIG_BREAK_INTERVAL] = config.readEntry("BigInterval", 60) * mult;
0067     m_intervals[BIG_BREAK_DURATION] = config.readEntry("BigDuration", 1) * mult;
0068     m_intervals[BIG_BREAK_THRESHOLD] = config.readEntry("BigThreshold", 1) * mult;
0069     m_intervals[POSTPONE_BREAK_INTERVAL] = config.readEntry("PostponeBreakDuration", 5) * mult;
0070     m_intervals[PATIENCE_INTERVAL] = config.readEntry("Patience", 30);
0071     m_intervals[SHORT_INPUT_INTERVAL] = config.readEntry("ShortInputInterval", 2);
0072 }
0073 
0074 QColor RSIGlobals::getTinyBreakColor(int secsToBreak) const
0075 {
0076     int minimized = m_intervals[TINY_BREAK_INTERVAL];
0077     double v = 100 * secsToBreak / (double)minimized;
0078 
0079     v = v > 100 ? 100 : v;
0080     v = v < 0 ? 0 : v;
0081 
0082     return QColor((int)(255 - 2.55 * v), (int)(1.60 * v), 0);
0083 }
0084 
0085 QColor RSIGlobals::getBigBreakColor(int secsToBreak) const
0086 {
0087     int minimized = m_intervals[BIG_BREAK_INTERVAL];
0088     double v = 100 * secsToBreak / (double)minimized;
0089 
0090     v = v > 100 ? 100 : v;
0091     v = v < 0 ? 0 : v;
0092 
0093     return QColor((int)(255 - 2.55 * v), (int)(1.60 * v), 0);
0094 }
0095 
0096 void RSIGlobals::resetUsage()
0097 {
0098     m_usageArray.fill(false, 60 * 60 * 24);
0099 }