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