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