diff --git a/src/resources/resources.h b/src/resources/resources.h index 451b6b36..d326c38e 100644 --- a/src/resources/resources.h +++ b/src/resources/resources.h @@ -1,319 +1,321 @@ /* * resources.h - container for all ResourceType instances * Program: kalarm * Copyright © 2019-2020 David Jarvie * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef RESOURCES_H #define RESOURCES_H #include "datamodel.h" #include "resource.h" #include "resourcemodel.h" #include class QEventLoop; using namespace KAlarmCal; /** Class to contain all ResourceType instances. * It provides connection to signals from all ResourceType instances. */ class Resources : public QObject { Q_OBJECT public: /** Creates the unique Resources instance. */ static Resources* instance(); ~Resources(); Resources(const Resources&) = delete; Resources& operator=(const Resources&) const = delete; /** Return a copy of the resource with a given ID. * @return The resource, or invalid if the ID doesn't already exist or is invalid. */ static Resource resource(ResourceId); /** Remove a resource. The calendar file is not removed. * @return true if the resource has been removed or a removal job has been scheduled. */ static bool removeResource(Resource&); + /** Sorting criteria for allResources(Type, Sorting). May be OR'ed together. */ + enum Sorts + { + NoSort = 0, + DisplayName = 0x01, // sort by display name + DefaultFirst = 0x02 // default resource is first in list. Requires a CalEvent::Type to be specified. + }; + Q_DECLARE_FLAGS(Sorting, Sorts) /** Return all resources of a kind which contain a specified alarm type. - * @tparam Type Resource type to fetch, default = all types. + * @tparam RType Resource type to fetch, default = all types. * @param alarmType Alarm type to check for, or CalEvent::EMPTY for any type. + * @param sorting Sorting criteria to use. */ - template - static QVector allResources(CalEvent::Type alarmType = CalEvent::EMPTY); - - /** Return all resources of a kind which contain a specified alarm type, - * with the standard resource for the alarm type first in the list. - * @tparam Type Resource type to fetch, default = all types. - * @param alarmType Alarm type to check for. - */ - template - static QVector allResourcesOrdered(CalEvent::Type alarmType); + template + static QVector allResources(CalEvent::Type alarmType = CalEvent::EMPTY, Sorting sorting = NoSort); /** Return the enabled resources which contain a specified alarm type. * @param type Alarm type to check for, or CalEvent::EMPTY for any type. * @param writable If true, only writable resources are included. */ static QVector enabledResources(CalEvent::Type type = CalEvent::EMPTY, bool writable = false); /** Return the standard resource for an alarm type. This is the resource * which can be set as the default to add new alarms to. * Only enabled and writable resources can be standard. * In the case of archived alarm resources, if no resource is specified * as standard and there is exactly one writable archived alarm resource, * that resource will be automatically set as standard. * * @param type alarm type * @return standard resource, or null if none. */ static Resource getStandard(CalEvent::Type type); /** Return whether a resource is the standard resource for a specified alarm * type. Only enabled and writable resources can be standard. * In the case of archived alarms, if no resource is specified as standard * and the resource is the only writable archived alarm resource, it will * be automatically set as standard. */ static bool isStandard(const Resource& resource, CalEvent::Type); /** Return the alarm type(s) for which a resource is the standard resource. * Only enabled and writable resources can be standard. * @param useDefault false to return the defined standard types, if any; * true to return the types for which it is the standard * or only resource. */ static CalEvent::Types standardTypes(const Resource& resource, bool useDefault = false); /** Set or clear a resource as the standard resource for a specified alarm * type. This does not affect its status for other alarm types. * The resource must be writable and enabled for the type, to set * standard = true. * If the resource is being set as standard, the standard status for the * alarm type is cleared for any other resources. */ static void setStandard(Resource& resource, CalEvent::Type, bool standard); /** Set which alarm types a resource is the standard resource for. * Its standard status is cleared for other alarm types. * The resource must be writable and enabled for the type, to set * standard = true. * If the resource is being set as standard for any alarm types, the * standard status is cleared for those alarm types for any other resources. */ static void setStandard(Resource& resource, CalEvent::Types); /** Find the resource to be used to store an event of a given type. * This will be the standard resource for the type, but if this is not valid, * the user will be prompted to select a resource. * @param type The event type * @param promptParent The parent widget for the prompt * @param noPrompt Don't prompt the user even if the standard resource is not valid * @param cancelled If non-null: set to true if the user cancelled the * prompt dialogue; set to false if any other error */ static Resource destination(CalEvent::Type type, QWidget* promptParent = nullptr, bool noPrompt = false, bool* cancelled = nullptr); /** Return whether all configured resources have been created. */ static bool allCreated(); /** Return whether all configured resources have been loaded at least once. */ static bool allPopulated(); /** Return the resource which an event belongs to, provided that the event's * alarm type is enabled. */ static Resource resourceForEvent(const QString& eventId); /** Return the resource which an event belongs to, and the event, provided * that the event's alarm type is enabled. */ static Resource resourceForEvent(const QString& eventId, KAEvent& event); /** Return the resource which has a given configuration identifier. */ static Resource resourceForConfigName(const QString& configName); /** Called to notify that a new resource has completed its initialisation, * in order to emit the resourceAdded() signal. */ static void notifyNewResourceInitialised(Resource&); /** Called to notify that all configured resources have now been created. */ static void notifyResourcesCreated(); /** Called by a resource to notify that loading of events has successfully completed. */ static void notifyResourcePopulated(const ResourceType*); /** Called to notify that migration/creation of resources has completed. */ static void notifyResourcesMigrated(); /** Called to notify that a resource is about to be removed. */ static void notifyResourceToBeRemoved(ResourceType*); /** Called by a resource to notify that its settings have changed. * This will cause the settingsChanged() signal to be emitted. */ static void notifySettingsChanged(ResourceType*, ResourceType::Changes, CalEvent::Types oldEnabled); /** Called by a resource when a user message should be displayed. * This will cause the resourceMessage() signal to be emitted. * @param message Must include the resource's display name in order to * identify the resource to the user. */ static void notifyResourceMessage(ResourceType*, ResourceType::MessageType, const QString& message, const QString& details); /** Called when a user message should be displayed for a resource. * This will cause the resourceMessage() signal to be emitted. * @param message Must include the resource's display name in order to * identify the resource to the user. */ static void notifyResourceMessage(ResourceId, ResourceType::MessageType, const QString& message, const QString& details); /** Called by a resource to notify that it has added events. */ static void notifyEventsAdded(ResourceType*, const QList&); /** Called by a resource to notify that it has changed an event. * The event's UID must be unchanged. */ static void notifyEventUpdated(ResourceType*, const KAEvent& event); /** Called by a resource to notify that it is about to delete events. */ static void notifyEventsToBeRemoved(ResourceType*, const QList&); Q_SIGNALS: /** Emitted when a resource's settings have changed. */ void settingsChanged(Resource&, ResourceType::Changes); /** Emitted when all configured resource have been created (but not * necessarily populated). Note that after this, resource migration and * the creation of default resources is performed and notified by the * signal migrationCompleted(). */ void resourcesCreated(); /** Emitted when all configured resources have been loaded for the first time. */ void resourcesPopulated(); /** Signal emitted when resource migration/creation at startup has completed. */ void migrationCompleted(); /** Emitted when a new resource has been created. */ void resourceAdded(Resource&); /** Emitted when a resource's events have been successfully loaded. */ void resourcePopulated(Resource&); /** Emitted when a resource's config and settings are about to be removed. */ void resourceToBeRemoved(Resource&); /** Emitted when a resource's config and settings have been removed. */ void resourceRemoved(ResourceId); /** Emitted when a resource message should be displayed to the user. * @note Connections to this signal should use Qt::QueuedConnection type * to allow processing to continue while the user message is displayed. */ void resourceMessage(ResourceType::MessageType, const QString& message, const QString& details); /** Emitted when events have been added to a resource. * Events are only notified whose alarm type is enabled. */ void eventsAdded(Resource&, const QList&); /** Emitted when an event has been updated in a resource. * Events are only notified whose alarm type is enabled. * The event's UID is unchanged. */ void eventUpdated(Resource&, const KAEvent&); /** Emitted when events are about to be deleted from a resource. * Events are only notified whose alarm type is enabled. */ void eventsToBeRemoved(Resource&, const QList&); private: Resources(); /** Add a new ResourceType instance, with a Resource owner. * Once the resource has completed its initialisation, call * notifyNewResourceInitialised() to emit the resourceAdded() signal. * is require * @param type Newly constructed ResourceType instance, which will belong to * 'resource' if successful. On error, it will be deleted. * @param resource If type is invalid, updated to an invalid resource; * If type ID already exists, updated to the existing resource with that ID; * If type ID doesn't exist, updated to the new resource containing res. * @return true if a new resource has been created, false if invalid or already exists. */ static bool addResource(ResourceType* type, Resource& resource); /** Remove the resource with a given ID. * @note The ResourceType instance will only be deleted once all Resource * instances which refer to this ID go out of scope. */ static void removeResource(ResourceId); static void checkResourcesPopulated(); static Resources* mInstance; // the unique instance static QHash mResources; // contains all ResourceType instances with an ID static bool mCreated; // all resources have been created static bool mPopulated; // all resources have been loaded once friend class ResourceType; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(Resources::Sorting) + /*============================================================================= * Template definitions. *============================================================================*/ -template -QVector Resources::allResources(CalEvent::Type type) +template +QVector Resources::allResources(CalEvent::Type type, Sorting sorting) { const CalEvent::Types types = (type == CalEvent::EMPTY) ? CalEvent::ACTIVE | CalEvent::ARCHIVED | CalEvent::TEMPLATE : type; QVector result; - for (auto it = mResources.constBegin(); it != mResources.constEnd(); ++it) + Resource std; + if ((sorting & DefaultFirst) && type != CalEvent::EMPTY) { - const Resource& res = it.value(); - if (res.is() && (res.alarmTypes() & types)) - result += res; + std = getStandard(type); + if (std.isValid() && std.is()) + result += std; } - return result; -} + const int start = result.size(); -template -QVector Resources::allResourcesOrdered(CalEvent::Type type) -{ - Resource std = getStandard(type); - QVector result {std}; for (auto it = mResources.constBegin(); it != mResources.constEnd(); ++it) { const Resource& res = it.value(); - if (res != std && res.is() && (res.alarmTypes() & type)) + if (res != std && res.is() && (res.alarmTypes() & types)) result += res; } + + if (sorting & DisplayName) + std::sort(result.begin() + start, result.end(), [](const Resource& a, const Resource& b) { return a.displayName().compare(b.displayName(), Qt::CaseInsensitive) < 0; }); + return result; } #endif // RESOURCES_H // vim: et sw=4: diff --git a/src/resources/resourcetype.h b/src/resources/resourcetype.h index 4be08a1e..a284ee62 100644 --- a/src/resources/resourcetype.h +++ b/src/resources/resourcetype.h @@ -1,488 +1,489 @@ /* * resourcetype.h - base class for an alarm calendar resource type * Program: kalarm * Copyright © 2019-2020 David Jarvie * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef RESOURCETYPE_H #define RESOURCETYPE_H #include #include #include #include #include #include class Resource; using namespace KAlarmCal; /** Abstract base class for an alarm calendar resource type. */ class ResourceType : public QObject { Q_OBJECT public: /** Flag set in resource ID to distinguish File Resource IDs from Akonadi * Collection IDs. * This is the second-topmost bit, which is extremely unlikely to be set by * Akonadi, and does not make the ID negative. */ static const ResourceId IdFlag = 1LL << (64 - 2); static_assert(sizeof(IdFlag) == sizeof(qint64), "ResourceType::IdFlag is wrong"); /** The type of storage used by a resource. */ enum StorageType { NoStorage, File, Directory }; /** Settings change types. These may be combined. * @note A resource's location is not allowed to change, except by * deleting the resource and creating another resource with the new * location. (Note that Akonadi resources do actually allow * location change, but this is handled internally by Akonadi and * has no impact on clients.) */ enum Change { NoChange = 0, Name = 0x01, //!< The resource's display name. AlarmTypes = 0x02, //!< Alarm types contained in the resource. Enabled = 0x04, //!< Alarm types which are enabled. Standard = 0x08, //!< Alarm types which the resource is standard for. ReadOnly = 0x10, //!< The resource's read-only setting. KeepFormat = 0x20, //!< Whether the user has chosen not to convert to the current KAlarm format. UpdateFormat = 0x40, //!< The resource should now be converted to the current KAlarm format. BackgroundColour = 0x80 //!< The background colour to display the resource. }; Q_DECLARE_FLAGS(Changes, Change) /** Resource message types. */ enum class MessageType { Info, Error }; /** A shared pointer to an Resource object. */ typedef QSharedPointer Ptr; ResourceType() {} /** Constructor. * @param temporary If false, the new instance will be added to the list * of instances for lookup; * If true, it's a temporary instance not added to the list. */ explicit ResourceType(ResourceId); virtual ~ResourceType() = 0; /** Return whether the resource has a valid configuration. * Note that the resource may be unusable even if it has a valid * configuration: see failed(). */ virtual bool isValid() const = 0; /** Return whether the resource has a fatal error. * Note that failed() will return true if the configuration is invalid * (i.e. isValid() returns false). It will also return true if some other * error prevents the resource being used, e.g. if the calendar file * cannot be created. */ bool failed() const; /** Return the resource's unique ID. */ ResourceId id() const { return mId; } /** Return the resource's unique ID, as shown to the user. */ virtual ResourceId displayId() const; /** Return the type of storage used by the resource. */ virtual StorageType storageType() const = 0; /** Return the type of the resource (file, remote file, etc.) * for display purposes. * @param description true for description (e.g. "Remote file"), * false for brief label (e.g. "URL"). */ virtual QString storageTypeString(bool description) const = 0; /** Return the type description of a resource (file, remote file, etc.) * for display purposes. This is equivalent to storageTypeString(true). */ static QString storageTypeString(StorageType); /** Return the location(s) of the resource (URL, file path, etc.) */ virtual QUrl location() const = 0; /** Return the location of the resource (URL, file path, etc.) * for display purposes. */ virtual QString displayLocation() const = 0; /** Return the resource's display name. */ virtual QString displayName() const = 0; /** Return the resource's configuration identifier. This is not the * name normally displayed to the user. */ virtual QString configName() const = 0; /** Return which types of alarms the resource can contain. */ virtual CalEvent::Types alarmTypes() const = 0; /** Return whether the resource is enabled for a specified alarm type * (active, archived, template or displaying). * @param type alarm type to check for, or EMPTY to check for any type. */ bool isEnabled(CalEvent::Type type) const; /** Return which alarm types (active, archived or template) the * resource is enabled for. */ virtual CalEvent::Types enabledTypes() const = 0; /** Set the enabled/disabled state of the resource and its alarms, * for a specified alarm type (active, archived or template). The * enabled/disabled state for other alarm types is not affected. * The alarms of that type in a disabled resource are ignored, and * not displayed in the alarm list. The standard status for that type * for a disabled resource is automatically cleared. * @param type alarm type * @param enabled true to set enabled, false to set disabled. */ virtual void setEnabled(CalEvent::Type type, bool enabled) = 0; /** Set which alarm types (active, archived or template) the resource * is enabled for. * @param types alarm types */ virtual void setEnabled(CalEvent::Types types) = 0; /** Return whether the resource is configured as read-only or is * read-only on disc. */ virtual bool readOnly() const = 0; /** Return whether the resource is both enabled and fully writable for a * given alarm type, i.e. not read-only, and compatible with the current * KAlarm calendar format. * * @param type alarm type to check for, or EMPTY to check for any type. * @return 1 = fully enabled and writable, * 0 = enabled and writable except that backend calendar is in an * old KAlarm format, * -1 = read-only, disabled or incompatible format. */ virtual int writableStatus(CalEvent::Type type = CalEvent::EMPTY) const = 0; /** Return whether the resource is both enabled and fully writable for a * given alarm type, i.e. not read-only, and compatible with the current * KAlarm calendar format. * * @param type alarm type to check for, or EMPTY to check for any type. */ bool isWritable(CalEvent::Type type = CalEvent::EMPTY) const; #if 0 /** Return whether the event can be written to now, i.e. the resource is * active and read-write, and the event is in the current KAlarm format. */ virtual bool isWritable(const KAEvent&) const = 0; #endif /** Return whether the user has chosen not to update the resource's * calendar storage format. */ virtual bool keepFormat() const = 0; /** Set or clear whether the user has chosen not to update the resource's * calendar storage format. */ virtual void setKeepFormat(bool keep) = 0; /** Return the background colour used to display alarms belonging to * this resource. * @return display colour, or invalid if none specified */ virtual QColor backgroundColour() const = 0; /** Set the background colour used to display alarms belonging to this * resource. * @param colour display colour, or invalid to use the default colour */ virtual void setBackgroundColour(const QColor& colour) = 0; /** Return the foreground colour used to display alarms belonging to * this resource, for given alarm type(s). * @param types Alarm type(s), or EMPTY for the alarm types which the * resource contains. * @return display colour, or invalid if none specified */ QColor foregroundColour(CalEvent::Types types = CalEvent::EMPTY) const; /** Return whether the resource is set in the resource's config to be the * standard resource for a specified alarm type (active, archived or * template). There is no check for whether the resource is enabled, is * writable, or is the only resource set as standard. * * @note To determine whether the resource is actually the standard * resource, call the resource manager's isStandard() method. * * @param type alarm type */ virtual bool configIsStandard(CalEvent::Type type) const = 0; /** Return which alarm types (active, archived or template) the resource * is standard for, as set in its config. This is restricted to the alarm * types which the resource can contain (@see alarmTypes()). * There is no check for whether the resource is enabled, is writable, or * is the only resource set as standard. * * @note To determine what alarm types the resource is actually the standard * resource for, call the resource manager's standardTypes() method. * * @return alarm types. */ virtual CalEvent::Types configStandardTypes() const = 0; /** Set or clear the resource as the standard resource for a specified alarm * type (active, archived or template), storing the setting in the resource's * config. There is no check for whether the resource is eligible to be * set as standard, or to ensure that it is the only standard resource for * the type. * * @note To set the resource's standard status and ensure that it is * eligible and the only standard resource for the type, call * the resource manager's setStandard() method. * * @param type alarm type * @param standard true to set as standard, false to clear standard status. */ virtual void configSetStandard(CalEvent::Type type, bool standard) = 0; /** Set which alarm types (active, archived or template) the resource is * the standard resource for, storing the setting in the resource's config. * There is no check for whether the resource is eligible to be set as * standard, or to ensure that it is the only standard resource for the * types. * * @note To set the resource's standard status and ensure that it is * eligible and the only standard resource for the types, call * the resource manager's setStandard() method. * * @param types alarm types.to set as standard */ virtual void configSetStandard(CalEvent::Types types) = 0; /** Return whether the resource is in the current KAlarm format. * @see compatibility(), compatibilityVersion() */ bool isCompatible() const; /** Return whether the resource is in a different format from the * current KAlarm format, in which case it cannot be written to. * Note that isWritable() takes account of incompatible format * as well as read-only and enabled statuses. */ KACalendar::Compat compatibility() const; /** Return whether the resource is in a different format from the * current KAlarm format, in which case it cannot be written to. * Note that isWritable() takes account of incompatible format * as well as read-only and enabled statuses. * @param versionString Receives calendar's KAlarm version as a string. */ virtual KACalendar::Compat compatibilityVersion(QString& versionString) const = 0; /** Edit the resource's configuration. */ virtual void editResource(QWidget* dialogParent) = 0; /** Remove the resource. The calendar file is not removed. * Derived classes must call removeResource(ResourceId) once they have removed * the resource, in order to invalidate this instance and remove it from the * list held by Resources. * @note The instance will be invalid once it has been removed. * @return true if the resource has been removed or a removal job has been scheduled. */ virtual bool removeResource() = 0; /** Load the resource from the file, and fetch all events. * If loading is initiated, Resources::resourcePopulated() will be emitted * on completion. * Loading is not performed if the resource is disabled. * If the resource is cached, it will be loaded from the cache file (which * if @p readThroughCache is true, will first be downloaded from the resource file). * * Derived classes must implement loading in doLoad(). * * @param readThroughCache If the resource is cached, refresh the cache first. * @return true if loading succeeded or has been initiated. * false if it failed. */ virtual bool load(bool readThroughCache = true) = 0; /** Reload the resource. Any cached data is first discarded. */ virtual bool reload() = 0; /** Return whether the resource has fully loaded. * Once loading completes after the resource has initialised, this should * always return true. */ virtual bool isPopulated() const { return mLoaded; } /** Save the resource. * Saving is not performed if the resource is disabled. * If the resource is cached, it will be saved to the cache file (which * if @p writeThroughCache is true, will then be uploaded from the resource file). * @param writeThroughCache If the resource is cached, update the file * after writing to the cache. * @param force Save even if no changes have been made since last * loaded or saved. * @return true if saving succeeded or has been initiated. * false if it failed. */ virtual bool save(bool writeThroughCache = true, bool force = false) = 0; /** Return whether the resource is waiting for a save() to complete. */ virtual bool isSaving() const { return false; } /** Close the resource. This saves any unsaved data. * Saving is not performed if the resource is disabled. */ virtual void close() {} /** Return all events belonging to this resource, for enabled alarm types. */ QList events() const; /** Return the event with the given ID, provided its alarm type is enabled for * the resource. * @return The event, or invalid event if not found or alarm type is disabled. */ KAEvent event(const QString& eventId) const; using QObject::event; // prevent "hidden" warning /** Return whether the resource contains the event whose ID is given, provided * the event's alarm type is enabled for the resource. */ bool containsEvent(const QString& eventId) const; /** Add an event to the resource. */ virtual bool addEvent(const KAEvent&) = 0; /** Update an event in the resource. Its UID must be unchanged. */ virtual bool updateEvent(const KAEvent&) = 0; /** Delete an event from the resource. */ virtual bool deleteEvent(const KAEvent&) = 0; /** Called to notify the resource that an event's command error has changed. */ virtual void handleCommandErrorChange(const KAEvent&) = 0; /** Must be called to notify the resource that it is being deleted. * This is to prevent expected errors being displayed to the user. * @see isBeingDeleted */ void notifyDeletion(); /** Return whether the resource has been notified that it is being deleted. * @see notifyDeletion */ bool isBeingDeleted() const; Q_SIGNALS: /** Emitted by the all() instance, when the resource's settings have changed. */ void settingsChanged(ResourceId, Changes); /** Emitted by the all() instance, when a resource message should be displayed to the user. * @note Connections to this signal should use Qt::QueuedConnection type. * @param message Derived classes must include the resource's display name. */ void resourceMessage(ResourceId, MessageType, const QString& message, const QString& details); protected: /** Add a new ResourceType instance, with a Resource owner. * @param type Newly constructed ResourceType instance, which will belong to * 'resource' if successful. On error, it will be deleted. * @param resource If type is invalid, updated to an invalid resource; * If type ID already exists, updated to the existing resource with that ID; * If type ID doesn't exist, updated to the new resource containing res. * @return true if a new resource has been created, false if invalid or already exists. */ static bool addResource(ResourceType* type, Resource& resource); /** Remove the resource with a given ID. * @note The ResourceType instance will only be deleted once all Resource * instances which refer to this ID go out of scope. */ static void removeResource(ResourceId); /** To be called when the resource has loaded, to update the list of loaded * events for the resource. This should include both enabled and disabled * alarm types. */ void setLoadedEvents(QHash& newEvents); /** To be called when events have been created or updated, to amend them in * the resource's list. * @param notify Whether to notify added and updated events; if false, * notifyUpdatedEvents() must be called afterwards. */ void setUpdatedEvents(const QList& events, bool notify = true); /** Notify added and updated events, if setUpdatedEvents() was called with * notify = false. */ void notifyUpdatedEvents(); /** To be called when events have been deleted, to delete them from the resource's list. */ void setDeletedEvents(const QList& events); /** To be called when the loaded status of the resource has changed. */ void setLoaded(bool loaded) const; /** To be called if the resource has encountered a fatal error. * A fatal error is one that can never be recovered from. */ void setFailed(); static QString storageTypeStr(bool description, bool file, bool local); template static T* resource(Resource&); template static const T* resource(const Resource&); private: static ResourceType* data(Resource&); static const ResourceType* data(const Resource&); QHash mEvents; // all events (of ALL types) in the resource, indexed by ID QList mEventsAdded; // events added to mEvents but not yet notified QList mEventsUpdated; // events updated in mEvents but not yet notified ResourceId mId {-1}; // resource's ID, which can't be changed bool mFailed {false}; // the resource has a fatal error mutable bool mLoaded {false}; // the resource has finished loading bool mBeingDeleted {false}; // the resource is currently being deleted }; +Q_DECLARE_OPERATORS_FOR_FLAGS(ResourceType::Changes) Q_DECLARE_METATYPE(ResourceType::MessageType) /*============================================================================= * Template definitions. *============================================================================*/ template T* ResourceType::resource(Resource& res) { return qobject_cast(data(res)); } template const T* ResourceType::resource(const Resource& res) { return qobject_cast(data(res)); } #endif // RESOURCETYPE_H // vim: et sw=4: