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