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