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