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