Fix mis-merge.
[dcpomatic.git] / src / lib / config.h
1 /*
2     Copyright (C) 2012-2016 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/config.h
22  *  @brief Class holding configuration.
23  */
24
25 #ifndef DCPOMATIC_CONFIG_H
26 #define DCPOMATIC_CONFIG_H
27
28 #include "isdcf_metadata.h"
29 #include "types.h"
30 #include <dcp/name_format.h>
31 #include <dcp/certificate_chain.h>
32 #include <dcp/encrypted_kdm.h>
33 #include <boost/shared_ptr.hpp>
34 #include <boost/signals2.hpp>
35 #include <boost/filesystem.hpp>
36 #include <vector>
37
38 class CinemaSoundProcessor;
39 class DCPContentType;
40 class Ratio;
41 class Cinema;
42 class Film;
43
44 /** @class Config
45  *  @brief A singleton class holding configuration.
46  */
47 class Config : public boost::noncopyable
48 {
49 public:
50         /** @return number of threads to use for J2K encoding on the local machine */
51         int num_local_encoding_threads () const {
52                 return _num_local_encoding_threads;
53         }
54
55         boost::optional<boost::filesystem::path> default_directory () const {
56                 return _default_directory;
57         }
58
59         boost::optional<boost::filesystem::path> default_kdm_directory () const {
60                 return _default_kdm_directory;
61         }
62
63         boost::filesystem::path default_directory_or (boost::filesystem::path a) const;
64         boost::filesystem::path default_kdm_directory_or (boost::filesystem::path a) const;
65
66         enum Property {
67                 USE_ANY_SERVERS,
68                 SERVERS,
69                 CINEMAS,
70                 SOUND_OUTPUT,
71                 OTHER
72         };
73
74         /** @return base port number to use for J2K encoding servers */
75         int server_port_base () const {
76                 return _server_port_base;
77         }
78
79         void set_use_any_servers (bool u) {
80                 _use_any_servers = u;
81                 changed (USE_ANY_SERVERS);
82         }
83
84         bool use_any_servers () const {
85                 return _use_any_servers;
86         }
87
88         /** @param s New list of servers */
89         void set_servers (std::vector<std::string> s) {
90                 _servers = s;
91                 changed (SERVERS);
92         }
93
94         /** @return Host names / IP addresses of J2K encoding servers that should definitely be used */
95         std::vector<std::string> servers () const {
96                 return _servers;
97         }
98
99         bool only_servers_encode () const {
100                 return _only_servers_encode;
101         }
102
103         Protocol tms_protocol () const {
104                 return _tms_protocol;
105         }
106
107         /** @return The IP address of a TMS that we can copy DCPs to */
108         std::string tms_ip () const {
109                 return _tms_ip;
110         }
111
112         /** @return The path on a TMS that we should changed DCPs to */
113         std::string tms_path () const {
114                 return _tms_path;
115         }
116
117         /** @return User name to log into the TMS with */
118         std::string tms_user () const {
119                 return _tms_user;
120         }
121
122         /** @return Password to log into the TMS with */
123         std::string tms_password () const {
124                 return _tms_password;
125         }
126
127         /** @return The cinema sound processor that we are using */
128         CinemaSoundProcessor const * cinema_sound_processor () const {
129                 return _cinema_sound_processor;
130         }
131
132         std::list<boost::shared_ptr<Cinema> > cinemas () const {
133                 return _cinemas;
134         }
135
136         std::list<int> allowed_dcp_frame_rates () const {
137                 return _allowed_dcp_frame_rates;
138         }
139
140         bool allow_any_dcp_frame_rate () const {
141                 return _allow_any_dcp_frame_rate;
142         }
143
144         ISDCFMetadata default_isdcf_metadata () const {
145                 return _default_isdcf_metadata;
146         }
147
148         boost::optional<std::string> language () const {
149                 return _language;
150         }
151
152         int default_still_length () const {
153                 return _default_still_length;
154         }
155
156         Ratio const * default_container () const {
157                 return _default_container;
158         }
159
160         DCPContentType const * default_dcp_content_type () const {
161                 return _default_dcp_content_type;
162         }
163
164         int default_dcp_audio_channels () const {
165                 return _default_dcp_audio_channels;
166         }
167
168         std::string dcp_issuer () const {
169                 return _dcp_issuer;
170         }
171
172         std::string dcp_creator () const {
173                 return _dcp_creator;
174         }
175
176         int default_j2k_bandwidth () const {
177                 return _default_j2k_bandwidth;
178         }
179
180         int default_audio_delay () const {
181                 return _default_audio_delay;
182         }
183
184         bool default_interop () const {
185                 return _default_interop;
186         }
187
188         void set_default_kdm_directory (boost::filesystem::path d) {
189                 if (_default_kdm_directory && _default_kdm_directory.get() == d) {
190                         return;
191                 }
192                 _default_kdm_directory = d;
193                 changed ();
194         }
195
196         std::string mail_server () const {
197                 return _mail_server;
198         }
199
200         int mail_port () const {
201                 return _mail_port;
202         }
203
204         std::string mail_user () const {
205                 return _mail_user;
206         }
207
208         std::string mail_password () const {
209                 return _mail_password;
210         }
211
212         std::string kdm_subject () const {
213                 return _kdm_subject;
214         }
215
216         std::string kdm_from () const {
217                 return _kdm_from;
218         }
219
220         std::vector<std::string> kdm_cc () const {
221                 return _kdm_cc;
222         }
223
224         std::string kdm_bcc () const {
225                 return _kdm_bcc;
226         }
227
228         std::string kdm_email () const {
229                 return _kdm_email;
230         }
231
232         boost::shared_ptr<const dcp::CertificateChain> signer_chain () const {
233                 return _signer_chain;
234         }
235
236         boost::shared_ptr<const dcp::CertificateChain> decryption_chain () const {
237                 return _decryption_chain;
238         }
239
240         bool check_for_updates () const {
241                 return _check_for_updates;
242         }
243
244         bool check_for_test_updates () const {
245                 return _check_for_test_updates;
246         }
247
248         int maximum_j2k_bandwidth () const {
249                 return _maximum_j2k_bandwidth;
250         }
251
252         int log_types () const {
253                 return _log_types;
254         }
255
256         bool analyse_ebur128 () const {
257                 return _analyse_ebur128;
258         }
259
260         bool automatic_audio_analysis () const {
261                 return _automatic_audio_analysis;
262         }
263
264 #ifdef DCPOMATIC_WINDOWS
265         bool win32_console () const {
266                 return _win32_console;
267         }
268 #endif
269
270         std::vector<boost::filesystem::path> history () const {
271                 return _history;
272         }
273
274         std::vector<dcp::EncryptedKDM> dkdms () const {
275                 return _dkdms;
276         }
277
278         boost::filesystem::path cinemas_file () const {
279                 return _cinemas_file;
280         }
281
282         bool show_hints_before_make_dcp () const {
283                 return _show_hints_before_make_dcp;
284         }
285
286         bool confirm_kdm_email () const {
287                 return _confirm_kdm_email;
288         }
289
290         dcp::NameFormat kdm_container_name_format () const {
291                 return _kdm_container_name_format;
292         }
293
294         dcp::NameFormat kdm_filename_format () const {
295                 return _kdm_filename_format;
296         }
297
298         dcp::NameFormat dcp_metadata_filename_format () const {
299                 return _dcp_metadata_filename_format;
300         }
301
302         dcp::NameFormat dcp_asset_filename_format () const {
303                 return _dcp_asset_filename_format;
304         }
305
306         bool jump_to_selected () const {
307                 return _jump_to_selected;
308         }
309
310         boost::optional<std::string> sound_output () const {
311                 return _sound_output;
312         }
313
314         /** @param n New number of local encoding threads */
315         void set_num_local_encoding_threads (int n) {
316                 maybe_set (_num_local_encoding_threads, n);
317         }
318
319         void set_default_directory (boost::filesystem::path d) {
320                 if (_default_directory && *_default_directory == d) {
321                         return;
322                 }
323                 _default_directory = d;
324                 changed ();
325         }
326
327         /** @param p New server port */
328         void set_server_port_base (int p) {
329                 maybe_set (_server_port_base, p);
330         }
331
332         void set_only_servers_encode (bool o) {
333                 maybe_set (_only_servers_encode, o);
334         }
335
336         void set_tms_protocol (Protocol p) {
337                 maybe_set (_tms_protocol, p);
338         }
339
340         /** @param i IP address of a TMS that we can copy DCPs to */
341         void set_tms_ip (std::string i) {
342                 maybe_set (_tms_ip, i);
343         }
344
345         /** @param p Path on a TMS that we should changed DCPs to */
346         void set_tms_path (std::string p) {
347                 maybe_set (_tms_path, p);
348         }
349
350         /** @param u User name to log into the TMS with */
351         void set_tms_user (std::string u) {
352                 maybe_set (_tms_user, u);
353         }
354
355         /** @param p Password to log into the TMS with */
356         void set_tms_password (std::string p) {
357                 maybe_set (_tms_password, p);
358         }
359
360         void add_cinema (boost::shared_ptr<Cinema> c) {
361                 _cinemas.push_back (c);
362                 changed (CINEMAS);
363         }
364
365         void remove_cinema (boost::shared_ptr<Cinema> c) {
366                 _cinemas.remove (c);
367                 changed (CINEMAS);
368         }
369
370         void set_allowed_dcp_frame_rates (std::list<int> const & r) {
371                 maybe_set (_allowed_dcp_frame_rates, r);
372         }
373
374         void set_allow_any_dcp_frame_rate (bool a) {
375                 maybe_set (_allow_any_dcp_frame_rate, a);
376         }
377
378         void set_default_isdcf_metadata (ISDCFMetadata d) {
379                 maybe_set (_default_isdcf_metadata, d);
380         }
381
382         void set_language (std::string l) {
383                 if (_language && _language.get() == l) {
384                         return;
385                 }
386                 _language = l;
387                 changed ();
388         }
389
390         void unset_language () {
391                 if (!_language) {
392                         return;
393                 }
394
395                 _language = boost::none;
396                 changed ();
397         }
398
399         void set_default_still_length (int s) {
400                 maybe_set (_default_still_length, s);
401         }
402
403         void set_default_container (Ratio const * c) {
404                 maybe_set (_default_container, c);
405         }
406
407         void set_default_dcp_content_type (DCPContentType const * t) {
408                 maybe_set (_default_dcp_content_type, t);
409         }
410
411         void set_default_dcp_audio_channels (int c) {
412                 maybe_set (_default_dcp_audio_channels, c);
413         }
414
415         void set_dcp_issuer (std::string i) {
416                 maybe_set (_dcp_issuer, i);
417         }
418
419         void set_dcp_creator (std::string c) {
420                 maybe_set (_dcp_creator, c);
421         }
422
423         void set_default_j2k_bandwidth (int b) {
424                 maybe_set (_default_j2k_bandwidth, b);
425         }
426
427         void set_default_audio_delay (int d) {
428                 maybe_set (_default_audio_delay, d);
429         }
430
431         void set_default_interop (bool i) {
432                 maybe_set (_default_interop, i);
433         }
434
435         void set_mail_server (std::string s) {
436                 maybe_set (_mail_server, s);
437         }
438
439         void set_mail_port (int p) {
440                 maybe_set (_mail_port, p);
441         }
442
443         void set_mail_user (std::string u) {
444                 maybe_set (_mail_user, u);
445         }
446
447         void set_mail_password (std::string p) {
448                 maybe_set (_mail_password, p);
449         }
450
451         void set_kdm_subject (std::string s) {
452                 maybe_set (_kdm_subject, s);
453         }
454
455         void set_kdm_from (std::string f) {
456                 maybe_set (_kdm_from, f);
457         }
458
459         void set_kdm_cc (std::vector<std::string> f) {
460                 maybe_set (_kdm_cc, f);
461         }
462
463         void set_kdm_bcc (std::string f) {
464                 maybe_set (_kdm_bcc, f);
465         }
466
467         void set_kdm_email (std::string e) {
468                 maybe_set (_kdm_email, e);
469         }
470
471         void reset_kdm_email ();
472
473         void set_signer_chain (boost::shared_ptr<const dcp::CertificateChain> s) {
474                 maybe_set (_signer_chain, s);
475         }
476
477         void set_decryption_chain (boost::shared_ptr<const dcp::CertificateChain> c) {
478                 maybe_set (_decryption_chain, c);
479         }
480
481         void set_check_for_updates (bool c) {
482                 maybe_set (_check_for_updates, c);
483                 if (!c) {
484                         set_check_for_test_updates (false);
485                 }
486         }
487
488         void set_check_for_test_updates (bool c) {
489                 maybe_set (_check_for_test_updates, c);
490         }
491
492         void set_maximum_j2k_bandwidth (int b) {
493                 maybe_set (_maximum_j2k_bandwidth, b);
494         }
495
496         void set_log_types (int t) {
497                 maybe_set (_log_types, t);
498         }
499
500         void set_analyse_ebur128 (bool a) {
501                 maybe_set (_analyse_ebur128, a);
502         }
503
504         void set_automatic_audio_analysis (bool a) {
505                 maybe_set (_automatic_audio_analysis, a);
506         }
507
508 #ifdef DCPOMATIC_WINDOWS
509         void set_win32_console (bool c) {
510                 maybe_set (_win32_console, c);
511         }
512 #endif
513
514         void set_dkdms (std::vector<dcp::EncryptedKDM> dkdms) {
515                 _dkdms = dkdms;
516                 changed ();
517         }
518
519         void set_cinemas_file (boost::filesystem::path file);
520
521         void set_show_hints_before_make_dcp (bool s) {
522                 maybe_set (_show_hints_before_make_dcp, s);
523         }
524
525         void set_confirm_kdm_email (bool s) {
526                 maybe_set (_confirm_kdm_email, s);
527         }
528
529         void set_sound_output (std::string o)
530         {
531                 maybe_set (_sound_output, o, SOUND_OUTPUT);
532         }
533
534         void unset_sound_output ()
535         {
536                 if (!_sound_output) {
537                         return;
538                 }
539
540                 _sound_output = boost::none;
541                 changed ();
542         }
543
544         void set_kdm_container_name_format (dcp::NameFormat n) {
545                 maybe_set (_kdm_container_name_format, n);
546         }
547
548         void set_kdm_filename_format (dcp::NameFormat n) {
549                 maybe_set (_kdm_filename_format, n);
550         }
551
552         void set_dcp_metadata_filename_format (dcp::NameFormat n) {
553                 maybe_set (_dcp_metadata_filename_format, n);
554         }
555
556         void set_dcp_asset_filename_format (dcp::NameFormat n) {
557                 maybe_set (_dcp_asset_filename_format, n);
558         }
559
560         void clear_history () {
561                 _history.clear ();
562                 changed ();
563         }
564
565         void add_to_history (boost::filesystem::path p);
566
567         void set_jump_to_selected (bool j) {
568                 maybe_set (_jump_to_selected, j);
569         }
570
571         void changed (Property p = OTHER);
572         boost::signals2::signal<void (Property)> Changed;
573         /** Emitted if read() failed on an existing Config file.  There is nothing
574             a listener can do about it: this is just for information.
575         */
576         static boost::signals2::signal<void ()> FailedToLoad;
577
578         void write () const;
579         void write_config () const;
580         void write_cinemas () const;
581
582         void save_template (boost::shared_ptr<const Film> film, std::string name) const;
583         bool existing_template (std::string name) const;
584         std::list<std::string> templates () const;
585         boost::filesystem::path template_path (std::string name) const;
586         void rename_template (std::string old_name, std::string new_name) const;
587         void delete_template (std::string name) const;
588
589         static Config* instance ();
590         static void drop ();
591         static void restore_defaults ();
592         static bool have_existing (std::string);
593
594 private:
595         Config ();
596         static boost::filesystem::path path (std::string file, bool create_directories = true);
597         void read ();
598         void set_defaults ();
599         void set_kdm_email_to_default ();
600         void read_cinemas (cxml::Document const & f);
601         boost::shared_ptr<dcp::CertificateChain> create_certificate_chain ();
602         boost::filesystem::path directory_or (boost::optional<boost::filesystem::path> dir, boost::filesystem::path a) const;
603
604         template <class T>
605         void maybe_set (T& member, T new_value, Property prop = OTHER) {
606                 if (member == new_value) {
607                         return;
608                 }
609                 member = new_value;
610                 changed (prop);
611         }
612
613         template <class T>
614         void maybe_set (boost::optional<T>& member, T new_value, Property prop = OTHER) {
615                 if (member && member.get() == new_value) {
616                         return;
617                 }
618                 member = new_value;
619                 changed (prop);
620         }
621
622         /** number of threads to use for J2K encoding on the local machine */
623         int _num_local_encoding_threads;
624         /** default directory to put new films in */
625         boost::optional<boost::filesystem::path> _default_directory;
626         /** base port number to use for J2K encoding servers;
627          *  this port and the two above it will be used.
628          */
629         int _server_port_base;
630         /** true to broadcast on the `any' address to look for servers */
631         bool _use_any_servers;
632         /** J2K encoding servers that should definitely be used */
633         std::vector<std::string> _servers;
634         bool _only_servers_encode;
635         Protocol _tms_protocol;
636         /** The IP address of a TMS that we can copy DCPs to */
637         std::string _tms_ip;
638         /** The path on a TMS that we should write DCPs to */
639         std::string _tms_path;
640         /** User name to log into the TMS with */
641         std::string _tms_user;
642         /** Password to log into the TMS with */
643         std::string _tms_password;
644         /** Our cinema sound processor */
645         CinemaSoundProcessor const * _cinema_sound_processor;
646         std::list<int> _allowed_dcp_frame_rates;
647         /** Allow any video frame rate for the DCP; if true, overrides _allowed_dcp_frame_rates */
648         bool _allow_any_dcp_frame_rate;
649         /** Default ISDCF metadata for newly-created Films */
650         ISDCFMetadata _default_isdcf_metadata;
651         boost::optional<std::string> _language;
652         /** Default length of still image content (seconds) */
653         int _default_still_length;
654         Ratio const * _default_container;
655         DCPContentType const * _default_dcp_content_type;
656         int _default_dcp_audio_channels;
657         std::string _dcp_issuer;
658         std::string _dcp_creator;
659         int _default_j2k_bandwidth;
660         int _default_audio_delay;
661         bool _default_interop;
662         /** Default directory to offer to write KDMs to; if it's not set,
663             the home directory will be offered.
664         */
665         boost::optional<boost::filesystem::path> _default_kdm_directory;
666         std::list<boost::shared_ptr<Cinema> > _cinemas;
667         std::string _mail_server;
668         int _mail_port;
669         std::string _mail_user;
670         std::string _mail_password;
671         std::string _kdm_subject;
672         std::string _kdm_from;
673         std::vector<std::string> _kdm_cc;
674         std::string _kdm_bcc;
675         std::string _kdm_email;
676         boost::shared_ptr<const dcp::CertificateChain> _signer_chain;
677         /** Chain used to decrypt KDMs; the leaf of this chain is the target
678          *  certificate for making KDMs given to DCP-o-matic.
679          */
680         boost::shared_ptr<const dcp::CertificateChain> _decryption_chain;
681         /** true to check for updates on startup */
682         bool _check_for_updates;
683         bool _check_for_test_updates;
684         /** maximum allowed J2K bandwidth in bits per second */
685         int _maximum_j2k_bandwidth;
686         int _log_types;
687         bool _analyse_ebur128;
688         bool _automatic_audio_analysis;
689 #ifdef DCPOMATIC_WINDOWS
690         bool _win32_console;
691 #endif
692         std::vector<boost::filesystem::path> _history;
693         std::vector<dcp::EncryptedKDM> _dkdms;
694         boost::filesystem::path _cinemas_file;
695         bool _show_hints_before_make_dcp;
696         bool _confirm_kdm_email;
697         dcp::NameFormat _kdm_filename_format;
698         dcp::NameFormat _kdm_container_name_format;
699         dcp::NameFormat _dcp_metadata_filename_format;
700         dcp::NameFormat _dcp_asset_filename_format;
701         bool _jump_to_selected;
702         /** name of a specific sound output stream to use for preview */
703         boost::optional<std::string> _sound_output;
704
705         /** Singleton instance, or 0 */
706         static Config* _instance;
707 };
708
709 #endif