4726c734ab19db889ea97e8f3bcda684db3d1b71
[dcpomatic.git] / src / lib / upload_job.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file src/upload_job.cc
22  *  @brief A job to copy DCPs to a server using libcurl.
23  */
24
25 #include "compose.hpp"
26 #include "upload_job.h"
27 #include "config.h"
28 #include "log.h"
29 #include "film.h"
30 #include "scp_uploader.h"
31 #include "curl_uploader.h"
32 #include <iostream>
33
34 #include "i18n.h"
35
36 #define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_GENERAL);
37
38 using std::string;
39 using std::min;
40 using boost::shared_ptr;
41 using boost::scoped_ptr;
42
43 UploadJob::UploadJob (shared_ptr<const Film> film)
44         : Job (film)
45         , _status (_("Waiting"))
46 {
47
48 }
49
50 string
51 UploadJob::name () const
52 {
53         return _("Copy DCP to TMS");
54 }
55
56 string
57 UploadJob::json_name () const
58 {
59         return N_("upload");
60 }
61
62 void
63 UploadJob::run ()
64 {
65         LOG_GENERAL_NC (N_("Upload job starting"));
66
67         scoped_ptr<Uploader> uploader;
68         switch (Config::instance()->tms_protocol ()) {
69         case PROTOCOL_SCP:
70                 uploader.reset (new SCPUploader (bind (&UploadJob::set_status, this, _1), bind (&UploadJob::set_progress, this, _1, false)));
71                 break;
72         case PROTOCOL_FTP:
73                 uploader.reset (new CurlUploader (bind (&UploadJob::set_status, this, _1), bind (&UploadJob::set_progress, this, _1, false)));
74                 break;
75         }
76
77         uploader->upload (_film->dir (_film->dcp_name ()));
78
79         set_progress (1);
80         set_status (N_(""));
81         set_state (FINISHED_OK);
82 }
83
84 string
85 UploadJob::status () const
86 {
87         boost::mutex::scoped_lock lm (_status_mutex);
88         string s = Job::status ();
89         if (!_status.empty () && !finished_in_error ()) {
90                 s += N_("; ") + _status;
91         }
92         return s;
93 }
94
95 void
96 UploadJob::set_status (string s)
97 {
98         boost::mutex::scoped_lock lm (_status_mutex);
99         _status = s;
100 }