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