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

0001 /*
0002  *  fileresourceconfigmanager.h  -  config manager for resources accessed via file system
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2020-2023 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include "resource.h"
0012 #include "fileresource.h"
0013 #include "fileresourcesettings.h"
0014 
0015 #include <QSharedPointer>
0016 
0017 class KConfig;
0018 
0019 /** Manager for configuration files for file system resources.
0020  *  Reads configuration files and creates resources at startup, and updates
0021  *  configuration files when resource configurations change.
0022  */
0023 class FileResourceConfigManager
0024 {
0025 public:
0026     /** Returns the unique instance, and creates it if necessary.
0027      *  Call createResources() to read the resource configuration and create
0028      *  the resources defined in it.
0029      */
0030     static FileResourceConfigManager* instance();
0031 
0032     FileResourceConfigManager(const FileResourceConfigManager&) = delete;
0033     FileResourceConfigManager& operator=(const FileResourceConfigManager&) = delete;
0034 
0035     /** Destructor.
0036      *  Writes the 'kalarmresources' config file.
0037      */
0038     ~FileResourceConfigManager();
0039 
0040     /** Reads the 'kalarmresources' config file and creates the resources
0041      *  defined in it. If called more than once, this method will do nothing.
0042      */
0043     static void createResources(QObject* parent);
0044 
0045     /** Writes the 'kalarmresources' config file. */
0046     static void writeConfig();
0047 
0048     /** Return the IDs of all file system calendar resources. */
0049     static QList<ResourceId> resourceIds();
0050 
0051     /** Create a new file system calendar resource with the given settings.
0052      *  Use writeConfig() to write the updated config file.
0053      *  @param settings  The resource's configuration; updated with the unique
0054      *                   ID for the resource.
0055      */
0056     static Resource addResource(FileResourceSettings::Ptr& settings);
0057 
0058     /** Delete a specified file system calendar resource and its settings.
0059      *  The calendar file is not removed.
0060      *  Use writeConfig() to write the updated config file.
0061      *  To be called only from FileResource, which will delete the resource
0062      *  from Resources.
0063      */
0064     static bool removeResource(Resource&);
0065 
0066     /** Return the available file system resource types handled by the manager. */
0067     static QList<ResourceType::Storage> storageTypes();
0068 
0069 private:
0070     FileResourceConfigManager();
0071     int findResourceGroup(ResourceId id) const;
0072     static QString groupName(int groupIndex);
0073     static Resource createResource(FileResourceSettings::Ptr&);
0074 
0075     struct ResourceData
0076     {
0077         Resource                 resource;
0078         FileResourceSettings::Ptr settings;
0079 
0080         ResourceData() = default;
0081         ResourceData(const Resource &r, FileResourceSettings::Ptr s) : resource(r), settings(s) {}
0082         ResourceData(const ResourceData&) = default;
0083         ResourceData& operator=(const ResourceData&) = default;
0084     };
0085     static FileResourceConfigManager* mInstance;
0086     KConfig*                        mConfig;
0087     QHash<ResourceId, ResourceData> mResources;      // resource ID, resource & its settings
0088     QMap<int, ResourceId>           mConfigGroups;   // group name index, resource ID
0089     ResourceId                      mLastId {0};     // last ID which was allocated to any resource
0090     int                             mCreated {0};    // 1 = createResources() has been run, 2 = completed
0091 };
0092 
0093 // vim: et sw=4: