2 Copyright (C) 2008 Paul Davis
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.
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.
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.
21 #include <ardour/export_profile_manager.h>
26 #include <glibmm/fileutils.h>
28 #include <pbd/enumwriter.h>
29 #include <pbd/xml++.h>
30 #include <pbd/convert.h>
32 #include <ardour/export_failed.h>
33 #include <ardour/export_file_io.h>
34 #include <ardour/export_format_specification.h>
35 #include <ardour/export_timespan.h>
36 #include <ardour/export_channel_configuration.h>
37 #include <ardour/export_filename.h>
38 #include <ardour/export_preset.h>
39 #include <ardour/export_handler.h>
40 #include <ardour/filename_extensions.h>
41 #include <ardour/session.h>
50 ExportProfileManager::ExportProfileManager (Session & s) :
51 handler (s.get_export_handler()),
54 session_range (new Location ()),
55 ranges (new LocationList ()),
56 single_range_mode (false),
58 format_list (new FormatList ())
61 /* Initialize path variables */
65 export_config_dir = user_config_directory();
66 export_config_dir /= "export";
67 search_path += export_config_dir;
69 path = ardour_search_path().to_string();
73 path = system_config_search_path().to_string();
77 /* create export config directory if necessary */
79 if (!sys::exists (export_config_dir)) {
80 sys::create_directory (export_config_dir);
86 /* Initialize all lists with an empty config */
89 init_timespans (dummy);
90 init_channel_configs (dummy);
92 init_filenames (dummy);
95 ExportProfileManager::~ExportProfileManager ()
97 if (single_range_mode) { return; }
99 XMLNode * instant_xml (new XMLNode ("ExportProfile"));
100 serialize_profile (*instant_xml);
101 session.add_instant_xml (*instant_xml, false);
105 ExportProfileManager::load_profile ()
107 XMLNode * instant_node = session.instant_xml ("ExportProfile");
109 set_state (*instant_node);
111 XMLNode empty_node ("ExportProfile");
112 set_state (empty_node);
117 ExportProfileManager::prepare_for_export ()
119 ChannelConfigPtr channel_config = channel_configs.front()->config;
120 TimespanListPtr ts_list = timespans.front()->timespans;
122 FormatStateList::const_iterator format_it;
123 FilenameStateList::const_iterator filename_it;
125 for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
126 for (format_it = formats.begin(), filename_it = filenames.begin();
127 format_it != formats.end() && filename_it != filenames.end();
128 ++format_it, ++filename_it) {
130 // filename->include_timespan = (ts_list->size() > 1); Disabled for now...
131 handler->add_export_config (*ts_it, channel_config, (*format_it)->format, (*filename_it)->filename);
137 ExportProfileManager::load_preset (PresetPtr preset)
141 current_preset = preset;
142 if (!preset) { return false; }
144 XMLNode const * state;
145 if ((state = preset->get_local_state())) {
146 set_local_state (*state);
147 } else { ok = false; }
149 if ((state = preset->get_global_state())) {
150 if (!set_global_state (*state)) {
153 } else { ok = false; }
159 ExportProfileManager::load_presets ()
161 vector<sys::path> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
163 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
164 load_preset_from_disk (*it);
168 ExportProfileManager::PresetPtr
169 ExportProfileManager::save_preset (string const & name)
171 if (!current_preset) {
172 string filename = export_config_dir.to_string() + "/" + name + export_preset_suffix;
173 current_preset.reset (new ExportPreset (filename, session));
174 preset_list.push_back (current_preset);
177 XMLNode * global_preset = new XMLNode ("ExportPreset");
178 XMLNode * local_preset = new XMLNode ("ExportPreset");
180 serialize_global_profile (*global_preset);
181 serialize_local_profile (*local_preset);
183 current_preset->set_name (name);
184 current_preset->set_global_state (*global_preset);
185 current_preset->set_local_state (*local_preset);
187 current_preset->save();
189 return current_preset;
193 ExportProfileManager::remove_preset ()
195 if (!current_preset) { return; }
197 for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
198 if (*it == current_preset) {
199 preset_list.erase (it);
204 FileMap::iterator it = preset_file_map.find (current_preset->id());
205 if (it != preset_file_map.end()) {
206 sys::remove (it->second);
207 preset_file_map.erase (it);
210 current_preset->remove_local();
211 current_preset.reset();
215 ExportProfileManager::load_preset_from_disk (PBD::sys::path const & path)
217 PresetPtr preset (new ExportPreset (path.to_string(), session));
218 preset_list.push_back (preset);
220 /* Handle id to filename mapping */
222 FilePair pair (preset->id(), path);
223 preset_file_map.insert (pair);
227 ExportProfileManager::set_state (XMLNode const & root)
229 return set_global_state (root) && set_local_state (root);
233 ExportProfileManager::set_global_state (XMLNode const & root)
235 return init_filenames (root.children ("ExportFilename")) &&
236 init_formats (root.children ("ExportFormat"));
240 ExportProfileManager::set_local_state (XMLNode const & root)
242 return init_timespans (root.children ("ExportTimespan")) &&
243 init_channel_configs (root.children ("ExportChannelConfiguration"));
247 ExportProfileManager::serialize_profile (XMLNode & root)
249 serialize_local_profile (root);
250 serialize_global_profile (root);
254 ExportProfileManager::serialize_global_profile (XMLNode & root)
256 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
257 root.add_child_nocopy (serialize_format (*it));
260 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
261 root.add_child_nocopy ((*it)->filename->get_state());
266 ExportProfileManager::serialize_local_profile (XMLNode & root)
268 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
269 root.add_child_nocopy (serialize_timespan (*it));
272 for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
273 root.add_child_nocopy ((*it)->config->get_state());
277 std::vector<sys::path>
278 ExportProfileManager::find_file (std::string const & pattern)
280 vector<sys::path> found;
282 Glib::PatternSpec pattern_spec (pattern);
283 find_matching_files_in_search_path (search_path, pattern_spec, found);
289 ExportProfileManager::set_selection_range (nframes_t start, nframes_t end)
293 selection_range.reset (new Location());
294 selection_range->set_name (_("Selection"));
295 selection_range->set (start, end);
297 selection_range.reset();
300 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
301 (*it)->selection_range = selection_range;
306 ExportProfileManager::set_single_range (nframes_t start, nframes_t end, Glib::ustring name)
308 single_range_mode = true;
310 single_range.reset (new Location());
311 single_range->set_name (name);
312 single_range->set (start, end);
316 return single_range->id().to_s();
320 ExportProfileManager::init_timespans (XMLNodeList nodes)
326 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
327 TimespanStatePtr span = deserialize_timespan (**it);
329 timespans.push_back (span);
330 } else { ok = false; }
333 if (timespans.empty()) {
334 TimespanStatePtr timespan (new TimespanState (session_range, selection_range, ranges));
335 timespans.push_back (timespan);
342 ExportProfileManager::TimespanStatePtr
343 ExportProfileManager::deserialize_timespan (XMLNode & root)
345 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
346 XMLProperty const * prop;
348 XMLNodeList spans = root.children ("Range");
349 for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
351 prop = (*node_it)->property ("id");
352 if (!prop) { continue; }
353 ustring id = prop->value();
355 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
356 if ((!id.compare ("session") && *it == session_range.get()) ||
357 (!id.compare ("selection") && *it == selection_range.get()) ||
358 (!id.compare ((*it)->id().to_s()))) {
359 TimespanPtr timespan = handler->add_timespan();
360 timespan->set_name ((*it)->name());
361 timespan->set_range_id (id);
362 timespan->set_range ((*it)->start(), (*it)->end());
363 state->timespans->push_back (timespan);
368 if ((prop = root.property ("format"))) {
369 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
376 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
378 XMLNode & root = *(new XMLNode ("ExportTimespan"));
383 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
384 if ((span = root.add_child ("Range"))) {
385 span->add_property ("id", (*it)->range_id());
389 root.add_property ("format", enum_2_string (state->time_format));
395 ExportProfileManager::update_ranges () {
398 if (single_range_mode) {
399 ranges->push_back (single_range.get());
405 session_range->set_name (_("Session"));
406 session_range->set (session.current_start_frame(), session.current_end_frame());
407 ranges->push_back (session_range.get());
411 if (selection_range) {
412 ranges->push_back (selection_range.get());
417 LocationList const & list (session.locations()->list());
418 for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
419 if ((*it)->is_range_marker()) {
420 ranges->push_back (*it);
426 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
428 channel_configs.clear();
431 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
432 channel_configs.push_back (config);
436 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
437 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
438 config->config->set_state (**it);
439 channel_configs.push_back (config);
445 ExportProfileManager::FormatStatePtr
446 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
448 /* Note: The pointer in the new FormatState should point to the same format spec
449 as the original state's pointer. The spec itself should not be copied! */
451 FormatStatePtr format (new FormatState (format_list, state->format));
452 formats.push_back (format);
457 ExportProfileManager::remove_format_state (FormatStatePtr state)
459 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
468 ExportProfileManager::save_format_to_disk (FormatPtr format)
470 // TODO filename character stripping
472 /* Get filename for file */
474 Glib::ustring new_name = format->name();
475 new_name += export_format_suffix;
477 sys::path new_path (export_config_dir);
478 new_path /= new_name;
480 /* Check if format is on disk already */
481 FileMap::iterator it;
482 if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
485 XMLTree tree (it->second.to_string());
486 tree.set_root (&format->get_state());
490 /* Rename if necessary */
492 if (new_name.compare (it->second.leaf())) {
493 sys::rename (it->second, new_path);
499 XMLTree tree (new_path.to_string());
500 tree.set_root (&format->get_state());
504 FormatListChanged ();
509 ExportProfileManager::remove_format_profile (FormatPtr format)
511 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
513 format_list->erase (it);
518 FileMap::iterator it = format_file_map.find (format->id());
519 if (it != format_file_map.end()) {
520 sys::remove (it->second);
521 format_file_map.erase (it);
524 FormatListChanged ();
527 ExportProfileManager::FormatPtr
528 ExportProfileManager::get_new_format (FormatPtr original)
532 format.reset (new ExportFormatSpecification (*original));
534 format = handler->add_format();
535 format->set_name ("empty format");
538 sys::path path = save_format_to_disk (format);
539 FilePair pair (format->id(), path);
540 format_file_map.insert (pair);
542 format_list->push_back (format);
543 FormatListChanged ();
549 ExportProfileManager::init_formats (XMLNodeList nodes)
554 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
555 FormatStatePtr format = deserialize_format (**it);
557 formats.push_back (format);
558 } else { ok = false; }
561 if (formats.empty ()) {
562 FormatStatePtr format (new FormatState (format_list, FormatPtr ()));
563 formats.push_back (format);
570 ExportProfileManager::FormatStatePtr
571 ExportProfileManager::deserialize_format (XMLNode & root)
576 if ((prop = root.property ("id"))) {
580 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
581 if ((*it)->id() == id) {
582 return FormatStatePtr (new FormatState (format_list, *it));
586 return FormatStatePtr ();
590 ExportProfileManager::serialize_format (FormatStatePtr state)
592 XMLNode * root = new XMLNode ("ExportFormat");
594 string id = state->format ? state->format->id().to_s() : "";
595 root->add_property ("id", id);
601 ExportProfileManager::load_formats ()
603 vector<sys::path> found = find_file (string_compose ("*%1", export_format_suffix));
605 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
606 load_format_from_disk (*it);
611 ExportProfileManager::load_format_from_disk (PBD::sys::path const & path)
613 XMLTree const tree (path.to_string());
614 FormatPtr format = handler->add_format (*tree.root());
615 format_list->push_back (format);
617 /* Handle id to filename mapping */
619 FilePair pair (format->id(), path);
620 format_file_map.insert (pair);
622 FormatListChanged ();
625 ExportProfileManager::FilenameStatePtr
626 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
628 FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
629 filenames.push_back (filename);
634 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
636 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
638 filenames.erase (it);
645 ExportProfileManager::init_filenames (XMLNodeList nodes)
649 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
650 FilenamePtr filename = handler->add_filename();
651 filename->set_state (**it);
652 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
655 if (filenames.empty()) {
656 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
657 filenames.push_back (filename);
664 boost::shared_ptr<ExportProfileManager::Warnings>
665 ExportProfileManager::get_warnings ()
667 boost::shared_ptr<Warnings> warnings (new Warnings ());
669 ChannelConfigStatePtr channel_config_state = channel_configs.front();
670 TimespanStatePtr timespan_state = timespans.front();
672 /*** Check "global" config ***/
674 TimespanListPtr timespans = timespan_state->timespans;
675 ChannelConfigPtr channel_config = channel_config_state->config;
677 /* Check Timespans are not empty */
679 if (timespans->empty()) {
680 warnings->errors.push_back (_("No timespan has been selected!"));
683 /* Check channel config ports */
685 if (!channel_config->all_channels_have_ports ()) {
686 warnings->warnings.push_back (_("Some channels are empty"));
689 /*** Check files ***/
691 FormatStateList::const_iterator format_it;
692 FilenameStateList::const_iterator filename_it;
693 for (format_it = formats.begin(), filename_it = filenames.begin();
694 format_it != formats.end() && filename_it != filenames.end();
695 ++format_it, ++filename_it) {
696 check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
703 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
704 TimespanStatePtr timespan_state,
705 ChannelConfigStatePtr channel_config_state,
706 FormatStatePtr format_state,
707 FilenameStatePtr filename_state)
709 TimespanListPtr timespans = timespan_state->timespans;
710 ChannelConfigPtr channel_config = channel_config_state->config;
711 FormatPtr format = format_state->format;
712 FilenamePtr filename = filename_state->filename;
714 /* Check format and maximum channel count */
715 if (!format || !format->type()) {
716 warnings->errors.push_back (_("No format selected!"));
717 } else if (!ExportFileFactory::check (format, channel_config->get_n_chans())) {
718 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
719 } else if (format->channel_limit() < channel_config->get_n_chans()) {
720 warnings->errors.push_back
721 (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
722 format->format_name(),
723 format->channel_limit(),
724 channel_config->get_n_chans()));
727 if (!warnings->errors.empty()) { return; }
729 /* Check filenames */
731 // filename->include_timespan = (timespans->size() > 1); Disabled for now...
733 for (std::list<TimespanPtr>::iterator timespan_it = timespans->begin(); timespan_it != timespans->end(); ++timespan_it) {
734 filename->set_timespan (*timespan_it);
736 if (channel_config->get_split()) {
737 filename->include_channel = true;
739 for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
740 filename->set_channel (chan);
742 Glib::ustring path = filename->get_path (format);
744 if (sys::exists (sys::path (path))) {
745 warnings->conflicting_filenames.push_back (path);
750 Glib::ustring path = filename->get_path (format);
752 if (sys::exists (sys::path (path))) {
753 warnings->conflicting_filenames.push_back (path);
759 }; // namespace ARDOUR