File indexing completed on 2024-12-01 07:34:26

0001 /*
0002 SPDX-FileCopyrightText: 2021 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003 This file is part of Kdenlive. See www.kdenlive.org.
0004 
0005 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 
0009 #pragma once
0010 
0011 #include "abstracttask.h"
0012 #include "definitions.h"
0013 
0014 #include <QAbstractListModel>
0015 #include <QFutureWatcher>
0016 #include <QObject>
0017 #include <QReadWriteLock>
0018 #include <QThreadPool>
0019 #include <QUuid>
0020 #include <map>
0021 #include <memory>
0022 #include <unordered_map>
0023 #include <vector>
0024 
0025 class AbstractTask;
0026 
0027 enum class TaskManagerStatus { NoJob, Pending, Running, Finished, Canceled };
0028 Q_DECLARE_METATYPE(TaskManagerStatus)
0029 
0030 /** @class TaskManager
0031     @brief This class is responsible for clip jobs management.
0032  */
0033 class TaskManager : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 public:
0038     explicit TaskManager(QObject *parent);
0039     ~TaskManager() override;
0040 
0041     /** @brief Discard specific job type for a clip.
0042      *  @param owner the owner item for this task
0043      *  @param type The type of job that you want to abort, leave to NOJOBTYPE to abort all jobs
0044      */
0045     void discardJobs(const ObjectId &owner, AbstractTask::JOBTYPE type = AbstractTask::NOJOBTYPE, bool softDelete = false, const QVector<AbstractTask::JOBTYPE> exceptions = {});
0046     void discardJob(const ObjectId &owner, const QUuid &uuid);
0047 
0048     /** @brief Check if there is a pending / running job a clip.
0049      *  @param owner the owner item for this task
0050      *  @param type The type of job that you want to query
0051      */
0052     bool hasPendingJob(const ObjectId &owner, AbstractTask::JOBTYPE type = AbstractTask::NOJOBTYPE) const;
0053     
0054     TaskManagerStatus jobStatus(const ObjectId &owner) const;
0055 
0056     /** @brief return the progress of a given job on a given clip */
0057     int getJobProgressForClip(const ObjectId &owner);
0058 
0059     /** @brief Add a task in the list and push it on the thread pool */
0060     void startTask(int ownerId, AbstractTask *task);
0061 
0062     /** @brief Remove a finished task */
0063     void taskDone(int cid, AbstractTask *task);
0064     
0065     /** @brief Update the number of concurrent jobs allowed */
0066     void updateConcurrency();
0067 
0068     /** @brief We are aborting all tasks and don't want them to send any updates */
0069     bool isBlocked() const;
0070 
0071     /** @brief The clip currently opened in Clip Monitor (to display clip jobs) */
0072     int displayedClip;
0073 
0074     /** @brief Allow starting new tasks */
0075     void unBlock();
0076 
0077 public Q_SLOTS:
0078     /** @brief Discard all running jobs. */
0079     void slotCancelJobs(bool leaveBlocked = false, const QVector<AbstractTask::JOBTYPE> exceptions = {});
0080 
0081 private Q_SLOTS:
0082     /** @brief Update number of running jobs. */
0083     void updateJobCount();
0084 
0085 private:
0086     QThreadPool m_taskPool;
0087     QThreadPool m_transcodePool;
0088     std::unordered_map<int, std::vector<AbstractTask*> > m_taskList;
0089     mutable QReadWriteLock m_tasksListLock;
0090     bool m_blockUpdates;
0091 
0092 Q_SIGNALS:
0093     void jobCount(int);
0094     void detailedProgress(const ObjectId &owner, const QStringList &, const QList<int> &, const QStringList &);
0095 };