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