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