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