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