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