File indexing completed on 2025-04-27 03:58:06

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-01-16
0007  * Description : image file IO threaded interface.
0008  *
0009  * SPDX-FileCopyrightText: 2005-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  * SPDX-FileCopyrightText: 2005-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef DIGIKAM_MANAGED_LOAD_SAVE_THREAD_H
0017 #define DIGIKAM_MANAGED_LOAD_SAVE_THREAD_H
0018 
0019 // Local includes
0020 
0021 #include "loadsavethread.h"
0022 
0023 namespace Digikam
0024 {
0025 
0026 class LoadingTask;
0027 class LoadSaveTask;
0028 
0029 class DIGIKAM_EXPORT ManagedLoadSaveThread : public LoadSaveThread
0030 {
0031     Q_OBJECT
0032 
0033 public:
0034 
0035     enum LoadingPolicy
0036     {
0037         /**
0038          * Load image immediately, remove and stop all previous loading tasks.
0039          */
0040         LoadingPolicyFirstRemovePrevious,
0041         /**
0042          * Prepend loading in front of all other tasks, but wait for the current task to finish.
0043          * No other tasks will be removed, preloading tasks will be stopped and postponed.
0044          */
0045         LoadingPolicyPrepend,
0046         /**
0047          * Prepend in front of all other tasks (not touching the current task).
0048          * Do not check for duplicate tasks, do not check for preloading tasks.
0049          */
0050         LoadingPolicySimplePrepend,
0051         /**
0052          * Append loading task to the end of the list, but in front of all preloading tasks.
0053          * No other tasks will be removed, preloading tasks will be stopped and postponed.
0054          * This is similar to the simple load() operation from LoadSaveThread, except for the
0055          * special care taken for preloading.
0056          */
0057         LoadingPolicyAppend,
0058         /**
0059          * Append to the lists of tasks.
0060          * Do not check for duplicate tasks, do not check for preloading tasks.
0061          */
0062         LoadingPolicySimpleAppend,
0063         /**
0064          * Preload image, i.e. load it with low priority when no other tasks are scheduled.
0065          * All other tasks will take precedence, and preloading tasks will be stopped and
0066          * postponed when another task is added.
0067          * No progress info will be sent for preloaded images
0068          */
0069         LoadingPolicyPreload
0070     };
0071 
0072     enum TerminationPolicy
0073     {
0074         /**
0075          * Wait for saving tasks, stop and remove loading tasks
0076          * This is the default.
0077          */
0078         TerminationPolicyTerminateLoading,
0079         /**
0080          * Wait for loading and saving tasks, stop and remove preloading tasks
0081          */
0082         TerminationPolicyTerminatePreloading,
0083         /**
0084          * Wait for all pending tasks
0085          */
0086         TerminationPolicyWait,
0087         /**
0088          * Stop all pending tasks
0089          */
0090         TerminationPolicyTerminateAll
0091     };
0092 
0093     enum LoadingTaskFilter
0094     {
0095         /**
0096          * filter all loading tasks
0097          */
0098         LoadingTaskFilterAll,
0099         /**
0100          * filter only tasks with preloading policy
0101          */
0102         LoadingTaskFilterPreloading
0103     };
0104 
0105     /**
0106      * used by SharedLoadSaveThread only
0107      */
0108     enum LoadingMode
0109     {
0110         /**
0111          * no sharing of loading process, no caching of image
0112          */
0113         LoadingModeNormal,
0114         /**
0115          * loading process is shared, image is cached
0116          */
0117         LoadingModeShared
0118     };
0119 
0120 public:
0121 
0122     /**
0123      * Termination is controlled by setting the TerminationPolicy
0124      * Default is TerminationPolicyTerminateLoading
0125      */
0126     explicit ManagedLoadSaveThread(QObject* const parent = nullptr);
0127     ~ManagedLoadSaveThread() override;
0128 
0129     /**
0130      * Append a task to load the given file to the task list.
0131      * If there is already a task for the given file, it will possibly be rescheduled,
0132      * but no second task will be added.
0133      * Only loading tasks will - if required by the policy - be stopped or removed,
0134      * saving tasks will not be touched.
0135      */
0136     void load(const LoadingDescription& description);
0137     void load(const LoadingDescription& description, LoadingPolicy policy);
0138 
0139     /**
0140      * Stop and remove tasks filtered by filePath and policy.
0141      * If filePath isNull, applies to all file paths.
0142      */
0143     void stopLoading(const QString& filePath = QString(),
0144                      LoadingTaskFilter filter = LoadingTaskFilterAll);
0145 
0146     /**
0147      * Same than previous method, but Stop and remove tasks filtered by LoadingDescription.
0148      */
0149     void stopLoading(const LoadingDescription& desc,
0150                      LoadingTaskFilter filter = LoadingTaskFilterAll);
0151 
0152     /**
0153      * Stop and remove saving tasks filtered by filePath.
0154      * If filePath isNull, applies to all file paths.
0155      */
0156     void stopSaving(const QString& filePath = QString());
0157 
0158     void stopAllTasks();
0159 
0160     /**
0161      * Append a task to save the image to the task list
0162      */
0163     void save(const DImg& image, const QString& filePath, const QString& format);
0164 
0165     void              setTerminationPolicy(TerminationPolicy terminationPolicy);
0166     TerminationPolicy terminationPolicy()                                               const;
0167 
0168     /**
0169      * Set the loading policy.
0170      * Default is LoadingPolicyAppend.
0171      * You can override the default value for each operation.
0172      */
0173     void          setLoadingPolicy(LoadingPolicy policy);
0174     LoadingPolicy loadingPolicy()                                                       const;
0175 
0176 protected:
0177 
0178     void shutDown();
0179 
0180     void load(const LoadingDescription& description, LoadingMode loadingMode,
0181               AccessMode mode = AccessModeReadWrite);
0182 
0183     void load(const LoadingDescription& description, LoadingMode loadingMode,
0184               LoadingPolicy policy, AccessMode mode = AccessModeReadWrite);
0185 
0186     void loadPreview(const LoadingDescription& description, LoadingPolicy policy);
0187     void loadThumbnail(const LoadingDescription& description);
0188 
0189     void preloadThumbnail(const LoadingDescription& description);
0190     void preloadThumbnailGroup(const QList<LoadingDescription>& descriptions);
0191     void prependThumbnailGroup(const QList<LoadingDescription>& descriptions);
0192 
0193 protected:
0194 
0195     LoadingPolicy     m_loadingPolicy;
0196     TerminationPolicy m_terminationPolicy;
0197 
0198 private:
0199 
0200     LoadingTask* checkLoadingTask(LoadSaveTask* const task, LoadingTaskFilter filter)   const;
0201     LoadingTask* findExistingTask(const LoadingDescription& description)                const;
0202     LoadingTask* createLoadingTask(const LoadingDescription& description,
0203                                    bool preloading,
0204                                    LoadingMode loadingMode,
0205                                    AccessMode accessMode);
0206 
0207     void removeLoadingTasks(const LoadingDescription& description, LoadingTaskFilter filter);
0208 
0209 private:
0210 
0211     // Disable
0212     ManagedLoadSaveThread(const ManagedLoadSaveThread&)            = delete;
0213     ManagedLoadSaveThread& operator=(const ManagedLoadSaveThread&) = delete;
0214 };
0215 
0216 } // namespace Digikam
0217 
0218 #endif // DIGIKAM_MANAGED_LOAD_SAVE_THREAD_H