e5795d2c300db8ac02b02cb4c6f97369d713900d
[dcpomatic.git] / src / lib / config.h
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/config.h
21  *  @brief Class holding configuration.
22  */
23
24 #ifndef DCPOMATIC_CONFIG_H
25 #define DCPOMATIC_CONFIG_H
26
27 #include "isdcf_metadata.h"
28 #include "types.h"
29 #include <dcp/certificate_chain.h>
30 #include <dcp/encrypted_kdm.h>
31 #include <boost/shared_ptr.hpp>
32 #include <boost/signals2.hpp>
33 #include <boost/filesystem.hpp>
34 #include <vector>
35
36 class CinemaSoundProcessor;
37 class DCPContentType;
38 class Ratio;
39 class Cinema;
40
41 /** @class Config
42  *  @brief A singleton class holding configuration.
43  */
44 class Config : public boost::noncopyable
45 {
46 public:
47         /** @return number of threads to use for J2K encoding on the local machine */
48         int num_local_encoding_threads () const {
49                 return _num_local_encoding_threads;
50         }
51
52         boost::filesystem::path default_directory () const {
53                 return _default_directory;
54         }
55
56         boost::filesystem::path default_directory_or (boost::filesystem::path a) const;
57
58         enum Property {
59                 USE_ANY_SERVERS,
60                 SERVERS,
61                 OTHER
62         };
63
64         /** @return base port number to use for J2K encoding servers */
65         int server_port_base () const {
66                 return _server_port_base;
67         }
68
69         void set_use_any_servers (bool u) {
70                 _use_any_servers = u;
71                 changed (USE_ANY_SERVERS);
72         }
73
74         bool use_any_servers () const {
75                 return _use_any_servers;
76         }
77
78         /** @param s New list of servers */
79         void set_servers (std::vector<std::string> s) {
80                 _servers = s;
81                 changed (SERVERS);
82         }
83
84         /** @return Host names / IP addresses of J2K encoding servers that should definitely be used */
85         std::vector<std::string> servers () const {
86                 return _servers;
87         }
88
89         bool only_servers_encode () const {
90                 return _only_servers_encode;
91         }
92
93         Protocol tms_protocol () const {
94                 return _tms_protocol;
95         }
96
97         /** @return The IP address of a TMS that we can copy DCPs to */
98         std::string tms_ip () const {
99                 return _tms_ip;
100         }
101
102         /** @return The path on a TMS that we should changed DCPs to */
103         std::string tms_path () const {
104                 return _tms_path;
105         }
106
107         /** @return User name to log into the TMS with */
108         std::string tms_user () const {
109                 return _tms_user;
110         }
111
112         /** @return Password to log into the TMS with */
113         std::string tms_password () const {
114                 return _tms_password;
115         }
116
117         /** @return The cinema sound processor that we are using */
118         CinemaSoundProcessor const * cinema_sound_processor () const {
119                 return _cinema_sound_processor;
120         }
121
122         std::list<boost::shared_ptr<Cinema> > cinemas () const {
123                 return _cinemas;
124         }
125
126         std::list<int> allowed_dcp_frame_rates () const {
127                 return _allowed_dcp_frame_rates;
128         }
129
130         bool allow_any_dcp_frame_rate () const {
131                 return _allow_any_dcp_frame_rate;
132         }
133
134         ISDCFMetadata default_isdcf_metadata () const {
135                 return _default_isdcf_metadata;
136         }
137
138         boost::optional<std::string> language () const {
139                 return _language;
140         }
141
142         int default_still_length () const {
143                 return _default_still_length;
144         }
145
146         Ratio const * default_container () const {
147                 return _default_container;
148         }
149
150         DCPContentType const * default_dcp_content_type () const {
151                 return _default_dcp_content_type;
152         }
153
154         std::string dcp_issuer () const {
155                 return _dcp_issuer;
156         }
157
158         std::string dcp_creator () const {
159                 return _dcp_creator;
160         }
161
162         int default_j2k_bandwidth () const {
163                 return _default_j2k_bandwidth;
164         }
165
166         int default_audio_delay () const {
167                 return _default_audio_delay;
168         }
169
170         bool default_interop () const {
171                 return _default_interop;
172         }
173
174         std::string mail_server () const {
175                 return _mail_server;
176         }
177
178         int mail_port () const {
179                 return _mail_port;
180         }
181
182         std::string mail_user () const {
183                 return _mail_user;
184         }
185
186         std::string mail_password () const {
187                 return _mail_password;
188         }
189
190         std::string kdm_subject () const {
191                 return _kdm_subject;
192         }
193
194         std::string kdm_from () const {
195                 return _kdm_from;
196         }
197
198         std::vector<std::string> kdm_cc () const {
199                 return _kdm_cc;
200         }
201
202         std::string kdm_bcc () const {
203                 return _kdm_bcc;
204         }
205
206         std::string kdm_email () const {
207                 return _kdm_email;
208         }
209
210         boost::shared_ptr<const dcp::CertificateChain> signer_chain () const {
211                 return _signer_chain;
212         }
213
214         boost::shared_ptr<const dcp::CertificateChain> decryption_chain () const {
215                 return _decryption_chain;
216         }
217
218         bool check_for_updates () const {
219                 return _check_for_updates;
220         }
221
222         bool check_for_test_updates () const {
223                 return _check_for_test_updates;
224         }
225
226         int maximum_j2k_bandwidth () const {
227                 return _maximum_j2k_bandwidth;
228         }
229
230         int log_types () const {
231                 return _log_types;
232         }
233
234         bool analyse_ebur128 () const {
235                 return _analyse_ebur128;
236         }
237
238         bool automatic_audio_analysis () const {
239                 return _automatic_audio_analysis;
240         }
241
242 #ifdef DCPOMATIC_WINDOWS
243         bool win32_console () const {
244                 return _win32_console;
245         }
246 #endif
247
248         std::vector<boost::filesystem::path> history () const {
249                 return _history;
250         }
251
252         std::vector<dcp::EncryptedKDM> dkdms () const {
253                 return _dkdms;
254         }
255
256         boost::filesystem::path cinemas_file () const {
257                 return _cinemas_file;
258         }
259
260         /** @param n New number of local encoding threads */
261         void set_num_local_encoding_threads (int n) {
262                 maybe_set (_num_local_encoding_threads, n);
263         }
264
265         void set_default_directory (boost::filesystem::path d) {
266                 maybe_set (_default_directory, d);
267         }
268
269         /** @param p New server port */
270         void set_server_port_base (int p) {
271                 maybe_set (_server_port_base, p);
272         }
273
274         void set_only_servers_encode (bool o) {
275                 maybe_set (_only_servers_encode, o);
276         }
277
278         void set_tms_protocol (Protocol p) {
279                 maybe_set (_tms_protocol, p);
280         }
281
282         /** @param i IP address of a TMS that we can copy DCPs to */
283         void set_tms_ip (std::string i) {
284                 maybe_set (_tms_ip, i);
285         }
286
287         /** @param p Path on a TMS that we should changed DCPs to */
288         void set_tms_path (std::string p) {
289                 maybe_set (_tms_path, p);
290         }
291
292         /** @param u User name to log into the TMS with */
293         void set_tms_user (std::string u) {
294                 maybe_set (_tms_user, u);
295         }
296
297         /** @param p Password to log into the TMS with */
298         void set_tms_password (std::string p) {
299                 maybe_set (_tms_password, p);
300         }
301
302         void add_cinema (boost::shared_ptr<Cinema> c) {
303                 _cinemas.push_back (c);
304                 changed ();
305         }
306
307         void remove_cinema (boost::shared_ptr<Cinema> c) {
308                 _cinemas.remove (c);
309                 changed ();
310         }
311
312         void set_allowed_dcp_frame_rates (std::list<int> const & r) {
313                 maybe_set (_allowed_dcp_frame_rates, r);
314         }
315
316         void set_allow_any_dcp_frame_rate (bool a) {
317                 maybe_set (_allow_any_dcp_frame_rate, a);
318         }
319
320         void set_default_isdcf_metadata (ISDCFMetadata d) {
321                 maybe_set (_default_isdcf_metadata, d);
322         }
323
324         void set_language (std::string l) {
325                 if (_language && _language.get() == l) {
326                         return;
327                 }
328                 _language = l;
329                 changed ();
330         }
331
332         void unset_language () {
333                 if (!_language) {
334                         return;
335                 }
336
337                 _language = boost::none;
338                 changed ();
339         }
340
341         void set_default_still_length (int s) {
342                 maybe_set (_default_still_length, s);
343         }
344
345         void set_default_container (Ratio const * c) {
346                 maybe_set (_default_container, c);
347         }
348
349         void set_default_dcp_content_type (DCPContentType const * t) {
350                 maybe_set (_default_dcp_content_type, t);
351         }
352
353         void set_dcp_issuer (std::string i) {
354                 maybe_set (_dcp_issuer, i);
355         }
356
357         void set_dcp_creator (std::string c) {
358                 maybe_set (_dcp_creator, c);
359         }
360
361         void set_default_j2k_bandwidth (int b) {
362                 maybe_set (_default_j2k_bandwidth, b);
363         }
364
365         void set_default_audio_delay (int d) {
366                 maybe_set (_default_audio_delay, d);
367         }
368
369         void set_default_interop (bool i) {
370                 maybe_set (_default_interop, i);
371         }
372
373         void set_mail_server (std::string s) {
374                 maybe_set (_mail_server, s);
375         }
376
377         void set_mail_port (int p) {
378                 maybe_set (_mail_port, p);
379         }
380
381         void set_mail_user (std::string u) {
382                 maybe_set (_mail_user, u);
383         }
384
385         void set_mail_password (std::string p) {
386                 maybe_set (_mail_password, p);
387         }
388
389         void set_kdm_subject (std::string s) {
390                 maybe_set (_kdm_subject, s);
391         }
392
393         void set_kdm_from (std::string f) {
394                 maybe_set (_kdm_from, f);
395         }
396
397         void set_kdm_cc (std::vector<std::string> f) {
398                 maybe_set (_kdm_cc, f);
399         }
400
401         void set_kdm_bcc (std::string f) {
402                 maybe_set (_kdm_bcc, f);
403         }
404
405         void set_kdm_email (std::string e) {
406                 maybe_set (_kdm_email, e);
407         }
408
409         void reset_kdm_email ();
410
411         void set_signer_chain (boost::shared_ptr<const dcp::CertificateChain> s) {
412                 maybe_set (_signer_chain, s);
413         }
414
415         void set_decryption_chain (boost::shared_ptr<const dcp::CertificateChain> c) {
416                 maybe_set (_decryption_chain, c);
417         }
418
419         void set_check_for_updates (bool c) {
420                 maybe_set (_check_for_updates, c);
421                 if (!c) {
422                         set_check_for_test_updates (false);
423                 }
424         }
425
426         void set_check_for_test_updates (bool c) {
427                 maybe_set (_check_for_test_updates, c);
428         }
429
430         void set_maximum_j2k_bandwidth (int b) {
431                 maybe_set (_maximum_j2k_bandwidth, b);
432         }
433
434         void set_log_types (int t) {
435                 maybe_set (_log_types, t);
436         }
437
438         void set_analyse_ebur128 (bool a) {
439                 maybe_set (_analyse_ebur128, a);
440         }
441
442         void set_automatic_audio_analysis (bool a) {
443                 maybe_set (_automatic_audio_analysis, a);
444         }
445
446 #ifdef DCPOMATIC_WINDOWS
447         void set_win32_console (bool c) {
448                 maybe_set (_win32_console, c);
449         }
450 #endif
451
452         void set_dkdms (std::vector<dcp::EncryptedKDM> dkdms)
453         {
454                 _dkdms = dkdms;
455                 changed ();
456         }
457
458         void set_cinemas_file (boost::filesystem::path file);
459
460         void clear_history () {
461                 _history.clear ();
462                 changed ();
463         }
464
465         void add_to_history (boost::filesystem::path p);
466
467         void changed (Property p = OTHER);
468         boost::signals2::signal<void (Property)> Changed;
469
470         void write () const;
471
472         static Config* instance ();
473         static void drop ();
474         static void restore_defaults ();
475         static bool have_existing (std::string);
476
477 private:
478         Config ();
479         static boost::filesystem::path path (std::string file, bool create_directories = true);
480         void read ();
481         void set_defaults ();
482         void set_kdm_email_to_default ();
483         void write_config_xml () const;
484         void write_cinemas_xml () const;
485         void read_cinemas (cxml::Document const & f);
486         boost::shared_ptr<dcp::CertificateChain> create_certificate_chain ();
487
488         template <class T>
489         void maybe_set (T& member, T new_value) {
490                 if (member == new_value) {
491                         return;
492                 }
493                 member = new_value;
494                 changed ();
495         }
496
497         /** number of threads to use for J2K encoding on the local machine */
498         int _num_local_encoding_threads;
499         /** default directory to put new films in */
500         boost::filesystem::path _default_directory;
501         /** base port number to use for J2K encoding servers;
502          *  this port and the two above it will be used.
503          */
504         int _server_port_base;
505         /** true to broadcast on the `any' address to look for servers */
506         bool _use_any_servers;
507         /** J2K encoding servers that should definitely be used */
508         std::vector<std::string> _servers;
509         bool _only_servers_encode;
510         Protocol _tms_protocol;
511         /** The IP address of a TMS that we can copy DCPs to */
512         std::string _tms_ip;
513         /** The path on a TMS that we should write DCPs to */
514         std::string _tms_path;
515         /** User name to log into the TMS with */
516         std::string _tms_user;
517         /** Password to log into the TMS with */
518         std::string _tms_password;
519         /** Our cinema sound processor */
520         CinemaSoundProcessor const * _cinema_sound_processor;
521         std::list<int> _allowed_dcp_frame_rates;
522         /** Allow any video frame rate for the DCP; if true, overrides _allowed_dcp_frame_rates */
523         bool _allow_any_dcp_frame_rate;
524         /** Default ISDCF metadata for newly-created Films */
525         ISDCFMetadata _default_isdcf_metadata;
526         boost::optional<std::string> _language;
527         /** Default length of still image content (seconds) */
528         int _default_still_length;
529         Ratio const * _default_container;
530         DCPContentType const * _default_dcp_content_type;
531         std::string _dcp_issuer;
532         std::string _dcp_creator;
533         int _default_j2k_bandwidth;
534         int _default_audio_delay;
535         bool _default_interop;
536         std::list<boost::shared_ptr<Cinema> > _cinemas;
537         std::string _mail_server;
538         int _mail_port;
539         std::string _mail_user;
540         std::string _mail_password;
541         std::string _kdm_subject;
542         std::string _kdm_from;
543         std::vector<std::string> _kdm_cc;
544         std::string _kdm_bcc;
545         std::string _kdm_email;
546         boost::shared_ptr<const dcp::CertificateChain> _signer_chain;
547         /** Chain used to decrypt KDMs; the leaf of this chain is the target
548          *  certificate for making KDMs given to DCP-o-matic.
549          */
550         boost::shared_ptr<const dcp::CertificateChain> _decryption_chain;
551         /** true to check for updates on startup */
552         bool _check_for_updates;
553         bool _check_for_test_updates;
554         /** maximum allowed J2K bandwidth in bits per second */
555         int _maximum_j2k_bandwidth;
556         int _log_types;
557         bool _analyse_ebur128;
558         bool _automatic_audio_analysis;
559 #ifdef DCPOMATIC_WINDOWS
560         bool _win32_console;
561 #endif
562         std::vector<boost::filesystem::path> _history;
563         std::vector<dcp::EncryptedKDM> _dkdms;
564         boost::filesystem::path _cinemas_file;
565
566         /** Singleton instance, or 0 */
567         static Config* _instance;
568 };
569
570 #endif