f7281fc10e303479c2a0bcfdaef6e26121df059f
[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 "film_state.h"
29 #include "dvd.h"
30 #include "cross.h"
31
32 using namespace std;
33 using namespace boost;
34
35 /** @param fs FilmState for the film to write DVD data into.
36  *  @param l Log that we can write to.
37  */
38 CopyFromDVDJob::CopyFromDVDJob (shared_ptr<const FilmState> fs, Log* l, shared_ptr<Job> req)
39         : Job (fs, shared_ptr<Options> (), l, req)
40 {
41
42 }
43
44 string
45 CopyFromDVDJob::name () const
46 {
47         return "Copy film from DVD";
48 }
49
50 void
51 CopyFromDVDJob::run ()
52 {
53         /* Remove any old DVD rips */
54         filesystem::remove_all (_fs->dir ("dvd"));
55
56         string const dvd = find_dvd ();
57         if (dvd.empty ()) {
58                 set_error ("could not find DVD");
59                 set_state (FINISHED_ERROR);
60         }
61
62         list<DVDTitle> const t = dvd_titles (dvd);
63         if (t.empty ()) {
64                 set_error ("no titles found on DVD");
65                 set_state (FINISHED_ERROR);
66         }
67
68         int longest_title = 0;
69         uint64_t longest_size = 0;
70         for (list<DVDTitle>::const_iterator i = t.begin(); i != t.end(); ++i) {
71                 if (longest_size < i->size) {
72                         longest_size = i->size;
73                         longest_title = i->number;
74                 }
75         }
76
77         stringstream c;
78         c << "vobcopy -n " << longest_title << " -l -o \"" << _fs->dir ("dvd") << "\" 2>&1";
79
80         FILE* f = popen (c.str().c_str(), "r");
81         if (f == 0) {
82                 set_error ("could not run vobcopy command");
83                 set_state (FINISHED_ERROR);
84                 return;
85         }
86
87         while (!feof (f)) {
88                 char buf[256];
89                 if (fscanf (f, "%s", buf)) {
90                         string s (buf);
91                         if (!s.empty () && s[s.length() - 1] == '%') {
92                                 set_progress (atof (s.substr(0, s.length() - 1).c_str()) / 100.0);
93                         }
94                 }
95         }
96
97         int const r = pclose (f);
98         if (WEXITSTATUS (r) != 0) {
99                 set_error ("call to vobcopy failed");
100                 set_state (FINISHED_ERROR);
101         } else {
102                 set_state (FINISHED_OK);
103         }
104 }