Normalize API rename part 2
[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_channel_configuration.h"
28 #include "ardour/export_status.h"
29 #include "ardour/export_handler.h"
30 #include "ardour/profile.h"
31
32 #include "export_dialog.h"
33 #include "export_report.h"
34 #include "gui_thread.h"
35 #include "nag.h"
36
37 #include "pbd/i18n.h"
38
39 using namespace ARDOUR;
40 using namespace PBD;
41 using std::string;
42
43 ExportDialog::ExportDialog (PublicEditor & editor, std::string title, ARDOUR::ExportProfileManager::ExportType type)
44   : ArdourDialog (title)
45   , type (type)
46   , editor (editor)
47
48   , warn_label ("", Gtk::ALIGN_LEFT)
49   , list_files_label (_("<span color=\"#ffa755\">Some already existing files will be overwritten.</span>"), Gtk::ALIGN_RIGHT)
50   , list_files_button (_("List files"))
51 { }
52
53 ExportDialog::~ExportDialog ()
54 { }
55
56 void
57 ExportDialog::set_session (ARDOUR::Session* s)
58 {
59         SessionHandlePtr::set_session (s);
60
61         if (!_session) {
62                 return;
63         }
64
65         /* Init handler and profile manager */
66
67         handler = _session->get_export_handler ();
68         status = _session->get_export_status ();
69
70         profile_manager.reset (new ExportProfileManager (*_session, type));
71
72         /* Possibly init stuff in derived classes */
73
74         init ();
75
76         /* Rest of _session related initialization */
77
78         preset_selector->set_manager (profile_manager);
79         file_notebook->set_session_and_manager (_session, profile_manager);
80
81         /* Hand on selection range to profile manager  */
82
83         TimeSelection const & time (editor.get_selection().time);
84         if (!time.empty()) {
85                 profile_manager->set_selection_range (time.front().start, time.front().end);
86         } else {
87                 profile_manager->set_selection_range ();
88         }
89
90         /* Load states */
91
92         profile_manager->load_profile ();
93         sync_with_manager ();
94
95         /* Warnings */
96
97         preset_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::sync_with_manager));
98         timespan_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
99         channel_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
100         channel_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_realtime_selection));
101         file_notebook->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
102
103         update_warnings_and_example_filename ();
104         update_realtime_selection ();
105
106         _session->config.ParameterChanged.connect (*this, invalidator (*this), boost::bind (&ExportDialog::parameter_changed, this, _1), gui_context());
107 }
108
109 void
110 ExportDialog::init ()
111 {
112         init_components ();
113         init_gui ();
114
115         /* warnings */
116
117         warning_widget.pack_start (warn_hbox, true, true, 6);
118         warning_widget.pack_end (list_files_hbox, false, false, 0);
119
120         warn_hbox.pack_start (warn_label, true, true, 16);
121         warn_label.set_use_markup (true);
122
123         list_files_hbox.pack_end (list_files_button, false, false, 6);
124         list_files_hbox.pack_end (list_files_label, false, false, 6);
125         list_files_label.set_use_markup (true);
126
127         list_files_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::show_conflicting_files));
128
129         /* Progress indicators */
130
131         progress_widget.pack_start (progress_bar, false, false, 6);
132
133         /* Buttons */
134
135         cancel_button = add_button (Gtk::Stock::CANCEL, RESPONSE_CANCEL);
136         export_button = add_button (_("Export"), RESPONSE_FAST);
137         set_default_response (RESPONSE_FAST);
138
139         cancel_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::close_dialog));
140         export_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::do_export));
141
142         file_notebook->soundcloud_export_selector = soundcloud_selector;
143
144         /* Done! */
145
146         show_all_children ();
147         progress_widget.hide_all();
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
157         Gtk::VBox * file_format_selector = Gtk::manage (new Gtk::VBox());
158         file_format_selector->set_homogeneous (false);
159         file_format_selector->pack_start (*preset_align, false, false, 0);
160         file_format_selector->pack_start (*file_notebook, false, false, 0);
161         file_format_selector->pack_start (*soundcloud_selector, false, false, 0);
162
163         export_notebook.append_page (*file_format_selector, _("File format"));
164         export_notebook.append_page (*timespan_selector, _("Time Span"));
165         export_notebook.append_page (*channel_selector, _("Channels"));
166
167         get_vbox()->pack_start (export_notebook, true, true, 0);
168         get_vbox()->pack_end   (warning_widget, false, false, 0);
169         get_vbox()->pack_end   (progress_widget, false, false, 0);
170
171 }
172
173 void
174 ExportDialog::init_components ()
175 {
176         preset_selector.reset (new ExportPresetSelector ());
177         timespan_selector.reset (new ExportTimespanSelectorMultiple (_session, profile_manager));
178         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
179         soundcloud_selector.reset (new SoundcloudExportSelector ());
180         file_notebook.reset (new ExportFileNotebook ());
181 }
182
183 void
184 ExportDialog::notify_errors (bool force)
185 {
186         if (force || status->errors()) {
187                 std::string txt = _("Export has been aborted due to an error!\nSee the Log for details.");
188                 Gtk::MessageDialog msg (txt, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
189                 msg.run();
190         }
191 }
192
193 void
194 ExportDialog::close_dialog ()
195 {
196         if (status->running ()) {
197                 status->abort();
198         }
199
200         hide_all ();
201         set_modal (false);
202
203 }
204
205 void
206 ExportDialog::sync_with_manager ()
207 {
208         timespan_selector->sync_with_manager();
209         channel_selector->sync_with_manager();
210         file_notebook->sync_with_manager ();
211
212         update_warnings_and_example_filename ();
213         update_realtime_selection ();
214 }
215
216 void
217 ExportDialog::update_warnings_and_example_filename ()
218 {
219         /* Reset state */
220
221         warn_string = "";
222         warn_label.set_markup (warn_string);
223
224         list_files_hbox.hide ();
225         list_files_string = "";
226
227         export_button->set_sensitive (true);
228
229         /* Add new warnings */
230
231         boost::shared_ptr<ExportProfileManager::Warnings> warnings = profile_manager->get_warnings();
232
233         for (std::list<string>::iterator it = warnings->errors.begin(); it != warnings->errors.end(); ++it) {
234                 add_error (*it);
235         }
236
237         for (std::list<string>::iterator it = warnings->warnings.begin(); it != warnings->warnings.end(); ++it) {
238                 add_warning (*it);
239         }
240
241         if (!warnings->conflicting_filenames.empty()) {
242                 list_files_hbox.show ();
243                 for (std::list<string>::iterator it = warnings->conflicting_filenames.begin(); it != warnings->conflicting_filenames.end(); ++it) {
244                         string::size_type pos = it->find_last_of ("/");
245                         list_files_string += it->substr (0, pos + 1) + "<b>" + it->substr (pos + 1) + "</b>\n";
246                 }
247         }
248
249         /* Update example filename */
250
251         file_notebook->update_example_filenames();
252 }
253
254 void
255 ExportDialog::update_realtime_selection ()
256 {
257         bool rt_ok = true;
258         switch (profile_manager->type ()) {
259                 case ExportProfileManager::RegularExport:
260                         break;
261                 case ExportProfileManager::RangeExport:
262                         break;
263                 case ExportProfileManager::SelectionExport:
264                         break;
265                 case ExportProfileManager::RegionExport:
266                         if (!profile_manager->get_channel_configs().empty ()) {
267                                 switch (profile_manager->get_channel_configs().front()->config->region_processing_type ()) {
268                                         case RegionExportChannelFactory::Raw:
269                                         case RegionExportChannelFactory::Fades:
270                                                 rt_ok = false;
271                                                 break;
272                                         default:
273                                                 break;
274                                 }
275                         }
276                         break;
277                 case ExportProfileManager::StemExport:
278                         if (! static_cast<TrackExportChannelSelector*>(channel_selector.get())->track_output ()) {
279                                 rt_ok = false;
280                         }
281                         break;
282         }
283
284         timespan_selector->allow_realtime_export (rt_ok);
285 }
286
287 void
288 ExportDialog::parameter_changed (std::string const& p)
289 {
290         if (p == "realtime-export") {
291                 update_realtime_selection ();
292         }
293 }
294
295 void
296 ExportDialog::show_conflicting_files ()
297 {
298         ArdourDialog dialog (_("Files that will be overwritten"), true);
299
300         Gtk::Label label ("", Gtk::ALIGN_LEFT);
301         label.set_use_markup (true);
302         label.set_markup (list_files_string);
303
304         dialog.get_vbox()->pack_start (label);
305         dialog.add_button (Gtk::Stock::OK, 0);
306         dialog.show_all_children ();
307
308         dialog.run();
309 }
310
311 void
312 ExportDialog::soundcloud_upload_progress(double total, double now, std::string title)
313 {
314         soundcloud_selector->do_progress_callback(total, now, title);
315
316 }
317
318 void
319 ExportDialog::do_export ()
320 {
321         try {
322                 profile_manager->prepare_for_export ();
323                 handler->soundcloud_username     = soundcloud_selector->username ();
324                 handler->soundcloud_password     = soundcloud_selector->password ();
325                 handler->soundcloud_make_public  = soundcloud_selector->make_public ();
326                 handler->soundcloud_open_page    = soundcloud_selector->open_page ();
327                 handler->soundcloud_downloadable = soundcloud_selector->downloadable ();
328
329                 handler->SoundcloudProgress.connect_same_thread(
330                                 *this,
331                                 boost::bind(&ExportDialog::soundcloud_upload_progress, this, _1, _2, _3)
332                                 );
333 #if 0
334                 handler->SoundcloudProgress.connect(
335                                 *this, invalidator (*this),
336                                 boost::bind(&ExportDialog::soundcloud_upload_progress, this, _1, _2, _3),
337                                 gui_context()
338                                 );
339 #endif
340                 handler->do_export ();
341                 show_progress ();
342         } catch(std::exception & e) {
343                 error << string_compose (_("Export initialization failed: %1"), e.what()) << endmsg;
344                 notify_errors(true);
345         }
346 }
347
348 void
349 ExportDialog::show_progress ()
350 {
351         export_notebook.set_sensitive (false);
352
353         cancel_button->set_label (_("Stop Export"));
354         export_button->set_sensitive (false);
355
356         progress_bar.set_fraction (0.0);
357         warning_widget.hide_all();
358         progress_widget.show ();
359         progress_widget.show_all_children ();
360         progress_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ExportDialog::progress_timeout), 100);
361
362         gtk_main_iteration ();
363
364         while (status->running ()) {
365                 if (gtk_events_pending()) {
366                         gtk_main_iteration ();
367                 } else {
368                         Glib::usleep (10000);
369                 }
370         }
371
372         status->finish ();
373
374         if (!status->aborted() && status->result_map.size() > 0) {
375                 hide();
376                 ExportReport er (_session, status);
377                 er.run();
378         }
379
380         if (!status->aborted()) {
381                 hide();
382                 if (!ARDOUR::Profile->get_mixbus()) {
383                         NagScreen* ns = NagScreen::maybe_nag (_("export"));
384                         if (ns) {
385                                 ns->nag ();
386                                 delete ns;
387                         }
388                 }
389         } else {
390                 notify_errors ();
391         }
392         export_notebook.set_sensitive (true);
393 }
394
395 gint
396 ExportDialog::progress_timeout ()
397 {
398         std::string status_text;
399         float progress = -1;
400         switch (status->active_job) {
401         case ExportStatus::Exporting:
402                 status_text = string_compose (_("Exporting '%3' (timespan %1 of %2)"),
403                                               status->timespan, status->total_timespans, status->timespan_name);
404                 progress = ((float) status->processed_frames_current_timespan) / status->total_frames_current_timespan;
405                 break;
406         case ExportStatus::Normalizing:
407                 status_text = string_compose (_("Normalizing '%3' (timespan %1 of %2)"),
408                                               status->timespan, status->total_timespans, status->timespan_name);
409                 progress = ((float) status->current_postprocessing_cycle) / status->total_postprocessing_cycles;
410                 break;
411         case ExportStatus::Encoding:
412                 status_text = string_compose (_("Encoding '%3' (timespan %1 of %2)"),
413                                               status->timespan, status->total_timespans, status->timespan_name);
414                 progress = ((float) status->current_postprocessing_cycle) / status->total_postprocessing_cycles;
415                 break;
416         case ExportStatus::Tagging:
417                 status_text = string_compose (_("Tagging '%3' (timespan %1 of %2)"),
418                                               status->timespan, status->total_timespans, status->timespan_name);
419                 break;
420         case ExportStatus::Uploading:
421                 status_text = string_compose (_("Uploading '%3' (timespan %1 of %2)"),
422                                               status->timespan, status->total_timespans, status->timespan_name);
423                 break;
424         case ExportStatus::Command:
425                 status_text = string_compose (_("Running Post Export Command for '%1'"), status->timespan_name);
426                 break;
427         }
428
429         progress_bar.set_text (status_text);
430
431         if (progress < previous_progress) {
432                 // Work around gtk bug
433                 progress_bar.hide();
434                 progress_bar.show();
435         }
436         previous_progress = progress;
437
438         if (progress >= 0) {
439                 progress_bar.set_fraction (progress);
440         } else {
441                 progress_bar.set_pulse_step(.1);
442                 progress_bar.pulse();
443         }
444         return TRUE;
445 }
446
447 void
448 ExportDialog::add_error (string const & text)
449 {
450         export_button->set_sensitive (false);
451
452         if (warn_string.empty()) {
453                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>";
454         } else {
455                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>\n" + warn_string;
456         }
457
458         warn_label.set_markup (warn_string);
459 }
460
461 void
462 ExportDialog::add_warning (string const & text)
463 {
464         if (warn_string.empty()) {
465                 warn_string = _("<span color=\"#ffa755\">Warning: ") + text + "</span>";
466         } else {
467                 warn_string = warn_string + _("\n<span color=\"#ffa755\">Warning: ") + text + "</span>";
468         }
469
470         warn_label.set_markup (warn_string);
471 }
472
473 /*** Dialog specializations ***/
474
475 ExportRangeDialog::ExportRangeDialog (PublicEditor & editor, string range_id) :
476   ExportDialog (editor, _("Export Range"), ExportProfileManager::RangeExport),
477   range_id (range_id)
478 {}
479
480 void
481 ExportRangeDialog::init_components ()
482 {
483         preset_selector.reset (new ExportPresetSelector ());
484         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, range_id));
485         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
486         soundcloud_selector.reset (new SoundcloudExportSelector ());
487         file_notebook.reset (new ExportFileNotebook ());
488 }
489
490 ExportSelectionDialog::ExportSelectionDialog (PublicEditor & editor) :
491   ExportDialog (editor, _("Export Selection"), ExportProfileManager::SelectionExport)
492 {}
493
494 void
495 ExportSelectionDialog::init_components ()
496 {
497         preset_selector.reset (new ExportPresetSelector ());
498         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, X_("selection")));
499         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
500         soundcloud_selector.reset (new SoundcloudExportSelector ());
501         file_notebook.reset (new ExportFileNotebook ());
502 }
503
504 ExportRegionDialog::ExportRegionDialog (PublicEditor & editor, ARDOUR::AudioRegion const & region, ARDOUR::AudioTrack & track) :
505   ExportDialog (editor, _("Export Region"), ExportProfileManager::RegionExport),
506   region (region),
507   track (track)
508 {}
509
510 void
511 ExportRegionDialog::init_gui ()
512 {
513         ExportDialog::init_gui ();
514         export_notebook.set_tab_label_text(*export_notebook.get_nth_page(2), _("Source"));
515 }
516
517 void
518 ExportRegionDialog::init_components ()
519 {
520         string loc_id = profile_manager->set_single_range (region.position(), region.position() + region.length(), region.name());
521
522         preset_selector.reset (new ExportPresetSelector ());
523         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, loc_id));
524         channel_selector.reset (new RegionExportChannelSelector (_session, profile_manager, region, track));
525         soundcloud_selector.reset (new SoundcloudExportSelector ());
526         file_notebook.reset (new ExportFileNotebook ());
527 }
528
529 StemExportDialog::StemExportDialog (PublicEditor & editor)
530   : ExportDialog(editor, _("Stem Export"), ExportProfileManager::StemExport)
531 {
532
533 }
534
535 void
536 StemExportDialog::init_components ()
537 {
538         preset_selector.reset (new ExportPresetSelector ());
539         timespan_selector.reset (new ExportTimespanSelectorMultiple (_session, profile_manager));
540         channel_selector.reset (new TrackExportChannelSelector (_session, profile_manager));
541         soundcloud_selector.reset (new SoundcloudExportSelector ());
542         file_notebook.reset (new ExportFileNotebook ());
543 }