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