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