Add stem export dialog and make all different export dialogs save their config to...
[ardour.git] / gtk2_ardour / export_dialog.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
22 #include <sigc++/signal.h>
23
24 #include "pbd/filesystem.h"
25
26 #include "ardour/audioregion.h"
27 #include "ardour/export_status.h"
28 #include "ardour/export_handler.h"
29
30 #include "export_dialog.h"
31 #include "gui_thread.h"
32
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36 using namespace PBD;
37 using std::string;
38
39 ExportDialog::ExportDialog (PublicEditor & editor, std::string title, std::string xml_node_name)
40   : ArdourDialog (title)
41   , xml_node_name (xml_node_name)
42   , editor (editor)
43
44   , warn_label ("", Gtk::ALIGN_LEFT)
45   , list_files_label (_("<span color=\"#ffa755\">Some already existing files will be overwritten.</span>"), Gtk::ALIGN_RIGHT)
46   , list_files_button (_("List files"))
47 { }
48
49 ExportDialog::~ExportDialog ()
50 { }
51
52 void
53 ExportDialog::set_session (ARDOUR::Session* s)
54 {
55         SessionHandlePtr::set_session (s);
56
57         if (!_session) {
58                 return;
59         }
60
61         /* Init handler and profile manager */
62
63         handler = _session->get_export_handler ();
64         status = _session->get_export_status ();
65         profile_manager.reset (new ExportProfileManager (*_session, xml_node_name));
66
67         /* Possibly init stuff in derived classes */
68
69         init ();
70
71         /* Rest of _session related initialization */
72
73         preset_selector->set_manager (profile_manager);
74         file_notebook->set_session_and_manager (_session, profile_manager);
75
76         /* Hand on selection range to profile manager  */
77
78         TimeSelection const & time (editor.get_selection().time);
79         if (!time.empty()) {
80                 profile_manager->set_selection_range (time.front().start, time.front().end);
81         } else {
82                 profile_manager->set_selection_range ();
83         }
84
85         /* Load states */
86
87         profile_manager->load_profile ();
88         sync_with_manager ();
89
90         /* Warnings */
91
92         preset_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::sync_with_manager));
93         timespan_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings));
94         channel_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings));
95         file_notebook->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings));
96
97         status->Aborting.connect (abort_connection, invalidator (*this), boost::bind (&ExportDialog::notify_errors, this), gui_context());
98
99         update_warnings ();
100 }
101
102 void
103 ExportDialog::init ()
104 {
105         init_components ();
106         init_gui ();
107
108         /* warnings */
109
110         warning_widget.pack_start (warn_hbox, true, true, 6);
111         warning_widget.pack_end (list_files_hbox, false, false, 0);
112
113         warn_hbox.pack_start (warn_label, true, true, 16);
114         warn_label.set_use_markup (true);
115
116         list_files_hbox.pack_end (list_files_button, false, false, 6);
117         list_files_hbox.pack_end (list_files_label, false, false, 6);
118         list_files_label.set_use_markup (true);
119
120         list_files_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::show_conflicting_files));
121
122         /* Progress indicators */
123
124         progress_widget.pack_start (progress_label, false, false, 6);
125         progress_widget.pack_start (progress_bar, false, false, 6);
126
127         /* Buttons */
128
129         cancel_button = add_button (Gtk::Stock::CANCEL, RESPONSE_CANCEL);
130         // Realtime export is disabled for now, as it will most probably not work
131         //rt_export_button = add_button (_("Realtime Export"), RESPONSE_RT);
132         //fast_export_button = add_button (_("Fast Export"), RESPONSE_FAST);
133         fast_export_button = add_button (_("Export"), RESPONSE_FAST);
134
135         list_files_button.set_name ("PaddedButton");
136
137         cancel_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::close_dialog));
138         //rt_export_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::export_rt));
139         fast_export_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::export_fw));
140
141         /* Done! */
142
143         show_all_children ();
144         progress_widget.hide_all();
145 }
146
147 void
148 ExportDialog::init_gui ()
149 {
150         Gtk::Alignment * preset_align = Gtk::manage (new Gtk::Alignment());
151         preset_align->add (*preset_selector);
152         preset_align->set_padding (0, 12, 0, 0);
153         get_vbox()->pack_start (*preset_align, false, false, 0);
154
155         Gtk::VBox* advanced_vbox = Gtk::manage (new Gtk::VBox());
156         advanced_vbox->set_spacing (12);
157         advanced_vbox->set_border_width (12);
158
159         Gtk::Alignment * timespan_align = Gtk::manage (new Gtk::Alignment());
160         timespan_label = Gtk::manage (new Gtk::Label (_("Time Span"), Gtk::ALIGN_LEFT));
161         timespan_align->add (*timespan_selector);
162         timespan_align->set_padding (0, 12, 18, 0);
163         advanced_vbox->pack_start (*timespan_label, false, false, 0);
164         advanced_vbox->pack_start (*timespan_align, false, false, 0);
165
166         Gtk::Alignment * channels_align = Gtk::manage (new Gtk::Alignment());
167         channels_label = Gtk::manage (new Gtk::Label (_("Channels"), Gtk::ALIGN_LEFT));
168         channels_align->add (*channel_selector);
169         channels_align->set_padding (0, 12, 18, 0);
170         advanced_vbox->pack_start (*channels_label, false, false, 0);
171         advanced_vbox->pack_start (*channels_align, false, false, 0);
172
173         get_vbox()->pack_start (*file_notebook, false, false, 0);
174         get_vbox()->pack_start (warning_widget, false, false, 0);
175         get_vbox()->pack_start (progress_widget, false, false, 0);
176
177         Gtk::Expander* advanced = Gtk::manage (new Gtk::Expander (_("Advanced options")));
178         advanced->add (*advanced_vbox);
179
180         get_vbox()->pack_start (*advanced, false, false);
181
182         Pango::AttrList bold;
183         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
184         bold.insert (b);
185
186         timespan_label->set_attributes (bold);
187         channels_label->set_attributes (bold);
188 }
189
190 void
191 ExportDialog::init_components ()
192 {
193         preset_selector.reset (new ExportPresetSelector ());
194         timespan_selector.reset (new ExportTimespanSelectorMultiple (_session, profile_manager));
195         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
196         file_notebook.reset (new ExportFileNotebook ());
197 }
198
199 void
200 ExportDialog::notify_errors ()
201 {
202         if (status->errors()) {
203                 std::string txt = _("Export has been aborted due to an error!\nSee the Log for details.");
204                 Gtk::MessageDialog msg (txt, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
205                 msg.run();
206         }
207 }
208
209 void
210 ExportDialog::close_dialog ()
211 {
212         if (status->running) {
213                 status->abort();
214         }
215
216         hide_all ();
217         set_modal (false);
218
219 }
220
221 void
222 ExportDialog::sync_with_manager ()
223 {
224         timespan_selector->sync_with_manager();
225         channel_selector->sync_with_manager();
226         file_notebook->sync_with_manager ();
227
228         update_warnings ();
229 }
230
231 void
232 ExportDialog::update_warnings ()
233 {
234         /* Reset state */
235
236         warn_string = "";
237         warn_label.set_markup (warn_string);
238
239         list_files_hbox.hide ();
240         list_files_string = "";
241
242         fast_export_button->set_sensitive (true);
243         //rt_export_button->set_sensitive (true);
244
245         /* Add new warnings */
246
247         boost::shared_ptr<ExportProfileManager::Warnings> warnings = profile_manager->get_warnings();
248
249         for (std::list<string>::iterator it = warnings->errors.begin(); it != warnings->errors.end(); ++it) {
250                 add_error (*it);
251         }
252
253         for (std::list<string>::iterator it = warnings->warnings.begin(); it != warnings->warnings.end(); ++it) {
254                 add_warning (*it);
255         }
256
257         if (!warnings->conflicting_filenames.empty()) {
258                 list_files_hbox.show ();
259                 for (std::list<string>::iterator it = warnings->conflicting_filenames.begin(); it != warnings->conflicting_filenames.end(); ++it) {
260                         string::size_type pos = it->find_last_of ("/");
261                         list_files_string += "\n" + it->substr (0, pos + 1) + "<b>" + it->substr (pos + 1) + "</b>";
262                 }
263         }
264 }
265
266 void
267 ExportDialog::show_conflicting_files ()
268 {
269         ArdourDialog dialog (_("Files that will be overwritten"), true);
270
271         Gtk::Label label ("", Gtk::ALIGN_LEFT);
272         label.set_use_markup (true);
273         label.set_markup (list_files_string);
274
275         dialog.get_vbox()->pack_start (label);
276         dialog.add_button (Gtk::Stock::OK, 0);
277         dialog.show_all_children ();
278
279         dialog.run();
280 }
281
282 void
283 ExportDialog::export_rt ()
284 {
285         profile_manager->prepare_for_export ();
286         handler->do_export (true);
287         show_progress ();
288 }
289
290 void
291 ExportDialog::export_fw ()
292 {
293         profile_manager->prepare_for_export ();
294         handler->do_export (false);
295         show_progress ();
296 }
297
298 void
299 ExportDialog::show_progress ()
300 {
301         status->running = true;
302
303         cancel_button->set_label (_("Stop Export"));
304         //rt_export_button->set_sensitive (false);
305         fast_export_button->set_sensitive (false);
306
307         progress_bar.set_fraction (0.0);
308         warning_widget.hide_all();
309         progress_widget.show ();
310         progress_widget.show_all_children ();
311         progress_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ExportDialog::progress_timeout), 100);
312
313         gtk_main_iteration ();
314         while (status->running) {
315                 if (gtk_events_pending()) {
316                         gtk_main_iteration ();
317                 } else {
318                         usleep (10000);
319                 }
320         }
321         
322         if (!status->aborted()) {
323                 status->finish ();
324         }
325 }
326
327 gint
328 ExportDialog::progress_timeout ()
329 {
330         switch (status->stage) {
331           case export_None:
332                 progress_label.set_text ("");
333                 break;
334           case export_ReadTimespan:
335                 progress_label.set_text (string_compose (_("Reading timespan %1 of %2"), status->timespan, status->total_timespans));
336                 break;
337           case export_PostProcess:
338                 progress_label.set_text (string_compose (_("Processing file %2 of %3 (%1) from timespan %4 of %5"),
339                                                          file_notebook->get_nth_format_name (status->format),
340                                                          status->format, status->total_formats,
341                                                          status->timespan, status->total_timespans));
342                 break;
343           case export_Write:
344                 progress_label.set_text (string_compose (_("Encoding file %2 of %3 (%1) from timespan %4 of %5"),
345                                                          file_notebook->get_nth_format_name (status->format),
346                                                          status->format, status->total_formats,
347                                                          status->timespan, status->total_timespans));
348                 break;
349         }
350
351         progress_bar.set_fraction (status->progress);
352         return TRUE;
353 }
354
355 void
356 ExportDialog::add_error (string const & text)
357 {
358         fast_export_button->set_sensitive (false);
359         //rt_export_button->set_sensitive (false);
360
361         if (warn_string.empty()) {
362                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>";
363         } else {
364                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>\n" + warn_string;
365         }
366
367         warn_label.set_markup (warn_string);
368 }
369
370 void
371 ExportDialog::add_warning (string const & text)
372 {
373         if (warn_string.empty()) {
374                 warn_string = _("<span color=\"#ffa755\">Warning: ") + text + "</span>";
375         } else {
376                 warn_string = warn_string + _("\n<span color=\"#ffa755\">Warning: ") + text + "</span>";
377         }
378
379         warn_label.set_markup (warn_string);
380 }
381
382 /*** Dialog specializations ***/
383
384 ExportRangeDialog::ExportRangeDialog (PublicEditor & editor, string range_id) :
385   ExportDialog (editor, _("Export Range"), X_("RangeExportProfile")),
386   range_id (range_id)
387 {}
388
389 void
390 ExportRangeDialog::init_components ()
391 {
392         preset_selector.reset (new ExportPresetSelector ());
393         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, range_id));
394         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
395         file_notebook.reset (new ExportFileNotebook ());
396 }
397
398 ExportSelectionDialog::ExportSelectionDialog (PublicEditor & editor) :
399   ExportDialog (editor, _("Export Selection"), X_("SelectionExportProfile"))
400 {}
401
402 void
403 ExportSelectionDialog::init_components ()
404 {
405         preset_selector.reset (new ExportPresetSelector ());
406         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, X_("selection")));
407         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
408         file_notebook.reset (new ExportFileNotebook ());
409 }
410
411 ExportRegionDialog::ExportRegionDialog (PublicEditor & editor, ARDOUR::AudioRegion const & region, ARDOUR::AudioTrack & track) :
412   ExportDialog (editor, _("Export Region"), X_("RegionExportProfile")),
413   region (region),
414   track (track)
415 {}
416
417 void
418 ExportRegionDialog::init_gui ()
419 {
420         ExportDialog::init_gui ();
421
422         channels_label->set_text (_("Source"));
423 }
424
425 void
426 ExportRegionDialog::init_components ()
427 {
428         string loc_id = profile_manager->set_single_range (region.position(), region.position() + region.length(), region.name());
429
430         preset_selector.reset (new ExportPresetSelector ());
431         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, loc_id));
432         channel_selector.reset (new RegionExportChannelSelector (_session, profile_manager, region, track));
433         file_notebook.reset (new ExportFileNotebook ());
434 }
435
436 StemExportDialog::StemExportDialog (PublicEditor & editor)
437   : ExportDialog(editor, _("Stem Export"), X_("StemExportProfile"))
438 {
439         
440 }
441
442 void
443 StemExportDialog::init_components ()
444 {
445         preset_selector.reset (new ExportPresetSelector ());
446         timespan_selector.reset (new ExportTimespanSelectorMultiple (_session, profile_manager));
447         channel_selector.reset (new TrackExportChannelSelector (_session, profile_manager));
448         file_notebook.reset (new ExportFileNotebook ());
449 }
450