Commit fced1a0b authored by Eric Jiang's avatar Eric Jiang Committed by Jean-Baptiste Mardelle
Browse files

Auto-call taskManager.taskDone() when run() ends

Create AbstractTaskDone at the beginning of run() so that it is
destroyed when it goes out of scope (i.e. run() finishes). When
AbstractTaskDone is destroyed, it calls

pCore->taskManager.taskDone(...)

so that we don't need to manually insert calls to taskDone() before
every return statement. By calling it at the very end of the function,
we ensure that taskDone() is not called too early, which can delete the
AbstractTask before it actually finishes.
parent 8588b948
......@@ -120,3 +120,7 @@ void AbstractTask::setPreferredPriority(qint64 pid)
}
#endif
}
AbstractTaskDone::~AbstractTaskDone() {
pCore->taskManager.taskDone(m_cid, m_task);
}
......@@ -58,7 +58,21 @@ private:
JOBTYPE m_type;
int m_priority;
void cancelJob(bool softDelete = false);
signals:
void jobCanceled();
};
/**
* @brief When destroyed, notifies the taskManager that this task is done.
*/
class AbstractTaskDone {
public:
AbstractTaskDone(int cid, AbstractTask *task)
: m_cid(cid)
, m_task(task) {}
~AbstractTaskDone();
private:
int m_cid;
AbstractTask *m_task;
};
......@@ -52,8 +52,8 @@ void AudioLevelsTask::start(const ObjectId &owner, QObject *object, bool force)
void AudioLevelsTask::run()
{
AbstractTaskDone whenFinished(m_owner.second, this);
if (m_isCanceled || pCore->taskManager.isBlocked()) {
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QMutexLocker lock(&m_runMutex);
......@@ -62,12 +62,10 @@ void AudioLevelsTask::run()
auto binClip = pCore->projectItemModel()->getClipByBinID(QString::number(m_owner.second));
if (binClip == nullptr) {
// Clip was deleted
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
if (binClip->audioChannels() == 0 || binClip->audioThumbCreated()) {
// nothing to do
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
std::shared_ptr<Mlt::Producer> producer = binClip->originalProducer();
......@@ -75,7 +73,6 @@ void AudioLevelsTask::run()
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection,
Q_ARG(QString, i18n("Audio thumbs: cannot open file %1", QFileInfo(binClip->url()).fileName())),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
int lengthInFrames = producer->get_length(); // Multiply this if we want more than 1 sample per frame
......@@ -84,7 +81,6 @@ void AudioLevelsTask::run()
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection,
Q_ARG(QString, i18n("Audio thumbs: unknown file length for %1", QFileInfo(binClip->url()).fileName())),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
int frequency = binClip->audioInfo()->samplingRate();
......@@ -140,7 +136,6 @@ void AudioLevelsTask::run()
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection,
Q_ARG(QString, i18n("Audio thumbs: cannot open file %1", producer->get("resource"))),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
audioProducer->set("video_index", "-1");
......@@ -243,6 +238,5 @@ void AudioLevelsTask::run()
// Audio was cached, ensure the bin thumbnail is loaded
QMetaObject::invokeMethod(m_object, "updateAudioThumbnail", Q_ARG(bool, true));
}
pCore->taskManager.taskDone(m_owner.second, this);
QMetaObject::invokeMethod(m_object, "updateJobProgress");
}
......@@ -97,8 +97,8 @@ void CacheTask::generateThumbnail(std::shared_ptr<ProjectClip> binClip)
void CacheTask::run()
{
AbstractTaskDone whenFinished(m_owner.second, this);
if (m_isCanceled || pCore->taskManager.isBlocked()) {
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QMutexLocker lock(&m_runMutex);
......@@ -106,6 +106,5 @@ void CacheTask::run()
if (binClip) {
generateThumbnail(binClip);
}
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
......@@ -307,6 +307,7 @@ void ClipLoadTask::generateThumbnail(std::shared_ptr<ProjectClip> binClip, std::
void ClipLoadTask::run()
{
AbstractTaskDone whenFinished(m_owner.second, this);
if (m_isCanceled.loadAcquire() == 1 || pCore->taskManager.isBlocked()) {
abort();
return;
......@@ -324,8 +325,6 @@ void ClipLoadTask::run()
}
if (m_isCanceled.loadAcquire() == 1 || pCore->taskManager.isBlocked()) {
abort();
} else {
pCore->taskManager.taskDone(m_owner.second, this);
}
return;
}
......@@ -745,7 +744,6 @@ void ClipLoadTask::run()
abort();
return;
}
pCore->taskManager.taskDone(m_owner.second, this);
}
void ClipLoadTask::abort()
......@@ -754,7 +752,6 @@ void ClipLoadTask::abort()
if (pCore->taskManager.isBlocked()) {
return;
}
pCore->taskManager.taskDone(m_owner.second, this);
Fun undo = []() { return true; };
Fun redo = []() { return true; };
if (!m_softDelete) {
......
......@@ -186,8 +186,8 @@ void CutTask::start(const ObjectId &owner, int in, int out, QObject *object, boo
void CutTask::run()
{
AbstractTaskDone whenFinished(m_owner.second, this);
if (m_isCanceled || pCore->taskManager.isBlocked()) {
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QMutexLocker lock(&m_runMutex);
......@@ -208,14 +208,12 @@ void CutTask::run()
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("No producer for this clip.")),
Q_ARG(int, int(KMessageWidget::Warning)));
m_errorMessage.append(i18n("No producer for this clip."));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
if (QFileInfo(m_destination).absoluteFilePath() == QFileInfo(url).absoluteFilePath()) {
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("You cannot overwrite original clip.")),
Q_ARG(int, int(KMessageWidget::Warning)));
m_errorMessage.append(i18n("You cannot overwrite original clip."));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QStringList params = {QStringLiteral("-y"),
......@@ -263,7 +261,6 @@ void CutTask::run()
Q_ARG(int, int(KMessageWidget::Warning)), Q_ARG(QString, m_logDetails));
}
}
pCore->taskManager.taskDone(m_owner.second, this);
}
void CutTask::processLogInfo()
......
......@@ -51,8 +51,8 @@ void FilterTask::start(const ObjectId &owner, const QString &binId, const std::w
void FilterTask::run()
{
AbstractTaskDone whenFinished(m_owner.second, this);
if (m_isCanceled || pCore->taskManager.isBlocked()) {
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QMutexLocker lock(&m_runMutex);
......@@ -68,7 +68,6 @@ void FilterTask::run()
if (url.isEmpty()) {
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("No producer for this clip.")),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
if (KdenliveSettings::gpu_accel()) {
......@@ -85,7 +84,6 @@ void FilterTask::run()
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("Cannot open file %1", binClip->url())),
Q_ARG(int, int(KMessageWidget::Warning)));
}
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
if (m_outPoint == -1) {
......@@ -111,7 +109,6 @@ void FilterTask::run()
// Clip was removed or something went wrong, Notify user?
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("Cannot open source.")),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
length = producer->get_playtime();
......@@ -136,7 +133,6 @@ void FilterTask::run()
if (!consumer->is_valid()) {
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("Cannot create consumer.")),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
......@@ -149,7 +145,6 @@ void FilterTask::run()
if (!filter.is_valid()) {
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("Cannot create filter %1", m_filterName)),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
......@@ -226,7 +221,6 @@ void FilterTask::run()
if (auto ptr = m_model.lock()) {
QMetaObject::invokeMethod(ptr.get(), "setProgress", Q_ARG(int, 100));
}
pCore->taskManager.taskDone(m_owner.second, this);
if (m_isCanceled || !result) {
if (!m_isCanceled) {
QMetaObject::invokeMethod(pCore.get(), "displayBinLogMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("Failed to filter source.")),
......
......@@ -42,8 +42,8 @@ void ProxyTask::start(const ObjectId &owner, QObject *object, bool force)
void ProxyTask::run()
{
AbstractTaskDone whenFinished(m_owner.second, this);
if (m_isCanceled || pCore->taskManager.isBlocked()) {
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QMutexLocker lock(&m_runMutex);
......@@ -57,7 +57,6 @@ void ProxyTask::run()
if (binClip->getProducerIntProperty(QStringLiteral("_overwriteproxy")) == 0 && fInfo.exists() && fInfo.size() > 0) {
// Proxy clip already created
m_progress = 100;
pCore->taskManager.taskDone(m_owner.second, this);
QMetaObject::invokeMethod(m_object, "updateJobProgress");
QMetaObject::invokeMethod(binClip.get(), "updateProxyProducer", Qt::QueuedConnection, Q_ARG(QString, dest));
return;
......@@ -192,7 +191,6 @@ void ProxyTask::run()
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("Cannot load image %1.", source)),
Q_ARG(int, int(KMessageWidget::Warning)));
m_progress = 100;
pCore->taskManager.taskDone(m_owner.second, this);
QMetaObject::invokeMethod(m_object, "updateJobProgress");
return;
}
......@@ -242,7 +240,6 @@ void ProxyTask::run()
}
result = true;
m_progress = 100;
pCore->taskManager.taskDone(m_owner.second, this);
QMetaObject::invokeMethod(m_object, "updateJobProgress");
return;
} else {
......@@ -254,7 +251,6 @@ void ProxyTask::run()
Q_ARG(int, int(KMessageWidget::Warning)));
result = true;
m_progress = 100;
pCore->taskManager.taskDone(m_owner.second, this);
QMetaObject::invokeMethod(m_object, "updateJobProgress");
return;
}
......@@ -370,7 +366,6 @@ void ProxyTask::run()
Q_ARG(int, int(KMessageWidget::Warning)), Q_ARG(QString, m_logDetails));
}
}
pCore->taskManager.taskDone(m_owner.second, this);
QMetaObject::invokeMethod(m_object, "updateJobProgress");
return;
}
......
......@@ -101,8 +101,8 @@ void SceneSplitTask::start(QObject *object, bool force)
void SceneSplitTask::run()
{
AbstractTaskDone whenFinished(m_owner.second, this);
if (m_isCanceled || pCore->taskManager.isBlocked()) {
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QMutexLocker lock(&m_runMutex);
......@@ -115,7 +115,6 @@ void SceneSplitTask::run()
// This job can only process video files
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("Cannot analyse this clip type.")),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
qDebug() << "=== ABORT 1";
return;
}
......@@ -124,7 +123,6 @@ void SceneSplitTask::run()
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection,
Q_ARG(QString, i18n("FFmpeg not found, please set path in Kdenlive's settings Environment.")),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
qDebug() << "=== ABORT 2";
return;
}
......@@ -165,7 +163,6 @@ void SceneSplitTask::run()
// remove temporary playlist if it exists
m_progress = 100;
pCore->taskManager.taskDone(m_owner.second, this);
QMetaObject::invokeMethod(m_object, "updateJobProgress");
if (result && !m_isCanceled) {
qDebug() << "========================\n\nGOR RESULTS: " << m_results << "\n\n=========";
......
......@@ -165,13 +165,13 @@ void SpeedTask::start(QObject *object, bool force)
void SpeedTask::run()
{
AbstractTaskDone whenFinished(m_owner.second, this);
if (m_isCanceled || pCore->taskManager.isBlocked()) {
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QMutexLocker lock(&m_runMutex);
m_running = true;
qDebug() << " + + + + + + + + STARTING STAB TASK";
qDebug() << " + + + + + + + + STARTING SPEED TASK";
QString url;
auto binClip = pCore->projectItemModel()->getClipByBinID(m_binId);
......@@ -182,7 +182,6 @@ void SpeedTask::run()
if (url.isEmpty()) {
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("No producer for this clip.")),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
producerArgs << QString("timewarp:%1:%2").arg(m_speed).arg(url);
......@@ -195,7 +194,6 @@ void SpeedTask::run()
} else {
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("No producer for this clip.")),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
// Filter applied on a track of master producer, leave config to source job
// We are on master or track, configure producer accordingly
......@@ -208,7 +206,6 @@ void SpeedTask::run()
if ((producer == nullptr) || !producer->is_valid()) {
// Clip was removed or something went wrong, Notify user?
m_errorMessage.append(i18n("Invalid clip"));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}*/
}
......@@ -241,7 +238,6 @@ void SpeedTask::run()
bool result = m_jobProcess->exitStatus() == QProcess::NormalExit;
m_progress = 100;
QMetaObject::invokeMethod(m_object, "updateJobProgress");
pCore->taskManager.taskDone(m_owner.second, this);
if (m_isCanceled || !result) {
if (!m_isCanceled) {
QMetaObject::invokeMethod(pCore.get(), "displayBinLogMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("Failed to create speed clip.")),
......
......@@ -95,8 +95,8 @@ void StabilizeTask::start(QObject *, bool force)
void StabilizeTask::run()
{
AbstractTaskDone whenFinished(m_owner.second, this);
if (m_isCanceled || pCore->taskManager.isBlocked()) {
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QMutexLocker lock(&m_runMutex);
......@@ -112,7 +112,6 @@ void StabilizeTask::run()
if (url.isEmpty()) {
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("No producer for this clip.")),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
producerArgs << url;
......@@ -130,7 +129,6 @@ void StabilizeTask::run()
// TODO
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("No producer for this clip.")),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
/*if (m_owner.first == ObjectType::Master) {
producer = pCore->getMasterProducerInstance();
......@@ -140,7 +138,6 @@ void StabilizeTask::run()
if ((producer == nullptr) || !producer->is_valid()) {
// Clip was removed or something went wrong, Notify user?
m_errorMessage.append(i18n("Invalid clip"));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}*/
}
......@@ -183,7 +180,6 @@ void StabilizeTask::run()
bool result = m_jobProcess->exitStatus() == QProcess::NormalExit;
m_progress = 100;
QMetaObject::invokeMethod(m_object, "updateJobProgress");
pCore->taskManager.taskDone(m_owner.second, this);
if (m_isCanceled || !result) {
if (!m_isCanceled) {
QMetaObject::invokeMethod(pCore.get(), "displayBinLogMessage", Qt::QueuedConnection, Q_ARG(QString, i18n("Failed to stabilize.")),
......
......@@ -56,8 +56,8 @@ void TranscodeTask::start(const ObjectId &owner, const QString &suffix, const QS
void TranscodeTask::run()
{
AbstractTaskDone whenFinished(m_owner.second, this);
if (m_isCanceled || pCore->taskManager.isBlocked()) {
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QMutexLocker lock(&m_runMutex);
......@@ -90,7 +90,6 @@ void TranscodeTask::run()
if (transcoderExt.isEmpty()) {
qDebug() << "// INVALID TRANSCODING PROFILE";
m_progress = 100;
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
QFileInfo finfo(source);
......@@ -182,7 +181,6 @@ void TranscodeTask::run()
QMetaObject::invokeMethod(pCore.get(), "displayBinMessage", Qt::QueuedConnection,
Q_ARG(QString, i18n("FFmpeg not found, please set path in Kdenlive's settings Environment")),
Q_ARG(int, int(KMessageWidget::Warning)));
pCore->taskManager.taskDone(m_owner.second, this);
return;
}
m_jobDuration = int(binClip->duration().seconds());
......@@ -224,7 +222,6 @@ void TranscodeTask::run()
destUrl.append(transcoderExt);
// remove temporary playlist if it exists
m_progress = 100;
pCore->taskManager.taskDone(m_owner.second, this);
QMetaObject::invokeMethod(m_object, "updateJobProgress");
if (result) {
if (QFileInfo(destUrl).size() == 0) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment