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