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