merge with master.
[ardour.git] / libs / ardour / export_profile_manager.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 <cassert>
22 #include <stdexcept>
23 #include <cerrno>
24
25 #include <glib.h>
26 #include <glib/gstdio.h>
27
28 #include <glibmm/fileutils.h>
29 #include <glibmm/miscutils.h>
30
31 #include "pbd/enumwriter.h"
32 #include "pbd/xml++.h"
33 #include "pbd/convert.h"
34
35 #include "ardour/export_profile_manager.h"
36 #include "ardour/export_format_specification.h"
37 #include "ardour/search_paths.h"
38 #include "ardour/export_timespan.h"
39 #include "ardour/export_channel_configuration.h"
40 #include "ardour/export_filename.h"
41 #include "ardour/export_preset.h"
42 #include "ardour/export_handler.h"
43 #include "ardour/export_failed.h"
44 #include "ardour/directory_names.h"
45 #include "ardour/filename_extensions.h"
46 #include "ardour/route.h"
47 #include "ardour/session.h"
48 #include "ardour/broadcast_info.h"
49
50 #include "i18n.h"
51
52 using namespace std;
53 using namespace Glib;
54 using namespace PBD;
55
56 namespace ARDOUR
57 {
58
59 ExportProfileManager::ExportProfileManager (Session & s, ExportType type)
60   : type(type)
61   , handler (s.get_export_handler())
62   , session (s)
63
64   , ranges (new LocationList ())
65   , single_range_mode (false)
66
67   , format_list (new FormatList ())
68 {
69         switch(type) {
70         case RegularExport:
71                 xml_node_name = X_("ExportProfile");
72                 break;
73         case RangeExport:
74                 xml_node_name = X_("RangeExportProfile");
75                 break;
76         case SelectionExport:
77                 xml_node_name = X_("SelectionExportProfile");
78                 break;
79         case RegionExport:
80                 xml_node_name = X_("RegionExportProfile");
81                 break;
82         case StemExport:
83                 xml_node_name = X_("StemExportProfile");
84                 break;
85         }
86
87         /* Initialize path variables */
88
89         export_config_dir = Glib::build_filename (user_config_directory(), export_dir_name);
90
91         search_path += export_formats_search_path();
92
93         info << string_compose (_("Searching for export formats in %1"), search_path.to_string()) << endmsg;
94
95         /* create export config directory if necessary */
96
97         if (!Glib::file_test (export_config_dir, Glib::FILE_TEST_EXISTS)) {
98                 if (g_mkdir_with_parents (export_config_dir.c_str(), 0755) != 0) {
99                         error << string_compose (_("Unable to create export format directory %1: %2"), export_config_dir, g_strerror(errno)) << endmsg;
100                 }
101         }
102
103         load_presets ();
104         load_formats ();
105
106         /* Initialize all lists with an empty config */
107
108         XMLNodeList dummy;
109         init_timespans (dummy);
110         init_channel_configs (dummy);
111         init_formats (dummy);
112         init_filenames (dummy);
113 }
114
115 ExportProfileManager::~ExportProfileManager ()
116 {
117         XMLNode * instant_xml (new XMLNode (xml_node_name));
118         serialize_profile (*instant_xml);
119         session.add_instant_xml (*instant_xml, false);
120 }
121
122 void
123 ExportProfileManager::load_profile ()
124 {
125         XMLNode * instant_node = session.instant_xml (xml_node_name);
126         if (instant_node) {
127                 set_state (*instant_node);
128         } else {
129                 XMLNode empty_node (xml_node_name);
130                 set_state (empty_node);
131         }
132 }
133
134 void
135 ExportProfileManager::prepare_for_export ()
136 {
137         TimespanListPtr ts_list = timespans.front()->timespans;
138
139         FormatStateList::const_iterator format_it;
140         FilenameStateList::const_iterator filename_it;
141
142         // For each timespan
143         for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
144                 // ..., each format-filename pair
145                 for (format_it = formats.begin(), filename_it = filenames.begin();
146                      format_it != formats.end() && filename_it != filenames.end();
147                      ++format_it, ++filename_it) {
148
149                         ExportFilenamePtr filename = (*filename_it)->filename;
150 //                      filename->include_timespan = (ts_list->size() > 1); Disabled for now...
151
152                         boost::shared_ptr<BroadcastInfo> b;
153                         if ((*format_it)->format->has_broadcast_info()) {
154                                 b.reset (new BroadcastInfo);
155                                 b->set_from_session (session, (*ts_it)->get_start());
156                         }
157
158                         // ...and each channel config
159                         filename->include_channel_config = (type == StemExport) ||
160                                                            (channel_configs.size() > 1);
161                         for(ChannelConfigStateList::iterator cc_it = channel_configs.begin(); cc_it != channel_configs.end(); ++cc_it) {
162                                 handler->add_export_config (*ts_it, (*cc_it)->config, (*format_it)->format, filename, b);
163                         }
164                 }
165         }
166 }
167
168 bool
169 ExportProfileManager::load_preset (ExportPresetPtr preset)
170 {
171         bool ok = true;
172
173         current_preset = preset;
174         if (!preset) { return false; }
175
176         XMLNode const * state;
177         if ((state = preset->get_local_state())) {
178                 set_local_state (*state);
179         } else { ok = false; }
180
181         if ((state = preset->get_global_state())) {
182                 if (!set_global_state (*state)) {
183                         ok = false;
184                 }
185         } else { ok = false; }
186
187         return ok;
188 }
189
190 void
191 ExportProfileManager::load_presets ()
192 {
193         vector<std::string> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
194
195         for (vector<std::string>::iterator it = found.begin(); it != found.end(); ++it) {
196                 load_preset_from_disk (*it);
197         }
198 }
199
200 std::string
201 ExportProfileManager::preset_filename (std::string const & preset_name)
202 {
203         string safe_name = legalize_for_path (preset_name);
204         return Glib::build_filename (export_config_dir, safe_name + export_preset_suffix);
205 }
206
207 ExportPresetPtr
208 ExportProfileManager::new_preset (string const & name)
209 {
210         // Generate new ID and do regular save
211         string filename = preset_filename (name);
212         current_preset.reset (new ExportPreset (filename, session));
213         preset_list.push_back (current_preset);
214         return save_preset (name);
215 }
216
217 ExportPresetPtr
218 ExportProfileManager::save_preset (string const & name)
219 {
220         string filename = preset_filename (name);
221
222         if (!current_preset) {
223                 current_preset.reset (new ExportPreset (filename, session));
224                 preset_list.push_back (current_preset);
225         }
226
227         XMLNode * global_preset = new XMLNode ("ExportPreset");
228         XMLNode * local_preset = new XMLNode ("ExportPreset");
229
230         serialize_global_profile (*global_preset);
231         serialize_local_profile (*local_preset);
232
233         current_preset->set_name (name);
234         current_preset->set_global_state (*global_preset);
235         current_preset->set_local_state (*local_preset);
236
237         current_preset->save (filename);
238
239         return current_preset;
240 }
241
242 void
243 ExportProfileManager::remove_preset ()
244 {
245         if (!current_preset) { return; }
246
247         for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
248                 if (*it == current_preset) {
249                         preset_list.erase (it);
250                         break;
251                 }
252         }
253
254         FileMap::iterator it = preset_file_map.find (current_preset->id());
255         if (it != preset_file_map.end()) {
256                 if (g_remove (it->second.c_str()) != 0) {
257                         error << string_compose (_("Unable to remove export preset %1: %2"), it->second, g_strerror(errno)) << endmsg;
258                 }
259                 preset_file_map.erase (it);
260         }
261
262         current_preset->remove_local();
263         current_preset.reset();
264 }
265
266 void
267 ExportProfileManager::load_preset_from_disk (std::string const & path)
268 {
269         ExportPresetPtr preset (new ExportPreset (path, session));
270
271         /* Handle id to filename mapping and don't add duplicates to list */
272
273         FilePair pair (preset->id(), path);
274         if (preset_file_map.insert (pair).second) {
275                 preset_list.push_back (preset);
276         }
277 }
278
279 bool
280 ExportProfileManager::set_state (XMLNode const & root)
281 {
282         return set_global_state (root) & set_local_state (root);
283 }
284
285 bool
286 ExportProfileManager::set_global_state (XMLNode const & root)
287 {
288         return init_filenames (root.children ("ExportFilename")) &
289                init_formats (root.children ("ExportFormat"));
290 }
291
292 bool
293 ExportProfileManager::set_local_state (XMLNode const & root)
294 {
295         return init_timespans (root.children ("ExportTimespan")) &
296                init_channel_configs (root.children ("ExportChannelConfiguration"));
297 }
298
299 void
300 ExportProfileManager::serialize_profile (XMLNode & root)
301 {
302         serialize_local_profile (root);
303         serialize_global_profile (root);
304 }
305
306 void
307 ExportProfileManager::serialize_global_profile (XMLNode & root)
308 {
309         for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
310                 root.add_child_nocopy (serialize_format (*it));
311         }
312
313         for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
314                 root.add_child_nocopy ((*it)->filename->get_state());
315         }
316 }
317
318 void
319 ExportProfileManager::serialize_local_profile (XMLNode & root)
320 {
321         for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
322                 root.add_child_nocopy (serialize_timespan (*it));
323         }
324
325         for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
326                 root.add_child_nocopy ((*it)->config->get_state());
327         }
328 }
329
330 std::vector<std::string>
331 ExportProfileManager::find_file (std::string const & pattern)
332 {
333         vector<std::string> found;
334
335         find_files_matching_pattern (found, search_path, pattern);
336
337         return found;
338 }
339
340 void
341 ExportProfileManager::set_selection_range (framepos_t start, framepos_t end)
342 {
343
344         if (start || end) {
345                 selection_range.reset (new Location (session));
346                 selection_range->set_name (_("Selection"));
347                 selection_range->set (start, end);
348         } else {
349                 selection_range.reset();
350         }
351
352         for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
353                 (*it)->selection_range = selection_range;
354         }
355 }
356
357 std::string
358 ExportProfileManager::set_single_range (framepos_t start, framepos_t end, string name)
359 {
360         single_range_mode = true;
361
362         single_range.reset (new Location (session));
363         single_range->set_name (name);
364         single_range->set (start, end);
365
366         update_ranges ();
367
368         return single_range->id().to_s();
369 }
370
371 bool
372 ExportProfileManager::init_timespans (XMLNodeList nodes)
373 {
374         timespans.clear ();
375         update_ranges ();
376
377         bool ok = true;
378         for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
379                 TimespanStatePtr span = deserialize_timespan (**it);
380                 if (span) {
381                         timespans.push_back (span);
382                 } else { ok = false; }
383         }
384
385         if (timespans.empty()) {
386                 TimespanStatePtr state (new TimespanState (selection_range, ranges));
387                 timespans.push_back (state);
388
389                 // Add session as default selection
390                 Location * session_range = session.locations()->session_range_location();
391                 if (!session_range) { return false; }
392
393                 ExportTimespanPtr timespan = handler->add_timespan();
394                 timespan->set_name (session_range->name());
395                 timespan->set_range_id (session_range->id().to_s());
396                 timespan->set_range (session_range->start(), session_range->end());
397                 state->timespans->push_back (timespan);
398                 return false;
399         }
400
401         return ok;
402 }
403
404 ExportProfileManager::TimespanStatePtr
405 ExportProfileManager::deserialize_timespan (XMLNode & root)
406 {
407         TimespanStatePtr state (new TimespanState (selection_range, ranges));
408         XMLProperty const * prop;
409
410         XMLNodeList spans = root.children ("Range");
411         for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
412
413                 prop = (*node_it)->property ("id");
414                 if (!prop) { continue; }
415                 string id = prop->value();
416
417                 Location * location = 0;
418                 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
419                         if ((id == "selection" && *it == selection_range.get()) ||
420                             (id == (*it)->id().to_s())) {
421                                 location = *it;
422                                 break;
423                         }
424                 }
425
426                 if (!location) { continue; }
427
428                 ExportTimespanPtr timespan = handler->add_timespan();
429                 timespan->set_name (location->name());
430                 timespan->set_range_id (location->id().to_s());
431                 timespan->set_range (location->start(), location->end());
432                 state->timespans->push_back (timespan);
433         }
434
435         if ((prop = root.property ("format"))) {
436                 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
437         }
438
439         if (state->timespans->empty()) {
440                 return TimespanStatePtr();
441         }
442
443         return state;
444 }
445
446 XMLNode &
447 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
448 {
449         XMLNode & root = *(new XMLNode ("ExportTimespan"));
450         XMLNode * span;
451
452         update_ranges ();
453         for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
454                 if ((span = root.add_child ("Range"))) {
455                         span->add_property ("id", (*it)->range_id());
456                 }
457         }
458
459         root.add_property ("format", enum_2_string (state->time_format));
460
461         return root;
462 }
463
464 void
465 ExportProfileManager::update_ranges () {
466         ranges->clear();
467
468         if (single_range_mode) {
469                 ranges->push_back (single_range.get());
470                 return;
471         }
472
473         /* Session */
474
475         Location * session_range = session.locations()->session_range_location();
476         if (session_range) {
477                 ranges->push_back (session_range);
478         }
479
480         /* Selection */
481
482         if (selection_range) {
483                 ranges->push_back (selection_range.get());
484         }
485
486         /* ranges */
487
488         LocationList const & list (session.locations()->list());
489         for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
490                 if ((*it)->is_range_marker()) {
491                         ranges->push_back (*it);
492                 }
493         }
494 }
495
496 ExportProfileManager::ChannelConfigStatePtr
497 ExportProfileManager::add_channel_config ()
498 {
499         ChannelConfigStatePtr ptr(new ChannelConfigState(handler->add_channel_config()));
500         channel_configs.push_back(ptr);
501         return ptr;
502 }
503
504 bool
505 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
506 {
507         channel_configs.clear();
508
509         if (nodes.empty()) {
510                 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
511                 channel_configs.push_back (config);
512
513                 // Add master outs as default
514                 if (!session.master_out()) { return false; }
515
516                 IO* master_out = session.master_out()->output().get();
517                 if (!master_out) { return false; }
518
519                 for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
520                         PortExportChannel * channel = new PortExportChannel ();
521                         channel->add_port (master_out->audio (n));
522
523                         ExportChannelPtr chan_ptr (channel);
524                         config->config->register_channel (chan_ptr);
525                 }
526                 return false;
527         }
528
529         for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
530                 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
531                 config->config->set_state (**it);
532                 channel_configs.push_back (config);
533         }
534
535         return true;
536 }
537
538 ExportProfileManager::FormatStatePtr
539 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
540 {
541         /* Note: The pointer in the new FormatState should point to the same format spec
542                  as the original state's pointer. The spec itself should not be copied!   */
543
544         FormatStatePtr format (new FormatState (format_list, state->format));
545         formats.push_back (format);
546         return format;
547 }
548
549 void
550 ExportProfileManager::remove_format_state (FormatStatePtr state)
551 {
552         for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
553                 if (*it == state) {
554                         formats.erase (it);
555                         return;
556                 }
557         }
558 }
559
560 std::string
561 ExportProfileManager::save_format_to_disk (ExportFormatSpecPtr format)
562 {
563         // TODO filename character stripping
564
565         /* Get filename for file */
566
567         string new_name = format->name();
568         new_name += export_format_suffix;
569
570         /* make sure its legal for the filesystem */
571
572         new_name = legalize_for_path (new_name);
573
574         std::string new_path = Glib::build_filename (export_config_dir, new_name);
575
576         /* Check if format is on disk already */
577         FileMap::iterator it;
578         if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
579
580                 /* Check if config is not in user config dir */
581                 if (Glib::path_get_dirname (it->second) != export_config_dir) {
582
583                         /* Write new file */
584
585                         XMLTree tree (new_path);
586                         tree.set_root (&format->get_state());
587                         tree.write();
588
589                 } else {
590
591                         /* Update file and rename if necessary */
592
593                         XMLTree tree (it->second);
594                         tree.set_root (&format->get_state());
595                         tree.write();
596
597                         if (new_name != Glib::path_get_basename (it->second)) {
598                                 if (g_rename (it->second.c_str(), new_path.c_str()) != 0) {
599                                         error << string_compose (_("Unable to rename export format %1 to %2: %3"), it->second, new_path, g_strerror(errno)) << endmsg;
600                                 };
601                         }
602                 }
603
604                 it->second = new_path;
605
606         } else {
607                 /* Write new file */
608
609                 XMLTree tree (new_path);
610                 tree.set_root (&format->get_state());
611                 tree.write();
612         }
613
614         FormatListChanged ();
615         return new_path;
616 }
617
618 void
619 ExportProfileManager::remove_format_profile (ExportFormatSpecPtr format)
620 {
621         for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
622                 if (*it == format) {
623                         format_list->erase (it);
624                         break;
625                 }
626         }
627
628         FileMap::iterator it = format_file_map.find (format->id());
629         if (it != format_file_map.end()) {
630                 if (g_remove (it->second.c_str()) != 0) {
631                         error << string_compose (_("Unable to remove export profile %1: %2"), it->second, g_strerror(errno)) << endmsg;
632                         return;
633                 }
634                 format_file_map.erase (it);
635         }
636
637         FormatListChanged ();
638 }
639
640 ExportFormatSpecPtr
641 ExportProfileManager::get_new_format (ExportFormatSpecPtr original)
642 {
643         ExportFormatSpecPtr format;
644         if (original) {
645                 format.reset (new ExportFormatSpecification (*original));
646                 std::cerr << "After new format created from original, format has id [" << format->id().to_s() << ']' << std::endl;
647         } else {
648                 format = handler->add_format();
649                 format->set_name (_("empty format"));
650         }
651
652         std::string path = save_format_to_disk (format);
653         FilePair pair (format->id(), path);
654         format_file_map.insert (pair);
655
656         format_list->push_back (format);
657         FormatListChanged ();
658
659         return format;
660 }
661
662 bool
663 ExportProfileManager::init_formats (XMLNodeList nodes)
664 {
665         formats.clear();
666
667         bool ok = true;
668         for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
669                 FormatStatePtr format = deserialize_format (**it);
670                 if (format) {
671                         formats.push_back (format);
672                 } else { ok = false; }
673         }
674
675         if (formats.empty ()) {
676                 FormatStatePtr format (new FormatState (format_list, ExportFormatSpecPtr ()));
677                 formats.push_back (format);
678                 return false;
679         }
680
681         return ok;
682 }
683
684 ExportProfileManager::FormatStatePtr
685 ExportProfileManager::deserialize_format (XMLNode & root)
686 {
687         XMLProperty * prop;
688         PBD::UUID id;
689
690         if ((prop = root.property ("id"))) {
691                 id = prop->value();
692         }
693
694         for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
695                 if ((*it)->id() == id) {
696                         return FormatStatePtr (new FormatState (format_list, *it));
697                 }
698         }
699
700         return FormatStatePtr ();
701 }
702
703 XMLNode &
704 ExportProfileManager::serialize_format (FormatStatePtr state)
705 {
706         XMLNode * root = new XMLNode ("ExportFormat");
707
708         string id = state->format ? state->format->id().to_s() : "";
709         root->add_property ("id", id);
710
711         return *root;
712 }
713
714 void
715 ExportProfileManager::load_formats ()
716 {
717         vector<std::string> found = find_file (string_compose ("*%1", export_format_suffix));
718
719         for (vector<std::string>::iterator it = found.begin(); it != found.end(); ++it) {
720                 load_format_from_disk (*it);
721         }
722 }
723
724 void
725 ExportProfileManager::load_format_from_disk (std::string const & path)
726 {
727         XMLTree tree;
728
729         if (!tree.read (path)) {
730                 error << string_compose (_("Cannot load export format from %1"), path) << endmsg;
731                 return;
732         }
733
734         XMLNode* root = tree.root();
735         if (!root) {
736                 error << string_compose (_("Cannot export format read from %1"), path) << endmsg;
737                 return;
738         }
739
740         ExportFormatSpecPtr format = handler->add_format (*root);
741
742         /* Handle id to filename mapping and don't add duplicates to list */
743
744         FilePair pair (format->id(), path);
745         if (format_file_map.insert (pair).second) {
746                 format_list->push_back (format);
747         }
748
749         FormatListChanged ();
750 }
751
752 ExportProfileManager::FilenameStatePtr
753 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
754 {
755         FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
756         filenames.push_back (filename);
757         return filename;
758 }
759
760 void
761 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
762 {
763         for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
764                 if (*it == state) {
765                         filenames.erase (it);
766                         return;
767                 }
768         }
769 }
770
771 std::string
772 ExportProfileManager::get_sample_filename_for_format (ExportFilenamePtr filename, ExportFormatSpecPtr format)
773 {
774         assert (format);
775         
776         if (channel_configs.empty()) { return ""; }
777
778         std::list<string> filenames;
779         build_filenames (filenames, filename, timespans.front()->timespans,
780                          channel_configs.front()->config, format);
781
782         if (filenames.empty()) { return ""; }
783         return filenames.front();
784 }
785
786 bool
787 ExportProfileManager::init_filenames (XMLNodeList nodes)
788 {
789         filenames.clear ();
790
791         for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
792                 ExportFilenamePtr filename = handler->add_filename();
793                 filename->set_state (**it);
794                 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
795         }
796
797         if (filenames.empty()) {
798                 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
799                 filenames.push_back (filename);
800                 return false;
801         }
802
803         return true;
804 }
805
806 boost::shared_ptr<ExportProfileManager::Warnings>
807 ExportProfileManager::get_warnings ()
808 {
809         boost::shared_ptr<Warnings> warnings (new Warnings ());
810
811         ChannelConfigStatePtr channel_config_state;
812         if (!channel_configs.empty ()) {
813                 channel_config_state = channel_configs.front();
814         }
815         
816         TimespanStatePtr timespan_state = timespans.front();
817
818         /*** Check "global" config ***/
819
820         TimespanListPtr timespans = timespan_state->timespans;
821
822         ExportChannelConfigPtr channel_config;
823         if (channel_config_state) {
824                 channel_config = channel_config_state->config;
825         }
826
827         /* Check Timespans are not empty */
828
829         if (timespans->empty()) {
830                 warnings->errors.push_back (_("No timespan has been selected!"));
831         }
832
833         if (channel_config_state == 0) {
834                 warnings->errors.push_back (_("No channels have been selected!"));
835         } else {
836                 /* Check channel config ports */
837                 if (!channel_config->all_channels_have_ports ()) {
838                         warnings->warnings.push_back (_("Some channels are empty"));
839                 }
840         }
841
842         /*** Check files ***/
843
844         if (channel_config_state) {
845                 FormatStateList::const_iterator format_it;
846                 FilenameStateList::const_iterator filename_it;
847                 for (format_it = formats.begin(), filename_it = filenames.begin();
848                      format_it != formats.end() && filename_it != filenames.end();
849                      ++format_it, ++filename_it) {
850                         check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
851                 }
852         }
853         
854         return warnings;
855 }
856
857 void
858 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
859                                     TimespanStatePtr timespan_state,
860                                     ChannelConfigStatePtr channel_config_state,
861                                     FormatStatePtr format_state,
862                                     FilenameStatePtr filename_state)
863 {
864         TimespanListPtr timespans = timespan_state->timespans;
865         ExportChannelConfigPtr channel_config = channel_config_state->config;
866         ExportFormatSpecPtr format = format_state->format;
867         ExportFilenamePtr filename = filename_state->filename;
868
869         /* Check format and maximum channel count */
870         if (!format || !format->type()) {
871                 warnings->errors.push_back (_("No format selected!"));
872         } else if (!channel_config->get_n_chans()) {
873                 warnings->errors.push_back (_("All channels are empty!"));
874         } else if (!check_format (format, channel_config->get_n_chans())) {
875                 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
876         } else if (format->channel_limit() < channel_config->get_n_chans()) {
877                 warnings->errors.push_back
878                   (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
879                                      format->format_name(),
880                                      format->channel_limit(),
881                                      channel_config->get_n_chans()));
882         }
883
884         if (!warnings->errors.empty()) { return; }
885
886         /* Check filenames */
887
888 //      filename->include_timespan = (timespans->size() > 1); Disabled for now...
889
890         std::list<string> paths;
891         build_filenames(paths, filename, timespans, channel_config, format);
892
893         for (std::list<string>::const_iterator path_it = paths.begin(); path_it != paths.end(); ++path_it) {
894
895                 string path = *path_it;
896
897                 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
898                         warnings->conflicting_filenames.push_back (path);
899                 }
900
901                 if (format->with_toc()) {
902                         string marker_file = handler->get_cd_marker_filename(path, CDMarkerTOC);
903                         if (Glib::file_test (marker_file, Glib::FILE_TEST_EXISTS)) {
904                                 warnings->conflicting_filenames.push_back (marker_file);
905                         }
906                 }
907
908                 if (format->with_cue()) {
909                         string marker_file = handler->get_cd_marker_filename(path, CDMarkerCUE);
910                         if (Glib::file_test (marker_file, Glib::FILE_TEST_EXISTS)) {
911                                 warnings->conflicting_filenames.push_back (marker_file);
912                         }
913                 }
914         }
915 }
916
917 bool
918 ExportProfileManager::check_format (ExportFormatSpecPtr format, uint32_t channels)
919 {
920         switch (format->type()) {
921           case ExportFormatBase::T_Sndfile:
922                 return check_sndfile_format (format, channels);
923
924           default:
925                 throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
926         }
927 }
928
929 bool
930 ExportProfileManager::check_sndfile_format (ExportFormatSpecPtr format, unsigned int channels)
931 {
932         SF_INFO sf_info;
933         sf_info.channels = channels;
934         sf_info.samplerate = format->sample_rate ();
935         sf_info.format = format->format_id () | format->sample_format ();
936
937         return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
938 }
939
940 void
941 ExportProfileManager::build_filenames(std::list<std::string> & result, ExportFilenamePtr filename,
942                                       TimespanListPtr timespans, ExportChannelConfigPtr channel_config,
943                                       ExportFormatSpecPtr format)
944 {
945         for (std::list<ExportTimespanPtr>::iterator timespan_it = timespans->begin();
946              timespan_it != timespans->end(); ++timespan_it) {
947                 filename->set_timespan (*timespan_it);
948
949                 if (channel_config->get_split()) {
950                         filename->include_channel = true;
951
952                         for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
953                                 filename->set_channel (chan);
954                                 result.push_back(filename->get_path (format));
955                         }
956
957                 } else {
958                         filename->include_channel = false;
959                         result.push_back(filename->get_path (format));
960                 }
961         }
962 }
963
964 }; // namespace ARDOUR