File indexing completed on 2024-04-28 04:20:53

0001 // SPDX-FileCopyrightText: 2012-2022 The KPhotoAlbum Development Team
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #include "JobInterface.h"
0006 
0007 #include "JobManager.h"
0008 #include "Logging.h"
0009 
0010 /**
0011   \class BackgroundTaskManager::JobInterface
0012   \brief Interfaces for jobs to be executed using \ref BackgroundTaskManager::JobManager
0013 
0014   Each job must override \ref execute, and must Q_EMIT the signal completed.
0015   Emitting the signal is crusial, as the JobManager will otherwise stall.
0016 */
0017 
0018 BackgroundTaskManager::JobInterface::JobInterface(BackgroundTaskManager::Priority priority)
0019     : JobInfo(priority)
0020     , m_dependencies(0)
0021 {
0022     qCDebug(BackgroundTaskManagerLog) << "Created Job #" << jobIndex();
0023     connect(this, &JobInterface::completed, this, &JobInterface::stop);
0024 }
0025 
0026 BackgroundTaskManager::JobInterface::~JobInterface()
0027 {
0028 }
0029 
0030 void BackgroundTaskManager::JobInterface::start()
0031 {
0032     qCDebug(BackgroundTaskManagerLog, "Starting Job (#%d): %s %s", jobIndex(), qPrintable(title()), qPrintable(details()));
0033     JobInfo::start();
0034     execute();
0035 }
0036 
0037 void BackgroundTaskManager::JobInterface::addDependency(BackgroundTaskManager::JobInterface *job)
0038 {
0039     m_dependencies++;
0040     connect(job, SIGNAL(completed()), this, SLOT(dependedJobCompleted()));
0041 }
0042 
0043 void BackgroundTaskManager::JobInterface::dependedJobCompleted()
0044 {
0045     m_dependencies--;
0046     if (m_dependencies == 0)
0047         BackgroundTaskManager::JobManager::instance()->addJob(this);
0048 }
0049 
0050 // vi:expandtab:tabstop=4 shiftwidth=4:
0051 
0052 #include "moc_JobInterface.cpp"