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