a6a6b8dfcc2796d16ed857da17e21810deb34dd4
[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::Alignment * timespan_align = Gtk::manage (new Gtk::Alignment());
142         timespan_label = Gtk::manage (new Gtk::Label (_("Time Span"), Gtk::ALIGN_LEFT));
143         timespan_align->add (*timespan_selector);
144         timespan_align->set_padding (0, 12, 18, 0);
145         get_vbox()->pack_start (*timespan_label, false, false, 0);
146         get_vbox()->pack_start (*timespan_align, false, false, 0);
147         
148         Gtk::Alignment * channels_align = Gtk::manage (new Gtk::Alignment());
149         channels_label = Gtk::manage (new Gtk::Label (_("Channels"), Gtk::ALIGN_LEFT));
150         channels_align->add (*channel_selector);
151         channels_align->set_padding (0, 12, 18, 0);
152         get_vbox()->pack_start (*channels_label, false, false, 0);
153         get_vbox()->pack_start (*channels_align, false, false, 0);
154         
155         get_vbox()->pack_start (*file_notebook, false, false, 0);
156         get_vbox()->pack_start (warning_widget, true, true, 0);
157         get_vbox()->pack_start (progress_widget, true, true, 0);
158         
159         Pango::AttrList bold;
160         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
161         bold.insert (b);
162         
163         timespan_label->set_attributes (bold);
164         channels_label->set_attributes (bold);
165 }
166
167 void
168 ExportDialog::init_components ()
169 {
170         preset_selector.reset (new ExportPresetSelector ());
171         timespan_selector.reset (new ExportTimespanSelectorMultiple (session, profile_manager));
172         channel_selector.reset (new PortExportChannelSelector (session, profile_manager));
173         file_notebook.reset (new ExportFileNotebook ());
174 }
175
176 void
177 ExportDialog::notify_errors ()
178 {
179         if (status->errors()) {
180                 Glib::ustring 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 ();
206 }
207
208 void
209 ExportDialog::update_warnings ()
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         fast_export_button->set_sensitive (true);
220         rt_export_button->set_sensitive (true);
221
222         /* Add new warnings */
223
224         boost::shared_ptr<ExportProfileManager::Warnings> warnings = profile_manager->get_warnings();
225
226         for (std::list<Glib::ustring>::iterator it = warnings->errors.begin(); it != warnings->errors.end(); ++it) {
227                 add_error (*it);
228         }
229
230         for (std::list<Glib::ustring>::iterator it = warnings->warnings.begin(); it != warnings->warnings.end(); ++it) {
231                 add_warning (*it);
232         }
233
234         if (!warnings->conflicting_filenames.empty()) {
235                 list_files_hbox.show ();
236                 for (std::list<Glib::ustring>::iterator it = warnings->conflicting_filenames.begin(); it != warnings->conflicting_filenames.end(); ++it) {
237                         ustring::size_type pos = it->find_last_of ("/");
238                         list_files_string += "\n" + it->substr (0, pos + 1) + "<b>" + it->substr (pos + 1) + "</b>";
239                 }
240         }
241 }
242
243 void
244 ExportDialog::show_conflicting_files ()
245 {
246         ArdourDialog dialog (_("Files that will be overwritten"), true);
247         
248         Gtk::Label label ("", Gtk::ALIGN_LEFT);
249         label.set_use_markup (true);
250         label.set_markup (list_files_string);
251         
252         dialog.get_vbox()->pack_start (label);
253         dialog.add_button (Gtk::Stock::OK, 0);
254         dialog.show_all_children ();
255         
256         dialog.run();
257 }
258
259 void
260 ExportDialog::export_rt ()
261 {
262         profile_manager->prepare_for_export ();
263         handler->do_export (true);
264         show_progress ();
265 }
266
267 void
268 ExportDialog::export_fw ()
269 {
270         profile_manager->prepare_for_export ();
271         handler->do_export (false);
272         show_progress ();
273 }
274
275 void
276 ExportDialog::show_progress ()
277 {
278         status->running = true;
279
280         cancel_button->set_label (_("Stop Export"));
281         rt_export_button->set_sensitive (false);
282         fast_export_button->set_sensitive (false);
283
284         progress_bar.set_fraction (0.0);
285         warning_widget.hide_all();
286         progress_widget.show ();
287         progress_widget.show_all_children ();
288         progress_connection = Glib::signal_timeout().connect (mem_fun(*this, &ExportDialog::progress_timeout), 100);
289         
290         gtk_main_iteration ();
291         while (status->running) {
292                 if (gtk_events_pending()) {
293                         gtk_main_iteration ();
294                 } else {
295                         usleep (10000);
296                 }
297         }
298 }
299
300 gint
301 ExportDialog::progress_timeout ()
302 {
303         switch (status->stage) {
304           case export_None:
305                 progress_label.set_text ("");
306                 break;
307           case export_ReadTimespan:
308                 progress_label.set_text (string_compose (_("Reading timespan %1 of %2"), status->timespan, status->total_timespans));
309                 break;
310           case export_PostProcess:
311                 progress_label.set_text (string_compose (_("Processing file %2 of %3 (%1) from timespan %4 of %5"),
312                                                          file_notebook->get_nth_format_name (status->format),
313                                                          status->format, status->total_formats,
314                                                          status->timespan, status->total_timespans));
315                 break;
316           case export_Write:
317                 progress_label.set_text (string_compose (_("Encoding file %2 of %3 (%1) from timespan %4 of %5"),
318                                                          file_notebook->get_nth_format_name (status->format),
319                                                          status->format, status->total_formats,
320                                                          status->timespan, status->total_timespans));
321                 break;
322         }
323
324         progress_bar.set_fraction (status->progress);
325         return TRUE;
326 }
327
328 void
329 ExportDialog::add_error (Glib::ustring const & text)
330 {
331         fast_export_button->set_sensitive (false);
332         rt_export_button->set_sensitive (false);
333         
334         if (warn_string.empty()) {
335                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>";
336         } else {
337                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>\n" + warn_string;
338         }
339         
340         warn_label.set_markup (warn_string);
341 }
342
343 void
344 ExportDialog::add_warning (Glib::ustring const & text)
345 {
346         if (warn_string.empty()) {
347                 warn_string = _("<span color=\"#ffa755\">Warning: ") + text + "</span>";
348         } else {
349                 warn_string = warn_string + _("\n<span color=\"#ffa755\">Warning: ") + text + "</span>";
350         }
351         
352         warn_label.set_markup (warn_string);
353 }
354
355 /*** Dialog specializations ***/
356
357 ExportRangeDialog::ExportRangeDialog (PublicEditor & editor, Glib::ustring range_id) :
358   ExportDialog (editor, _("Export Range")),
359   range_id (range_id)
360 {}
361
362 void
363 ExportRangeDialog::init_components ()
364 {
365         preset_selector.reset (new ExportPresetSelector ());
366         timespan_selector.reset (new ExportTimespanSelectorSingle (session, profile_manager, range_id));
367         channel_selector.reset (new PortExportChannelSelector (session, profile_manager));
368         file_notebook.reset (new ExportFileNotebook ());
369 }
370
371 ExportSelectionDialog::ExportSelectionDialog (PublicEditor & editor) :
372   ExportDialog (editor, _("Export Selection"))
373 {}
374
375 void
376 ExportSelectionDialog::init_components ()
377 {
378         preset_selector.reset (new ExportPresetSelector ());
379         timespan_selector.reset (new ExportTimespanSelectorSingle (session, profile_manager, X_("selection")));
380         channel_selector.reset (new PortExportChannelSelector (session, profile_manager));
381         file_notebook.reset (new ExportFileNotebook ());
382 }
383
384 ExportRegionDialog::ExportRegionDialog (PublicEditor & editor, ARDOUR::AudioRegion const & region, ARDOUR::AudioTrack & track) :
385   ExportDialog (editor, _("Export Region")),
386   region (region),
387   track (track)
388 {}
389
390 void
391 ExportRegionDialog::init_gui ()
392 {
393         ExportDialog::init_gui ();
394         
395         channels_label->set_text (_("Source"));
396 }
397
398 void
399 ExportRegionDialog::init_components ()
400 {
401         Glib::ustring loc_id = profile_manager->set_single_range (region.position(), region.position() + region.length(), region.name());
402
403         preset_selector.reset (new ExportPresetSelector ());
404         timespan_selector.reset (new ExportTimespanSelectorSingle (session, profile_manager, loc_id));
405         channel_selector.reset (new RegionExportChannelSelector (session, profile_manager, region, track));
406         file_notebook.reset (new ExportFileNotebook ());
407 }