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