c4d5a65183ca93484e9b225628e46a59d836f3a1
[dcpomatic.git] / src / lib / send_notification_email_job.cc
1 /*
2     Copyright (C) 2018-2021 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
22 #include "send_notification_email_job.h"
23 #include "exceptions.h"
24 #include "config.h"
25 #include "emailer.h"
26 #include "compose.hpp"
27 #include <list>
28
29 #include "i18n.h"
30
31
32 using std::string;
33 using std::list;
34 using std::shared_ptr;
35
36
37 /** @param body Email body */
38 SendNotificationEmailJob::SendNotificationEmailJob (string body)
39         : Job (shared_ptr<Film>())
40         , _body (body)
41 {
42
43 }
44
45
46 SendNotificationEmailJob::~SendNotificationEmailJob ()
47 {
48         stop_thread ();
49 }
50
51
52 string
53 SendNotificationEmailJob::name () const
54 {
55         return _("Email notification");
56 }
57
58
59 string
60 SendNotificationEmailJob::json_name () const
61 {
62         return N_("send_notification_email");
63 }
64
65
66 void
67 SendNotificationEmailJob::run ()
68 {
69         auto config = Config::instance ();
70
71         if (config->mail_server().empty()) {
72                 throw NetworkError (_("No mail server configured in preferences"));
73         }
74
75         set_progress_unknown ();
76         list<string> to;
77         to.push_back (config->notification_to());
78         Emailer email (config->notification_from(), to, config->notification_subject(), _body);
79         for (auto i: config->notification_cc()) {
80                 email.add_cc (i);
81         }
82         if (!config->notification_bcc().empty()) {
83                 email.add_bcc (config->notification_bcc());
84         }
85
86         email.send (config->mail_server(), config->mail_port(), config->mail_protocol(), config->mail_user(), config->mail_password());
87
88         set_progress (1);
89         set_state (FINISHED_OK);
90 }