summaryrefslogtreecommitdiffstats
path: root/daemon/Task.cpp
blob: 322fe259fb18a1299403611a440f321f7acba0e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "Task.h"
#include <QtConcurrentRun>
#include <QDebug>

Task::Task(bool threaded, QObject *parent) : QObject(parent),
	m_isRunning(false),
	m_currentMovie(0)
{
	if (threaded) {
		m_watcher = new QFutureWatcher<bool>;
		m_watcher->setParent(this);
		connect(m_watcher, SIGNAL(finished()), this, SLOT(finished()));
	} else
		m_watcher = 0;
}
void Task::runTask(Movie *movie)
{
	if (!canRunTask(movie)) {
		qDebug() << this->objectName() << "is already running while attempting to run on" << movie->title();
		return;
	}
	m_currentMovie = movie;
	m_isRunning = true;
	if (m_watcher)
		m_watcher->setFuture(QtConcurrent::run(this, &Task::executeTask, movie));
	else
		executeTask(movie);
}
void Task::finished()
{
	setCompleted(m_watcher->future().result());
}
void Task::terminate()
{
	kill();
	if (!m_watcher)
		setCompleted(false);
}
void Task::setCompleted(bool result)
{
	cleanUp(result);
	m_isRunning = false;
	m_currentMovie = 0;
	emit completed(result);
}
bool Task::isRunning() const
{
	return m_isRunning;
}
Movie* Task::currentMovie() const
{
	return m_currentMovie;
}