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"
52 ExportProfileManager::ExportProfileManager (Session & s) :
53 handler (s.get_export_handler()),
56 session_range (new Location ()),
57 ranges (new LocationList ()),
58 single_range_mode (false),
60 format_list (new FormatList ())
63 /* Initialize path variables */
65 export_config_dir = user_config_directory();
66 export_config_dir /= "export";
67 search_path += export_config_dir;
69 search_path += ardour_search_path().add_subdirectory_to_paths("export");
70 search_path += system_config_search_path().add_subdirectory_to_paths("export");;
72 /* create export config directory if necessary */
74 if (!sys::exists (export_config_dir)) {
75 sys::create_directory (export_config_dir);
81 /* Initialize all lists with an empty config */
84 init_timespans (dummy);
85 init_channel_configs (dummy);
87 init_filenames (dummy);
90 ExportProfileManager::~ExportProfileManager ()
92 if (single_range_mode) { return; }
94 XMLNode * instant_xml (new XMLNode ("ExportProfile"));
95 serialize_profile (*instant_xml);
96 session.add_instant_xml (*instant_xml, false);
100 ExportProfileManager::load_profile ()
102 XMLNode * instant_node = session.instant_xml ("ExportProfile");
104 set_state (*instant_node);
106 XMLNode empty_node ("ExportProfile");
107 set_state (empty_node);
112 ExportProfileManager::prepare_for_export ()
114 ChannelConfigPtr channel_config = channel_configs.front()->config;
115 TimespanListPtr ts_list = timespans.front()->timespans;
117 FormatStateList::const_iterator format_it;
118 FilenameStateList::const_iterator filename_it;
120 for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
121 for (format_it = formats.begin(), filename_it = filenames.begin();
122 format_it != formats.end() && filename_it != filenames.end();
123 ++format_it, ++filename_it) {
125 // filename->include_timespan = (ts_list->size() > 1); Disabled for now...
126 handler->add_export_config (*ts_it, channel_config, (*format_it)->format, (*filename_it)->filename);
132 ExportProfileManager::load_preset (PresetPtr preset)
136 current_preset = preset;
137 if (!preset) { return false; }
139 XMLNode const * state;
140 if ((state = preset->get_local_state())) {
141 set_local_state (*state);
142 } else { ok = false; }
144 if ((state = preset->get_global_state())) {
145 if (!set_global_state (*state)) {
148 } else { ok = false; }
154 ExportProfileManager::load_presets ()
156 vector<sys::path> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
158 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
159 load_preset_from_disk (*it);
163 ExportProfileManager::PresetPtr
164 ExportProfileManager::save_preset (string const & name)
166 string filename = export_config_dir.to_string() + "/" + name + export_preset_suffix;
168 if (!current_preset) {
169 current_preset.reset (new ExportPreset (filename, session));
170 preset_list.push_back (current_preset);
173 XMLNode * global_preset = new XMLNode ("ExportPreset");
174 XMLNode * local_preset = new XMLNode ("ExportPreset");
176 serialize_global_profile (*global_preset);
177 serialize_local_profile (*local_preset);
179 current_preset->set_name (name);
180 current_preset->set_global_state (*global_preset);
181 current_preset->set_local_state (*local_preset);
183 current_preset->save (filename);
185 return current_preset;
189 ExportProfileManager::remove_preset ()
191 if (!current_preset) { return; }
193 for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
194 if (*it == current_preset) {
195 preset_list.erase (it);
200 FileMap::iterator it = preset_file_map.find (current_preset->id());
201 if (it != preset_file_map.end()) {
202 sys::remove (it->second);
203 preset_file_map.erase (it);
206 current_preset->remove_local();
207 current_preset.reset();
211 ExportProfileManager::load_preset_from_disk (PBD::sys::path const & path)
213 PresetPtr preset (new ExportPreset (path.to_string(), session));
215 /* Handle id to filename mapping and don't add duplicates to list */
217 FilePair pair (preset->id(), path);
218 if (preset_file_map.insert (pair).second) {
219 preset_list.push_back (preset);
224 ExportProfileManager::set_state (XMLNode const & root)
226 return set_global_state (root) && set_local_state (root);
230 ExportProfileManager::set_global_state (XMLNode const & root)
232 return init_filenames (root.children ("ExportFilename")) &&
233 init_formats (root.children ("ExportFormat"));
237 ExportProfileManager::set_local_state (XMLNode const & root)
239 return init_timespans (root.children ("ExportTimespan")) &&
240 init_channel_configs (root.children ("ExportChannelConfiguration"));
244 ExportProfileManager::serialize_profile (XMLNode & root)
246 serialize_local_profile (root);
247 serialize_global_profile (root);
251 ExportProfileManager::serialize_global_profile (XMLNode & root)
253 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
254 root.add_child_nocopy (serialize_format (*it));
257 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
258 root.add_child_nocopy ((*it)->filename->get_state());
263 ExportProfileManager::serialize_local_profile (XMLNode & root)
265 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
266 root.add_child_nocopy (serialize_timespan (*it));
269 for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
270 root.add_child_nocopy ((*it)->config->get_state());
274 std::vector<sys::path>
275 ExportProfileManager::find_file (std::string const & pattern)
277 vector<sys::path> found;
279 Glib::PatternSpec pattern_spec (pattern);
280 find_matching_files_in_search_path (search_path, pattern_spec, found);
286 ExportProfileManager::set_selection_range (nframes_t start, nframes_t end)
290 selection_range.reset (new Location());
291 selection_range->set_name (_("Selection"));
292 selection_range->set (start, end);
294 selection_range.reset();
297 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
298 (*it)->selection_range = selection_range;
303 ExportProfileManager::set_single_range (nframes_t start, nframes_t end, Glib::ustring name)
305 single_range_mode = true;
307 single_range.reset (new Location());
308 single_range->set_name (name);
309 single_range->set (start, end);
313 return single_range->id().to_s();
317 ExportProfileManager::init_timespans (XMLNodeList nodes)
323 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
324 TimespanStatePtr span = deserialize_timespan (**it);
326 timespans.push_back (span);
327 } else { ok = false; }
330 if (timespans.empty()) {
331 TimespanStatePtr timespan (new TimespanState (session_range, selection_range, ranges));
332 timespans.push_back (timespan);
339 ExportProfileManager::TimespanStatePtr
340 ExportProfileManager::deserialize_timespan (XMLNode & root)
342 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
343 XMLProperty const * prop;
345 XMLNodeList spans = root.children ("Range");
346 for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
348 prop = (*node_it)->property ("id");
349 if (!prop) { continue; }
350 ustring id = prop->value();
352 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
353 if ((!id.compare ("session") && *it == session_range.get()) ||
354 (!id.compare ("selection") && *it == selection_range.get()) ||
355 (!id.compare ((*it)->id().to_s()))) {
356 TimespanPtr timespan = handler->add_timespan();
357 timespan->set_name ((*it)->name());
358 timespan->set_range_id (id);
359 timespan->set_range ((*it)->start(), (*it)->end());
360 state->timespans->push_back (timespan);
365 if ((prop = root.property ("format"))) {
366 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
373 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
375 XMLNode & root = *(new XMLNode ("ExportTimespan"));
380 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
381 if ((span = root.add_child ("Range"))) {
382 span->add_property ("id", (*it)->range_id());
386 root.add_property ("format", enum_2_string (state->time_format));
392 ExportProfileManager::update_ranges () {
395 if (single_range_mode) {
396 ranges->push_back (single_range.get());
402 session_range->set_name (_("Session"));
403 session_range->set (session.current_start_frame(), session.current_end_frame());
404 ranges->push_back (session_range.get());
408 if (selection_range) {
409 ranges->push_back (selection_range.get());
414 LocationList const & list (session.locations()->list());
415 for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
416 if ((*it)->is_range_marker()) {
417 ranges->push_back (*it);
423 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
425 channel_configs.clear();
428 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
429 channel_configs.push_back (config);
433 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
434 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
435 config->config->set_state (**it);
436 channel_configs.push_back (config);
442 ExportProfileManager::FormatStatePtr
443 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
445 /* Note: The pointer in the new FormatState should point to the same format spec
446 as the original state's pointer. The spec itself should not be copied! */
448 FormatStatePtr format (new FormatState (format_list, state->format));
449 formats.push_back (format);
454 ExportProfileManager::remove_format_state (FormatStatePtr state)
456 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
465 ExportProfileManager::save_format_to_disk (FormatPtr format)
467 // TODO filename character stripping
469 /* Get filename for file */
471 Glib::ustring new_name = format->name();
472 new_name += export_format_suffix;
474 sys::path new_path (export_config_dir);
475 new_path /= new_name;
477 /* Check if format is on disk already */
478 FileMap::iterator it;
479 if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
481 /* Check if config is not in user config dir */
482 if (it->second.branch_path().to_string().compare (export_config_dir.to_string())) {
486 XMLTree tree (new_path.to_string());
487 tree.set_root (&format->get_state());
492 /* Update file and rename if necessary */
494 XMLTree tree (it->second.to_string());
495 tree.set_root (&format->get_state());
498 if (new_name.compare (it->second.leaf())) {
499 sys::rename (it->second, new_path);
503 it->second = new_path;
508 XMLTree tree (new_path.to_string());
509 tree.set_root (&format->get_state());
513 FormatListChanged ();
518 ExportProfileManager::remove_format_profile (FormatPtr format)
520 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
522 format_list->erase (it);
527 FileMap::iterator it = format_file_map.find (format->id());
528 if (it != format_file_map.end()) {
529 sys::remove (it->second);
530 format_file_map.erase (it);
533 FormatListChanged ();
536 ExportProfileManager::FormatPtr
537 ExportProfileManager::get_new_format (FormatPtr original)
541 format.reset (new ExportFormatSpecification (*original));
543 format = handler->add_format();
544 format->set_name ("empty format");
547 sys::path path = save_format_to_disk (format);
548 FilePair pair (format->id(), path);
549 format_file_map.insert (pair);
551 format_list->push_back (format);
552 FormatListChanged ();
558 ExportProfileManager::init_formats (XMLNodeList nodes)
563 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
564 FormatStatePtr format = deserialize_format (**it);
566 formats.push_back (format);
567 } else { ok = false; }
570 if (formats.empty ()) {
571 FormatStatePtr format (new FormatState (format_list, FormatPtr ()));
572 formats.push_back (format);
579 ExportProfileManager::FormatStatePtr
580 ExportProfileManager::deserialize_format (XMLNode & root)
585 if ((prop = root.property ("id"))) {
589 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
590 if ((*it)->id() == id) {
591 return FormatStatePtr (new FormatState (format_list, *it));
595 return FormatStatePtr ();
599 ExportProfileManager::serialize_format (FormatStatePtr state)
601 XMLNode * root = new XMLNode ("ExportFormat");
603 string id = state->format ? state->format->id().to_s() : "";
604 root->add_property ("id", id);
610 ExportProfileManager::load_formats ()
612 vector<sys::path> found = find_file (string_compose ("*%1", export_format_suffix));
614 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
615 load_format_from_disk (*it);
620 ExportProfileManager::load_format_from_disk (PBD::sys::path const & path)
622 XMLTree const tree (path.to_string());
623 FormatPtr format = handler->add_format (*tree.root());
625 /* Handle id to filename mapping and don't add duplicates to list */
627 FilePair pair (format->id(), path);
628 if (format_file_map.insert (pair).second) {
629 format_list->push_back (format);
632 FormatListChanged ();
635 ExportProfileManager::FilenameStatePtr
636 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
638 FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
639 filenames.push_back (filename);
644 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
646 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
648 filenames.erase (it);
655 ExportProfileManager::init_filenames (XMLNodeList nodes)
659 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
660 FilenamePtr filename = handler->add_filename();
661 filename->set_state (**it);
662 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
665 if (filenames.empty()) {
666 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
667 filenames.push_back (filename);
674 boost::shared_ptr<ExportProfileManager::Warnings>
675 ExportProfileManager::get_warnings ()
677 boost::shared_ptr<Warnings> warnings (new Warnings ());
679 ChannelConfigStatePtr channel_config_state = channel_configs.front();
680 TimespanStatePtr timespan_state = timespans.front();
682 /*** Check "global" config ***/
684 TimespanListPtr timespans = timespan_state->timespans;
685 ChannelConfigPtr channel_config = channel_config_state->config;
687 /* Check Timespans are not empty */
689 if (timespans->empty()) {
690 warnings->errors.push_back (_("No timespan has been selected!"));
693 /* Check channel config ports */
695 if (!channel_config->all_channels_have_ports ()) {
696 warnings->warnings.push_back (_("Some channels are empty"));
699 /*** Check files ***/
701 FormatStateList::const_iterator format_it;
702 FilenameStateList::const_iterator filename_it;
703 for (format_it = formats.begin(), filename_it = filenames.begin();
704 format_it != formats.end() && filename_it != filenames.end();
705 ++format_it, ++filename_it) {
706 check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
713 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
714 TimespanStatePtr timespan_state,
715 ChannelConfigStatePtr channel_config_state,
716 FormatStatePtr format_state,
717 FilenameStatePtr filename_state)
719 TimespanListPtr timespans = timespan_state->timespans;
720 ChannelConfigPtr channel_config = channel_config_state->config;
721 FormatPtr format = format_state->format;
722 FilenamePtr filename = filename_state->filename;
724 /* Check format and maximum channel count */
725 if (!format || !format->type()) {
726 warnings->errors.push_back (_("No format selected!"));
727 } else if (!channel_config->get_n_chans()) {
728 warnings->errors.push_back (_("All channels are empty!"));
729 } else if (!ExportFileFactory::check (format, channel_config->get_n_chans())) {
730 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
731 } else if (format->channel_limit() < channel_config->get_n_chans()) {
732 warnings->errors.push_back
733 (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
734 format->format_name(),
735 format->channel_limit(),
736 channel_config->get_n_chans()));
739 if (!warnings->errors.empty()) { return; }
741 /* Check filenames */
743 // filename->include_timespan = (timespans->size() > 1); Disabled for now...
745 for (std::list<TimespanPtr>::iterator timespan_it = timespans->begin(); timespan_it != timespans->end(); ++timespan_it) {
746 filename->set_timespan (*timespan_it);
748 if (channel_config->get_split()) {
749 filename->include_channel = true;
751 for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
752 filename->set_channel (chan);
754 Glib::ustring path = filename->get_path (format);
756 if (sys::exists (sys::path (path))) {
757 warnings->conflicting_filenames.push_back (path);
762 Glib::ustring path = filename->get_path (format);
764 if (sys::exists (sys::path (path))) {
765 warnings->conflicting_filenames.push_back (path);
771 }; // namespace ARDOUR