Save templates as directories with plugin state, if
authorCarl Hetherington <carl@carlh.net>
Sun, 11 Dec 2011 20:38:42 +0000 (20:38 +0000)
committerCarl Hetherington <carl@carlh.net>
Sun, 11 Dec 2011 20:38:42 +0000 (20:38 +0000)
there is any, and copy that state to sessions created
from those templates.  Should fix #4525.  Breaks
existing session templates, sorry; they can be fixed by
moving the .template file into a new directory with the
name of the template (minus the .template).

git-svn-id: svn://localhost/ardour2/branches/3.0@10982 d708f5d6-7413-0410-9779-e7cbd77b26cf

libs/ardour/ardour/session.h
libs/ardour/ardour/template_utils.h
libs/ardour/session_state.cc
libs/ardour/template_utils.cc
libs/pbd/filesystem.cc
libs/pbd/pbd/filesystem.h
templates/wscript

index 19935c5440f88849132b8fbfd3a54d91c7e2941e..734709be6aeda7dccf29bb392d13e39e65ade6f3 100644 (file)
@@ -390,9 +390,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        void remove_pending_capture_state ();
        int rename (const std::string&);
 
-       static int rename_template (std::string old_name, std::string new_name);
-       static int delete_template (std::string name);
-
        PBD::Signal1<void,std::string> StateSaved;
        PBD::Signal0<void> StateReady;
 
index fefb4a7c47f77368286e3646c01adbfb658a7de9..ec9fdaac8f17b069da687c80637ac68efe1e2dfd 100644 (file)
@@ -22,6 +22,8 @@ namespace ARDOUR {
        void find_route_templates (std::vector<TemplateInfo>& template_names);
        void find_session_templates (std::vector<TemplateInfo>& template_names);
 
+       std::string session_template_dir_to_file (std::string const &);
+
 } // namespace ARDOUR
 
 #endif
index 7e139b2b18de6c5230a9c5b4122cc8b95d286c7c..7d6f44b7861d2566c1974b48c7e4cc76ca5ad7f0 100644 (file)
@@ -502,9 +502,11 @@ Session::ensure_subdirs ()
        return 0;
 }
 
-/** Caller must not hold process lock */
+/** @param session_template directory containing session template, or empty.
+ *  Caller must not hold process lock.
+ */
 int
-Session::create (const string& mix_template, BusProfile* bus_profile)
+Session::create (const string& session_template, BusProfile* bus_profile)
 {
        if (g_mkdir_with_parents (_path.c_str(), 0755) < 0) {
                error << string_compose(_("Session: cannot create session folder \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
@@ -517,8 +519,8 @@ Session::create (const string& mix_template, BusProfile* bus_profile)
 
        _writable = exists_and_writable (sys::path (_path));
 
-       if (!mix_template.empty()) {
-               std::string in_path = mix_template;
+       if (!session_template.empty()) {
+               std::string in_path = session_template_dir_to_file (session_template);
 
                ifstream in(in_path.c_str());
 
@@ -532,16 +534,22 @@ Session::create (const string& mix_template, BusProfile* bus_profile)
                        if (out) {
                                out << in.rdbuf();
                                 _is_new = false;
+
+                               /* Copy plugin state files from template to new session */
+                               sys::path template_plugins = session_template;
+                               template_plugins /= X_("plugins");
+                               sys::copy_files (template_plugins, plugins_dir ());
+                               
                                return 0;
 
                        } else {
-                               error << string_compose (_("Could not open %1 for writing mix template"), out_path)
+                               error << string_compose (_("Could not open %1 for writing session template"), out_path)
                                        << endmsg;
                                return -1;
                        }
 
                } else {
-                       error << string_compose (_("Could not open mix template %1 for reading"), in_path)
+                       error << string_compose (_("Could not open session template %1 for reading"), in_path)
                                << endmsg;
                        return -1;
                }
@@ -2048,59 +2056,36 @@ Session::save_template (string template_name)
 
        tree.set_root (&get_template());
 
-       sys::path template_file_path(user_template_dir);
-       template_file_path /= template_name + template_suffix;
-
-       if (sys::exists (template_file_path))
+       sys::path template_dir_path(user_template_dir);
+       
+       /* directory to put the template in */
+       template_dir_path /= template_name;
+       if (sys::exists (template_dir_path))
        {
                warning << string_compose(_("Template \"%1\" already exists - new version not created"),
-                               template_file_path.to_string()) << endmsg;
+                               template_dir_path.to_string()) << endmsg;
                return -1;
        }
+       
+       sys::create_directories (template_dir_path);
+
+       /* file to write */
+       sys::path template_file_path = template_dir_path;
+       template_file_path /= template_name + template_suffix;
 
        if (!tree.write (template_file_path.to_string())) {
                error << _("template not saved") << endmsg;
                return -1;
        }
 
-       return 0;
-}
-
-int
-Session::rename_template (string old_name, string new_name)
-{
-       sys::path old_path (user_template_directory());
-       old_path /= old_name + template_suffix;
-
-       sys::path new_path(user_template_directory());
-       new_path /= new_name + template_suffix;
-
-       if (sys::exists (new_path)) {
-               warning << string_compose(_("Template \"%1\" already exists - template not renamed"),
-                                         new_path.to_string()) << endmsg;
-               return -1;
-       }
-
-       try {
-               sys::rename (old_path, new_path);
-               return 0;
-       } catch (...) {
-               return -1;
-       }
-}
+       /* copy plugin state directory */
 
-int
-Session::delete_template (string name)
-{
-       sys::path path = user_template_directory();
-       path /= name + template_suffix;
+       sys::path template_plugin_state_path = template_dir_path;
+       template_plugin_state_path /= X_("plugins");
+       sys::create_directories (template_plugin_state_path);
+       sys::copy_files (plugins_dir(), template_plugin_state_path);
 
-       try {
-               sys::remove (path);
-               return 0;
-       } catch (...) {
-               return -1;
-       }
+       return 0;
 }
 
 void
index aa0a583af4d982771658c92786720ad4108e2448..825a24d3e10a39406414899186f9ecc9a81dbd36 100644 (file)
@@ -1,6 +1,8 @@
 #include <algorithm>
 #include <cstring>
 
+#include <glibmm.h>
+
 #include "pbd/filesystem.h"
 #include "pbd/basename.h"
 #include "pbd/pathscanner.h"
@@ -66,11 +68,23 @@ user_route_template_directory ()
 static bool
 template_filter (const string &str, void */*arg*/)
 {
-       cerr << "Checking into " << str << " using " << template_suffix << endl;
-       return (str.length() > strlen(template_suffix) &&
-               str.find (template_suffix) == (str.length() - strlen (template_suffix)));
+       if (!Glib::file_test (str, Glib::FILE_TEST_IS_DIR)) {
+               return false;
+       }
+       
+       return true;
+}
+
+string
+session_template_dir_to_file (string const & dir)
+{
+       sys::path dir_path = dir;
+       sys::path file_path = dir;
+       file_path /= dir_path.leaf() + template_suffix;
+       return file_path.to_string ();
 }
 
+
 void
 find_session_templates (vector<TemplateInfo>& template_names)
 {
@@ -79,7 +93,7 @@ find_session_templates (vector<TemplateInfo>& template_names)
        SearchPath spath (system_template_directory());
        spath += user_template_directory ();
 
-       templates = scanner (spath.to_string(), template_filter, 0, false, true);
+       templates = scanner (spath.to_string(), template_filter, 0, true, true);
 
        if (!templates) {
                cerr << "Found nothing along " << spath.to_string() << endl;
@@ -89,18 +103,18 @@ find_session_templates (vector<TemplateInfo>& template_names)
        cerr << "Found " << templates->size() << " along " << spath.to_string() << endl;
 
        for (vector<string*>::iterator i = templates->begin(); i != templates->end(); ++i) {
-               string fullpath = *(*i);
+               string file = session_template_dir_to_file (**i);
 
                XMLTree tree;
 
-               if (!tree.read (fullpath.c_str())) {
+               if (!tree.read (file.c_str())) {
                        continue;
                }
 
                TemplateInfo rti;
 
-               rti.name = basename_nosuffix (fullpath);
-               rti.path = fullpath;
+               rti.name = basename_nosuffix (**i);
+               rti.path = **i;
 
                template_names.push_back (rti);
        }
index 661e21cbd5074bf971fb5e5b12c9225ede2912c0..a4bf78180261428e5e5b5fd344394fbee17ee7fc 100644 (file)
@@ -32,6 +32,7 @@
 #include "pbd/filesystem.h"
 #include "pbd/error.h"
 #include "pbd/compose.h"
+#include "pbd/pathscanner.h"
 
 #include "i18n.h"
 
@@ -198,6 +199,27 @@ copy_file(const path & from_path, const path & to_path)
        }
 }
 
+static
+bool accept_all_files (string const &, void *)
+{
+       return true;
+}
+       
+void
+copy_files(const path & from_path, const path & to_dir)
+{
+       PathScanner scanner;
+       vector<string*>* files = scanner (from_path.to_string(), accept_all_files, 0, true, false);
+       for (vector<string*>::iterator i = files->begin(); i != files->end(); ++i) {
+               sys::path from = from_path;
+               from /= **i;
+               sys::path to = to_dir;
+               to /= **i;
+
+               copy_file (from, to);
+       }
+}
+
 string
 basename (const path & p)
 {
index a20efaf61320606df66fceb39c16b2c78c381b88..e8073adf0ed7ae59be8e6d2e317ab6648dc7b38d 100644 (file)
@@ -178,6 +178,12 @@ void rename (const path& from_path, const path& to_path);
  */
 void copy_file(const path & from_path, const path & to_path);
 
+/**
+ * Attempt to copy all regular files from from_path to a new directory.
+ * This method does not recurse.
+ */
+void copy_files(const path & from_path, const path & to_dir);
+
 /**
  * @return The substring of the filename component of the path, starting
  * at the beginning of the filename up to but not including the last dot.
index 7d01a73bb8b0ad19f577c3657ed65bc5872a9d0c..3f00b785ae7ab2c020a9dacdbca8b5ae48982943 100644 (file)
@@ -22,9 +22,11 @@ def build(bld):
     for t in templates:
         obj = bld(features = 'subst')
         obj.source = [ t ]
-        obj.target = [ os.path.basename(t.srcpath()).replace('.in', '') ]
+        dir_name = os.path.basename(t.srcpath()).replace('.template.in', '')
+        file_name = os.path.basename(t.srcpath()).replace('.in', '')
+        obj.target = [ os.path.join(dir_name, file_name) ]
         obj.dict = subst_dict
-        obj.install_path = os.path.join(bld.env['DATADIR'], 'ardour3', 'templates')
+        obj.install_path = os.path.join(bld.env['DATADIR'], 'ardour3', os.path.join('templates', dir_name))
 
 def options(opt):
     pass