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