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