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