enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / export_filename_selector.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <gtkmm/messagedialog.h>
22
23 #include "pbd/openuri.h"
24 #include "export_filename_selector.h"
25
26 #include "pbd/i18n.h"
27
28 using namespace ARDOUR;
29
30 ExportFilenameSelector::ExportFilenameSelector () :
31         include_label ("", Gtk::ALIGN_LEFT),
32
33         label_label (_("Label:"), Gtk::ALIGN_LEFT),
34         session_checkbox (_("Session Name")),
35         timespan_checkbox (_("Timespan Name")),
36         revision_checkbox (_("Revision:")),
37
38         path_label (_("Folder:"), Gtk::ALIGN_LEFT),
39         browse_button (_("Browse")),
40         open_button (_("Open Folder")),
41
42         example_filename_label ("", Gtk::ALIGN_LEFT),
43         _require_timespan (false)
44 {
45         include_label.set_markup (_("Build filename(s) from these components:"));
46
47         pack_start (path_hbox, false, false, 12);
48         pack_start (include_label, false, false, 6);
49         pack_start (include_hbox, false, false, 0);
50         pack_start (example_filename_label, false, false, 12);
51
52         include_hbox.pack_start (session_checkbox, false, false, 3);
53         include_hbox.pack_start (label_label, false, false, 3);
54         include_hbox.pack_start (label_entry, false, false, 3);
55         include_hbox.pack_start (revision_checkbox, false, false, 3);
56         include_hbox.pack_start (revision_spinbutton, false, false, 3);
57         include_hbox.pack_start (timespan_checkbox, false, false, 3);
58         include_hbox.pack_start (date_format_combo, false, false, 3);
59         include_hbox.pack_start (time_format_combo, false, false, 3);
60
61         label_entry.set_activates_default ();
62
63         path_hbox.pack_start (path_label, false, false, 3);
64         path_hbox.pack_start (path_entry, true, true, 3);
65         path_hbox.pack_start (browse_button, false, false, 3);
66         path_hbox.pack_start (open_button, false, false, 3); // maybe Mixbus only ?
67
68         path_entry.set_activates_default ();
69
70         label_sizegroup = Gtk::SizeGroup::create (Gtk::SIZE_GROUP_HORIZONTAL);
71         label_sizegroup->add_widget (label_label);
72         label_sizegroup->add_widget (path_label);
73
74         /* Date */
75
76         date_format_list = Gtk::ListStore::create (date_format_cols);
77         date_format_combo.set_model (date_format_list);
78         date_format_combo.pack_start (date_format_cols.label);
79
80         date_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_date_format));
81
82         /* Time */
83
84         time_format_list = Gtk::ListStore::create (time_format_cols);
85         time_format_combo.set_model (time_format_list);
86         time_format_combo.pack_start (time_format_cols.label);
87
88         time_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_time_format));
89
90         /* Revision */
91
92         revision_spinbutton.set_digits (0);
93         revision_spinbutton.set_increments (1, 10);
94         revision_spinbutton.set_range (1, 1000);
95         revision_spinbutton.set_sensitive (false);
96
97         /* Signals */
98
99         label_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::update_label));
100         path_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::update_folder));
101         path_entry.signal_activate().connect (sigc::mem_fun (*this, &ExportFilenameSelector::check_folder), false);
102
103         session_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_session_selection));
104         timespan_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_timespan_selection));
105
106         revision_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_revision_selection));
107         revision_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &ExportFilenameSelector::change_revision_value));
108
109         browse_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportFilenameSelector::open_browse_dialog));
110         open_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportFilenameSelector::open_folder));
111 }
112
113 ExportFilenameSelector::~ExportFilenameSelector ()
114 {
115
116 }
117
118 void
119 ExportFilenameSelector::load_state ()
120 {
121         if (!filename) {
122                 return;
123         }
124
125         label_entry.set_text (filename->include_label ? filename->get_label() : "");
126         session_checkbox.set_active (filename->include_session);
127         timespan_checkbox.set_active (filename->include_timespan);
128         revision_checkbox.set_active (filename->include_revision);
129         revision_spinbutton.set_value (filename->get_revision());
130         path_entry.set_text (filename->get_folder());
131
132         Gtk::TreeModel::Children::iterator it;
133
134         for (it = date_format_list->children().begin(); it != date_format_list->children().end(); ++it) {
135                 if (it->get_value (date_format_cols.format) == filename->get_date_format()) {
136                         date_format_combo.set_active (it);
137                 }
138         }
139
140         for (it = time_format_list->children().begin(); it != time_format_list->children().end(); ++it) {
141                 if (it->get_value (time_format_cols.format) == filename->get_time_format()) {
142                         time_format_combo.set_active (it);
143                 }
144         }
145 }
146
147 void
148 ExportFilenameSelector::set_state (ARDOUR::ExportProfileManager::FilenameStatePtr state_, ARDOUR::Session * session_)
149 {
150         SessionHandlePtr::set_session (session_);
151
152         filename = state_->filename;
153
154         /* Fill combo boxes */
155
156         Gtk::TreeModel::iterator iter;
157         Gtk::TreeModel::Row row;
158
159         /* Dates */
160
161         date_format_list->clear();
162
163         iter = date_format_list->append();
164         row = *iter;
165         row[date_format_cols.format] = ExportFilename::D_None;
166         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_None);
167
168         iter = date_format_list->append();
169         row = *iter;
170         row[date_format_cols.format] = ExportFilename::D_ISO;
171         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_ISO);
172
173         iter = date_format_list->append();
174         row = *iter;
175         row[date_format_cols.format] = ExportFilename::D_ISOShortY;
176         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_ISOShortY);
177
178         iter = date_format_list->append();
179         row = *iter;
180         row[date_format_cols.format] = ExportFilename::D_BE;
181         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_BE);
182
183         iter = date_format_list->append();
184         row = *iter;
185         row[date_format_cols.format] = ExportFilename::D_BEShortY;
186         row[date_format_cols.label] = filename->get_date_format_str (ExportFilename::D_BEShortY);
187
188         /* Times */
189
190         time_format_list->clear();
191
192         iter = time_format_list->append();
193         row = *iter;
194         row[time_format_cols.format] = ExportFilename::T_None;
195         row[time_format_cols.label] = filename->get_time_format_str (ExportFilename::T_None);
196
197         iter = time_format_list->append();
198         row = *iter;
199         row[time_format_cols.format] = ExportFilename::T_NoDelim;
200         row[time_format_cols.label] = filename->get_time_format_str (ExportFilename::T_NoDelim);
201
202         iter = time_format_list->append();
203         row = *iter;
204         row[time_format_cols.format] = ExportFilename::T_Delim;
205         row[time_format_cols.label] = filename->get_time_format_str (ExportFilename::T_Delim);
206
207         /* Load state */
208
209         load_state();
210
211 }
212
213 void
214 ExportFilenameSelector::set_example_filename (std::string filename)
215 {
216         if (filename == "") {
217                 example_filename_label.set_markup (_("<small><i>Sorry, no example filename can be shown at the moment</i></small>"));
218         } else {
219                 example_filename_label.set_markup (string_compose(_("<i>Current (approximate) filename</i>: \"%1\""), filename));
220         }
221 }
222
223 void
224 ExportFilenameSelector::update_label ()
225 {
226         if (!filename) {
227                 return;
228         }
229
230         filename->set_label (label_entry.get_text());
231
232         filename->include_label = !label_entry.get_text().empty();
233         CriticalSelectionChanged();
234 }
235
236 void
237 ExportFilenameSelector::update_folder ()
238 {
239         if (!filename) {
240                 return;
241         }
242
243         filename->set_folder (path_entry.get_text());
244         CriticalSelectionChanged();
245 }
246
247 void
248 ExportFilenameSelector::check_folder ()
249 {
250         if (!filename) {
251                 return;
252         }
253
254         if (!Glib::file_test (path_entry.get_text(), Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
255                 Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n\
256 The filename will be chosen from the information just above the folder selector."), path_entry.get_text()));
257                 msg.run ();
258                 path_entry.set_text (Glib::path_get_dirname (path_entry.get_text()));
259                 filename->set_folder (path_entry.get_text());
260                 CriticalSelectionChanged();
261         }
262 }
263
264 void
265 ExportFilenameSelector::change_date_format ()
266 {
267         if (!filename) {
268                 return;
269         }
270
271         DateFormat format = date_format_combo.get_active()->get_value (date_format_cols.format);
272         filename->set_date_format (format);
273         CriticalSelectionChanged();
274 }
275
276 void
277 ExportFilenameSelector::change_time_format ()
278 {
279         if (!filename) {
280                 return;
281         }
282
283         TimeFormat format = time_format_combo.get_active()->get_value (time_format_cols.format);
284         filename->set_time_format (format);
285         CriticalSelectionChanged();
286 }
287
288 void
289 ExportFilenameSelector::require_timespan (bool r)
290 {
291         _require_timespan = r;
292         update_timespan_sensitivity ();
293 }
294
295 void
296 ExportFilenameSelector::update_timespan_sensitivity ()
297 {
298         bool implicit = _require_timespan;
299
300         if (!implicit
301                         && !filename->include_session
302                         && !filename->include_label
303                         && !filename->include_revision
304                         && !filename->include_channel_config
305                         && !filename->include_channel
306                         && !filename->include_date
307                         && !filename->include_format_name) {
308                 implicit = true;
309         }
310
311         // remember prev state, force enable if implicit active.
312         if (implicit && !timespan_checkbox.get_inconsistent()) {
313                 timespan_checkbox.set_inconsistent (true);
314                 filename->include_timespan = true;
315         }
316         else if (!implicit && timespan_checkbox.get_inconsistent()) {
317                 filename->include_timespan = timespan_checkbox.get_active();
318                 timespan_checkbox.set_inconsistent (false);
319         }
320
321 }
322
323 void
324 ExportFilenameSelector::change_timespan_selection ()
325 {
326         if (!filename) {
327                 return;
328         }
329         if (timespan_checkbox.get_inconsistent()) {
330                 return;
331         }
332
333         filename->include_timespan = timespan_checkbox.get_active();
334         CriticalSelectionChanged();
335 }
336
337 void
338 ExportFilenameSelector::change_session_selection ()
339 {
340         if (!filename) {
341                 return;
342         }
343
344         filename->include_session = session_checkbox.get_active();
345         CriticalSelectionChanged();
346 }
347
348 void
349 ExportFilenameSelector::change_revision_selection ()
350 {
351         if (!filename) {
352                 return;
353         }
354
355         bool selected = revision_checkbox.get_active();
356         filename->include_revision = selected;
357
358         revision_spinbutton.set_sensitive (selected);
359         CriticalSelectionChanged();
360 }
361
362 void
363 ExportFilenameSelector::change_revision_value ()
364 {
365         if (!filename) {
366                 return;
367         }
368
369         filename->set_revision ((uint32_t) revision_spinbutton.get_value_as_int());
370         CriticalSelectionChanged();
371 }
372
373 void
374 ExportFilenameSelector::open_folder ()
375 {
376         const std::string& dir (path_entry.get_text());
377         if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
378                 Gtk::MessageDialog msg (string_compose (_("%1: this is not a valid directory/folder."), dir));
379                 msg.run ();
380                 return;
381         }
382         PBD::open_folder (dir);
383 }
384
385 void
386 ExportFilenameSelector::open_browse_dialog ()
387 {
388         Gtk::FileChooserDialog dialog(_("Choose export folder"), Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
389         //dialog.set_transient_for(*this);
390         dialog.set_filename (path_entry.get_text());
391
392         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
393         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
394
395         while (true) {
396                 int result = dialog.run();
397
398                 if (result == Gtk::RESPONSE_OK) {
399                         std::string filename = dialog.get_filename();
400
401                         if (!Glib::file_test (filename, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
402                                 Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n\
403 The filename will be chosen from the information just above the folder selector."), filename));
404                                 msg.run ();
405                                 continue;
406                         }
407
408                         if (filename.length()) {
409                                 path_entry.set_text (filename);
410                                 break;
411                         }
412                 }
413                 break;
414         }
415
416         CriticalSelectionChanged();
417 }