make per-region note-tracking in MidiPlaylist work correctly, or something very close...
[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 #include "export_dialog.h"
22
23 #include <sigc++/signal.h>
24
25 #include "pbd/filesystem.h"
26
27 #include "ardour/export_status.h"
28 #include "ardour/export_handler.h"
29
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 ExportDialog::ExportDialog (PublicEditor & editor, Glib::ustring title) :
34   ArdourDialog (title),
35   editor (editor),
36
37   warn_label ("", Gtk::ALIGN_LEFT),
38   list_files_label (_("<span color=\"#ffa755\">Some already existing files will be overwritten.</span>"), Gtk::ALIGN_RIGHT),
39   list_files_button (_("List files"))
40 { }
41
42 ExportDialog::~ExportDialog ()
43 { }
44
45 void
46 ExportDialog::set_session (ARDOUR::Session* s)
47 {
48         session = s;
49
50         /* Init handler and profile manager */
51
52         handler = session->get_export_handler ();
53         status = session->get_export_status ();
54         profile_manager.reset (new ExportProfileManager (*session));
55
56         /* Possibly init stuff in derived classes */
57
58         init ();
59
60         /* Rest of session related initialization */
61
62         preset_selector->set_manager (profile_manager);
63         file_notebook->set_session_and_manager (session, profile_manager);
64
65         /* Hand on selection range to profile manager  */
66
67         TimeSelection const & time (editor.get_selection().time);
68         if (!time.empty()) {
69                 profile_manager->set_selection_range (time.front().start, time.front().end);
70         } else {
71                 profile_manager->set_selection_range ();
72         }
73
74         /* Load states */
75
76         profile_manager->load_profile ();
77         sync_with_manager ();
78
79         /* Warnings */
80
81         preset_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::sync_with_manager));
82         timespan_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings));
83         channel_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings));
84         file_notebook->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings));
85         status->Aborting.connect (sigc::mem_fun (*this, &ExportDialog::notify_errors));
86
87         update_warnings ();
88 }
89
90 void
91 ExportDialog::init ()
92 {
93         init_components ();
94         init_gui ();
95
96         /* warnings */
97
98         warning_widget.pack_start (warn_hbox, true, true, 6);
99         warning_widget.pack_end (list_files_hbox, false, false, 0);
100
101         warn_hbox.pack_start (warn_label, true, true, 16);
102         warn_label.set_use_markup (true);
103
104         list_files_hbox.pack_end (list_files_button, false, false, 6);
105         list_files_hbox.pack_end (list_files_label, false, false, 6);
106         list_files_label.set_use_markup (true);
107
108         list_files_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::show_conflicting_files));
109
110         /* Progress indicators */
111
112         progress_widget.pack_start (progress_label, false, false, 6);
113         progress_widget.pack_start (progress_bar, false, false, 6);
114
115         /* Buttons */
116
117         cancel_button = add_button (Gtk::Stock::CANCEL, RESPONSE_CANCEL);
118         rt_export_button = add_button (_("Realtime Export"), RESPONSE_RT);
119         fast_export_button = add_button (_("Fast Export"), RESPONSE_FAST);
120
121         list_files_button.set_name ("PaddedButton");
122
123         cancel_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::close_dialog));
124         rt_export_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::export_rt));
125         fast_export_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::export_fw));
126
127         /* Done! */
128
129         show_all_children ();
130         progress_widget.hide_all();
131 }
132
133 void
134 ExportDialog::init_gui ()
135 {
136         Gtk::Alignment * preset_align = Gtk::manage (new Gtk::Alignment());
137         preset_align->add (*preset_selector);
138         preset_align->set_padding (0, 12, 0, 0);
139         get_vbox()->pack_start (*preset_align, false, false, 0);
140
141         Gtk::VBox* advanced_vbox = Gtk::manage (new Gtk::VBox());
142         advanced_vbox->set_spacing (12);
143         advanced_vbox->set_border_width (12);
144
145         Gtk::Alignment * timespan_align = Gtk::manage (new Gtk::Alignment());
146         timespan_label = Gtk::manage (new Gtk::Label (_("Time Span"), Gtk::ALIGN_LEFT));
147         timespan_align->add (*timespan_selector);
148         timespan_align->set_padding (0, 12, 18, 0);
149         advanced_vbox->pack_start (*timespan_label, false, false, 0);
150         advanced_vbox->pack_start (*timespan_align, false, false, 0);
151
152         Gtk::Alignment * channels_align = Gtk::manage (new Gtk::Alignment());
153         channels_label = Gtk::manage (new Gtk::Label (_("Channels"), Gtk::ALIGN_LEFT));
154         channels_align->add (*channel_selector);
155         channels_align->set_padding (0, 12, 18, 0);
156         advanced_vbox->pack_start (*channels_label, false, false, 0);
157         advanced_vbox->pack_start (*channels_align, false, false, 0);
158
159         get_vbox()->pack_start (*file_notebook, false, false, 0);
160         get_vbox()->pack_start (warning_widget, false, false, 0);
161         get_vbox()->pack_start (progress_widget, false, false, 0);
162
163         Gtk::Expander* advanced = Gtk::manage (new Gtk::Expander (_("Advanced options")));
164         advanced->add (*advanced_vbox);
165
166         get_vbox()->pack_start (*advanced, false, false);
167
168         Pango::AttrList bold;
169         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
170         bold.insert (b);
171
172         timespan_label->set_attributes (bold);
173         channels_label->set_attributes (bold);
174 }
175
176 void
177 ExportDialog::init_components ()
178 {
179         preset_selector.reset (new ExportPresetSelector ());
180         timespan_selector.reset (new ExportTimespanSelectorMultiple (session, profile_manager));
181         channel_selector.reset (new PortExportChannelSelector (session, profile_manager));
182         file_notebook.reset (new ExportFileNotebook ());
183 }
184
185 void
186 ExportDialog::notify_errors ()
187 {
188         if (status->errors()) {
189                 Glib::ustring txt = _("Export has been aborted due to an error!\nSee the Log for details.");
190                 Gtk::MessageDialog msg (txt, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
191                 msg.run();
192         }
193 }
194
195 void
196 ExportDialog::close_dialog ()
197 {
198         if (status->running) {
199                 status->abort();
200         }
201
202         hide_all ();
203         set_modal (false);
204
205 }
206
207 void
208 ExportDialog::sync_with_manager ()
209 {
210         timespan_selector->sync_with_manager();
211         channel_selector->sync_with_manager();
212         file_notebook->sync_with_manager ();
213
214         update_warnings ();
215 }
216
217 void
218 ExportDialog::update_warnings ()
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         fast_export_button->set_sensitive (true);
229         rt_export_button->set_sensitive (true);
230
231         /* Add new warnings */
232
233         boost::shared_ptr<ExportProfileManager::Warnings> warnings = profile_manager->get_warnings();
234
235         for (std::list<Glib::ustring>::iterator it = warnings->errors.begin(); it != warnings->errors.end(); ++it) {
236                 add_error (*it);
237         }
238
239         for (std::list<Glib::ustring>::iterator it = warnings->warnings.begin(); it != warnings->warnings.end(); ++it) {
240                 add_warning (*it);
241         }
242
243         if (!warnings->conflicting_filenames.empty()) {
244                 list_files_hbox.show ();
245                 for (std::list<Glib::ustring>::iterator it = warnings->conflicting_filenames.begin(); it != warnings->conflicting_filenames.end(); ++it) {
246                         Glib::ustring::size_type pos = it->find_last_of ("/");
247                         list_files_string += "\n" + it->substr (0, pos + 1) + "<b>" + it->substr (pos + 1) + "</b>";
248                 }
249         }
250 }
251
252 void
253 ExportDialog::show_conflicting_files ()
254 {
255         ArdourDialog dialog (_("Files that will be overwritten"), true);
256
257         Gtk::Label label ("", Gtk::ALIGN_LEFT);
258         label.set_use_markup (true);
259         label.set_markup (list_files_string);
260
261         dialog.get_vbox()->pack_start (label);
262         dialog.add_button (Gtk::Stock::OK, 0);
263         dialog.show_all_children ();
264
265         dialog.run();
266 }
267
268 void
269 ExportDialog::export_rt ()
270 {
271         profile_manager->prepare_for_export ();
272         handler->do_export (true);
273         show_progress ();
274 }
275
276 void
277 ExportDialog::export_fw ()
278 {
279         profile_manager->prepare_for_export ();
280         handler->do_export (false);
281         show_progress ();
282 }
283
284 void
285 ExportDialog::show_progress ()
286 {
287         status->running = true;
288
289         cancel_button->set_label (_("Stop Export"));
290         rt_export_button->set_sensitive (false);
291         fast_export_button->set_sensitive (false);
292
293         progress_bar.set_fraction (0.0);
294         warning_widget.hide_all();
295         progress_widget.show ();
296         progress_widget.show_all_children ();
297         progress_connection = Glib::signal_timeout().connect (mem_fun(*this, &ExportDialog::progress_timeout), 100);
298
299         gtk_main_iteration ();
300         while (status->running) {
301                 if (gtk_events_pending()) {
302                         gtk_main_iteration ();
303                 } else {
304                         usleep (10000);
305                 }
306         }
307 }
308
309 gint
310 ExportDialog::progress_timeout ()
311 {
312         switch (status->stage) {
313           case export_None:
314                 progress_label.set_text ("");
315                 break;
316           case export_ReadTimespan:
317                 progress_label.set_text (string_compose (_("Reading timespan %1 of %2"), status->timespan, status->total_timespans));
318                 break;
319           case export_PostProcess:
320                 progress_label.set_text (string_compose (_("Processing file %2 of %3 (%1) from timespan %4 of %5"),
321                                                          file_notebook->get_nth_format_name (status->format),
322                                                          status->format, status->total_formats,
323                                                          status->timespan, status->total_timespans));
324                 break;
325           case export_Write:
326                 progress_label.set_text (string_compose (_("Encoding file %2 of %3 (%1) from timespan %4 of %5"),
327                                                          file_notebook->get_nth_format_name (status->format),
328                                                          status->format, status->total_formats,
329                                                          status->timespan, status->total_timespans));
330                 break;
331         }
332
333         progress_bar.set_fraction (status->progress);
334         return TRUE;
335 }
336
337 void
338 ExportDialog::add_error (Glib::ustring const & text)
339 {
340         fast_export_button->set_sensitive (false);
341         rt_export_button->set_sensitive (false);
342
343         if (warn_string.empty()) {
344                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>";
345         } else {
346                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>\n" + warn_string;
347         }
348
349         warn_label.set_markup (warn_string);
350 }
351
352 void
353 ExportDialog::add_warning (Glib::ustring const & text)
354 {
355         if (warn_string.empty()) {
356                 warn_string = _("<span color=\"#ffa755\">Warning: ") + text + "</span>";
357         } else {
358                 warn_string = warn_string + _("\n<span color=\"#ffa755\">Warning: ") + text + "</span>";
359         }
360
361         warn_label.set_markup (warn_string);
362 }
363
364 /*** Dialog specializations ***/
365
366 ExportRangeDialog::ExportRangeDialog (PublicEditor & editor, Glib::ustring range_id) :
367   ExportDialog (editor, _("Export Range")),
368   range_id (range_id)
369 {}
370
371 void
372 ExportRangeDialog::init_components ()
373 {
374         preset_selector.reset (new ExportPresetSelector ());
375         timespan_selector.reset (new ExportTimespanSelectorSingle (session, profile_manager, range_id));
376         channel_selector.reset (new PortExportChannelSelector (session, profile_manager));
377         file_notebook.reset (new ExportFileNotebook ());
378 }
379
380 ExportSelectionDialog::ExportSelectionDialog (PublicEditor & editor) :
381   ExportDialog (editor, _("Export Selection"))
382 {}
383
384 void
385 ExportSelectionDialog::init_components ()
386 {
387         preset_selector.reset (new ExportPresetSelector ());
388         timespan_selector.reset (new ExportTimespanSelectorSingle (session, profile_manager, X_("selection")));
389         channel_selector.reset (new PortExportChannelSelector (session, profile_manager));
390         file_notebook.reset (new ExportFileNotebook ());
391 }
392
393 ExportRegionDialog::ExportRegionDialog (PublicEditor & editor, ARDOUR::AudioRegion const & region, ARDOUR::AudioTrack & track) :
394   ExportDialog (editor, _("Export Region")),
395   region (region),
396   track (track)
397 {}
398
399 void
400 ExportRegionDialog::init_gui ()
401 {
402         ExportDialog::init_gui ();
403
404         channels_label->set_text (_("Source"));
405 }
406
407 void
408 ExportRegionDialog::init_components ()
409 {
410         Glib::ustring loc_id = profile_manager->set_single_range (region.position(), region.position() + region.length(), region.name());
411
412         preset_selector.reset (new ExportPresetSelector ());
413         timespan_selector.reset (new ExportTimespanSelectorSingle (session, profile_manager, loc_id));
414         channel_selector.reset (new RegionExportChannelSelector (session, profile_manager, region, track));
415         file_notebook.reset (new ExportFileNotebook ());
416 }