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