Remove unused Processor::process_begin; some docs.
[dcpomatic.git] / src / lib / copy_from_dvd_job.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/copy_from_dvd_job.cc
21  *  @brief A job to copy a film from a DVD.
22  */
23
24 #include <stdio.h>
25 #include <boost/algorithm/string.hpp>
26 #include <boost/filesystem.hpp>
27 #include "copy_from_dvd_job.h"
28 #include "dvd.h"
29 #include "cross.h"
30 #include "film.h"
31
32 using std::string;
33 using std::list;
34 using std::stringstream;
35 using boost::shared_ptr;
36
37 /** @param f Film to write DVD data into.
38  */
39 CopyFromDVDJob::CopyFromDVDJob (shared_ptr<Film> f, shared_ptr<Job> req)
40         : Job (f, req)
41 {
42
43 }
44
45 string
46 CopyFromDVDJob::name () const
47 {
48         return "Copy film from DVD";
49 }
50
51 void
52 CopyFromDVDJob::run ()
53 {
54         /* Remove any old DVD rips */
55         boost::filesystem::remove_all (_film->dir ("dvd"));
56
57         string const dvd = find_dvd ();
58         if (dvd.empty ()) {
59                 set_error ("could not find DVD");
60                 set_state (FINISHED_ERROR);
61         }
62
63         list<DVDTitle> const t = dvd_titles (dvd);
64         if (t.empty ()) {
65                 set_error ("no titles found on DVD");
66                 set_state (FINISHED_ERROR);
67         }
68
69         int longest_title = 0;
70         uint64_t longest_size = 0;
71         for (list<DVDTitle>::const_iterator i = t.begin(); i != t.end(); ++i) {
72                 if (longest_size < i->size) {
73                         longest_size = i->size;
74                         longest_title = i->number;
75                 }
76         }
77
78         stringstream c;
79         c << "vobcopy -n " << longest_title << " -l -o \"" << _film->dir ("dvd") << "\" 2>&1";
80
81         FILE* f = popen (c.str().c_str(), "r");
82         if (f == 0) {
83                 set_error ("could not run vobcopy command");
84                 set_state (FINISHED_ERROR);
85                 return;
86         }
87
88         while (!feof (f)) {
89                 char buf[256];
90                 if (fscanf (f, "%s", buf)) {
91                         string s (buf);
92                         if (!s.empty () && s[s.length() - 1] == '%') {
93                                 set_progress (atof (s.substr(0, s.length() - 1).c_str()) / 100.0);
94                         }
95                 }
96         }
97
98         const string dvd_dir = _film->dir ("dvd");
99
100         string largest_file;
101         uintmax_t largest_size = 0;
102         for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dvd_dir); i != boost::filesystem::directory_iterator(); ++i) {
103                 uintmax_t const s = boost::filesystem::file_size (*i);
104                 if (s > largest_size) {
105
106 #if BOOST_FILESYSTEM_VERSION == 3               
107                         largest_file = boost::filesystem::path(*i).generic_string();
108 #else
109                         largest_file = i->string ();
110 #endif
111                         largest_size = s;
112                 }
113         }
114
115         _film->set_content (largest_file);
116         
117         int const r = pclose (f);
118         if (WEXITSTATUS (r) != 0) {
119                 set_error ("call to vobcopy failed");
120                 set_state (FINISHED_ERROR);
121         } else {
122                 set_state (FINISHED_OK);
123         }
124 }