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