s/destroy_thread/stop_thread/
[dcpomatic.git] / src / lib / upload_job.cc
1 /*
2     Copyright (C) 2012-2019 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 "dcpomatic_log.h"
30 #include "film.h"
31 #include "scp_uploader.h"
32 #include "curl_uploader.h"
33 #include <iostream>
34
35 #include "i18n.h"
36
37 using std::string;
38 using std::min;
39 using boost::shared_ptr;
40 using boost::scoped_ptr;
41
42 UploadJob::UploadJob (shared_ptr<const Film> film)
43         : Job (film)
44         , _status (_("Waiting"))
45 {
46
47 }
48
49 UploadJob::~UploadJob ()
50 {
51         stop_thread ();
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         scoped_ptr<Uploader> uploader;
72         switch (Config::instance()->tms_protocol ()) {
73         case FILE_TRANSFER_PROTOCOL_SCP:
74                 uploader.reset (new SCPUploader (bind (&UploadJob::set_status, this, _1), bind (&UploadJob::set_progress, this, _1, false)));
75                 break;
76         case FILE_TRANSFER_PROTOCOL_FTP:
77                 uploader.reset (new CurlUploader (bind (&UploadJob::set_status, this, _1), bind (&UploadJob::set_progress, this, _1, false)));
78                 break;
79         }
80
81         uploader->upload (_film->dir (_film->dcp_name ()));
82
83         set_progress (1);
84         set_status (N_(""));
85         set_state (FINISHED_OK);
86 }
87
88 string
89 UploadJob::status () const
90 {
91         boost::mutex::scoped_lock lm (_status_mutex);
92         string s = Job::status ();
93         if (!_status.empty () && !finished_in_error ()) {
94                 s += N_("; ") + _status;
95         }
96         return s;
97 }
98
99 void
100 UploadJob::set_status (string s)
101 {
102         boost::mutex::scoped_lock lm (_status_mutex);
103         _status = s;
104 }