File indexing completed on 2024-05-05 05:28:18
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Dimitris Kardarakos <dimkard@posteo.net> 0003 * 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "alarmsmodel.h" 0008 #include <KCalendarCore/MemoryCalendar> 0009 #include <KSharedConfig> 0010 #include <KConfigGroup> 0011 #include <QFile> 0012 #include <QDebug> 0013 0014 AlarmsModel::AlarmsModel(QObject *parent) : QObject(parent), m_calendars {QVector<Calendar::Ptr>()}, m_file_storages {QVector<FileStorage::Ptr>()}, m_alarms {Alarm::List()}, m_calendar_files {QStringList()} 0015 { 0016 connect(this, &AlarmsModel::periodChanged, this, &AlarmsModel::loadAlarms); 0017 connect(this, &AlarmsModel::calendarsChanged, this, &AlarmsModel::loadAlarms); 0018 } 0019 0020 AlarmsModel::~AlarmsModel() = default; 0021 0022 void AlarmsModel::loadAlarms() 0023 { 0024 m_alarms.clear(); 0025 0026 if (!(m_period.from.isValid()) && !(m_period.to.isValid())) { 0027 return; 0028 } 0029 0030 openLoadStorages(); 0031 0032 for (const auto &m : qAsConst(m_calendars)) { 0033 0034 Alarm::List calendarAlarms; 0035 0036 if (m_period.from.isValid() && m_period.to.isValid()) { 0037 calendarAlarms = m->alarms(m_period.from, m_period.to, true); 0038 } else if (!(m_period.from.isValid()) && m_period.to.isValid()) { 0039 calendarAlarms = m->alarms(QDateTime(QDate(1900, 1, 1), QTime(0, 0, 0)), m_period.to); 0040 } 0041 0042 if (!(calendarAlarms.empty())) { 0043 m_alarms.append(calendarAlarms); 0044 } 0045 } 0046 0047 qDebug() << "loadAlarms:" << m_period.from.toString(QStringLiteral("dd.MM.yyyy hh:mm:ss")) << "to" << m_period.to.toString(QStringLiteral("dd.MM.yyyy hh:mm:ss")) << m_alarms.count() << "alarms found"; 0048 0049 closeStorages(); 0050 } 0051 0052 void AlarmsModel::setCalendars() 0053 { 0054 m_file_storages.clear(); 0055 m_calendars.clear(); 0056 0057 qDebug() << "setCalendars:" << "Appending calendars" << m_calendar_files.join(QStringLiteral(",")); 0058 0059 for (const auto &cf : qAsConst(m_calendar_files)) { 0060 Calendar::Ptr calendar(new MemoryCalendar(QTimeZone::systemTimeZoneId())); 0061 FileStorage::Ptr storage(new FileStorage(calendar)); 0062 storage->setFileName(cf); 0063 if (!(storage->fileName().isNull())) { 0064 m_file_storages.append(storage); 0065 m_calendars.append(calendar); 0066 } 0067 } 0068 0069 Q_EMIT calendarsChanged(); 0070 } 0071 0072 void AlarmsModel::openLoadStorages() 0073 { 0074 auto loaded { true }; 0075 for (const auto &fs : qAsConst(m_file_storages)) { 0076 loaded = fs->open() && fs->load() && loaded; 0077 } 0078 qDebug() << "openLoadStorages:" << "Loaded:" << loaded; 0079 0080 } 0081 0082 void AlarmsModel::closeStorages() 0083 { 0084 auto closed { true }; 0085 for (const auto &fs : qAsConst(m_file_storages)) { 0086 closed = fs->close() && closed; 0087 } 0088 0089 qDebug() << "closeStorages:" << "Closed:" << closed; 0090 } 0091 0092 QDateTime AlarmsModel::parentStartDt(const int idx) const 0093 { 0094 Alarm::Ptr alarm = m_alarms.at(idx); 0095 Duration offsetDuration; 0096 QDateTime alarmTime = m_alarms.at(idx)->time(); 0097 if (alarm->hasStartOffset()) { 0098 offsetDuration = alarm->startOffset(); 0099 } 0100 0101 if (!(offsetDuration.isNull())) { 0102 int secondsFromStart = offsetDuration.asSeconds(); 0103 0104 return alarmTime.addSecs(-1 * secondsFromStart); 0105 } 0106 0107 return alarmTime; 0108 } 0109 0110 Alarm::List AlarmsModel::alarms() const 0111 { 0112 return m_alarms; 0113 } 0114 0115 FilterPeriod AlarmsModel::period() const 0116 { 0117 return m_period; 0118 } 0119 0120 void AlarmsModel::setPeriod(const FilterPeriod &filterPeriod) 0121 { 0122 m_period = filterPeriod; 0123 0124 Q_EMIT periodChanged(); 0125 } 0126 0127 QStringList AlarmsModel::calendarFiles() const 0128 { 0129 return m_calendar_files; 0130 } 0131 0132 void AlarmsModel::setCalendarFiles(const QStringList &fileList) 0133 { 0134 m_calendar_files = fileList; 0135 0136 setCalendars(); 0137 } 0138 0139 QDateTime AlarmsModel::firstAlarmTime() const 0140 { 0141 if (m_alarms.isEmpty()) { 0142 return QDateTime {}; 0143 } 0144 0145 auto firstAlarmTime = m_alarms.first()->nextTime(m_period.from); 0146 0147 for (const auto &alarm : m_alarms) { 0148 auto alarmTime = alarm->nextTime(m_period.from); 0149 if (alarmTime < firstAlarmTime) { 0150 firstAlarmTime = alarmTime; 0151 } 0152 } 0153 0154 return firstAlarmTime; 0155 }