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