Add default J2K bandwidth option.
[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/ratio.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_metadata_panel ();
57         _notebook->AddPage (_metadata_panel, _("Metadata"), false);
58         make_tms_panel ();
59         _notebook->AddPage (_tms_panel, _("TMS"), 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, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
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)"), false);
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"), true);
105         _num_local_encoding_threads = new wxSpinCtrl (_misc_panel);
106         table->Add (_num_local_encoding_threads, 1);
107         table->AddSpacer (0);
108
109         add_label_to_sizer (table, _misc_panel, _("Default duration of still images"), true);
110         _default_still_length = new wxSpinCtrl (_misc_panel);
111         table->Add (_default_still_length, 1, wxEXPAND);
112         add_label_to_sizer (table, _misc_panel, _("s"), false);
113
114         add_label_to_sizer (table, _misc_panel, _("Default directory for new films"), true);
115 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
116         _default_directory = new DirPickerCtrl (_misc_panel);
117 #else   
118         _default_directory = new wxDirPickerCtrl (_misc_panel, wxDD_DIR_MUST_EXIST);
119 #endif
120         table->Add (_default_directory, 1, wxEXPAND);
121         table->AddSpacer (0);
122
123         add_label_to_sizer (table, _misc_panel, _("Default DCI name details"), true);
124         _default_dci_metadata_button = new wxButton (_misc_panel, wxID_ANY, _("Edit..."));
125         table->Add (_default_dci_metadata_button);
126         table->AddSpacer (1);
127
128         add_label_to_sizer (table, _misc_panel, _("Default container"), true);
129         _default_container = new wxChoice (_misc_panel, wxID_ANY);
130         table->Add (_default_container);
131         table->AddSpacer (1);
132
133         add_label_to_sizer (table, _misc_panel, _("Default content type"), true);
134         _default_dcp_content_type = new wxChoice (_misc_panel, wxID_ANY);
135         table->Add (_default_dcp_content_type);
136         table->AddSpacer (1);
137
138         add_label_to_sizer (table, _misc_panel, _("Default JPEG2000 bandwidth"), true);
139         _default_j2k_bandwidth = new wxSpinCtrl (_misc_panel);
140         table->Add (_default_j2k_bandwidth, 1);
141         add_label_to_sizer (table, _misc_panel, _("MBps"), false);
142         
143         Config* config = Config::instance ();
144
145         _set_language->SetValue (config->language ());
146
147         if (config->language().get_value_or ("") == "fr") {
148                 _language->SetSelection (1);
149         } else if (config->language().get_value_or ("") == "it") {
150                 _language->SetSelection (2);
151         } else if (config->language().get_value_or ("") == "es") {
152                 _language->SetSelection (3);
153         } else if (config->language().get_value_or ("") == "sv") {
154                 _language->SetSelection (4);
155         } else {
156                 _language->SetSelection (0);
157         }
158
159         setup_language_sensitivity ();
160
161         _set_language->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (ConfigDialog::set_language_changed), 0, this);
162         _language->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (ConfigDialog::language_changed), 0, this);
163
164         _num_local_encoding_threads->SetRange (1, 128);
165         _num_local_encoding_threads->SetValue (config->num_local_encoding_threads ());
166         _num_local_encoding_threads->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::num_local_encoding_threads_changed), 0, this);
167
168         _default_still_length->SetRange (1, 3600);
169         _default_still_length->SetValue (config->default_still_length ());
170         _default_still_length->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::default_still_length_changed), 0, this);
171
172         _default_directory->SetPath (std_to_wx (config->default_directory_or (wx_to_std (wxStandardPaths::Get().GetDocumentsDir()))));
173         _default_directory->Connect (wxID_ANY, wxEVT_COMMAND_DIRPICKER_CHANGED, wxCommandEventHandler (ConfigDialog::default_directory_changed), 0, this);
174
175         _default_dci_metadata_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_default_dci_metadata_clicked), 0, this);
176
177         vector<Ratio const *> ratio = Ratio::all ();
178         int n = 0;
179         for (vector<Ratio const *>::iterator i = ratio.begin(); i != ratio.end(); ++i) {
180                 _default_container->Append (std_to_wx ((*i)->nickname ()));
181                 if (*i == config->default_container ()) {
182                         _default_container->SetSelection (n);
183                 }
184                 ++n;
185         }
186
187         _default_container->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (ConfigDialog::default_container_changed), 0, this);
188         
189         vector<DCPContentType const *> const ct = DCPContentType::all ();
190         n = 0;
191         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
192                 _default_dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
193                 if (*i == config->default_dcp_content_type ()) {
194                         _default_dcp_content_type->SetSelection (n);
195                 }
196                 ++n;
197         }
198
199         _default_dcp_content_type->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (ConfigDialog::default_dcp_content_type_changed), 0, this);
200
201         _default_j2k_bandwidth->SetRange (50, 250);
202         _default_j2k_bandwidth->SetValue (config->default_j2k_bandwidth() / 1e6);
203         _default_j2k_bandwidth->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::default_j2k_bandwidth_changed), 0, this);
204 }
205
206 void
207 ConfigDialog::make_tms_panel ()
208 {
209         _tms_panel = new wxPanel (_notebook);
210         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
211         _tms_panel->SetSizer (s);
212
213         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
214         table->AddGrowableCol (1, 1);
215         s->Add (table, 1, wxALL | wxEXPAND, 8);
216
217         add_label_to_sizer (table, _tms_panel, _("IP address"), true);
218         _tms_ip = new wxTextCtrl (_tms_panel, wxID_ANY);
219         table->Add (_tms_ip, 1, wxEXPAND);
220
221         add_label_to_sizer (table, _tms_panel, _("Target path"), true);
222         _tms_path = new wxTextCtrl (_tms_panel, wxID_ANY);
223         table->Add (_tms_path, 1, wxEXPAND);
224
225         add_label_to_sizer (table, _tms_panel, _("User name"), true);
226         _tms_user = new wxTextCtrl (_tms_panel, wxID_ANY);
227         table->Add (_tms_user, 1, wxEXPAND);
228
229         add_label_to_sizer (table, _tms_panel, _("Password"), true);
230         _tms_password = new wxTextCtrl (_tms_panel, wxID_ANY);
231         table->Add (_tms_password, 1, wxEXPAND);
232
233         Config* config = Config::instance ();
234         
235         _tms_ip->SetValue (std_to_wx (config->tms_ip ()));
236         _tms_ip->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_ip_changed), 0, this);
237         _tms_path->SetValue (std_to_wx (config->tms_path ()));
238         _tms_path->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_path_changed), 0, this);
239         _tms_user->SetValue (std_to_wx (config->tms_user ()));
240         _tms_user->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_user_changed), 0, this);
241         _tms_password->SetValue (std_to_wx (config->tms_password ()));
242         _tms_password->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_password_changed), 0, this);
243 }
244
245 void
246 ConfigDialog::make_metadata_panel ()
247 {
248         _metadata_panel = new wxPanel (_notebook);
249         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
250         _metadata_panel->SetSizer (s);
251
252         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
253         table->AddGrowableCol (1, 1);
254         s->Add (table, 1, wxALL | wxEXPAND, 8);
255
256         add_label_to_sizer (table, _metadata_panel, _("Issuer"), true);
257         _issuer = new wxTextCtrl (_metadata_panel, wxID_ANY);
258         table->Add (_issuer, 1, wxEXPAND);
259
260         add_label_to_sizer (table, _metadata_panel, _("Creator"), true);
261         _creator = new wxTextCtrl (_metadata_panel, wxID_ANY);
262         table->Add (_creator, 1, wxEXPAND);
263
264         Config* config = Config::instance ();
265
266         _issuer->SetValue (std_to_wx (config->dcp_metadata().issuer));
267         _issuer->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::issuer_changed), 0, this);
268         _creator->SetValue (std_to_wx (config->dcp_metadata().creator));
269         _creator->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::creator_changed), 0, this);
270 }
271
272 void
273 ConfigDialog::make_servers_panel ()
274 {
275         _servers_panel = new wxPanel (_notebook);
276         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
277         _servers_panel->SetSizer (s);
278
279         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
280         table->AddGrowableCol (0, 1);
281         s->Add (table, 1, wxALL | wxEXPAND, 8);
282
283         Config* config = Config::instance ();
284
285         _servers = new wxListCtrl (_servers_panel, wxID_ANY, wxDefaultPosition, wxSize (220, 100), wxLC_REPORT | wxLC_SINGLE_SEL);
286         wxListItem ip;
287         ip.SetId (0);
288         ip.SetText (_("IP address"));
289         ip.SetWidth (120);
290         _servers->InsertColumn (0, ip);
291         ip.SetId (1);
292         ip.SetText (_("Threads"));
293         ip.SetWidth (80);
294         _servers->InsertColumn (1, ip);
295         table->Add (_servers, 1, wxEXPAND | wxALL);
296
297         {
298                 wxSizer* s = new wxBoxSizer (wxVERTICAL);
299                 _add_server = new wxButton (_servers_panel, wxID_ANY, _("Add"));
300                 s->Add (_add_server, 0, wxTOP | wxBOTTOM, 2);
301                 _edit_server = new wxButton (_servers_panel, wxID_ANY, _("Edit"));
302                 s->Add (_edit_server, 0, wxTOP | wxBOTTOM, 2);
303                 _remove_server = new wxButton (_servers_panel, wxID_ANY, _("Remove"));
304                 s->Add (_remove_server, 0, wxTOP | wxBOTTOM, 2);
305                 table->Add (s, 0);
306         }
307
308         vector<ServerDescription*> servers = config->servers ();
309         for (vector<ServerDescription*>::iterator i = servers.begin(); i != servers.end(); ++i) {
310                 add_server_to_control (*i);
311         }
312         
313         _add_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::add_server_clicked), 0, this);
314         _edit_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_server_clicked), 0, this);
315         _remove_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::remove_server_clicked), 0, this);
316
317         _servers->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler (ConfigDialog::server_selection_changed), 0, this);
318         _servers->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_DESELECTED, wxListEventHandler (ConfigDialog::server_selection_changed), 0, this);
319         wxListEvent ev;
320         server_selection_changed (ev);
321 }
322
323 void
324 ConfigDialog::language_changed (wxCommandEvent &)
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 (wxCommandEvent &)
347 {
348         Config::instance()->set_tms_ip (wx_to_std (_tms_ip->GetValue ()));
349 }
350
351 void
352 ConfigDialog::tms_path_changed (wxCommandEvent &)
353 {
354         Config::instance()->set_tms_path (wx_to_std (_tms_path->GetValue ()));
355 }
356
357 void
358 ConfigDialog::tms_user_changed (wxCommandEvent &)
359 {
360         Config::instance()->set_tms_user (wx_to_std (_tms_user->GetValue ()));
361 }
362
363 void
364 ConfigDialog::tms_password_changed (wxCommandEvent &)
365 {
366         Config::instance()->set_tms_password (wx_to_std (_tms_password->GetValue ()));
367 }
368
369 void
370 ConfigDialog::num_local_encoding_threads_changed (wxCommandEvent &)
371 {
372         Config::instance()->set_num_local_encoding_threads (_num_local_encoding_threads->GetValue ());
373 }
374
375 void
376 ConfigDialog::default_directory_changed (wxCommandEvent &)
377 {
378         Config::instance()->set_default_directory (wx_to_std (_default_directory->GetPath ()));
379 }
380
381 void
382 ConfigDialog::add_server_to_control (ServerDescription* s)
383 {
384         wxListItem item;
385         int const n = _servers->GetItemCount ();
386         item.SetId (n);
387         _servers->InsertItem (item);
388         _servers->SetItem (n, 0, std_to_wx (s->host_name ()));
389         _servers->SetItem (n, 1, std_to_wx (boost::lexical_cast<string> (s->threads ())));
390 }
391
392 void
393 ConfigDialog::add_server_clicked (wxCommandEvent &)
394 {
395         ServerDialog* d = new ServerDialog (this, 0);
396         d->ShowModal ();
397         ServerDescription* s = d->server ();
398         d->Destroy ();
399         
400         add_server_to_control (s);
401         vector<ServerDescription*> o = Config::instance()->servers ();
402         o.push_back (s);
403         Config::instance()->set_servers (o);
404 }
405
406 void
407 ConfigDialog::edit_server_clicked (wxCommandEvent &)
408 {
409         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
410         if (i == -1) {
411                 return;
412         }
413
414         wxListItem item;
415         item.SetId (i);
416         item.SetColumn (0);
417         _servers->GetItem (item);
418
419         vector<ServerDescription*> servers = Config::instance()->servers ();
420         assert (i >= 0 && i < int (servers.size ()));
421
422         ServerDialog* d = new ServerDialog (this, servers[i]);
423         d->ShowModal ();
424         d->Destroy ();
425
426         _servers->SetItem (i, 0, std_to_wx (servers[i]->host_name ()));
427         _servers->SetItem (i, 1, std_to_wx (boost::lexical_cast<string> (servers[i]->threads ())));
428 }
429
430 void
431 ConfigDialog::remove_server_clicked (wxCommandEvent &)
432 {
433         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
434         if (i >= 0) {
435                 _servers->DeleteItem (i);
436         }
437
438         vector<ServerDescription*> o = Config::instance()->servers ();
439         o.erase (o.begin() + i);
440         Config::instance()->set_servers (o);
441 }
442
443 void
444 ConfigDialog::server_selection_changed (wxListEvent &)
445 {
446         int const i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
447         _edit_server->Enable (i >= 0);
448         _remove_server->Enable (i >= 0);
449 }
450
451 void
452 ConfigDialog::edit_default_dci_metadata_clicked (wxCommandEvent &)
453 {
454         DCIMetadataDialog* d = new DCIMetadataDialog (this, Config::instance()->default_dci_metadata ());
455         d->ShowModal ();
456         Config::instance()->set_default_dci_metadata (d->dci_metadata ());
457         d->Destroy ();
458 }
459
460 void
461 ConfigDialog::set_language_changed (wxCommandEvent& ev)
462 {
463         setup_language_sensitivity ();
464         if (_set_language->GetValue ()) {
465                 language_changed (ev);
466         } else {
467                 Config::instance()->unset_language ();
468         }
469 }
470
471 void
472 ConfigDialog::setup_language_sensitivity ()
473 {
474         _language->Enable (_set_language->GetValue ());
475 }
476
477 void
478 ConfigDialog::default_still_length_changed (wxCommandEvent &)
479 {
480         Config::instance()->set_default_still_length (_default_still_length->GetValue ());
481 }
482
483 void
484 ConfigDialog::default_container_changed (wxCommandEvent &)
485 {
486         vector<Ratio const *> ratio = Ratio::all ();
487         Config::instance()->set_default_container (ratio[_default_container->GetSelection()]);
488 }
489
490 void
491 ConfigDialog::default_dcp_content_type_changed (wxCommandEvent &)
492 {
493         vector<DCPContentType const *> ct = DCPContentType::all ();
494         Config::instance()->set_default_dcp_content_type (ct[_default_dcp_content_type->GetSelection()]);
495 }
496
497 void
498 ConfigDialog::issuer_changed (wxCommandEvent &)
499 {
500         libdcp::XMLMetadata m = Config::instance()->dcp_metadata ();
501         m.issuer = wx_to_std (_issuer->GetValue ());
502         Config::instance()->set_dcp_metadata (m);
503 }
504
505 void
506 ConfigDialog::creator_changed (wxCommandEvent &)
507 {
508         libdcp::XMLMetadata m = Config::instance()->dcp_metadata ();
509         m.creator = wx_to_std (_creator->GetValue ());
510         Config::instance()->set_dcp_metadata (m);
511 }
512
513 void
514 ConfigDialog::default_j2k_bandwidth_changed (wxCommandEvent &)
515 {
516         Config::instance()->set_default_j2k_bandwidth (_default_j2k_bandwidth->GetValue() * 1e6);
517 }