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