File indexing completed on 2025-01-19 03:55:48

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2012-02-05
0007  * Description : file action manager task list
0008  *
0009  * SPDX-FileCopyrightText: 2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #ifndef DIGIKAM_FILE_ACTION_IMAGE_INFO_LIST_H
0016 #define DIGIKAM_FILE_ACTION_IMAGE_INFO_LIST_H
0017 
0018 // Qt includes
0019 
0020 #include <QAtomicPointer>
0021 #include <QList>
0022 #include <QExplicitlySharedDataPointer>
0023 #include <QDebug>
0024 
0025 // Local includes
0026 
0027 #include "iteminfo.h"
0028 #include "progressmanager.h"
0029 
0030 namespace Digikam
0031 {
0032 
0033 class FileActionProgressItemCreator
0034 {
0035 public:
0036 
0037     FileActionProgressItemCreator()                                       = default;
0038     virtual ~FileActionProgressItemCreator()                              = default;
0039     virtual ProgressItem* createProgressItem(const QString& action) const = 0;
0040     virtual void addProgressItem(ProgressItem* const item)                = 0;
0041 
0042 private:
0043 
0044     Q_DISABLE_COPY(FileActionProgressItemCreator)
0045 };
0046 
0047 // -------------------------------------------------------------------------------------------------------------------
0048 
0049 class TwoProgressItemsContainer :  public QSharedData
0050 {
0051 protected:
0052 
0053     QAtomicPointer<ProgressItem> firstItem;
0054     QAtomicPointer<ProgressItem> secondItem;
0055 
0056 protected:
0057 
0058     // Note: It is currently not safe to schedule after the framework had a chance to
0059     // advance all already scheduled items. For this, we'd need to add a mechanism (flag to block completion?)
0060 
0061     void scheduleOnProgressItem(QAtomicPointer<ProgressItem>& ptr, int total,
0062                                 const QString& action, FileActionProgressItemCreator* const creator);
0063     void advance(QAtomicPointer<ProgressItem>& ptr, int n);
0064 };
0065 
0066 // -------------------------------------------------------------------------------------------------------------------
0067 
0068 class FileActionProgressItemContainer :public QObject, public TwoProgressItemsContainer
0069 {
0070     Q_OBJECT
0071 
0072 public:
0073 
0074     FileActionProgressItemContainer();
0075     void schedulingForDB(int numberOfInfos, const QString& action, FileActionProgressItemCreator* const creator);
0076     void dbProcessed(int numberOfInfos);
0077     void dbFinished();
0078     void schedulingForWrite(int numberOfInfos, const QString& action, FileActionProgressItemCreator* const creator);
0079     void written(int numberOfInfos);
0080     void finishedWriting();
0081 
0082 Q_SIGNALS:
0083 
0084     void signalWrittingDone();
0085 
0086 private:
0087 
0088     // Disable
0089     explicit FileActionProgressItemContainer(QObject*);
0090 };
0091 
0092 // -------------------------------------------------------------------------------------------------------------------
0093 
0094 class FileActionItemInfoList : public QList<ItemInfo>
0095 {
0096 public:
0097 
0098     FileActionItemInfoList()
0099     {
0100     }
0101 
0102     FileActionItemInfoList(const FileActionItemInfoList& copy)
0103         : QList    (copy),
0104           container(copy.container)
0105     {
0106     }
0107 
0108     ~FileActionItemInfoList()
0109     {
0110     }
0111 
0112 public:
0113 
0114     static FileActionItemInfoList create(const QList<ItemInfo>& list);
0115     static FileActionItemInfoList continueTask(const QList<ItemInfo>& list,
0116                                                FileActionProgressItemContainer* const container);
0117 
0118     FileActionProgressItemContainer* progress() const
0119     {
0120         return container.data();
0121     }
0122 
0123     /// before sending to db worker
0124     void schedulingForDB(int numberOfInfos,
0125                          const QString& action,
0126                          FileActionProgressItemCreator* const creator)
0127     {
0128         progress()->schedulingForDB(numberOfInfos, action, creator);
0129     }
0130 
0131     void schedulingForDB(const QString& action,
0132                          FileActionProgressItemCreator* const creator)
0133     {
0134         schedulingForDB(size(), action, creator);
0135     }
0136 
0137     /// db worker progress info
0138     void dbProcessedOne()               const { dbProcessed(1);                         }
0139     void dbProcessed(int numberOfInfos) const { progress()->dbProcessed(numberOfInfos); }
0140     void dbFinished()                   const { progress()->dbFinished();               }
0141 
0142     /// db worker calls this before sending to file worker
0143     void schedulingForWrite(int numberOfInfos,
0144                             const QString& action,
0145                             FileActionProgressItemCreator* const creator) const
0146     {
0147         progress()->schedulingForWrite(numberOfInfos, action, creator);
0148     }
0149 
0150     void schedulingForWrite(const QString& action,
0151                             FileActionProgressItemCreator* const creator) const
0152     {
0153         schedulingForWrite(size(), action, creator);
0154     }
0155 
0156     /// file worker calls this when finished
0157     void writtenToOne()             const    { written(1);                             }
0158     void written(int numberOfInfos) const    { progress()->written(numberOfInfos);     }
0159     void finishedWriting()          const    { progress()->finishedWriting();          }
0160 
0161 public:
0162 
0163     QExplicitlySharedDataPointer<FileActionProgressItemContainer> container;
0164 
0165 private:
0166 
0167     explicit FileActionItemInfoList(const QList<ItemInfo>& list)
0168         : QList<ItemInfo>(list)
0169     {
0170     }
0171 };
0172 
0173 } // namespace Digikam
0174 
0175 Q_DECLARE_METATYPE(Digikam::FileActionItemInfoList)
0176 
0177 #endif // DIGIKAM_FILE_ACTION_IMAGE_INFO_LIST_H