summaryrefslogtreecommitdiffstats
path: root/video.cpp
blob: 39d1e8a14d8deaed4aa49c69eed861315edd2345 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "video.h"
#include "job.h"
#include "dvdimagejob.h"
#include "encodemp4job.h"
#include "uploadjob.h"
#include "titleloadjob.h"
#include "videogui.h"
#include "dvddrive.h"
#include <QDir>
#include <QSettings>

Video::Video(QString title, QObject *parent) :
		QObject(parent),
		m_jobsCompleted(QBitArray(6)),
		m_jobsInProgress(QBitArray(6)),
		m_dvdTitle(1)
{
	QString topKey = QString("Videos/%1").arg(title.replace(QChar('/'), QChar('-')));
	m_settingsKey = topKey.append(QLatin1String("/%1"));
	QSettings settings;
	if (settings.contains(topKey)) {
		m_jobsCompleted = settings.value(m_settingsKey.arg("Jobs Completed")).toBitArray();
		if (m_jobsCompleted.size() != 6)
			m_jobsCompleted.resize(6);
		m_dvdTitle = settings.value(m_settingsKey.arg("DVD Title"), 1).toInt();
		settings.beginGroup(m_settingsKey.arg("DVD Titles"));
		foreach(QString titleNumber, settings.childKeys())
			m_dvdTitles.insert(titleNumber.toInt(), settings.value(titleNumber).toString());
	}
	m_title = title;
	m_rootPath = QString("%1/AnyRip/%2").arg(QDir::homePath()).arg(m_title);
	QDir().mkpath(m_rootPath);
	m_imagePath = QString("%1/%2 - Image.iso").arg(m_rootPath).arg(m_title);
	m_encodePath = QString("%1/%2 - Encode.mp4").arg(m_rootPath).arg(m_title);
	m_subtitlePath = QString("%1/%2 - Subtitles.srt").arg(m_rootPath).arg(m_title);
	m_posterPath = QString("%1/%2 - Poster.jpg").arg(m_rootPath).arg(m_title);
}
void Video::saveState()
{
	QSettings settings;
	settings.setValue(m_settingsKey.arg("Jobs Completed"), m_jobsCompleted);
	settings.setValue(m_settingsKey.arg("DVD Title"), m_dvdTitle);
	settings.remove(m_settingsKey.arg("DVD Titles"));
	QMapIterator<int, QString> i(m_dvdTitles);
	while (i.hasNext()) {
		i.next();
		settings.setValue(QString("%1/%2").arg(m_settingsKey.arg("DVD Titles")).arg(QString::number(i.key())), i.value());
	}
}
void Video::completedJob(bool success)
{
	Job *job = qobject_cast<Job*>(sender());
	qDebug() << "Job code" << job->jobType() << "completed" << success;
	m_jobsCompleted.setBit(job->jobType(), success);
	saveState();
	m_jobsInProgress.setBit(job->jobType(), false);
	emit jobCompleted(job->jobType(), success);
	job->deleteLater();
}
DVDImageJob* Video::dvdImageJob()
{
	if (!m_jobsInProgress.at(Video::DVDImage) && !m_jobsCompleted.at(Video::DVDImage) && DVDDrive::instance()->dvdInserted()) {
		DVDImageJob *job = new DVDImageJob(this, m_imagePath);
		connect(job, SIGNAL(completed(bool)), this, SLOT(completedJob(bool)));
		m_jobsInProgress.setBit(Video::DVDImage, true);
		return job;
	}
	return 0;
}
EncodeMP4Job* Video::encodeMP4Job()
{
	if (!m_jobsInProgress.at(Video::EncodeMP4) && !m_jobsCompleted.at(Video::EncodeMP4) && m_jobsCompleted.at(Video::DVDImage)) {
		EncodeMP4Job *job = new EncodeMP4Job(this, m_encodePath, m_imagePath);
		connect(job, SIGNAL(completed(bool)), this, SLOT(completedJob(bool)));
		m_jobsInProgress.setBit(Video::EncodeMP4, true);
		return job;
	}
	return 0;
}
UploadJob* Video::uploadJob()
{
	if (!m_jobsInProgress.at(Video::Upload) && !m_jobsCompleted.at(Video::Upload) && m_jobsCompleted.at(Video::EncodeMP4)) {
		UploadJob *job = new UploadJob(this, m_encodePath);
		connect(job, SIGNAL(completed(bool)), this, SLOT(completedJob(bool)));
		m_jobsInProgress.setBit(Video::Upload, true);
		return job;
	}
	return 0;
}
TitleLoadJob* Video::titleLoadJob()
{
	if (!m_jobsInProgress.at(Video::TitleLoad) && !m_jobsCompleted.at(Video::TitleLoad) && m_jobsCompleted.at(Video::Upload) && m_jobsCompleted.at(Video::Subtitle) && m_jobsCompleted.at(Video::Poster)) {
		TitleLoadJob *job = new TitleLoadJob(this, m_subtitlePath, m_posterPath);
		connect(job, SIGNAL(completed(bool)), this, SLOT(completedJob(bool)));
		m_jobsInProgress.setBit(Video::TitleLoad, true);
		return job;
	}
	return 0;
}
QList<Job*> Video::availableJobs()
{
	QList<Job*> jobs;
	Job *job;
	if ((job = dvdImageJob()))
		jobs.append(job);
	if ((job = encodeMP4Job()))
		jobs.append(job);
	if ((job = uploadJob()))
		jobs.append(job);
	if ((job = titleLoadJob()))
		jobs.append(job);
	return jobs;
}
void Video::setSubtitle(const QIODevice &input)
{
	m_jobsInProgress.setBit(Video::Subtitle, true);
	//TODO: save input to subtitle file
	m_jobsCompleted.setBit(Video::Subtitle, true);
	saveState();
	m_jobsInProgress.setBit(Video::Subtitle, false);
	emit jobCompleted(Video::Subtitle, true);
}
void Video::setPoster(const QIODevice &input)
{
	m_jobsInProgress.setBit(Video::Poster, true);
	//TODO: save input to subtitle file
	m_jobsCompleted.setBit(Video::Poster, true);
	saveState();
	m_jobsInProgress.setBit(Video::Poster, false);
	emit jobCompleted(Video::Poster, true);
}
QString Video::title() const
{
	return m_title;
}
VideoGui* Video::widget()
{
	return new VideoGui(this);
}
bool Video::isJobCompleted(Video::Jobs job) const
{
	return m_jobsCompleted.at(job);
}
int Video::dvdTitle() const
{
	return m_dvdTitle;
}
void Video::setDvdTitle(int title)
{
	m_dvdTitle = title;
	saveState();
}
QMap<int, QString> Video::dvdTitles() const
{
	return m_dvdTitles;
}
void Video::setDvdTitles(QMap<int, QString> titles)
{
	m_dvdTitles = titles;
	saveState();
}