File indexing completed on 2024-06-23 05:45:41

0001 /*
0002  * Copyright 2021 Swapnil Tripathi <swapnil06.st@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "timerpresetmodel.h"
0008 
0009 #include <QDebug>
0010 #include <QJsonArray>
0011 #include <QJsonDocument>
0012 
0013 const QString TIMERPRESETS_CFG_GROUP = QStringLiteral("TimerPresets"), TIMERPRESETS_CFG_KEY = QStringLiteral("timerPresets");
0014 
0015 TimerPreset::TimerPreset(QObject *parent, const QString &presetName, int presetDuration)
0016     : QObject(parent)
0017     , m_presetName(presetName)
0018     , m_presetDuration(presetDuration)
0019 {
0020 }
0021 TimerPreset::TimerPreset(const QJsonObject &obj)
0022     : m_presetName(obj[QStringLiteral("presetName")].toString())
0023     , m_presetDuration(obj[QStringLiteral("presetDuration")].toInt())
0024 {
0025 }
0026 
0027 TimerPreset::~TimerPreset()
0028 {
0029 }
0030 
0031 QJsonObject TimerPreset::toJson() const
0032 {
0033     QJsonObject obj;
0034     obj[QStringLiteral("presetName")] = m_presetName;
0035     obj[QStringLiteral("presetDuration")] = m_presetDuration;
0036     return obj;
0037 }
0038 
0039 QString TimerPreset::presetName() const
0040 {
0041     return m_presetName;
0042 }
0043 
0044 int TimerPreset::presetDuration() const
0045 {
0046     return m_presetDuration;
0047 }
0048 
0049 void TimerPreset::setPresetName(const QString &presetName)
0050 {
0051     m_presetName = presetName;
0052     Q_EMIT propertyChanged();
0053 }
0054 
0055 void TimerPreset::setDurationLength(int presetDuration)
0056 {
0057     m_presetDuration = presetDuration;
0058     Q_EMIT propertyChanged();
0059 }
0060 
0061 /* - TimerPresetModel - */
0062 
0063 TimerPresetModel *TimerPresetModel::instance()
0064 {
0065     static TimerPresetModel *s_presetModel = new TimerPresetModel(qApp);
0066     return s_presetModel;
0067 }
0068 
0069 TimerPresetModel::TimerPresetModel(QObject *parent)
0070     : QAbstractListModel(parent)
0071 {
0072     load();
0073 }
0074 
0075 TimerPresetModel::~TimerPresetModel()
0076 {
0077     save();
0078 
0079     qDeleteAll(m_presets);
0080 }
0081 
0082 void TimerPresetModel::load()
0083 {
0084     auto config = KSharedConfig::openConfig();
0085     KConfigGroup group = config->group(TIMERPRESETS_CFG_GROUP);
0086     QJsonDocument doc = QJsonDocument::fromJson(group.readEntry(TIMERPRESETS_CFG_KEY, "{}").toUtf8());
0087 
0088     const auto array = doc.array();
0089     std::transform(array.begin(), array.end(), std::back_inserter(m_presets), [](const QJsonValue &pre) {
0090         return new TimerPreset(pre.toObject());
0091     });
0092 }
0093 
0094 void TimerPresetModel::save()
0095 {
0096     QJsonArray arr;
0097 
0098     const auto presets = std::as_const(m_presets);
0099     std::transform(presets.begin(), presets.end(), std::back_inserter(arr), [](const TimerPreset *preset) {
0100         return QJsonValue(preset->toJson());
0101     });
0102 
0103     auto config = KSharedConfig::openConfig();
0104     KConfigGroup group = config->group(TIMERPRESETS_CFG_GROUP);
0105     group.writeEntry(TIMERPRESETS_CFG_KEY, QString::fromStdString(QJsonDocument(arr).toJson(QJsonDocument::Compact).toStdString()));
0106 
0107     group.sync();
0108 }
0109 
0110 QHash<int, QByteArray> TimerPresetModel::roleNames() const
0111 {
0112     return {{Roles::TimerPresetRole, "preset"}};
0113 }
0114 
0115 QVariant TimerPresetModel::data(const QModelIndex &index, int role) const
0116 {
0117     if (!index.isValid() || index.row() >= m_presets.count() || index.row() < 0)
0118         return {};
0119 
0120     auto *preset = m_presets.at(index.row());
0121     if (role == Roles::TimerPresetRole)
0122         return QVariant::fromValue(preset);
0123 
0124     return {};
0125 }
0126 
0127 int TimerPresetModel::rowCount(const QModelIndex &parent) const
0128 {
0129     return parent.isValid() ? 0 : m_presets.count();
0130 }
0131 
0132 void TimerPresetModel::insertPreset(const QString &presetName, int presetDuration)
0133 {
0134     Q_EMIT beginInsertRows({}, 0, 0);
0135     m_presets.insert(0, new TimerPreset(this, presetName, presetDuration));
0136     Q_EMIT endInsertRows();
0137 
0138     save();
0139 }
0140 
0141 void TimerPresetModel::deletePreset(const int index)
0142 {
0143     beginRemoveRows({}, index, index);
0144     m_presets.removeAt(index);
0145     endRemoveRows();
0146 
0147     save();
0148 }