Make a generic base for uploaders and move the SCP code into a subclass of that.
[dcpomatic.git] / src / lib / upload_job.cc
1 /*
2     Copyright (C) 2012-2015 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/upload_job.cc
21  *  @brief A job to copy DCPs to a server using libcurl.
22  */
23
24 #include <iostream>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #include <boost/filesystem.hpp>
29 #include <libssh/libssh.h>
30 #include "compose.hpp"
31 #include "upload_job.h"
32 #include "exceptions.h"
33 #include "config.h"
34 #include "log.h"
35 #include "film.h"
36 #include "cross.h"
37 #include "scp_uploader.h"
38
39 #include "i18n.h"
40
41 #define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_GENERAL);
42
43 using std::string;
44 using std::min;
45 using boost::shared_ptr;
46
47 UploadJob::UploadJob (shared_ptr<const Film> film)
48         : Job (film)
49         , _status (_("Waiting"))
50 {
51
52 }
53
54 string
55 UploadJob::name () const
56 {
57         return _("Copy DCP to TMS");
58 }
59
60 string
61 UploadJob::json_name () const
62 {
63         return N_("upload");
64 }
65
66 void
67 UploadJob::run ()
68 {
69         LOG_GENERAL_NC (N_("Upload job starting"));
70
71         SCPUploader uploader (bind (&UploadJob::set_status, this, _1), bind (&UploadJob::set_progress, this, _1, false));
72         uploader.upload (_film->dir (_film->dcp_name ()));
73
74         set_progress (1);
75         set_status (N_(""));
76         set_state (FINISHED_OK);
77 }
78
79 string
80 UploadJob::status () const
81 {
82         boost::mutex::scoped_lock lm (_status_mutex);
83         string s = Job::status ();
84         if (!_status.empty () && !finished_in_error ()) {
85                 s += N_("; ") + _status;
86         }
87         return s;
88 }
89
90 void
91 UploadJob::set_status (string s)
92 {
93         boost::mutex::scoped_lock lm (_status_mutex);
94         _status = s;
95 }