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