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