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