Allow mail server / KDM from email configuration.
[dcpomatic.git] / src / wx / config_dialog.cc
1 /*
2     Copyright (C) 2012 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_dialog.cc
21  *  @brief A dialogue to edit DCP-o-matic configuration.
22  */
23
24 #include <iostream>
25 #include <boost/lexical_cast.hpp>
26 #include <boost/filesystem.hpp>
27 #include <wx/stdpaths.h>
28 #include <wx/notebook.h>
29 #include <libdcp/colour_matrix.h>
30 #include "lib/config.h"
31 #include "lib/server.h"
32 #include "lib/ratio.h"
33 #include "lib/scaler.h"
34 #include "lib/filter.h"
35 #include "lib/dcp_content_type.h"
36 #include "lib/colour_conversion.h"
37 #include "config_dialog.h"
38 #include "wx_util.h"
39 #include "filter_dialog.h"
40 #include "server_dialog.h"
41 #include "dir_picker_ctrl.h"
42 #include "dci_metadata_dialog.h"
43 #include "preset_colour_conversion_dialog.h"
44
45 using std::vector;
46 using std::string;
47 using std::list;
48 using boost::bind;
49 using boost::shared_ptr;
50 using boost::lexical_cast;
51
52 ConfigDialog::ConfigDialog (wxWindow* parent)
53         : wxDialog (parent, wxID_ANY, _("DCP-o-matic Preferences"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
54 {
55         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
56         _notebook = new wxNotebook (this, wxID_ANY);
57         s->Add (_notebook, 1);
58
59         make_misc_panel ();
60         _notebook->AddPage (_misc_panel, _("Miscellaneous"), true);
61         make_servers_panel ();
62         _notebook->AddPage (_servers_panel, _("Encoding servers"), false);
63         make_colour_conversions_panel ();
64         _notebook->AddPage (_colour_conversions_panel, _("Colour conversions"), false);
65         make_metadata_panel ();
66         _notebook->AddPage (_metadata_panel, _("Metadata"), false);
67         make_tms_panel ();
68         _notebook->AddPage (_tms_panel, _("TMS"), false);
69
70         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
71         overall_sizer->Add (s, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
72
73         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
74         if (buttons) {
75                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
76         }
77
78         SetSizer (overall_sizer);
79         overall_sizer->Layout ();
80         overall_sizer->SetSizeHints (this);
81 }
82
83 void
84 ConfigDialog::make_misc_panel ()
85 {
86         _misc_panel = new wxPanel (_notebook);
87         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
88         _misc_panel->SetSizer (s);
89
90         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
91         table->AddGrowableCol (1, 1);
92         s->Add (table, 1, wxALL | wxEXPAND, 8);
93
94         _set_language = new wxCheckBox (_misc_panel, wxID_ANY, _("Set language"));
95         table->Add (_set_language, 1);
96         _language = new wxChoice (_misc_panel, wxID_ANY);
97         _language->Append (wxT ("English"));
98         _language->Append (wxT ("Français"));
99         _language->Append (wxT ("Italiano"));
100         _language->Append (wxT ("Español"));
101         _language->Append (wxT ("Svenska"));
102         table->Add (_language);
103
104         wxStaticText* restart = add_label_to_sizer (table, _misc_panel, _("(restart DCP-o-matic to see language changes)"), false);
105         wxFont font = restart->GetFont();
106         font.SetStyle (wxFONTSTYLE_ITALIC);
107         font.SetPointSize (font.GetPointSize() - 1);
108         restart->SetFont (font);
109         table->AddSpacer (0);
110
111         add_label_to_sizer (table, _misc_panel, _("Threads to use for encoding on this host"), true);
112         _num_local_encoding_threads = new wxSpinCtrl (_misc_panel);
113         table->Add (_num_local_encoding_threads, 1);
114
115         add_label_to_sizer (table, _misc_panel, _("Outgoing mail server"), true);
116         _mail_server = new wxTextCtrl (_misc_panel, wxID_ANY);
117         table->Add (_mail_server, 1, wxEXPAND | wxALL);
118
119         add_label_to_sizer (table, _misc_panel, _("From address for KDM emails"), true);
120         _kdm_from = new wxTextCtrl (_misc_panel, wxID_ANY);
121         table->Add (_kdm_from, 1, wxEXPAND | wxALL);
122         
123         {
124                 add_label_to_sizer (table, _misc_panel, _("Default duration of still images"), true);
125                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
126                 _default_still_length = new wxSpinCtrl (_misc_panel);
127                 s->Add (_default_still_length);
128                 add_label_to_sizer (s, _misc_panel, _("s"), false);
129                 table->Add (s, 1);
130         }
131
132         add_label_to_sizer (table, _misc_panel, _("Default directory for new films"), true);
133 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
134         _default_directory = new DirPickerCtrl (_misc_panel);
135 #else   
136         _default_directory = new wxDirPickerCtrl (_misc_panel, wxDD_DIR_MUST_EXIST);
137 #endif
138         table->Add (_default_directory, 1, wxEXPAND);
139
140         add_label_to_sizer (table, _misc_panel, _("Default DCI name details"), true);
141         _default_dci_metadata_button = new wxButton (_misc_panel, wxID_ANY, _("Edit..."));
142         table->Add (_default_dci_metadata_button);
143
144         add_label_to_sizer (table, _misc_panel, _("Default container"), true);
145         _default_container = new wxChoice (_misc_panel, wxID_ANY);
146         table->Add (_default_container);
147
148         add_label_to_sizer (table, _misc_panel, _("Default content type"), true);
149         _default_dcp_content_type = new wxChoice (_misc_panel, wxID_ANY);
150         table->Add (_default_dcp_content_type);
151
152         {
153                 add_label_to_sizer (table, _misc_panel, _("Default JPEG2000 bandwidth"), true);
154                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
155                 _default_j2k_bandwidth = new wxSpinCtrl (_misc_panel);
156                 s->Add (_default_j2k_bandwidth);
157                 add_label_to_sizer (s, _misc_panel, _("MBps"), false);
158                 table->Add (s, 1);
159         }
160         
161         Config* config = Config::instance ();
162
163         _set_language->SetValue (config->language ());
164
165         if (config->language().get_value_or ("") == "fr") {
166                 _language->SetSelection (1);
167         } else if (config->language().get_value_or ("") == "it") {
168                 _language->SetSelection (2);
169         } else if (config->language().get_value_or ("") == "es") {
170                 _language->SetSelection (3);
171         } else if (config->language().get_value_or ("") == "sv") {
172                 _language->SetSelection (4);
173         } else {
174                 _language->SetSelection (0);
175         }
176
177         setup_language_sensitivity ();
178
179         _set_language->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ConfigDialog::set_language_changed, this));
180         _language->Bind     (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&ConfigDialog::language_changed,     this));
181
182         _num_local_encoding_threads->SetRange (1, 128);
183         _num_local_encoding_threads->SetValue (config->num_local_encoding_threads ());
184         _num_local_encoding_threads->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&ConfigDialog::num_local_encoding_threads_changed, this));
185
186         _mail_server->SetValue (std_to_wx (config->mail_server ()));
187         _mail_server->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ConfigDialog::mail_server_changed, this));
188         _kdm_from->SetValue (std_to_wx (config->kdm_from ()));
189         _kdm_from->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ConfigDialog::kdm_from_changed, this));
190
191         _default_still_length->SetRange (1, 3600);
192         _default_still_length->SetValue (config->default_still_length ());
193         _default_still_length->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&ConfigDialog::default_still_length_changed, this));
194
195         _default_directory->SetPath (std_to_wx (config->default_directory_or (wx_to_std (wxStandardPaths::Get().GetDocumentsDir())).string ()));
196         _default_directory->Bind (wxEVT_COMMAND_DIRPICKER_CHANGED, boost::bind (&ConfigDialog::default_directory_changed, this));
197
198         _default_dci_metadata_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ConfigDialog::edit_default_dci_metadata_clicked, this));
199
200         vector<Ratio const *> ratio = Ratio::all ();
201         int n = 0;
202         for (vector<Ratio const *>::iterator i = ratio.begin(); i != ratio.end(); ++i) {
203                 _default_container->Append (std_to_wx ((*i)->nickname ()));
204                 if (*i == config->default_container ()) {
205                         _default_container->SetSelection (n);
206                 }
207                 ++n;
208         }
209
210         _default_container->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ConfigDialog::default_container_changed, this));
211         
212         vector<DCPContentType const *> const ct = DCPContentType::all ();
213         n = 0;
214         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
215                 _default_dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
216                 if (*i == config->default_dcp_content_type ()) {
217                         _default_dcp_content_type->SetSelection (n);
218                 }
219                 ++n;
220         }
221
222         _default_dcp_content_type->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ConfigDialog::default_dcp_content_type_changed, this));
223
224         _default_j2k_bandwidth->SetRange (50, 250);
225         _default_j2k_bandwidth->SetValue (config->default_j2k_bandwidth() / 1e6);
226         _default_j2k_bandwidth->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&ConfigDialog::default_j2k_bandwidth_changed, this));
227 }
228
229 void
230 ConfigDialog::make_tms_panel ()
231 {
232         _tms_panel = new wxPanel (_notebook);
233         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
234         _tms_panel->SetSizer (s);
235
236         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
237         table->AddGrowableCol (1, 1);
238         s->Add (table, 1, wxALL | wxEXPAND, 8);
239
240         add_label_to_sizer (table, _tms_panel, _("IP address"), true);
241         _tms_ip = new wxTextCtrl (_tms_panel, wxID_ANY);
242         table->Add (_tms_ip, 1, wxEXPAND);
243
244         add_label_to_sizer (table, _tms_panel, _("Target path"), true);
245         _tms_path = new wxTextCtrl (_tms_panel, wxID_ANY);
246         table->Add (_tms_path, 1, wxEXPAND);
247
248         add_label_to_sizer (table, _tms_panel, _("User name"), true);
249         _tms_user = new wxTextCtrl (_tms_panel, wxID_ANY);
250         table->Add (_tms_user, 1, wxEXPAND);
251
252         add_label_to_sizer (table, _tms_panel, _("Password"), true);
253         _tms_password = new wxTextCtrl (_tms_panel, wxID_ANY);
254         table->Add (_tms_password, 1, wxEXPAND);
255
256         Config* config = Config::instance ();
257         
258         _tms_ip->SetValue (std_to_wx (config->tms_ip ()));
259         _tms_ip->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ConfigDialog::tms_ip_changed, this));
260         _tms_path->SetValue (std_to_wx (config->tms_path ()));
261         _tms_path->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ConfigDialog::tms_path_changed, this));
262         _tms_user->SetValue (std_to_wx (config->tms_user ()));
263         _tms_user->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ConfigDialog::tms_user_changed, this));
264         _tms_password->SetValue (std_to_wx (config->tms_password ()));
265         _tms_password->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ConfigDialog::tms_password_changed, this));
266 }
267
268 void
269 ConfigDialog::make_metadata_panel ()
270 {
271         _metadata_panel = new wxPanel (_notebook);
272         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
273         _metadata_panel->SetSizer (s);
274
275         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
276         table->AddGrowableCol (1, 1);
277         s->Add (table, 1, wxALL | wxEXPAND, 8);
278
279         add_label_to_sizer (table, _metadata_panel, _("Issuer"), true);
280         _issuer = new wxTextCtrl (_metadata_panel, wxID_ANY);
281         table->Add (_issuer, 1, wxEXPAND);
282
283         add_label_to_sizer (table, _metadata_panel, _("Creator"), true);
284         _creator = new wxTextCtrl (_metadata_panel, wxID_ANY);
285         table->Add (_creator, 1, wxEXPAND);
286
287         Config* config = Config::instance ();
288
289         _issuer->SetValue (std_to_wx (config->dcp_metadata().issuer));
290         _issuer->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ConfigDialog::issuer_changed, this));
291         _creator->SetValue (std_to_wx (config->dcp_metadata().creator));
292         _creator->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ConfigDialog::creator_changed, this));
293 }
294
295 static std::string
296 server_column (ServerDescription s, int c)
297 {
298         switch (c) {
299         case 0:
300                 return s.host_name ();
301         case 1:
302                 return lexical_cast<string> (s.threads ());
303         }
304
305         return "";
306 }
307
308 void
309 ConfigDialog::make_servers_panel ()
310 {
311         vector<string> columns;
312         columns.push_back (wx_to_std (_("IP address")));
313         columns.push_back (wx_to_std (_("Threads")));
314         _servers_panel = new EditableList<ServerDescription, ServerDialog> (
315                 _notebook,
316                 columns,
317                 boost::bind (&Config::servers, Config::instance()),
318                 boost::bind (&Config::set_servers, Config::instance(), _1),
319                 boost::bind (&server_column, _1, _2)
320                 );
321 }
322
323 void
324 ConfigDialog::language_changed ()
325 {
326         switch (_language->GetSelection ()) {
327         case 0:
328                 Config::instance()->set_language ("en");
329                 break;
330         case 1:
331                 Config::instance()->set_language ("fr");
332                 break;
333         case 2:
334                 Config::instance()->set_language ("it");
335                 break;
336         case 3:
337                 Config::instance()->set_language ("es");
338                 break;
339         case 4:
340                 Config::instance()->set_language ("sv");
341                 break;
342         }
343 }
344
345 void
346 ConfigDialog::tms_ip_changed ()
347 {
348         Config::instance()->set_tms_ip (wx_to_std (_tms_ip->GetValue ()));
349 }
350
351 void
352 ConfigDialog::tms_path_changed ()
353 {
354         Config::instance()->set_tms_path (wx_to_std (_tms_path->GetValue ()));
355 }
356
357 void
358 ConfigDialog::tms_user_changed ()
359 {
360         Config::instance()->set_tms_user (wx_to_std (_tms_user->GetValue ()));
361 }
362
363 void
364 ConfigDialog::tms_password_changed ()
365 {
366         Config::instance()->set_tms_password (wx_to_std (_tms_password->GetValue ()));
367 }
368
369 void
370 ConfigDialog::num_local_encoding_threads_changed ()
371 {
372         Config::instance()->set_num_local_encoding_threads (_num_local_encoding_threads->GetValue ());
373 }
374
375 void
376 ConfigDialog::default_directory_changed ()
377 {
378         Config::instance()->set_default_directory (wx_to_std (_default_directory->GetPath ()));
379 }
380
381 void
382 ConfigDialog::edit_default_dci_metadata_clicked ()
383 {
384         DCIMetadataDialog* d = new DCIMetadataDialog (this, Config::instance()->default_dci_metadata ());
385         d->ShowModal ();
386         Config::instance()->set_default_dci_metadata (d->dci_metadata ());
387         d->Destroy ();
388 }
389
390 void
391 ConfigDialog::set_language_changed ()
392 {
393         setup_language_sensitivity ();
394         if (_set_language->GetValue ()) {
395                 language_changed ();
396         } else {
397                 Config::instance()->unset_language ();
398         }
399 }
400
401 void
402 ConfigDialog::setup_language_sensitivity ()
403 {
404         _language->Enable (_set_language->GetValue ());
405 }
406
407 void
408 ConfigDialog::default_still_length_changed ()
409 {
410         Config::instance()->set_default_still_length (_default_still_length->GetValue ());
411 }
412
413 void
414 ConfigDialog::default_container_changed ()
415 {
416         vector<Ratio const *> ratio = Ratio::all ();
417         Config::instance()->set_default_container (ratio[_default_container->GetSelection()]);
418 }
419
420 void
421 ConfigDialog::default_dcp_content_type_changed ()
422 {
423         vector<DCPContentType const *> ct = DCPContentType::all ();
424         Config::instance()->set_default_dcp_content_type (ct[_default_dcp_content_type->GetSelection()]);
425 }
426
427 void
428 ConfigDialog::issuer_changed ()
429 {
430         libdcp::XMLMetadata m = Config::instance()->dcp_metadata ();
431         m.issuer = wx_to_std (_issuer->GetValue ());
432         Config::instance()->set_dcp_metadata (m);
433 }
434
435 void
436 ConfigDialog::creator_changed ()
437 {
438         libdcp::XMLMetadata m = Config::instance()->dcp_metadata ();
439         m.creator = wx_to_std (_creator->GetValue ());
440         Config::instance()->set_dcp_metadata (m);
441 }
442
443 void
444 ConfigDialog::default_j2k_bandwidth_changed ()
445 {
446         Config::instance()->set_default_j2k_bandwidth (_default_j2k_bandwidth->GetValue() * 1e6);
447 }
448
449 static std::string
450 colour_conversion_column (PresetColourConversion c)
451 {
452         return c.name;
453 }
454
455 void
456 ConfigDialog::make_colour_conversions_panel ()
457 {
458         vector<string> columns;
459         columns.push_back (wx_to_std (_("Name")));
460         _colour_conversions_panel = new EditableList<PresetColourConversion, PresetColourConversionDialog> (
461                 _notebook,
462                 columns,
463                 boost::bind (&Config::colour_conversions, Config::instance()),
464                 boost::bind (&Config::set_colour_conversions, Config::instance(), _1),
465                 boost::bind (&colour_conversion_column, _1)
466                 );
467 }
468
469 void
470 ConfigDialog::mail_server_changed ()
471 {
472         Config::instance()->set_mail_server (wx_to_std (_mail_server->GetValue ()));
473 }
474
475
476 void
477 ConfigDialog::kdm_from_changed ()
478 {
479         Config::instance()->set_kdm_from (wx_to_std (_kdm_from->GetValue ()));
480 }