Logging improvements to allow prettier displays in the server GUI.
[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         }
409
410         void set_check_for_test_updates (bool c) {
411                 maybe_set (_check_for_test_updates, c);
412         }
413
414         void set_maximum_j2k_bandwidth (int b) {
415                 maybe_set (_maximum_j2k_bandwidth, b);
416         }
417
418         void set_log_types (int t) {
419                 maybe_set (_log_types, t);
420         }
421
422         void set_automatic_audio_analysis (bool a) {
423                 maybe_set (_automatic_audio_analysis, a);
424         }
425
426 #ifdef DCPOMATIC_WINDOWS
427         void set_win32_console (bool c) {
428                 maybe_set (_win32_console, c);
429         }
430 #endif
431
432         void clear_history () {
433                 _history.clear ();
434                 changed ();
435         }
436
437         void add_to_history (boost::filesystem::path p);
438
439         void changed (Property p = OTHER);
440         boost::signals2::signal<void (Property)> Changed;
441
442         void write () const;
443
444         static Config* instance ();
445         static void drop ();
446         static void restore_defaults ();
447         static bool have_existing ();
448
449 private:
450         Config ();
451         static boost::filesystem::path file (bool create_directories = true);
452         void read ();
453         void set_defaults ();
454         void set_kdm_email_to_default ();
455
456         template <class T>
457         void maybe_set (T& member, T new_value) {
458                 if (member == new_value) {
459                         return;
460                 }
461                 member = new_value;
462                 changed ();
463         }
464
465         /** number of threads to use for J2K encoding on the local machine */
466         int _num_local_encoding_threads;
467         /** default directory to put new films in */
468         boost::filesystem::path _default_directory;
469         /** base port number to use for J2K encoding servers;
470          *  this port and the one above it will be used.
471          */
472         int _server_port_base;
473         /** true to broadcast on the `any' address to look for servers */
474         bool _use_any_servers;
475         /** J2K encoding servers that should definitely be used */
476         std::vector<std::string> _servers;
477         bool _only_servers_encode;
478         Protocol _tms_protocol;
479         /** The IP address of a TMS that we can copy DCPs to */
480         std::string _tms_ip;
481         /** The path on a TMS that we should write DCPs to */
482         std::string _tms_path;
483         /** User name to log into the TMS with */
484         std::string _tms_user;
485         /** Password to log into the TMS with */
486         std::string _tms_password;
487         /** Our cinema sound processor */
488         CinemaSoundProcessor const * _cinema_sound_processor;
489         std::list<int> _allowed_dcp_frame_rates;
490         /** Allow any video frame rate for the DCP; if true, overrides _allowed_dcp_frame_rates */
491         bool _allow_any_dcp_frame_rate;
492         /** Default ISDCF metadata for newly-created Films */
493         ISDCFMetadata _default_isdcf_metadata;
494         boost::optional<std::string> _language;
495         /** Default length of still image content (seconds) */
496         int _default_still_length;
497         Ratio const * _default_container;
498         DCPContentType const * _default_dcp_content_type;
499         std::string _dcp_issuer;
500         std::string _dcp_creator;
501         int _default_j2k_bandwidth;
502         int _default_audio_delay;
503         bool _default_interop;
504         std::list<boost::shared_ptr<Cinema> > _cinemas;
505         std::string _mail_server;
506         int _mail_port;
507         std::string _mail_user;
508         std::string _mail_password;
509         std::string _kdm_subject;
510         std::string _kdm_from;
511         std::string _kdm_cc;
512         std::string _kdm_bcc;
513         std::string _kdm_email;
514         boost::shared_ptr<const dcp::CertificateChain> _signer_chain;
515         /** Chain used to decrypt KDMs; the leaf of this chain is the target
516          *  certificate for making KDMs given to DCP-o-matic.
517          */
518         boost::shared_ptr<const dcp::CertificateChain> _decryption_chain;
519         /** true to check for updates on startup */
520         bool _check_for_updates;
521         bool _check_for_test_updates;
522         /** maximum allowed J2K bandwidth in bits per second */
523         int _maximum_j2k_bandwidth;
524         int _log_types;
525         bool _automatic_audio_analysis;
526 #ifdef DCPOMATIC_WINDOWS
527         bool _win32_console;
528 #endif
529         std::vector<boost::filesystem::path> _history;
530
531         /** Singleton instance, or 0 */
532         static Config* _instance;
533 };
534
535 #endif