File indexing completed on 2024-05-05 05:28:20
0001 /* 0002 * SPDX-FileCopyrightText: 2018 Dimitris Kardarakos <dimkard@posteo.net> 0003 * 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "localcalendar.h" 0008 #include "calindoriconfig.h" 0009 #include "alarmchecker.h" 0010 #include <QDebug> 0011 #include <KCalendarCore/Todo> 0012 #include <KCalendarCore/MemoryCalendar> 0013 #include <QFile> 0014 #include <QFileInfo> 0015 #include <QStandardPaths> 0016 #include <KLocalizedString> 0017 0018 LocalCalendar::LocalCalendar(QObject *parent) 0019 : QObject(parent), m_alarm_checker {new AlarmChecker(this)} 0020 { 0021 loadCalendar(CalindoriConfig::instance().activeCalendar()); 0022 } 0023 0024 LocalCalendar::~LocalCalendar() = default; 0025 0026 KCalendarCore::Calendar::Ptr LocalCalendar::calendar() 0027 { 0028 reloadStorage(); 0029 return m_calendar; 0030 } 0031 0032 QString LocalCalendar::name() const 0033 { 0034 return m_name; 0035 } 0036 0037 void LocalCalendar::setName(const QString &calendarName) 0038 { 0039 if (m_name != calendarName) { 0040 loadCalendar(calendarName); 0041 } 0042 } 0043 0044 int LocalCalendar::todosCount(const QDate &date) const 0045 { 0046 if (m_calendar == nullptr) { 0047 return 0; 0048 } 0049 KCalendarCore::Todo::List todoList = m_calendar->rawTodos(date, date); 0050 0051 return todoList.size(); 0052 } 0053 0054 void LocalCalendar::deleteCalendar() 0055 { 0056 qDebug() << "Deleting calendar at " << m_fullpath; 0057 QFile calendarFile(m_fullpath); 0058 0059 if (calendarFile.exists()) { 0060 calendarFile.remove(); 0061 } 0062 } 0063 0064 int LocalCalendar::eventsCount(const QDate &date) const 0065 { 0066 if (m_calendar == nullptr) { 0067 return 0; 0068 } 0069 KCalendarCore::Event::List eventList = m_calendar->rawEventsForDate(date); 0070 0071 return eventList.count(); 0072 } 0073 0074 bool LocalCalendar::save() 0075 { 0076 if (m_cal_storage->save()) { 0077 qDebug() << "Saving to file"; 0078 m_fs_sync_dt = QDateTime::currentDateTime(); 0079 m_alarm_checker->scheduleAlarmCheck(); 0080 return true; 0081 } 0082 0083 return false; 0084 } 0085 0086 QVariantMap LocalCalendar::canCreateFile(const QString &calendarName) 0087 { 0088 QVariantMap result; 0089 result[QStringLiteral("success")] = QVariant(true); 0090 result[QStringLiteral("reason")] = QVariant(QString()); 0091 0092 QString targetPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/calindori_") + calendarName + QStringLiteral(".ics"); 0093 QFile calendarFile(targetPath); 0094 0095 if (calendarFile.exists()) { 0096 result[QStringLiteral("success")] = QVariant(false); 0097 result[QStringLiteral("reason")] = QVariant(QString(i18n("A calendar with the same name already exists"))); 0098 0099 return result; 0100 } 0101 0102 result[QStringLiteral("targetPath")] = QVariant(QString(targetPath)); 0103 0104 return result; 0105 } 0106 0107 void LocalCalendar::loadCalendar(const QString &calendarName) 0108 { 0109 m_fullpath = CalindoriConfig::instance().calendarFile(calendarName); 0110 0111 if (loadStorage()) { 0112 m_name = calendarName; 0113 Q_EMIT nameChanged(); 0114 Q_EMIT todosChanged(); 0115 Q_EMIT eventsChanged(); 0116 } 0117 } 0118 0119 void LocalCalendar::reloadStorage() 0120 { 0121 if (m_fullpath.isEmpty()) { 0122 qDebug() << "Not ready for reload, file path not set"; 0123 return; 0124 } 0125 0126 QFileInfo storageFileInfo { m_fullpath }; 0127 0128 if (storageFileInfo.lastModified() > m_fs_sync_dt) { 0129 qDebug() << "Last memory-fs sync: " << m_fs_sync_dt; 0130 qDebug() << "Filed modified: " << storageFileInfo.lastModified(); 0131 qDebug() << "Reload storage"; 0132 loadStorage(); 0133 } 0134 } 0135 0136 bool LocalCalendar::loadStorage() 0137 { 0138 if (m_fullpath.isEmpty()) { 0139 qDebug() << "Not ready for load, file path not set"; 0140 return false; 0141 } 0142 0143 KCalendarCore::Calendar::Ptr calendar(new KCalendarCore::MemoryCalendar(QTimeZone::systemTimeZoneId())); 0144 KCalendarCore::FileStorage::Ptr storage(new KCalendarCore::FileStorage(calendar)); 0145 storage->setFileName(m_fullpath); 0146 0147 QFile calendarFile(m_fullpath); 0148 0149 if (!calendarFile.exists()) { 0150 bool saved = storage->save(); 0151 qDebug() << "New calendar file created: " << saved; 0152 } 0153 0154 if (storage->load()) { 0155 qDebug() << "Storage file loaded"; 0156 m_cal_storage = storage; 0157 m_calendar = calendar; 0158 m_fs_sync_dt = QDateTime::currentDateTime(); 0159 m_alarm_checker->scheduleAlarmCheck(); 0160 Q_EMIT calendarChanged(); 0161 return true; 0162 } 0163 0164 return false; 0165 } 0166 0167 QString LocalCalendar::ownerName() const 0168 { 0169 return CalindoriConfig::instance().ownerName(m_name); 0170 } 0171 0172 QString LocalCalendar::ownerEmail() const 0173 { 0174 return CalindoriConfig::instance().ownerEmail(m_name); 0175 } 0176 0177 bool LocalCalendar::isExternal() const 0178 { 0179 return CalindoriConfig::instance().isExternal(m_name); 0180 } 0181 0182 void LocalCalendar::setOwnerName(const QString &ownerName) 0183 { 0184 CalindoriConfig::instance().setOwnerName(m_name, ownerName); 0185 0186 Q_EMIT ownerNameChanged(); 0187 } 0188 0189 void LocalCalendar::setOwnerEmail(const QString &ownerEmail) 0190 { 0191 CalindoriConfig::instance().setOwnerEmail(m_name, ownerEmail); 0192 0193 Q_EMIT ownerEmailChanged(); 0194 } 0195 0196 KCalendarCore::Calendar *LocalCalendar::calendarRaw() const 0197 { 0198 return m_calendar.data(); 0199 }