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