File indexing completed on 2025-02-16 04:48:50

0001 /*
0002  *  resourcedatamodelbase.h  -  base for models containing calendars and events
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2007-2022 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include "resourcetype.h"
0012 #include "preferences.h"
0013 #include "kalarmcalendar/kacalendar.h"
0014 
0015 #include <QSize>
0016 
0017 class Resource;
0018 class ResourceListModel;
0019 class ResourceFilterCheckListModel;
0020 class AlarmListModel;
0021 class TemplateListModel;
0022 class ResourceCreator;
0023 class QPixmap;
0024 
0025 namespace KAlarmCal { class KAEvent; }
0026 
0027 using namespace KAlarmCal;
0028 
0029 
0030 /*=============================================================================
0031 = Class: ResourceDataModelBase
0032 = Base class for models containing all calendars and events.
0033 =============================================================================*/
0034 class ResourceDataModelBase
0035 {
0036 public:
0037     /** Data column numbers. */
0038     enum
0039     {
0040         // Item columns
0041         TimeColumn = 0, TimeToColumn, RepeatColumn, ColourColumn, TypeColumn, NameColumn, TextColumn,
0042         TemplateNameColumn,
0043         ColumnCount
0044     };
0045     /** Additional model data roles. */
0046     enum
0047     {
0048         UserRole = Qt::UserRole + 500,   // copied from Akonadi::EntityTreeModel
0049         ItemTypeRole = UserRole,   // item's type: calendar or event
0050         // Calendar roles
0051         ResourceIdRole,            // the resource ID
0052         BaseColourRole,            // background colour ignoring collection colour
0053         // Event roles
0054         EventIdRole,               // the event ID
0055         ParentResourceIdRole,      // the parent resource ID of the event
0056         EnabledRole,               // true for enabled alarm, false for disabled
0057         StatusRole,                // KAEvent::ACTIVE/ARCHIVED/TEMPLATE
0058         AlarmActionsRole,          // KAEvent::Actions
0059         AlarmSubActionRole,        // KAEvent::Action
0060         ValueRole,                 // numeric value
0061         SortRole,                  // the value to use for sorting
0062         TimeDisplayRole,           // time column value with '~' representing omitted leading zeroes
0063         ColumnTitleRole,           // column titles (whether displayed or not)
0064         CommandErrorRole           // last command execution error for alarm (per user)
0065     };
0066     /** The type of a model row. */
0067     enum class Type { Error = 0, Event, Resource };
0068 
0069     virtual ~ResourceDataModelBase();
0070 
0071     static QSize iconSize()       { return mIconSize; }
0072 
0073     /** Return a bulleted list of alarm types for inclusion in an i18n message. */
0074     static QString typeListForDisplay(CalEvent::Types);
0075 
0076     /** Get the tooltip for a resource. The resource's enabled status is
0077      *  evaluated for specified alarm types. */
0078     QString tooltip(const Resource&, CalEvent::Types) const;
0079 
0080     /** Return the read-only status tooltip for a resource.
0081      * A null string is returned if the resource is fully writable. */
0082     static QString readOnlyTooltip(const Resource&);
0083 
0084     /** Return offset to add to headerData() role, for item models. */
0085     virtual int headerDataEventRoleOffset() const  { return 0; }
0086 
0087     /** Return the alarm time text in the form "date time".
0088      *  @param dateTime     the date/time to format.
0089      *  @param leadingZero  the character to represent a leading zero, or '\0' for no leading zeroes.
0090      */
0091     static QString alarmTimeText(const DateTime& dateTime, char leadingZero = '\0');
0092 
0093     /** Return the time-to-alarm text. */
0094     static QString timeToAlarmText(const DateTime&);
0095 
0096 protected:
0097     ResourceDataModelBase();
0098 
0099     /** Terminate access to the data model, and tidy up. */
0100     virtual void terminate() = 0;
0101 
0102     /** Reload all resources' data from storage.
0103      *  @note In the case of Akonadi, this does not reload from the backend storage.
0104      */
0105     virtual void reload() = 0;
0106 
0107     /** Reload a resource's data from storage.
0108      *  @note In the case of Akonadi, this does not reload from the backend storage.
0109      */
0110     virtual bool reload(Resource&) = 0;
0111 
0112     /** Check for, and remove, any duplicate resources, i.e. those which use
0113      *  the same calendar file/directory.
0114      */
0115     virtual void removeDuplicateResources() = 0;
0116 
0117     /** Disable the widget if the database engine is not available, and display
0118      *  an error overlay.
0119      */
0120     virtual void widgetNeedsDatabase(QWidget*) = 0;
0121 
0122     /** Create a ResourceCreator instance for the model. */
0123     virtual ResourceCreator* createResourceCreator(KAlarmCal::CalEvent::Type defaultType, QWidget* parent) = 0;
0124 
0125     /** Update a resource's backend calendar file to the current KAlarm format. */
0126     virtual void updateCalendarToCurrentFormat(Resource&, bool ignoreKeepFormat, QObject* parent) = 0;
0127 
0128     virtual ResourceListModel* createResourceListModel(QObject* parent) = 0;
0129     virtual ResourceFilterCheckListModel* createResourceFilterCheckListModel(QObject* parent) = 0;
0130     virtual AlarmListModel*    createAlarmListModel(QObject* parent) = 0;
0131     virtual AlarmListModel*    allAlarmListModel() = 0;
0132     virtual TemplateListModel* createTemplateListModel(QObject* parent) = 0;
0133     virtual TemplateListModel* allTemplateListModel() = 0;
0134 
0135     /** Return the data storage backend type used by this model. */
0136     virtual Preferences::Backend dataStorageBackend() const = 0;
0137 
0138     static QVariant headerData(int section, Qt::Orientation, int role, bool eventHeaders, bool& handled);
0139 
0140     /** Return whether resourceData() and/or eventData() handle a role. */
0141     bool roleHandled(int role) const;
0142 
0143     /** Return the model data for a resource.
0144      *  @param role     may be updated for calling the base model.
0145      *  @param handled  updated to true if the reply is valid, else set to false.
0146      */
0147     QVariant resourceData(int& role, const Resource&, bool& handled) const;
0148 
0149     /** Return the model data for an event.
0150      *  @param handled  updated to true if the reply is valid, else set to false.
0151      */
0152     QVariant eventData(int role, int column, const KAEvent& event, const Resource&, bool& handled) const;
0153 
0154     /** Called when a resource notifies a message to display to the user. */
0155     void handleResourceMessage(ResourceType::MessageType, const QString& message, const QString& details);
0156 
0157     /** Return whether calendar migration/creation at initialisation has completed. */
0158     bool isMigrationComplete() const;
0159 
0160     /** Return whether calendar migration is currently in progress. */
0161     bool isMigrating() const;
0162 
0163     /** To be called when calendar migration has been initiated (or reset). */
0164     void setMigrationInitiated(bool started = true);
0165 
0166     /** To be called when calendar migration has been initiated (or reset). */
0167     void setMigrationComplete();
0168 
0169     /** To be called when all previously configured calendars have been created. */
0170     void setCalendarsCreated();
0171 
0172     static QString  repeatText(const KAEvent&);
0173     static QString  repeatOrder(const KAEvent&);
0174     static QString  whatsThisText(int column);
0175     static QPixmap* eventIcon(const KAEvent&);
0176 
0177     static ResourceDataModelBase* mInstance;
0178 
0179 private:
0180     static QPixmap* mTextIcon;
0181     static QPixmap* mFileIcon;
0182     static QPixmap* mCommandIcon;
0183     static QPixmap* mEmailIcon;
0184     static QPixmap* mAudioIcon;
0185     static QSize    mIconSize;      // maximum size of any icon
0186 
0187     int  mMigrationStatus {-1};     // migration status, -1 = no, 0 = initiated, 1 = complete
0188     bool mCreationStatus {false};   // previously configured calendar creation status
0189 
0190 friend class DataModel;
0191 };
0192 
0193 // vim: et sw=4: