Add load_session to profiling.
[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_and_example_filename));
95         channel_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
96         file_notebook->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
97
98         status->Aborting.connect (abort_connection, invalidator (*this), boost::bind (&ExportDialog::notify_errors, this), gui_context());
99
100         update_warnings_and_example_filename ();
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_bar, false, false, 6);
126
127         /* Buttons */
128
129         cancel_button = add_button (Gtk::Stock::CANCEL, RESPONSE_CANCEL);
130         export_button = add_button (_("Export"), RESPONSE_FAST);
131         set_default_response (RESPONSE_FAST);
132
133         list_files_button.set_name ("PaddedButton");
134
135         cancel_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::close_dialog));
136         export_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::do_export));
137
138         /* Done! */
139
140         show_all_children ();
141         progress_widget.hide_all();
142 }
143
144 void
145 ExportDialog::expanded_changed ()
146 {
147         set_resizable(advanced->get_expanded());
148 }
149
150 void
151 ExportDialog::init_gui ()
152 {
153         Gtk::Alignment * preset_align = Gtk::manage (new Gtk::Alignment());
154         preset_align->add (*preset_selector);
155         preset_align->set_padding (0, 12, 0, 0);
156         get_vbox()->pack_start (*preset_align, false, false, 0);
157
158         Gtk::VPaned * advanced_paned = Gtk::manage (new Gtk::VPaned());
159
160         Gtk::VBox* timespan_vbox = Gtk::manage (new Gtk::VBox());
161         timespan_vbox->set_spacing (12);
162         timespan_vbox->set_border_width (12);
163
164         Gtk::Alignment * timespan_align = Gtk::manage (new Gtk::Alignment());
165         timespan_label = Gtk::manage (new Gtk::Label (_("Time Span"), Gtk::ALIGN_LEFT));
166         timespan_align->add (*timespan_selector);
167         timespan_align->set_padding (0, 0, 18, 0);
168         timespan_vbox->pack_start (*timespan_label, false, false, 0);
169         timespan_vbox->pack_start (*timespan_align, true, true, 0);
170         advanced_paned->pack1(*timespan_vbox, true, false);
171
172         Gtk::VBox* channels_vbox = Gtk::manage (new Gtk::VBox());
173         channels_vbox->set_spacing (12);
174         channels_vbox->set_border_width (12);
175
176         Gtk::Alignment * channels_align = Gtk::manage (new Gtk::Alignment());
177         channels_label = Gtk::manage (new Gtk::Label (_("Channels"), Gtk::ALIGN_LEFT));
178         channels_align->add (*channel_selector);
179         channels_align->set_padding (0, 12, 18, 0);
180         channels_vbox->pack_start (*channels_label, false, false, 0);
181         channels_vbox->pack_start (*channels_align, true, true, 0);
182         advanced_paned->pack2(*channels_vbox, channel_selector_is_expandable(), false);
183
184         get_vbox()->pack_start (*file_notebook, false, false, 0);
185         get_vbox()->pack_start (warning_widget, false, false, 0);
186         get_vbox()->pack_start (progress_widget, false, false, 0);
187
188         advanced = Gtk::manage (new Gtk::Expander (_("Advanced options")));
189         advanced->property_expanded().signal_changed().connect(
190                 sigc::mem_fun(*this, &ExportDialog::expanded_changed));
191         advanced->add (*advanced_paned);
192
193         if (channel_selector_is_expandable()) {
194                 advanced_sizegroup = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_VERTICAL);
195                 advanced_sizegroup->add_widget(*timespan_selector);
196                 advanced_sizegroup->add_widget(*channel_selector);
197         }
198
199         get_vbox()->pack_start (*advanced, true, true);
200
201         Pango::AttrList bold;
202         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
203         bold.insert (b);
204
205         timespan_label->set_attributes (bold);
206         channels_label->set_attributes (bold);
207 }
208
209 void
210 ExportDialog::init_components ()
211 {
212         preset_selector.reset (new ExportPresetSelector ());
213         timespan_selector.reset (new ExportTimespanSelectorMultiple (_session, profile_manager));
214         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
215         file_notebook.reset (new ExportFileNotebook ());
216 }
217
218 void
219 ExportDialog::notify_errors ()
220 {
221         if (status->errors()) {
222                 std::string txt = _("Export has been aborted due to an error!\nSee the Log for details.");
223                 Gtk::MessageDialog msg (txt, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
224                 msg.run();
225         }
226 }
227
228 void
229 ExportDialog::close_dialog ()
230 {
231         if (status->running) {
232                 status->abort();
233         }
234
235         hide_all ();
236         set_modal (false);
237
238 }
239
240 void
241 ExportDialog::sync_with_manager ()
242 {
243         timespan_selector->sync_with_manager();
244         channel_selector->sync_with_manager();
245         file_notebook->sync_with_manager ();
246
247         update_warnings_and_example_filename ();
248 }
249
250 void
251 ExportDialog::update_warnings_and_example_filename ()
252 {
253         /* Reset state */
254
255         warn_string = "";
256         warn_label.set_markup (warn_string);
257
258         list_files_hbox.hide ();
259         list_files_string = "";
260
261         export_button->set_sensitive (true);
262
263         /* Add new warnings */
264
265         boost::shared_ptr<ExportProfileManager::Warnings> warnings = profile_manager->get_warnings();
266
267         for (std::list<string>::iterator it = warnings->errors.begin(); it != warnings->errors.end(); ++it) {
268                 add_error (*it);
269         }
270
271         for (std::list<string>::iterator it = warnings->warnings.begin(); it != warnings->warnings.end(); ++it) {
272                 add_warning (*it);
273         }
274
275         if (!warnings->conflicting_filenames.empty()) {
276                 list_files_hbox.show ();
277                 for (std::list<string>::iterator it = warnings->conflicting_filenames.begin(); it != warnings->conflicting_filenames.end(); ++it) {
278                         string::size_type pos = it->find_last_of ("/");
279                         list_files_string += it->substr (0, pos + 1) + "<b>" + it->substr (pos + 1) + "</b>\n";
280                 }
281         }
282
283         /* Update example filename */
284
285         file_notebook->update_example_filenames();
286 }
287
288 void
289 ExportDialog::show_conflicting_files ()
290 {
291         ArdourDialog dialog (_("Files that will be overwritten"), true);
292
293         Gtk::Label label ("", Gtk::ALIGN_LEFT);
294         label.set_use_markup (true);
295         label.set_markup (list_files_string);
296
297         dialog.get_vbox()->pack_start (label);
298         dialog.add_button (Gtk::Stock::OK, 0);
299         dialog.show_all_children ();
300
301         dialog.run();
302 }
303
304 void
305 ExportDialog::do_export ()
306 {
307         profile_manager->prepare_for_export ();
308         handler->do_export ();
309         show_progress ();
310 }
311
312 void
313 ExportDialog::show_progress ()
314 {
315         status->running = true;
316
317         cancel_button->set_label (_("Stop Export"));
318         export_button->set_sensitive (false);
319
320         progress_bar.set_fraction (0.0);
321         warning_widget.hide_all();
322         progress_widget.show ();
323         progress_widget.show_all_children ();
324         progress_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ExportDialog::progress_timeout), 100);
325
326         gtk_main_iteration ();
327         while (status->running) {
328                 if (gtk_events_pending()) {
329                         gtk_main_iteration ();
330                 } else {
331                         usleep (10000);
332                 }
333         }
334
335         if (!status->aborted()) {
336                 status->finish ();
337         }
338 }
339
340 gint
341 ExportDialog::progress_timeout ()
342 {
343         std::string status_text;
344         float progress = 0.0;
345         if (status->normalizing) {
346                 status_text = string_compose (_("Normalizing '%3' (timespan %1 of %2)"),
347                                               status->timespan, status->total_timespans, status->timespan_name);
348                 progress = ((float) status->current_normalize_cycle) / status->total_normalize_cycles;
349         } else {
350                 status_text = string_compose (_("Exporting '%3' (timespan %1 of %2)"),
351                                               status->timespan, status->total_timespans, status->timespan_name);
352                 progress = ((float) status->processed_frames_current_timespan) / status->total_frames_current_timespan;
353         }
354         progress_bar.set_text (status_text);
355
356         if (progress < previous_progress) {
357                 // Work around gtk bug
358                 progress_bar.hide();
359                 progress_bar.show();
360         }
361         previous_progress = progress;
362
363         progress_bar.set_fraction (progress);
364         return TRUE;
365 }
366
367 void
368 ExportDialog::add_error (string const & text)
369 {
370         export_button->set_sensitive (false);
371
372         if (warn_string.empty()) {
373                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>";
374         } else {
375                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>\n" + warn_string;
376         }
377
378         warn_label.set_markup (warn_string);
379 }
380
381 void
382 ExportDialog::add_warning (string const & text)
383 {
384         if (warn_string.empty()) {
385                 warn_string = _("<span color=\"#ffa755\">Warning: ") + text + "</span>";
386         } else {
387                 warn_string = warn_string + _("\n<span color=\"#ffa755\">Warning: ") + text + "</span>";
388         }
389
390         warn_label.set_markup (warn_string);
391 }
392
393 /*** Dialog specializations ***/
394
395 ExportRangeDialog::ExportRangeDialog (PublicEditor & editor, string range_id) :
396   ExportDialog (editor, _("Export Range"), X_("RangeExportProfile")),
397   range_id (range_id)
398 {}
399
400 void
401 ExportRangeDialog::init_components ()
402 {
403         preset_selector.reset (new ExportPresetSelector ());
404         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, range_id));
405         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
406         file_notebook.reset (new ExportFileNotebook ());
407 }
408
409 ExportSelectionDialog::ExportSelectionDialog (PublicEditor & editor) :
410   ExportDialog (editor, _("Export Selection"), X_("SelectionExportProfile"))
411 {}
412
413 void
414 ExportSelectionDialog::init_components ()
415 {
416         preset_selector.reset (new ExportPresetSelector ());
417         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, X_("selection")));
418         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
419         file_notebook.reset (new ExportFileNotebook ());
420 }
421
422 ExportRegionDialog::ExportRegionDialog (PublicEditor & editor, ARDOUR::AudioRegion const & region, ARDOUR::AudioTrack & track) :
423   ExportDialog (editor, _("Export Region"), X_("RegionExportProfile")),
424   region (region),
425   track (track)
426 {}
427
428 void
429 ExportRegionDialog::init_gui ()
430 {
431         ExportDialog::init_gui ();
432
433         channels_label->set_text (_("Source"));
434 }
435
436 void
437 ExportRegionDialog::init_components ()
438 {
439         string loc_id = profile_manager->set_single_range (region.position(), region.position() + region.length(), region.name());
440
441         preset_selector.reset (new ExportPresetSelector ());
442         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, loc_id));
443         channel_selector.reset (new RegionExportChannelSelector (_session, profile_manager, region, track));
444         file_notebook.reset (new ExportFileNotebook ());
445 }
446
447 StemExportDialog::StemExportDialog (PublicEditor & editor)
448   : ExportDialog(editor, _("Stem Export"), X_("StemExportProfile"))
449 {
450
451 }
452
453 void
454 StemExportDialog::init_components ()
455 {
456         preset_selector.reset (new ExportPresetSelector ());
457         timespan_selector.reset (new ExportTimespanSelectorMultiple (_session, profile_manager));
458         channel_selector.reset (new TrackExportChannelSelector (_session, profile_manager));
459         file_notebook.reset (new ExportFileNotebook ());
460 }