Fix save/load of MIDI automation state. Fixes #3354.
[ardour.git] / libs / ardour / file_source.cc
index f5b06ee046bfeb516f716e43034de40e853d309d..512d37a10b5745195131fa62c6c0a3bce80d3237 100644 (file)
@@ -39,6 +39,7 @@
 #include <glibmm/thread.h>
 
 #include "ardour/file_source.h"
+#include "ardour/directory_names.h"
 #include "ardour/session.h"
 #include "ardour/session_directory.h"
 #include "ardour/source_factory.h"
@@ -51,34 +52,41 @@ using namespace ARDOUR;
 using namespace PBD;
 using namespace Glib;
 
-static const std::string PATH_SEP = "/"; // I don't do windows
-
 map<DataType, ustring> FileSource::search_paths;
 
-FileSource::FileSource (Session& session, DataType type,
-               const ustring& path, bool embedded, Source::Flag flag)
+FileSource::FileSource (Session& session, DataType type, const ustring& path, Source::Flag flag)
        : Source(session, type, path, flag)
        , _path(path)
        , _file_is_new(true)
        , _channel (0)
-       , _is_embedded(embedded)
 {
+       set_within_session_from_path (path);
 }
 
 FileSource::FileSource (Session& session, const XMLNode& node, bool /*must_exist*/)
        : Source(session, node)
        , _file_is_new (false)
 {
+       /* this setting of _path is temporary - we expect derived classes
+          to call ::init() which will actually locate the file
+          and reset _path and _within_session correctly.
+       */
+
        _path = _name;
-       _is_embedded = (_path.find(PATH_SEP) != string::npos);
+       _within_session = true;
 }
 
 bool
 FileSource::removable () const
 {
-       return (_flags & Removable)
-               && (   (_flags & RemoveAtDestroy)
-                       || ((_flags & RemovableIfEmpty) && length(timeline_position()) == 0));
+        bool r = (_path.find (stub_dir_name) != string::npos) ||
+                ((_flags & Removable)
+                 && ((_flags & RemoveAtDestroy) || 
+                     ((_flags & RemovableIfEmpty) && empty() == 0)));
+
+        cerr << "is " << _path << " removable ? " << r << endl;
+
+        return r;
 }
 
 int
@@ -90,9 +98,9 @@ FileSource::init (const ustring& pathstr, bool must_exist)
                throw MissingSource ();
        }
 
-       /* XXX is this necessary? or even wise? */
+       set_within_session_from_path (pathstr);
 
-       if (_is_embedded) {
+       if (_within_session) {
                _name = Glib::path_get_basename (_name);
        }
 
@@ -104,7 +112,7 @@ FileSource::init (const ustring& pathstr, bool must_exist)
 }
 
 int
-FileSource::set_state (const XMLNode& node)
+FileSource::set_state (const XMLNode& node, int /*version*/)
 {
        const XMLProperty* prop;
 
@@ -114,8 +122,6 @@ FileSource::set_state (const XMLNode& node)
                _channel = 0;
        }
 
-       _is_embedded = (_name.find(PATH_SEP) == string::npos);
-
        return 0;
 }
 
@@ -130,12 +136,7 @@ FileSource::mark_take (const ustring& id)
 int
 FileSource::move_to_trash (const ustring& trash_dir_name)
 {
-       if (is_embedded()) {
-               cerr << "tried to move an embedded region to trash" << endl;
-               return -1;
-       }
-
-       if (!writable()) {
+       if (!within_session() || !writable()) {
                return -1;
        }
 
@@ -143,15 +144,16 @@ FileSource::move_to_trash (const ustring& trash_dir_name)
           trash_dir_name directory on whichever filesystem it was already on
        */
 
-       ustring newpath;
-       newpath = Glib::path_get_dirname (_path);
-       newpath = Glib::path_get_dirname (newpath);
+        vector<string> v;
+       v.push_back (Glib::path_get_dirname (Glib::path_get_dirname (_path)));
+        v.push_back (trash_dir_name);
+       v.push_back (Glib::path_get_basename (_path));
 
-       newpath += string(PATH_SEP) + trash_dir_name + PATH_SEP;
-       newpath += Glib::path_get_basename (_path);
+       string newpath = Glib::build_filename (v);
 
        /* the new path already exists, try versioning */
-       if (access (newpath.c_str(), F_OK) == 0) {
+
+       if (Glib::file_test (newpath.c_str(), Glib::FILE_TEST_EXISTS)) {
                char buf[PATH_MAX+1];
                int version = 1;
                ustring newpath_v;
@@ -196,7 +198,7 @@ FileSource::move_to_trash (const ustring& trash_dir_name)
 
 /** Find the actual source file based on \a filename.
  *
- * If the source is embedded, \a filename should be a simple filename (no slashes).
+ * If the source is within the session tree, \a filename should be a simple filename (no slashes).
  * If the source is external, \a filename should be a full path.
  * In either case, found_path is set to the complete absolute path of the source file.
  * \return true iff the file was found.
@@ -381,7 +383,7 @@ FileSource::set_source_name (const ustring& newname, bool destructive)
 {
        Glib::Mutex::Lock lm (_lock);
        ustring oldpath = _path;
-       ustring newpath = Session::change_source_path_by_name (oldpath, _name, newname, destructive);
+       ustring newpath = _session.change_source_path_by_name (oldpath, _name, newname, destructive);
 
        if (newpath.empty()) {
                error << string_compose (_("programming error: %1"), "cannot generate a changed file path") << endmsg;
@@ -389,15 +391,15 @@ FileSource::set_source_name (const ustring& newname, bool destructive)
        }
 
        // Test whether newpath exists, if yes notify the user but continue.
-       if (access(newpath.c_str(),F_OK) == 0) {
-               error << _("Programming error! Ardour tried to rename a file over another file! It's safe to continue working, but please report this to the developers.") << endmsg;
-               return -1;
-       }
-
-       if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
-               error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
+       if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
+               error << string_compose (_("Programming error! %1 tried to rename a file over another file! It's safe to continue working, but please report this to the developers."), PROGRAM_NAME) << endmsg;
                return -1;
        }
+        
+        if (::rename (oldpath.c_str(), newpath.c_str()) != 0) {
+                error << string_compose (_("cannot rename file %1 to %2 (%3)"), oldpath, newpath, strerror(errno)) << endmsg;
+                return -1;
+        }
 
        _name = Glib::path_get_basename (newpath);
        _path = newpath;
@@ -420,3 +422,53 @@ FileSource::mark_immutable ()
        }
 }
 
+void
+FileSource::mark_nonremovable ()
+{
+        _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
+}
+
+void
+FileSource::set_within_session_from_path (const std::string& path)
+{
+       _within_session = _session.path_is_within_session (path);
+}
+
+int
+FileSource::unstubify ()
+{
+        string::size_type pos = _path.find (stub_dir_name);
+
+        if (pos == string::npos || (_flags & Destructive)) {
+                return 0;
+        }
+
+        vector<string> v;
+
+        v.push_back (Glib::path_get_dirname (Glib::path_get_dirname (_path)));
+       v.push_back (Glib::path_get_basename(_path));
+        
+        string newpath = Glib::build_filename (v);
+
+        if (::rename (_path.c_str(), newpath.c_str()) != 0) {
+                error << string_compose (_("rename from %1 to %2 failed: %3)"), _path, newpath, strerror (errno)) << endmsg;
+                return -1;
+        }
+
+        set_path (newpath);
+
+        return 0;
+}
+
+void
+FileSource::set_path (const std::string& newpath)
+{
+        _path = newpath;
+}
+
+void 
+FileSource::inc_use_count ()
+{
+        Source::inc_use_count ();
+}