fc63c518cd944b71aaca336c5c1f1944702dace2
[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         std::string mail_server () const {
170                 return _mail_server;
171         }
172
173         int mail_port () const {
174                 return _mail_port;
175         }
176
177         std::string mail_user () const {
178                 return _mail_user;
179         }
180
181         std::string mail_password () const {
182                 return _mail_password;
183         }
184
185         std::string kdm_subject () const {
186                 return _kdm_subject;
187         }
188
189         std::string kdm_from () const {
190                 return _kdm_from;
191         }
192
193         std::string kdm_cc () const {
194                 return _kdm_cc;
195         }
196
197         std::string kdm_bcc () const {
198                 return _kdm_bcc;
199         }
200
201         std::string kdm_email () const {
202                 return _kdm_email;
203         }
204
205         boost::shared_ptr<const dcp::CertificateChain> signer_chain () const {
206                 return _signer_chain;
207         }
208
209         boost::shared_ptr<const dcp::CertificateChain> decryption_chain () const {
210                 return _decryption_chain;
211         }
212
213         bool check_for_updates () const {
214                 return _check_for_updates;
215         }
216
217         bool check_for_test_updates () const {
218                 return _check_for_test_updates;
219         }
220
221         int maximum_j2k_bandwidth () const {
222                 return _maximum_j2k_bandwidth;
223         }
224
225         int log_types () const {
226                 return _log_types;
227         }
228
229 #ifdef DCPOMATIC_WINDOWS
230         bool win32_console () const {
231                 return _win32_console;
232         }
233 #endif
234
235         std::vector<boost::filesystem::path> history () const {
236                 return _history;
237         }
238
239         /** @param n New number of local encoding threads */
240         void set_num_local_encoding_threads (int n) {
241                 maybe_set (_num_local_encoding_threads, n);
242         }
243
244         void set_default_directory (boost::filesystem::path d) {
245                 maybe_set (_default_directory, d);
246         }
247
248         /** @param p New server port */
249         void set_server_port_base (int p) {
250                 maybe_set (_server_port_base, p);
251         }
252
253         void set_only_servers_encode (bool o) {
254                 maybe_set (_only_servers_encode, o);
255         }
256
257         void set_tms_protocol (Protocol p) {
258                 maybe_set (_tms_protocol, p);
259         }
260
261         /** @param i IP address of a TMS that we can copy DCPs to */
262         void set_tms_ip (std::string i) {
263                 maybe_set (_tms_ip, i);
264         }
265
266         /** @param p Path on a TMS that we should changed DCPs to */
267         void set_tms_path (std::string p) {
268                 maybe_set (_tms_path, p);
269         }
270
271         /** @param u User name to log into the TMS with */
272         void set_tms_user (std::string u) {
273                 maybe_set (_tms_user, u);
274         }
275
276         /** @param p Password to log into the TMS with */
277         void set_tms_password (std::string p) {
278                 maybe_set (_tms_password, p);
279         }
280
281         void add_cinema (boost::shared_ptr<Cinema> c) {
282                 _cinemas.push_back (c);
283                 changed ();
284         }
285
286         void remove_cinema (boost::shared_ptr<Cinema> c) {
287                 _cinemas.remove (c);
288                 changed ();
289         }
290
291         void set_allowed_dcp_frame_rates (std::list<int> const & r) {
292                 maybe_set (_allowed_dcp_frame_rates, r);
293         }
294
295         void set_allow_any_dcp_frame_rate (bool a) {
296                 maybe_set (_allow_any_dcp_frame_rate, a);
297         }
298
299         void set_default_isdcf_metadata (ISDCFMetadata d) {
300                 maybe_set (_default_isdcf_metadata, d);
301         }
302
303         void set_language (std::string l) {
304                 if (_language && _language.get() == l) {
305                         return;
306                 }
307                 _language = l;
308                 changed ();
309         }
310
311         void unset_language () {
312                 if (!_language) {
313                         return;
314                 }
315
316                 _language = boost::none;
317                 changed ();
318         }
319
320         void set_default_still_length (int s) {
321                 maybe_set (_default_still_length, s);
322         }
323
324         void set_default_container (Ratio const * c) {
325                 maybe_set (_default_container, c);
326         }
327
328         void set_default_dcp_content_type (DCPContentType const * t) {
329                 maybe_set (_default_dcp_content_type, t);
330         }
331
332         void set_dcp_issuer (std::string i) {
333                 maybe_set (_dcp_issuer, i);
334         }
335
336         void set_dcp_creator (std::string c) {
337                 maybe_set (_dcp_creator, c);
338         }
339
340         void set_default_j2k_bandwidth (int b) {
341                 maybe_set (_default_j2k_bandwidth, b);
342         }
343
344         void set_default_audio_delay (int d) {
345                 maybe_set (_default_audio_delay, d);
346         }
347
348         void set_mail_server (std::string s) {
349                 maybe_set (_mail_server, s);
350         }
351
352         void set_mail_port (int p) {
353                 maybe_set (_mail_port, p);
354         }
355
356         void set_mail_user (std::string u) {
357                 maybe_set (_mail_user, u);
358         }
359
360         void set_mail_password (std::string p) {
361                 maybe_set (_mail_password, p);
362         }
363
364         void set_kdm_subject (std::string s) {
365                 maybe_set (_kdm_subject, s);
366         }
367
368         void set_kdm_from (std::string f) {
369                 maybe_set (_kdm_from, f);
370         }
371
372         void set_kdm_cc (std::string f) {
373                 maybe_set (_kdm_cc, f);
374         }
375
376         void set_kdm_bcc (std::string f) {
377                 maybe_set (_kdm_bcc, f);
378         }
379
380         void set_kdm_email (std::string e) {
381                 maybe_set (_kdm_email, e);
382         }
383
384         void reset_kdm_email ();
385
386         void set_signer_chain (boost::shared_ptr<const dcp::CertificateChain> s) {
387                 maybe_set (_signer_chain, s);
388         }
389
390         void set_decryption_chain (boost::shared_ptr<const dcp::CertificateChain> c) {
391                 maybe_set (_decryption_chain, c);
392         }
393
394         void set_check_for_updates (bool c) {
395                 maybe_set (_check_for_updates, c);
396         }
397
398         void set_check_for_test_updates (bool c) {
399                 maybe_set (_check_for_test_updates, c);
400         }
401
402         void set_maximum_j2k_bandwidth (int b) {
403                 maybe_set (_maximum_j2k_bandwidth, b);
404         }
405
406         void set_log_types (int t) {
407                 maybe_set (_log_types, t);
408         }
409
410 #ifdef DCPOMATIC_WINDOWS
411         void set_win32_console (bool c) {
412                 maybe_set (_win32_console, c);
413         }
414 #endif
415
416         void clear_history () {
417                 _history.clear ();
418                 changed ();
419         }
420
421         void add_to_history (boost::filesystem::path p);
422
423         void changed (Property p = OTHER);
424         boost::signals2::signal<void (Property)> Changed;
425
426         void write () const;
427
428         static Config* instance ();
429         static void drop ();
430         static void restore_defaults ();
431         static bool have_existing ();
432
433 private:
434         Config ();
435         static boost::filesystem::path file (bool create_directories = true);
436         void read ();
437         void set_defaults ();
438         void set_kdm_email_to_default ();
439
440         template <class T>
441         void maybe_set (T& member, T new_value) {
442                 if (member == new_value) {
443                         return;
444                 }
445                 member = new_value;
446                 changed ();
447         }
448
449         /** number of threads to use for J2K encoding on the local machine */
450         int _num_local_encoding_threads;
451         /** default directory to put new films in */
452         boost::filesystem::path _default_directory;
453         /** base port number to use for J2K encoding servers;
454          *  this port and the one above it will be used.
455          */
456         int _server_port_base;
457         /** true to broadcast on the `any' address to look for servers */
458         bool _use_any_servers;
459         /** J2K encoding servers that should definitely be used */
460         std::vector<std::string> _servers;
461         bool _only_servers_encode;
462         Protocol _tms_protocol;
463         /** The IP address of a TMS that we can copy DCPs to */
464         std::string _tms_ip;
465         /** The path on a TMS that we should write DCPs to */
466         std::string _tms_path;
467         /** User name to log into the TMS with */
468         std::string _tms_user;
469         /** Password to log into the TMS with */
470         std::string _tms_password;
471         /** Our cinema sound processor */
472         CinemaSoundProcessor const * _cinema_sound_processor;
473         std::list<int> _allowed_dcp_frame_rates;
474         /** Allow any video frame rate for the DCP; if true, overrides _allowed_dcp_frame_rates */
475         bool _allow_any_dcp_frame_rate;
476         /** Default ISDCF metadata for newly-created Films */
477         ISDCFMetadata _default_isdcf_metadata;
478         boost::optional<std::string> _language;
479         /** Default length of still image content (seconds) */
480         int _default_still_length;
481         Ratio const * _default_container;
482         DCPContentType const * _default_dcp_content_type;
483         std::string _dcp_issuer;
484         std::string _dcp_creator;
485         int _default_j2k_bandwidth;
486         int _default_audio_delay;
487         std::list<boost::shared_ptr<Cinema> > _cinemas;
488         std::string _mail_server;
489         int _mail_port;
490         std::string _mail_user;
491         std::string _mail_password;
492         std::string _kdm_subject;
493         std::string _kdm_from;
494         std::string _kdm_cc;
495         std::string _kdm_bcc;
496         std::string _kdm_email;
497         boost::shared_ptr<const dcp::CertificateChain> _signer_chain;
498         /** Chain used to decrypt KDMs; the leaf of this chain is the target
499          *  certificate for making KDMs given to DCP-o-matic.
500          */
501         boost::shared_ptr<const dcp::CertificateChain> _decryption_chain;
502         /** true to check for updates on startup */
503         bool _check_for_updates;
504         bool _check_for_test_updates;
505         /** maximum allowed J2K bandwidth in bits per second */
506         int _maximum_j2k_bandwidth;
507         int _log_types;
508 #ifdef DCPOMATIC_WINDOWS
509         bool _win32_console;
510 #endif
511         std::vector<boost::filesystem::path> _history;
512
513         /** Singleton instance, or 0 */
514         static Config* _instance;
515 };
516
517 #endif