Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / export_dialog.cc
1 /*
2  * Copyright (C) 2005-2006 Doug McLain <doug@nostar.net>
3  * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
4  * Copyright (C) 2005-2008 Nick Mainsbridge <mainsbridge@gmail.com>
5  * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
6  * Copyright (C) 2006-2011 David Robillard <d@drobilla.net>
7  * Copyright (C) 2007-2012 Tim Mayberry <mojofunk@gmail.com>
8  * Copyright (C) 2008-2013 Sakari Bergen <sakari.bergen@beatwaves.net>
9  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
10  * Copyright (C) 2013-2014 Colin Fletcher <colin.m.fletcher@googlemail.com>
11  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27
28
29 #include <sigc++/signal.h>
30
31 #include <gtkmm/messagedialog.h>
32 #include <gtkmm/stock.h>
33
34 #include "pbd/gstdio_compat.h"
35 #include "pbd/file_utils.h"
36
37 #include "ardour/audioregion.h"
38 #include "ardour/export_channel_configuration.h"
39 #include "ardour/export_format_specification.h"
40 #include "ardour/export_status.h"
41 #include "ardour/export_handler.h"
42 #include "ardour/profile.h"
43
44 #include "export_dialog.h"
45 #include "export_report.h"
46 #include "gui_thread.h"
47 #include "mixer_ui.h"
48 #include "nag.h"
49 #include "ui_config.h"
50
51 #include "pbd/i18n.h"
52
53 using namespace ARDOUR;
54 using namespace PBD;
55 using std::string;
56
57 ExportDialog::ExportDialog (PublicEditor & editor, std::string title, ARDOUR::ExportProfileManager::ExportType type)
58   : ArdourDialog (title)
59   , type (type)
60   , editor (editor)
61   , warn_label ("", Gtk::ALIGN_LEFT)
62   , list_files_label (_("<span color=\"#ffa755\">Some already existing files will be overwritten.</span>"), Gtk::ALIGN_RIGHT)
63   , list_files_button (_("List files"))
64   , previous_progress (0)
65 { }
66
67 ExportDialog::~ExportDialog ()
68 { }
69
70 void
71 ExportDialog::set_session (ARDOUR::Session* s)
72 {
73         SessionHandlePtr::set_session (s);
74
75         if (!_session) {
76                 return;
77         }
78
79         /* Init handler and profile manager */
80
81         handler = _session->get_export_handler ();
82         status = _session->get_export_status ();
83
84         profile_manager.reset (new ExportProfileManager (*_session, type));
85
86         /* Possibly init stuff in derived classes */
87
88         init ();
89
90         /* Rest of _session related initialization */
91
92         preset_selector->set_manager (profile_manager);
93         file_notebook->set_session_and_manager (_session, profile_manager);
94
95         /* Hand on selection range to profile manager  */
96
97         TimeSelection const & time (editor.get_selection().time);
98         if (!time.empty()) {
99                 profile_manager->set_selection_range (time.front().start, time.front().end);
100         } else {
101                 profile_manager->set_selection_range ();
102         }
103
104         /* Load states */
105
106         profile_manager->load_profile ();
107         sync_with_manager ();
108
109         /* Warnings */
110
111         preset_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::sync_with_manager));
112         timespan_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
113         channel_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
114         channel_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_realtime_selection));
115         file_notebook->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
116
117         update_warnings_and_example_filename ();
118         update_realtime_selection ();
119
120         _session->config.ParameterChanged.connect (*this, invalidator (*this), boost::bind (&ExportDialog::parameter_changed, this, _1), gui_context());
121 }
122
123 void
124 ExportDialog::init ()
125 {
126         init_components ();
127         init_gui ();
128
129         /* warnings */
130
131         warning_widget.pack_start (warn_hbox, true, true, 6);
132         warning_widget.pack_end (list_files_hbox, false, false, 0);
133
134         warn_hbox.pack_start (warn_label, true, true, 16);
135         warn_label.set_use_markup (true);
136
137         list_files_hbox.pack_end (list_files_button, false, false, 6);
138         list_files_hbox.pack_end (list_files_label, false, false, 6);
139         list_files_label.set_use_markup (true);
140
141         list_files_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::show_conflicting_files));
142
143         /* Progress indicators */
144
145         progress_widget.pack_start (progress_bar, false, false, 6);
146
147         /* Buttons */
148
149         cancel_button = add_button (Gtk::Stock::CANCEL, RESPONSE_CANCEL);
150         export_button = add_button (_("Export"), RESPONSE_FAST);
151         set_default_response (RESPONSE_FAST);
152
153         cancel_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::close_dialog));
154         export_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::do_export));
155
156         file_notebook->soundcloud_export_selector = soundcloud_selector;
157
158         /* Done! */
159
160         show_all_children ();
161         progress_widget.hide_all();
162 }
163
164 void
165 ExportDialog::init_gui ()
166 {
167         Gtk::Alignment * preset_align = Gtk::manage (new Gtk::Alignment());
168         preset_align->add (*preset_selector);
169         preset_align->set_padding (0, 12, 0, 0);
170
171         Gtk::VBox * file_format_selector = Gtk::manage (new Gtk::VBox());
172         file_format_selector->set_homogeneous (false);
173         file_format_selector->pack_start (*preset_align, false, false, 0);
174         file_format_selector->pack_start (*file_notebook, false, false, 0);
175         file_format_selector->pack_start (*soundcloud_selector, false, false, 0);
176
177         export_notebook.append_page (*file_format_selector, _("File format"));
178         export_notebook.append_page (*timespan_selector, _("Time Span"));
179         export_notebook.append_page (*channel_selector, _("Channels"));
180
181         get_vbox()->pack_start (export_notebook, true, true, 0);
182         get_vbox()->pack_end   (warning_widget, false, false, 0);
183         get_vbox()->pack_end   (progress_widget, false, false, 0);
184
185 }
186
187 void
188 ExportDialog::init_components ()
189 {
190         preset_selector.reset (new ExportPresetSelector ());
191         timespan_selector.reset (new ExportTimespanSelectorMultiple (_session, profile_manager));
192         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
193         soundcloud_selector.reset (new SoundcloudExportSelector ());
194         file_notebook.reset (new ExportFileNotebook ());
195 }
196
197 void
198 ExportDialog::notify_errors (bool force)
199 {
200         if (force || status->errors()) {
201                 std::string txt = _("Export has been aborted due to an error!\nSee the Log for details.");
202                 Gtk::MessageDialog msg (txt, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
203                 msg.run();
204         }
205 }
206
207 void
208 ExportDialog::close_dialog ()
209 {
210         if (status->running ()) {
211                 status->abort();
212         }
213
214         hide_all ();
215         set_modal (false);
216
217 }
218
219 void
220 ExportDialog::sync_with_manager ()
221 {
222         timespan_selector->sync_with_manager();
223         channel_selector->sync_with_manager();
224         file_notebook->sync_with_manager ();
225
226         update_warnings_and_example_filename ();
227         update_realtime_selection ();
228 }
229
230 void
231 ExportDialog::update_warnings_and_example_filename ()
232 {
233         /* Reset state */
234
235         warn_string = "";
236         warn_label.set_markup (warn_string);
237
238         list_files_hbox.hide ();
239         list_files_string = "";
240
241         export_button->set_sensitive (true);
242
243         /* Add new warnings */
244
245         boost::shared_ptr<ExportProfileManager::Warnings> warnings = profile_manager->get_warnings();
246
247         for (std::list<string>::iterator it = warnings->errors.begin(); it != warnings->errors.end(); ++it) {
248                 add_error (*it);
249         }
250
251         for (std::list<string>::iterator it = warnings->warnings.begin(); it != warnings->warnings.end(); ++it) {
252                 add_warning (*it);
253         }
254
255         if (!warnings->conflicting_filenames.empty()) {
256                 list_files_hbox.show ();
257                 for (std::list<string>::iterator it = warnings->conflicting_filenames.begin(); it != warnings->conflicting_filenames.end(); ++it) {
258                         string::size_type pos = it->find_last_of ("/");
259                         list_files_string += it->substr (0, pos + 1) + "<b>" + it->substr (pos + 1) + "</b>\n";
260                 }
261         }
262
263         /* Update example filename */
264
265         file_notebook->update_example_filenames();
266 }
267
268 void
269 ExportDialog::update_realtime_selection ()
270 {
271         bool rt_ok = true;
272         switch (profile_manager->type ()) {
273                 case ExportProfileManager::RegularExport:
274                         break;
275                 case ExportProfileManager::RangeExport:
276                         break;
277                 case ExportProfileManager::SelectionExport:
278                         break;
279                 case ExportProfileManager::RegionExport:
280                         if (!profile_manager->get_channel_configs().empty ()) {
281                                 switch (profile_manager->get_channel_configs().front()->config->region_processing_type ()) {
282                                         case RegionExportChannelFactory::Raw:
283                                         case RegionExportChannelFactory::Fades:
284                                                 rt_ok = false;
285                                                 break;
286                                         default:
287                                                 break;
288                                 }
289                         }
290                         break;
291                 case ExportProfileManager::StemExport:
292                         if (! static_cast<TrackExportChannelSelector*>(channel_selector.get())->track_output ()) {
293                                 rt_ok = false;
294                         }
295                         break;
296         }
297
298         timespan_selector->allow_realtime_export (rt_ok);
299 }
300
301 void
302 ExportDialog::parameter_changed (std::string const& p)
303 {
304         if (p == "realtime-export") {
305                 update_realtime_selection ();
306         }
307 }
308
309 void
310 ExportDialog::show_conflicting_files ()
311 {
312         ArdourDialog dialog (_("Files that will be overwritten"), true);
313
314         Gtk::Label label ("", Gtk::ALIGN_LEFT);
315         label.set_use_markup (true);
316         label.set_markup (list_files_string);
317
318         dialog.get_vbox()->pack_start (label);
319         dialog.add_button (Gtk::Stock::OK, 0);
320         dialog.show_all_children ();
321
322         dialog.run();
323 }
324
325 void
326 ExportDialog::soundcloud_upload_progress(double total, double now, std::string title)
327 {
328         soundcloud_selector->do_progress_callback(total, now, title);
329
330 }
331
332 void
333 ExportDialog::do_export ()
334 {
335         try {
336                 profile_manager->prepare_for_export ();
337                 handler->soundcloud_username     = soundcloud_selector->username ();
338                 handler->soundcloud_password     = soundcloud_selector->password ();
339                 handler->soundcloud_make_public  = soundcloud_selector->make_public ();
340                 handler->soundcloud_open_page    = soundcloud_selector->open_page ();
341                 handler->soundcloud_downloadable = soundcloud_selector->downloadable ();
342
343                 handler->SoundcloudProgress.connect_same_thread(
344                                 *this,
345                                 boost::bind(&ExportDialog::soundcloud_upload_progress, this, _1, _2, _3)
346                                 );
347 #if 0
348                 handler->SoundcloudProgress.connect(
349                                 *this, invalidator (*this),
350                                 boost::bind(&ExportDialog::soundcloud_upload_progress, this, _1, _2, _3),
351                                 gui_context()
352                                 );
353 #endif
354                 handler->do_export ();
355                 show_progress ();
356         } catch(std::exception & e) {
357                 error << string_compose (_("Export initialization failed: %1"), e.what()) << endmsg;
358                 notify_errors(true);
359         }
360 }
361
362 void
363 ExportDialog::show_progress ()
364 {
365         export_notebook.set_sensitive (false);
366
367         cancel_button->set_label (_("Stop Export"));
368         export_button->set_sensitive (false);
369
370         progress_bar.set_fraction (0.0);
371         warning_widget.hide_all();
372         progress_widget.show ();
373         progress_widget.show_all_children ();
374         progress_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ExportDialog::progress_timeout), 100);
375
376         gtk_main_iteration ();
377
378         while (status->running ()) {
379                 if (gtk_events_pending()) {
380                         gtk_main_iteration ();
381                 } else {
382                         Glib::usleep (10000);
383                 }
384         }
385
386         status->finish (TRS_UI);
387
388         if (!status->aborted() && UIConfiguration::instance().get_save_export_mixer_screenshot ()) {
389                 ExportProfileManager::TimespanStateList const& timespans = profile_manager->get_timespans();
390                 ExportProfileManager::FilenameStateList const& filenames = profile_manager->get_filenames ();
391
392                 std::list<std::string> paths;
393                 for (ExportProfileManager::FilenameStateList::const_iterator fi = filenames.begin(); fi != filenames.end(); ++fi) {
394                         for (ExportProfileManager::TimespanStateList::const_iterator ti = timespans.begin(); ti != timespans.end(); ++ti) {
395                                 ExportProfileManager::TimespanListPtr tlp = (*ti)->timespans;
396                                 for (ExportProfileManager::TimespanList::const_iterator eti = tlp->begin(); eti != tlp->end(); ++eti) {
397                                         (*fi)->filename->set_timespan (*eti);
398                                         paths.push_back ((*fi)->filename->get_path (ExportFormatSpecPtr ()) + "-mixer.png");
399                                 }
400                         }
401                 }
402
403                 if (paths.size() > 0) {
404                         PBD::info << string_compose(_("Writing Mixer Screenshot: %1."), paths.front()) << endmsg;
405                         Mixer_UI::instance()->screenshot (paths.front());
406
407                         std::list<std::string>::const_iterator it = paths.begin ();
408                         ++it;
409                         for (; it != paths.end(); ++it) {
410                                 PBD::info << string_compose(_("Copying Mixer Screenshot: %1."), *it) << endmsg;
411                                 ::g_unlink (it->c_str());
412                                 if (!hard_link (paths.front(), *it)) {
413                                         copy_file (paths.front(), *it);
414                                 }
415                         }
416                 }
417         }
418
419         if (!status->aborted() && status->result_map.size() > 0) {
420                 hide();
421                 ExportReport er (_session, status);
422                 er.run();
423         }
424
425         if (!status->aborted()) {
426                 hide();
427                 if (!ARDOUR::Profile->get_mixbus()) {
428                         NagScreen* ns = NagScreen::maybe_nag (_("export"));
429                         if (ns) {
430                                 ns->nag ();
431                                 delete ns;
432                         }
433                 }
434         } else {
435                 notify_errors ();
436         }
437         export_notebook.set_sensitive (true);
438 }
439
440 gint
441 ExportDialog::progress_timeout ()
442 {
443         std::string status_text;
444         float progress = -1;
445         switch (status->active_job) {
446         case ExportStatus::Exporting:
447                 status_text = string_compose (_("Exporting '%3' (timespan %1 of %2)"),
448                                               status->timespan, status->total_timespans, status->timespan_name);
449                 progress = ((float) status->processed_samples_current_timespan) / status->total_samples_current_timespan;
450                 break;
451         case ExportStatus::Normalizing:
452                 status_text = string_compose (_("Normalizing '%3' (timespan %1 of %2)"),
453                                               status->timespan, status->total_timespans, status->timespan_name);
454                 progress = ((float) status->current_postprocessing_cycle) / status->total_postprocessing_cycles;
455                 break;
456         case ExportStatus::Encoding:
457                 status_text = string_compose (_("Encoding '%3' (timespan %1 of %2)"),
458                                               status->timespan, status->total_timespans, status->timespan_name);
459                 progress = ((float) status->current_postprocessing_cycle) / status->total_postprocessing_cycles;
460                 break;
461         case ExportStatus::Tagging:
462                 status_text = string_compose (_("Tagging '%3' (timespan %1 of %2)"),
463                                               status->timespan, status->total_timespans, status->timespan_name);
464                 break;
465         case ExportStatus::Uploading:
466                 status_text = string_compose (_("Uploading '%3' (timespan %1 of %2)"),
467                                               status->timespan, status->total_timespans, status->timespan_name);
468                 break;
469         case ExportStatus::Command:
470                 status_text = string_compose (_("Running Post Export Command for '%1'"), status->timespan_name);
471                 break;
472         }
473
474         progress_bar.set_text (status_text);
475
476         if (progress < previous_progress) {
477                 // Work around gtk bug
478                 progress_bar.hide();
479                 progress_bar.show();
480         }
481         previous_progress = progress;
482
483         if (progress >= 0) {
484                 progress_bar.set_fraction (progress);
485         } else {
486                 progress_bar.set_pulse_step(.1);
487                 progress_bar.pulse();
488         }
489         return TRUE;
490 }
491
492 void
493 ExportDialog::add_error (string const & text)
494 {
495         export_button->set_sensitive (false);
496
497         if (warn_string.empty()) {
498                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>";
499         } else {
500                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>\n" + warn_string;
501         }
502
503         warn_label.set_markup (warn_string);
504 }
505
506 void
507 ExportDialog::add_warning (string const & text)
508 {
509         if (warn_string.empty()) {
510                 warn_string = _("<span color=\"#ffa755\">Warning: ") + text + "</span>";
511         } else {
512                 warn_string = warn_string + _("\n<span color=\"#ffa755\">Warning: ") + text + "</span>";
513         }
514
515         warn_label.set_markup (warn_string);
516 }
517
518 /*** Dialog specializations ***/
519
520 ExportRangeDialog::ExportRangeDialog (PublicEditor & editor, string range_id) :
521   ExportDialog (editor, _("Export Range"), ExportProfileManager::RangeExport),
522   range_id (range_id)
523 {}
524
525 void
526 ExportRangeDialog::init_components ()
527 {
528         preset_selector.reset (new ExportPresetSelector ());
529         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, range_id));
530         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
531         soundcloud_selector.reset (new SoundcloudExportSelector ());
532         file_notebook.reset (new ExportFileNotebook ());
533 }
534
535 ExportSelectionDialog::ExportSelectionDialog (PublicEditor & editor) :
536   ExportDialog (editor, _("Export Selection"), ExportProfileManager::SelectionExport)
537 {}
538
539 void
540 ExportSelectionDialog::init_components ()
541 {
542         preset_selector.reset (new ExportPresetSelector ());
543         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, X_("selection")));
544         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
545         soundcloud_selector.reset (new SoundcloudExportSelector ());
546         file_notebook.reset (new ExportFileNotebook ());
547 }
548
549 ExportRegionDialog::ExportRegionDialog (PublicEditor & editor, ARDOUR::AudioRegion const & region, ARDOUR::AudioTrack & track) :
550   ExportDialog (editor, _("Export Region"), ExportProfileManager::RegionExport),
551   region (region),
552   track (track)
553 {}
554
555 void
556 ExportRegionDialog::init_gui ()
557 {
558         ExportDialog::init_gui ();
559         export_notebook.set_tab_label_text(*export_notebook.get_nth_page(2), _("Source"));
560 }
561
562 void
563 ExportRegionDialog::init_components ()
564 {
565         string loc_id = profile_manager->set_single_range (region.position(), region.position() + region.length(), region.name());
566
567         preset_selector.reset (new ExportPresetSelector ());
568         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, loc_id));
569         channel_selector.reset (new RegionExportChannelSelector (_session, profile_manager, region, track));
570         soundcloud_selector.reset (new SoundcloudExportSelector ());
571         file_notebook.reset (new ExportFileNotebook ());
572 }
573
574 StemExportDialog::StemExportDialog (PublicEditor & editor)
575   : ExportDialog(editor, _("Stem Export"), ExportProfileManager::StemExport)
576 {
577
578 }
579
580 void
581 StemExportDialog::init_components ()
582 {
583         preset_selector.reset (new ExportPresetSelector ());
584         timespan_selector.reset (new ExportTimespanSelectorMultiple (_session, profile_manager));
585         channel_selector.reset (new TrackExportChannelSelector (_session, profile_manager));
586         soundcloud_selector.reset (new SoundcloudExportSelector ());
587         file_notebook.reset (new ExportFileNotebook ());
588 }