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