Configurable default format and content type (#133).
[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 "lib/config.h"
30 #include "lib/server.h"
31 #include "lib/format.h"
32 #include "lib/scaler.h"
33 #include "lib/filter.h"
34 #include "lib/dcp_content_type.h"
35 #include "config_dialog.h"
36 #include "wx_util.h"
37 #include "filter_dialog.h"
38 #include "server_dialog.h"
39 #include "dir_picker_ctrl.h"
40 #include "dci_metadata_dialog.h"
41
42 using namespace std;
43 using boost::bind;
44
45 ConfigDialog::ConfigDialog (wxWindow* parent)
46         : wxDialog (parent, wxID_ANY, _("DCP-o-matic Preferences"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
47 {
48         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
49         _notebook = new wxNotebook (this, wxID_ANY);
50         s->Add (_notebook, 1);
51
52         make_misc_panel ();
53         _notebook->AddPage (_misc_panel, _("Miscellaneous"), true);
54         make_servers_panel ();
55         _notebook->AddPage (_servers_panel, _("Encoding servers"), false);
56         make_tms_panel ();
57         _notebook->AddPage (_tms_panel, _("TMS"), false);
58         make_ab_panel ();
59         _notebook->AddPage (_ab_panel, _("A/B mode"), false);
60
61         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
62         overall_sizer->Add (s, 1, wxEXPAND | wxALL, 6);
63
64         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
65         if (buttons) {
66                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
67         }
68
69         SetSizer (overall_sizer);
70         overall_sizer->Layout ();
71         overall_sizer->SetSizeHints (this);
72 }
73
74 void
75 ConfigDialog::make_misc_panel ()
76 {
77         _misc_panel = new wxPanel (_notebook);
78         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
79         _misc_panel->SetSizer (s);
80
81         wxFlexGridSizer* table = new wxFlexGridSizer (3, 6, 6);
82         table->AddGrowableCol (1, 1);
83         s->Add (table, 1, wxALL | wxEXPAND, 8);
84
85         _set_language = new wxCheckBox (_misc_panel, wxID_ANY, _("Set language"));
86         table->Add (_set_language, 1, wxEXPAND);
87         _language = new wxChoice (_misc_panel, wxID_ANY);
88         _language->Append (wxT ("English"));
89         _language->Append (wxT ("Français"));
90         _language->Append (wxT ("Italiano"));
91         _language->Append (wxT ("Español"));
92         _language->Append (wxT ("Svenska"));
93         table->Add (_language, 1, wxEXPAND);
94         table->AddSpacer (0);
95
96         wxStaticText* restart = add_label_to_sizer (table, _misc_panel, _("(restart DCP-o-matic to see language changes)"));
97         wxFont font = restart->GetFont();
98         font.SetStyle (wxFONTSTYLE_ITALIC);
99         font.SetPointSize (font.GetPointSize() - 1);
100         restart->SetFont (font);
101         table->AddSpacer (0);
102         table->AddSpacer (0);
103
104         add_label_to_sizer (table, _misc_panel, _("Threads to use for encoding on this host"));
105         _num_local_encoding_threads = new wxSpinCtrl (_misc_panel);
106         table->Add (_num_local_encoding_threads, 1, wxEXPAND);
107         table->AddSpacer (0);
108
109         add_label_to_sizer (table, _misc_panel, _("Default directory for new films"));
110 #ifdef __WXMSW__
111         _default_directory = new DirPickerCtrl (_misc_panel);
112 #else   
113         _default_directory = new wxDirPickerCtrl (_misc_panel, wxDD_DIR_MUST_EXIST);
114 #endif
115         table->Add (_default_directory, 1, wxEXPAND);
116         table->AddSpacer (0);
117
118         add_label_to_sizer (table, _misc_panel, _("Default DCI name details"));
119         _default_dci_metadata_button = new wxButton (_misc_panel, wxID_ANY, _("Edit..."));
120         table->Add (_default_dci_metadata_button);
121         table->AddSpacer (1);
122
123         add_label_to_sizer (table, _misc_panel, _("Default format"));
124         _default_format = new wxChoice (_misc_panel, wxID_ANY);
125         table->Add (_default_format);
126         table->AddSpacer (1);
127
128         add_label_to_sizer (table, _misc_panel, _("Default content type"));
129         _default_dcp_content_type = new wxChoice (_misc_panel, wxID_ANY);
130         table->Add (_default_dcp_content_type);
131         table->AddSpacer (1);
132         
133         Config* config = Config::instance ();
134
135         _set_language->SetValue (config->language ());
136
137         if (config->language().get_value_or ("") == "fr") {
138                 _language->SetSelection (1);
139         } else if (config->language().get_value_or ("") == "it") {
140                 _language->SetSelection (2);
141         } else if (config->language().get_value_or ("") == "es") {
142                 _language->SetSelection (3);
143         } else if (config->language().get_value_or ("") == "sv") {
144                 _language->SetSelection (4);
145         } else {
146                 _language->SetSelection (0);
147         }
148
149         setup_language_sensitivity ();
150
151         _set_language->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (ConfigDialog::set_language_changed), 0, this);
152         _language->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (ConfigDialog::language_changed), 0, this);
153
154         _num_local_encoding_threads->SetRange (1, 128);
155         _num_local_encoding_threads->SetValue (config->num_local_encoding_threads ());
156         _num_local_encoding_threads->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::num_local_encoding_threads_changed), 0, this);
157
158         _default_directory->SetPath (std_to_wx (config->default_directory_or (wx_to_std (wxStandardPaths::Get().GetDocumentsDir()))));
159         _default_directory->Connect (wxID_ANY, wxEVT_COMMAND_DIRPICKER_CHANGED, wxCommandEventHandler (ConfigDialog::default_directory_changed), 0, this);
160
161         _default_dci_metadata_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_default_dci_metadata_clicked), 0, this);
162
163         vector<Format const *> fmt = Format::all ();
164         int n = 0;
165         for (vector<Format const *>::iterator i = fmt.begin(); i != fmt.end(); ++i) {
166                 _default_format->Append (std_to_wx ((*i)->name ()));
167                 if (*i == config->default_format ()) {
168                         _default_format->SetSelection (n);
169                 }
170                 ++n;
171         }
172
173         _default_format->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (ConfigDialog::default_format_changed), 0, this);
174         
175         vector<DCPContentType const *> const ct = DCPContentType::all ();
176         n = 0;
177         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
178                 _default_dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
179                 if (*i == config->default_dcp_content_type ()) {
180                         _default_dcp_content_type->SetSelection (n);
181                 }
182                 ++n;
183         }
184
185         _default_dcp_content_type->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (ConfigDialog::default_dcp_content_type_changed), 0, this);
186 }
187
188 void
189 ConfigDialog::make_tms_panel ()
190 {
191         _tms_panel = new wxPanel (_notebook);
192         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
193         _tms_panel->SetSizer (s);
194
195         wxFlexGridSizer* table = new wxFlexGridSizer (2, 6, 6);
196         table->AddGrowableCol (1, 1);
197         s->Add (table, 1, wxALL | wxEXPAND, 8);
198
199         add_label_to_sizer (table, _tms_panel, _("IP address"));
200         _tms_ip = new wxTextCtrl (_tms_panel, wxID_ANY);
201         table->Add (_tms_ip, 1, wxEXPAND);
202
203         add_label_to_sizer (table, _tms_panel, _("Target path"));
204         _tms_path = new wxTextCtrl (_tms_panel, wxID_ANY);
205         table->Add (_tms_path, 1, wxEXPAND);
206
207         add_label_to_sizer (table, _tms_panel, _("User name"));
208         _tms_user = new wxTextCtrl (_tms_panel, wxID_ANY);
209         table->Add (_tms_user, 1, wxEXPAND);
210
211         add_label_to_sizer (table, _tms_panel, _("Password"));
212         _tms_password = new wxTextCtrl (_tms_panel, wxID_ANY);
213         table->Add (_tms_password, 1, wxEXPAND);
214
215         Config* config = Config::instance ();
216         
217         _tms_ip->SetValue (std_to_wx (config->tms_ip ()));
218         _tms_ip->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_ip_changed), 0, this);
219         _tms_path->SetValue (std_to_wx (config->tms_path ()));
220         _tms_path->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_path_changed), 0, this);
221         _tms_user->SetValue (std_to_wx (config->tms_user ()));
222         _tms_user->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_user_changed), 0, this);
223         _tms_password->SetValue (std_to_wx (config->tms_password ()));
224         _tms_password->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_password_changed), 0, this);
225 }
226
227 void
228 ConfigDialog::make_ab_panel ()
229 {
230         _ab_panel = new wxPanel (_notebook);
231         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
232         _ab_panel->SetSizer (s);
233
234         wxFlexGridSizer* table = new wxFlexGridSizer (3, 6, 6);
235         table->AddGrowableCol (1, 1);
236         s->Add (table, 1, wxALL, 8);
237         
238         add_label_to_sizer (table, _ab_panel, _("Reference scaler"));
239         _reference_scaler = new wxChoice (_ab_panel, wxID_ANY);
240         vector<Scaler const *> const sc = Scaler::all ();
241         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
242                 _reference_scaler->Append (std_to_wx ((*i)->name ()));
243         }
244
245         table->Add (_reference_scaler, 1, wxEXPAND);
246         table->AddSpacer (0);
247
248         {
249                 add_label_to_sizer (table, _ab_panel, _("Reference filters"));
250                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
251                 _reference_filters = new wxStaticText (_ab_panel, wxID_ANY, wxT (""));
252                 s->Add (_reference_filters, 1, wxALIGN_CENTER_VERTICAL | wxEXPAND | wxALL, 6);
253                 _reference_filters_button = new wxButton (_ab_panel, wxID_ANY, _("Edit..."));
254                 s->Add (_reference_filters_button, 0);
255                 table->Add (s, 1, wxEXPAND);
256                 table->AddSpacer (0);
257         }
258
259         Config* config = Config::instance ();
260         
261         _reference_scaler->SetSelection (Scaler::as_index (config->reference_scaler ()));
262         _reference_scaler->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (ConfigDialog::reference_scaler_changed), 0, this);
263
264         pair<string, string> p = Filter::ffmpeg_strings (config->reference_filters ());
265         _reference_filters->SetLabel (std_to_wx (p.first) + N_(" ") + std_to_wx (p.second));
266         _reference_filters_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_reference_filters_clicked), 0, this);
267 }
268
269 void
270 ConfigDialog::make_servers_panel ()
271 {
272         _servers_panel = new wxPanel (_notebook);
273         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
274         _servers_panel->SetSizer (s);
275
276         wxFlexGridSizer* table = new wxFlexGridSizer (2, 6, 6);
277         table->AddGrowableCol (0, 1);
278         s->Add (table, 1, wxALL | wxEXPAND, 8);
279
280         Config* config = Config::instance ();
281
282         _servers = new wxListCtrl (_servers_panel, wxID_ANY, wxDefaultPosition, wxSize (220, 100), wxLC_REPORT | wxLC_SINGLE_SEL);
283         wxListItem ip;
284         ip.SetId (0);
285         ip.SetText (_("IP address"));
286         ip.SetWidth (120);
287         _servers->InsertColumn (0, ip);
288         ip.SetId (1);
289         ip.SetText (_("Threads"));
290         ip.SetWidth (80);
291         _servers->InsertColumn (1, ip);
292         table->Add (_servers, 1, wxEXPAND | wxALL);
293
294         {
295                 wxSizer* s = new wxBoxSizer (wxVERTICAL);
296                 _add_server = new wxButton (_servers_panel, wxID_ANY, _("Add"));
297                 s->Add (_add_server);
298                 _edit_server = new wxButton (_servers_panel, wxID_ANY, _("Edit"));
299                 s->Add (_edit_server);
300                 _remove_server = new wxButton (_servers_panel, wxID_ANY, _("Remove"));
301                 s->Add (_remove_server);
302                 table->Add (s, 0);
303         }
304
305         vector<ServerDescription*> servers = config->servers ();
306         for (vector<ServerDescription*>::iterator i = servers.begin(); i != servers.end(); ++i) {
307                 add_server_to_control (*i);
308         }
309         
310         _add_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::add_server_clicked), 0, this);
311         _edit_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_server_clicked), 0, this);
312         _remove_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::remove_server_clicked), 0, this);
313
314         _servers->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler (ConfigDialog::server_selection_changed), 0, this);
315         _servers->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_DESELECTED, wxListEventHandler (ConfigDialog::server_selection_changed), 0, this);
316         wxListEvent ev;
317         server_selection_changed (ev);
318 }
319
320 void
321 ConfigDialog::language_changed (wxCommandEvent &)
322 {
323         switch (_language->GetSelection ()) {
324         case 0:
325                 Config::instance()->set_language ("en");
326                 break;
327         case 1:
328                 Config::instance()->set_language ("fr");
329                 break;
330         case 2:
331                 Config::instance()->set_language ("it");
332                 break;
333         case 3:
334                 Config::instance()->set_language ("es");
335                 break;
336         case 4:
337                 Config::instance()->set_language ("sv");
338                 break;
339         }
340 }
341
342 void
343 ConfigDialog::tms_ip_changed (wxCommandEvent &)
344 {
345         Config::instance()->set_tms_ip (wx_to_std (_tms_ip->GetValue ()));
346 }
347
348 void
349 ConfigDialog::tms_path_changed (wxCommandEvent &)
350 {
351         Config::instance()->set_tms_path (wx_to_std (_tms_path->GetValue ()));
352 }
353
354 void
355 ConfigDialog::tms_user_changed (wxCommandEvent &)
356 {
357         Config::instance()->set_tms_user (wx_to_std (_tms_user->GetValue ()));
358 }
359
360 void
361 ConfigDialog::tms_password_changed (wxCommandEvent &)
362 {
363         Config::instance()->set_tms_password (wx_to_std (_tms_password->GetValue ()));
364 }
365
366 void
367 ConfigDialog::num_local_encoding_threads_changed (wxCommandEvent &)
368 {
369         Config::instance()->set_num_local_encoding_threads (_num_local_encoding_threads->GetValue ());
370 }
371
372 void
373 ConfigDialog::default_directory_changed (wxCommandEvent &)
374 {
375         Config::instance()->set_default_directory (wx_to_std (_default_directory->GetPath ()));
376 }
377
378 void
379 ConfigDialog::add_server_to_control (ServerDescription* s)
380 {
381         wxListItem item;
382         int const n = _servers->GetItemCount ();
383         item.SetId (n);
384         _servers->InsertItem (item);
385         _servers->SetItem (n, 0, std_to_wx (s->host_name ()));
386         _servers->SetItem (n, 1, std_to_wx (boost::lexical_cast<string> (s->threads ())));
387 }
388
389 void
390 ConfigDialog::add_server_clicked (wxCommandEvent &)
391 {
392         ServerDialog* d = new ServerDialog (this, 0);
393         d->ShowModal ();
394         ServerDescription* s = d->server ();
395         d->Destroy ();
396         
397         add_server_to_control (s);
398         vector<ServerDescription*> o = Config::instance()->servers ();
399         o.push_back (s);
400         Config::instance()->set_servers (o);
401 }
402
403 void
404 ConfigDialog::edit_server_clicked (wxCommandEvent &)
405 {
406         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
407         if (i == -1) {
408                 return;
409         }
410
411         wxListItem item;
412         item.SetId (i);
413         item.SetColumn (0);
414         _servers->GetItem (item);
415
416         vector<ServerDescription*> servers = Config::instance()->servers ();
417         assert (i >= 0 && i < int (servers.size ()));
418
419         ServerDialog* d = new ServerDialog (this, servers[i]);
420         d->ShowModal ();
421         d->Destroy ();
422
423         _servers->SetItem (i, 0, std_to_wx (servers[i]->host_name ()));
424         _servers->SetItem (i, 1, std_to_wx (boost::lexical_cast<string> (servers[i]->threads ())));
425 }
426
427 void
428 ConfigDialog::remove_server_clicked (wxCommandEvent &)
429 {
430         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
431         if (i >= 0) {
432                 _servers->DeleteItem (i);
433         }
434
435         vector<ServerDescription*> o = Config::instance()->servers ();
436         o.erase (o.begin() + i);
437         Config::instance()->set_servers (o);
438 }
439
440 void
441 ConfigDialog::server_selection_changed (wxListEvent &)
442 {
443         int const i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
444         _edit_server->Enable (i >= 0);
445         _remove_server->Enable (i >= 0);
446 }
447
448 void
449 ConfigDialog::reference_scaler_changed (wxCommandEvent &)
450 {
451         int const n = _reference_scaler->GetSelection ();
452         if (n >= 0) {
453                 Config::instance()->set_reference_scaler (Scaler::from_index (n));
454         }
455 }
456
457 void
458 ConfigDialog::edit_reference_filters_clicked (wxCommandEvent &)
459 {
460         FilterDialog* d = new FilterDialog (this, Config::instance()->reference_filters ());
461         d->ActiveChanged.connect (boost::bind (&ConfigDialog::reference_filters_changed, this, _1));
462         d->ShowModal ();
463         d->Destroy ();
464 }
465
466 void
467 ConfigDialog::reference_filters_changed (vector<Filter const *> f)
468 {
469         Config::instance()->set_reference_filters (f);
470         pair<string, string> p = Filter::ffmpeg_strings (Config::instance()->reference_filters ());
471         _reference_filters->SetLabel (std_to_wx (p.first) + N_(" ") + std_to_wx (p.second));
472 }
473
474 void
475 ConfigDialog::edit_default_dci_metadata_clicked (wxCommandEvent &)
476 {
477         DCIMetadataDialog* d = new DCIMetadataDialog (this, Config::instance()->default_dci_metadata ());
478         d->ShowModal ();
479         Config::instance()->set_default_dci_metadata (d->dci_metadata ());
480         d->Destroy ();
481 }
482
483 void
484 ConfigDialog::set_language_changed (wxCommandEvent& ev)
485 {
486         setup_language_sensitivity ();
487         if (_set_language->GetValue ()) {
488                 language_changed (ev);
489         } else {
490                 Config::instance()->unset_language ();
491         }
492 }
493
494 void
495 ConfigDialog::setup_language_sensitivity ()
496 {
497         _language->Enable (_set_language->GetValue ());
498 }
499
500 void
501 ConfigDialog::default_format_changed (wxCommandEvent &)
502 {
503         vector<Format const *> fmt = Format::all ();
504         Config::instance()->set_default_format (fmt[_default_format->GetSelection()]);
505 }
506
507 void
508 ConfigDialog::default_dcp_content_type_changed (wxCommandEvent &)
509 {
510         vector<DCPContentType const *> ct = DCPContentType::all ();
511         Config::instance()->set_default_dcp_content_type (ct[_default_dcp_content_type->GetSelection()]);
512 }