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