summaryrefslogtreecommitdiff
path: root/src/lib/copy_from_dvd_job.cc
blob: dcf53ac54e2baed566fde74bf1b2edc51ccf1e96 (plain)
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
/*
    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/** @file src/copy_from_dvd_job.cc
 *  @brief A job to copy a film from a DVD.
 */

#include <stdio.h>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include "copy_from_dvd_job.h"
#include "dvd.h"
#include "cross.h"
#include "film.h"

using std::string;
using std::list;
using std::stringstream;
using boost::shared_ptr;

/** @param f Film to write DVD data into.
 */
CopyFromDVDJob::CopyFromDVDJob (shared_ptr<Film> f, shared_ptr<Job> req)
	: Job (f, req)
{

}

string
CopyFromDVDJob::name () const
{
	return "Copy film from DVD";
}

void
CopyFromDVDJob::run ()
{
	/* Remove any old DVD rips */
	boost::filesystem::remove_all (_film->dir ("dvd"));

	string const dvd = find_dvd ();
	if (dvd.empty ()) {
		set_error ("could not find DVD");
		set_state (FINISHED_ERROR);
	}

	list<DVDTitle> const t = dvd_titles (dvd);
	if (t.empty ()) {
		set_error ("no titles found on DVD");
		set_state (FINISHED_ERROR);
	}

	int longest_title = 0;
	uint64_t longest_size = 0;
	for (list<DVDTitle>::const_iterator i = t.begin(); i != t.end(); ++i) {
		if (longest_size < i->size) {
			longest_size = i->size;
			longest_title = i->number;
		}
	}

	stringstream c;
	c << "vobcopy -n " << longest_title << " -l -o \"" << _film->dir ("dvd") << "\" 2>&1";

	FILE* f = popen (c.str().c_str(), "r");
	if (f == 0) {
		set_error ("could not run vobcopy command");
		set_state (FINISHED_ERROR);
		return;
	}

	while (!feof (f)) {
		char buf[256];
		if (fscanf (f, "%s", buf)) {
			string s (buf);
			if (!s.empty () && s[s.length() - 1] == '%') {
				set_progress (atof (s.substr(0, s.length() - 1).c_str()) / 100.0);
			}
		}
	}

	const string dvd_dir = _film->dir ("dvd");

	string largest_file;
	uintmax_t largest_size = 0;
	for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dvd_dir); i != boost::filesystem::directory_iterator(); ++i) {
		uintmax_t const s = boost::filesystem::file_size (*i);
		if (s > largest_size) {

#if BOOST_FILESYSTEM_VERSION == 3		
			largest_file = boost::filesystem::path(*i).generic_string();
#else
			largest_file = i->string ();
#endif
			largest_size = s;
		}
	}

	_film->set_content (largest_file);
	
	int const r = pclose (f);
	if (WEXITSTATUS (r) != 0) {
		set_error ("call to vobcopy failed");
		set_state (FINISHED_ERROR);
	} else {
		set_state (FINISHED_OK);
	}
}