Handle export presets from config dirs properly. Removed some debug output.
[ardour.git] / libs / ardour / export_profile_manager.cc
index e9edf5feef1042f04a044a4c91949f04e388f20c..600ce9e3ea9654bc636d37a2e30959ebf7c7b232 100644 (file)
@@ -30,6 +30,7 @@
 #include <pbd/convert.h>
 
 #include <ardour/export_failed.h>
+#include <ardour/export_file_io.h>
 #include <ardour/export_format_specification.h>
 #include <ardour/export_timespan.h>
 #include <ardour/export_channel_configuration.h>
@@ -52,25 +53,19 @@ ExportProfileManager::ExportProfileManager (Session & s) :
 
   session_range (new Location ()),
   ranges (new LocationList ()),
+  single_range_mode (false),
 
   format_list (new FormatList ())
 {
 
        /* Initialize path variables */
 
-       sys::path path;
-
        export_config_dir = user_config_directory();
        export_config_dir /= "export";
        search_path += export_config_dir;
        
-       path = ardour_search_path().to_string();
-       path /= "export";
-       search_path += path;
-       
-       path = system_config_search_path().to_string();
-       path /= "export";
-       search_path += path;
+       search_path += ardour_search_path().add_subdirectory_to_paths("export");
+       search_path += system_config_search_path().add_subdirectory_to_paths("export");;
        
        /* create export config directory if necessary */
 
@@ -92,6 +87,8 @@ ExportProfileManager::ExportProfileManager (Session & s) :
 
 ExportProfileManager::~ExportProfileManager ()
 {
+       if (single_range_mode) { return; }
+       
        XMLNode * instant_xml (new XMLNode ("ExportProfile"));
        serialize_profile (*instant_xml);
        session.add_instant_xml (*instant_xml, false);
@@ -164,8 +161,9 @@ ExportProfileManager::load_presets ()
 ExportProfileManager::PresetPtr
 ExportProfileManager::save_preset (string const & name)
 {
+       string filename = export_config_dir.to_string() + "/" + name + export_preset_suffix;
+
        if (!current_preset) {
-               string filename = export_config_dir.to_string() + "/" + name + export_preset_suffix;
                current_preset.reset (new ExportPreset (filename, session));
                preset_list.push_back (current_preset);
        }
@@ -180,7 +178,7 @@ ExportProfileManager::save_preset (string const & name)
        current_preset->set_global_state (*global_preset);
        current_preset->set_local_state (*local_preset);
        
-       current_preset->save();
+       current_preset->save (filename);
        
        return current_preset;
 }
@@ -211,12 +209,13 @@ void
 ExportProfileManager::load_preset_from_disk (PBD::sys::path const & path)
 {
        PresetPtr preset (new ExportPreset (path.to_string(), session));
-       preset_list.push_back (preset);
        
-       /* Handle id to filename mapping */
+       /* Handle id to filename mapping and don't add duplicates to list */
        
        FilePair pair (preset->id(), path);
-       preset_file_map.insert (pair);
+       if (preset_file_map.insert (pair).second) {
+               preset_list.push_back (preset);
+       }
 }
 
 bool
@@ -298,6 +297,20 @@ ExportProfileManager::set_selection_range (nframes_t start, nframes_t end)
        }
 }
 
+std::string
+ExportProfileManager::set_single_range (nframes_t start, nframes_t end, Glib::ustring name)
+{
+       single_range_mode = true;
+       
+       single_range.reset (new Location());
+       single_range->set_name (name);
+       single_range->set (start, end);
+       
+       update_ranges ();
+       
+       return single_range->id().to_s();
+}
+
 bool
 ExportProfileManager::init_timespans (XMLNodeList nodes)
 {
@@ -376,6 +389,11 @@ ExportProfileManager::serialize_timespan (TimespanStatePtr state)
 void
 ExportProfileManager::update_ranges () {
        ranges->clear();
+       
+       if (single_range_mode) {
+               ranges->push_back (single_range.get());
+               return;
+       }
 
        /* Session */
 
@@ -457,19 +475,31 @@ ExportProfileManager::save_format_to_disk (FormatPtr format)
        /* Check if format is on disk already */
        FileMap::iterator it;
        if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
-               /* Update data */
-               {
-                       XMLTree tree (it->second.to_string());
+               
+               /* Check if config is not in user config dir */
+               if (it->second.branch_path().to_string().compare (export_config_dir.to_string())) {
+               
+                       /* Write new file */
+               
+                       XMLTree tree (new_path.to_string());
                        tree.set_root (&format->get_state());
                        tree.write();
-               }
                
-               /* Rename if necessary */
+               } else {
+               
+                       /* Update file and rename if necessary */
                
-               if (new_name.compare (it->second.leaf())) {
-                       sys::rename (it->second, new_path);
+                       XMLTree tree (it->second.to_string());
+                       tree.set_root (&format->get_state());
+                       tree.write();
+                       
+                       if (new_name.compare (it->second.leaf())) {
+                               sys::rename (it->second, new_path);
+                       }
                }
                
+               it->second = new_path;
+               
        } else {
                /* Write new file */
                
@@ -589,12 +619,13 @@ ExportProfileManager::load_format_from_disk (PBD::sys::path const & path)
 {
        XMLTree const tree (path.to_string());
        FormatPtr format = handler->add_format (*tree.root());
-       format_list->push_back (format);
        
-       /* Handle id to filename mapping */
+       /* Handle id to filename mapping and don't add duplicates to list */
        
        FilePair pair (format->id(), path);
-       format_file_map.insert (pair);
+       if (format_file_map.insert (pair).second) {
+               format_list->push_back (format);
+       }
        
        FormatListChanged ();
 }
@@ -691,6 +722,8 @@ ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
        /* Check format and maximum channel count */
        if (!format || !format->type()) {
                warnings->errors.push_back (_("No format selected!"));
+       } else if (!ExportFileFactory::check (format, channel_config->get_n_chans())) {
+               warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
        } else if (format->channel_limit() < channel_config->get_n_chans()) {
                warnings->errors.push_back
                  (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),