File indexing completed on 2024-05-12 05:14:57

0001 /*
0002  *  resourcescalendar.h  -  KAlarm calendar resources access
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2001-2023 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include "kernelwakealarm.h"
0012 #include "resources/resource.h"
0013 #include "kalarmcalendar/kaevent.h"
0014 
0015 #include <QHash>
0016 #include <QObject>
0017 
0018 class EventId;
0019 
0020 using namespace KAlarmCal;
0021 
0022 
0023 /** Provides read and write access to resource calendars.
0024  *  This class provides the definitive access to events for the application.
0025  *  When events are added, modified or deleted, additional processing is
0026  *  performed beyond what the raw Resource classes do, to:
0027  *  - keep track of which events are to be triggered first in each resource.
0028  *  - keep track of whether any events are disabled.
0029  *  - control the triggering of repeat-at-login alarms.
0030  */
0031 class ResourcesCalendar : public QObject
0032 {
0033     Q_OBJECT
0034 public:
0035     ~ResourcesCalendar() override;
0036     static void           initialise(const QByteArray& appName, const QByteArray& appVersion);
0037     static void           terminate();
0038 
0039     /** Return the active alarm with the earliest trigger time.
0040      *  @param nextTriggerTime       The next trigger time of the earliest alarm.
0041      *  @param excludeDisplayAlarms  Ignore display alarms.
0042      *  @return  The earliest alarm.
0043      */
0044     static KAEvent        earliestAlarm(KADateTime& nextTriggerTime, bool excludeDisplayAlarms = false);
0045 
0046     static void           setAlarmPending(const KAEvent&, bool pending = true);
0047     static bool           haveDisabledAlarms()       { return mHaveDisabledAlarms; }
0048     static void           disabledChanged(const KAEvent&);
0049     using QObject::event;
0050     static KAEvent        event(const EventId& uniqueId, bool findUniqueId = false);
0051     static KAEvent        templateEvent(const QString& templateName);
0052     static QList<KAEvent> events(const QString& uniqueId);
0053     static QList<KAEvent> events(const Resource&, CalEvent::Types = CalEvent::EMPTY);
0054     static QList<KAEvent> events(CalEvent::Types s = CalEvent::EMPTY);
0055 
0056     /** Options for addEvent(). May be OR'ed together. */
0057     enum AddEventOption
0058     {
0059         NoOption         = 0,
0060         UseEventId       = 0x01,   // use event ID if it's provided
0061         NoResourcePrompt = 0x02    // don't prompt for resource
0062     };
0063     Q_DECLARE_FLAGS(AddEventOptions, AddEventOption)
0064 
0065     static bool           addEvent(KAEvent&, Resource&, QWidget* promptparent = nullptr, AddEventOptions options = NoOption, bool* cancelled = nullptr);
0066     static bool           modifyEvent(const EventId& oldEventId, KAEvent& newEvent);
0067     static KAEvent        updateEvent(const KAEvent&, bool saveIfReadOnly = true);
0068     static bool           deleteEvent(const KAEvent&, Resource&, bool save = false);
0069     static void           purgeEvents(const QList<KAlarmCal::KAEvent>&);
0070     static ResourcesCalendar* instance()     { return mInstance; }
0071 
0072 Q_SIGNALS:
0073     void                  earliestAlarmChanged();
0074     void                  haveDisabledAlarmsChanged(bool haveDisabled);
0075     void                  atLoginEventAdded(const KAlarmCal::KAEvent&);
0076 
0077 private Q_SLOTS:
0078     void                  slotResourceSettingsChanged(Resource&, ResourceType::Changes);
0079     void                  slotResourcesPopulated();
0080     void                  slotResourceAdded(Resource&);
0081     void                  slotEventsAdded(Resource&, const QList<KAlarmCal::KAEvent>&);
0082     void                  slotEventsToBeRemoved(Resource&, const QList<KAlarmCal::KAEvent>&);
0083     void                  slotEventUpdated(Resource&, const KAlarmCal::KAEvent&);
0084     void                  slotAlarmsEnabledToggled(bool enabled);
0085     void                  slotWakeFromSuspendAdvanceChanged(unsigned advance);
0086 private:
0087     ResourcesCalendar();
0088     static CalEvent::Type deleteEventInternal(const KAlarmCal::KAEvent&, Resource&, bool deleteFromResource = true);
0089     static CalEvent::Type deleteEventInternal(const QString& eventID, const KAEvent&, Resource&,
0090                                               bool deleteFromResource = true);
0091     void                  removeKAEvents(ResourceId, bool closing = false,
0092                                          CalEvent::Types = CalEvent::ACTIVE | CalEvent::ARCHIVED | CalEvent::TEMPLATE);
0093     static QList<KAEvent> events(CalEvent::Types, const Resource&);
0094     void                  findEarliestAlarm(const Resource&);
0095     void                  checkForDisabledAlarms();
0096     void                  checkForDisabledAlarms(bool oldEnabled, bool newEnabled);
0097     static QList<KAEvent> eventsForResource(const Resource&, const QSet<QString>& eventIds);
0098     void                  setKernelWakeSuspend();
0099     static void           checkKernelWakeSuspend(ResourceId, const KAlarmCal::KAEvent&);
0100 
0101     static ResourcesCalendar* mInstance;   // the unique instance
0102 
0103     typedef QHash<ResourceId, QSet<QString>> ResourceMap;  // event IDs for each resource
0104     typedef QHash<ResourceId, QString> EarliestMap;  // event ID of earliest alarm, for each resource
0105 
0106     static ResourceMap    mResourceMap;
0107     static EarliestMap    mEarliestAlarm;        // alarm with earliest trigger time, by resource
0108     static EarliestMap    mEarliestNonDispAlarm; // non-display alarm with earliest trigger time, by resource
0109     static QSet<QString>  mPendingAlarms;      // IDs of alarms which are currently being processed after triggering
0110     static bool           mIgnoreAtLogin;      // ignore new/updated repeat-at-login alarms
0111     static bool           mHaveDisabledAlarms; // there is at least one individually disabled alarm
0112     // Wake from suspend kernel timers: indexed by resource and event ID.
0113     // There is an entry for every enabled alarm with kernel wake from suspend specified.
0114     // If alarms are disabled (for all alarms), the entries still exist with kernel timers disarmed.
0115     static QHash<ResourceId, QHash<QString, KernelWakeAlarm>> mWakeSuspendTimers;
0116 };
0117 
0118 Q_DECLARE_OPERATORS_FOR_FLAGS(ResourcesCalendar::AddEventOptions)
0119 
0120 // vim: et sw=4: