cont'd work on plugin-state templates - #6709
authorRobin Gareus <robin@gareus.org>
Sat, 19 Dec 2015 13:46:15 +0000 (14:46 +0100)
committerRobin Gareus <robin@gareus.org>
Sat, 19 Dec 2015 13:46:15 +0000 (14:46 +0100)
Refactor and consolidate code and re-use it for session-templates.
This avoids recursive copying of the plugin-dir()

libs/ardour/ardour/route.h
libs/ardour/ardour/session.h
libs/ardour/lv2_plugin.cc
libs/ardour/route.cc
libs/ardour/session_state.cc

index b63cbb91475d2015f62cf4c0e750a506a2d8b521..0ef063b29cf0d27339e0524037bc203cea523d98 100644 (file)
@@ -714,6 +714,8 @@ class LIBARDOUR_API Route : public SessionObject, public Automatable, public Rou
        void setup_invisible_processors ();
        void unpan ();
 
+       void set_plugin_state_dir (boost::weak_ptr<Processor>, const std::string&);
+
        boost::shared_ptr<CapturingProcessor> _capturing_processor;
 
        /** A handy class to keep processor state while we attempt a reconfiguration
index 6a73e8566369b7bb95b333e6a9f64199d07eef86..a5b93af04125051180db870b00e17f5b2b51ce5b 100644 (file)
@@ -1839,6 +1839,8 @@ class LIBARDOUR_API Session : public PBD::StatefulDestructible, public PBD::Scop
 
        static int get_session_info_from_path (XMLTree& state_tree, const std::string& xmlpath);
        static const uint32_t session_end_shift;
+
+       std::string _template_state_dir;
 };
 
 
index 3753bd80661453ee8e95f47f66654b12175837a5..9c52bb255e2ae1dc3f1d7bf08879a98790e16655 100644 (file)
@@ -44,7 +44,6 @@
 #include "ardour/debug.h"
 #include "ardour/lv2_plugin.h"
 #include "ardour/session.h"
-#include "ardour/template_utils.h"
 #include "ardour/tempo.h"
 #include "ardour/types.h"
 #include "ardour/utils.h"
@@ -1744,10 +1743,7 @@ LV2Plugin::set_state(const XMLNode& node, int version)
        }
 
        if ((prop = node.property("template-dir")) != 0) {
-               // portable templates, strip absolute path
-               set_state_dir (Glib::build_filename (
-                                       ARDOUR::user_route_template_directory (),
-                                       Glib::path_get_basename (prop->value ())));
+               set_state_dir (prop->value ());
        }
 
        _state_version = 0;
index e24919a3b3198beef8c4b58affa5ef5833949c7b..3027019bff15216fede7c1e3f3b7b26c1c10bb7d 100644 (file)
@@ -2253,6 +2253,11 @@ Route::get_template()
 XMLNode&
 Route::state(bool full_state)
 {
+       if (!_session._template_state_dir.empty()) {
+               assert (!full_state); // only for templates
+               foreach_processor (sigc::bind (sigc::mem_fun (*this, &Route::set_plugin_state_dir), _session._template_state_dir));
+       }
+
        XMLNode *node = new XMLNode("Route");
        ProcessorList::iterator i;
        char buf[32];
@@ -2347,6 +2352,10 @@ Route::state(bool full_state)
                }
        }
 
+       if (!_session._template_state_dir.empty()) {
+               foreach_processor (sigc::bind (sigc::mem_fun (*this, &Route::set_plugin_state_dir), ""));
+       }
+
        return *node;
 }
 
@@ -4154,42 +4163,30 @@ Route::shift (framepos_t pos, framecnt_t frames)
        }
 }
 
+void
+Route::set_plugin_state_dir (boost::weak_ptr<Processor> p, const std::string& d)
+{
+       boost::shared_ptr<Processor> processor (p.lock ());
+       boost::shared_ptr<PluginInsert> pi  = boost::dynamic_pointer_cast<PluginInsert> (processor);
+       if (!pi) {
+               return;
+       }
+       pi->set_state_dir (d);
+}
 
 int
 Route::save_as_template (const string& path, const string& name)
 {
-       {
-               // would be nice to use foreach_processor()
-               Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
-               std::string state_dir = path.substr (0, path.find_last_of ('.')); // strip template_suffix
-               for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
-                       boost::shared_ptr<PluginInsert> pi  = boost::dynamic_pointer_cast<PluginInsert> (*i);
-                       if (pi) {
-                               pi->set_state_dir (state_dir);
-                       }
-               }
-       }
+       std::string state_dir = path.substr (0, path.find_last_of ('.')); // strip template_suffix
+       PBD::Unwinder<std::string> uw (_session._template_state_dir, state_dir);
 
        XMLNode& node (state (false));
 
-       {
-               Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
-               for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
-                       boost::shared_ptr<PluginInsert> pi  = boost::dynamic_pointer_cast<PluginInsert> (*i);
-                       if (pi) {
-                               pi->set_state_dir ();
-                       }
-               }
-       }
-
        XMLTree tree;
 
        IO::set_name_in_state (*node.children().front(), name);
 
        tree.set_root (&node);
-       // TODO: special case LV2 plugin state
-       // copy of serialize it. Alternatively:
-       // create a plugin-preset (which can be loaded separately)
 
        /* return zero on success, non-zero otherwise */
        return !tree.write (path.c_str());
index c440b77895bf68ae4e61e9913d4629a8e5181be7..bde7208867ee9401dba352cefc6136e454a90268 100644 (file)
@@ -73,6 +73,7 @@
 #include "pbd/stacktrace.h"
 #include "pbd/convert.h"
 #include "pbd/localtime_r.h"
+#include "pbd/unwind.h"
 
 #include "ardour/amp.h"
 #include "ardour/async_midi_port.h"
@@ -2161,25 +2162,16 @@ Session::save_template (string template_name, bool replace_existing)
 
        XMLTree tree;
 
-       tree.set_root (&get_template());
+       {
+               PBD::Unwinder<std::string> uw (_template_state_dir, template_dir_path);
+               tree.set_root (&get_template());
+       }
+
        if (!tree.write (template_file_path)) {
                error << _("template not saved") << endmsg;
                return -1;
        }
 
-       if (!ARDOUR::Profile->get_trx()) {
-               /* copy plugin state directory */
-
-               std::string template_plugin_state_path (Glib::build_filename (template_dir_path, X_("plugins")));
-
-               if (g_mkdir_with_parents (template_plugin_state_path.c_str(), 0755) != 0) {
-                       error << string_compose(_("Could not create directory for Session template plugin state\"%1\" (%2)"),
-                                                                       template_plugin_state_path, g_strerror (errno)) << endmsg;
-                       return -1;
-               }
-               copy_files (plugins_dir(), template_plugin_state_path);
-       }
-
        store_recent_templates (template_file_path);
 
        return 0;