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