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