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