fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / file_source.cc
index e06b3d624ef09adadc3b7f3131be8074d9fbe4bf..c05a0a4bb92826bebc876b62778c510a7c5e6c9d 100644 (file)
@@ -44,7 +44,7 @@
 #include "ardour/source.h"
 #include "ardour/utils.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
@@ -56,19 +56,17 @@ PBD::Signal2<int,std::string,std::vector<std::string> > FileSource::AmbiguousFil
 FileSource::FileSource (Session& session, DataType type, const string& path, const string& origin, Source::Flag flag)
        : Source(session, type, path, flag)
        , _path (path)
-       , _file_is_new (!origin.empty()) // origin empty => new file VS. origin !empty => new file
+       , _file_is_new (!origin.empty()) // if origin is left unspecified (empty string) then file must exist
        , _channel (0)
-        , _origin (origin)
-        , _open (false)
+       , _origin (origin)
 {
        set_within_session_from_path (path);
-
-        prevent_deletion ();
 }
 
 FileSource::FileSource (Session& session, const XMLNode& node, bool /*must_exist*/)
        : Source (session, node)
        , _file_is_new (false)
+       , _channel (0)
 {
        /* this setting of _path is temporary - we expect derived classes
           to call ::init() which will actually locate the file
@@ -77,23 +75,28 @@ FileSource::FileSource (Session& session, const XMLNode& node, bool /*must_exist
 
        _path = _name;
        _within_session = true;
+}
 
-        prevent_deletion ();
+FileSource::~FileSource()
+{
 }
 
 void
-FileSource::prevent_deletion ()
+FileSource::existence_check ()
 {
-        /* if this file already exists, it cannot be removed, ever
-         */
-
         if (Glib::file_test (_path, Glib::FILE_TEST_EXISTS)) {
-                if (!(_flags & Destructive)) {
-                        mark_immutable ();
-                } else {
-                        _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
-                }
-        }
+               prevent_deletion ();
+       }
+}
+
+void
+FileSource::prevent_deletion ()
+{
+       if (!(_flags & Destructive)) {
+               mark_immutable ();
+       } else {
+               _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
+       }
 }
 
 bool
@@ -101,7 +104,7 @@ FileSource::removable () const
 {
         bool r = ((_flags & Removable)
                   && ((_flags & RemoveAtDestroy) ||
-                      ((_flags & RemovableIfEmpty) && empty() == 0)));
+                      ((_flags & RemovableIfEmpty) && empty())));
 
         return r;
 }
@@ -137,7 +140,7 @@ FileSource::init (const string& pathstr, bool must_exist)
 int
 FileSource::set_state (const XMLNode& node, int /*version*/)
 {
-       const XMLProperty* prop;
+       XMLProperty const * prop;
 
        if ((prop = node.property (X_("channel"))) != 0) {
                _channel = atoi (prop->value());
@@ -145,9 +148,9 @@ FileSource::set_state (const XMLNode& node, int /*version*/)
                _channel = 0;
        }
 
-        if ((prop = node.property (X_("origin"))) != 0) {
-                _origin = prop->value();
-        }
+       if ((prop = node.property (X_("origin"))) != 0) {
+               _origin = prop->value();
+       }
 
        return 0;
 }
@@ -211,7 +214,7 @@ FileSource::move_to_trash (const string& trash_dir_name)
 
        if (move_dependents_to_trash() != 0) {
                /* try to back out */
-               rename (newpath.c_str(), _path.c_str());
+               ::rename (newpath.c_str(), _path.c_str());
                return -1;
        }
 
@@ -249,8 +252,6 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist
                         goto out;
                 }
 
-                hits.clear ();
-
                 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
 
                         fullpath = Glib::build_filename (*i, path);
@@ -273,7 +274,7 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist
 
                        vector<string>::iterator j = i;
                        ++j;
-                       
+
                        while (j != hits.end()) {
                                if (PBD::equivalent_paths (*i, *j)) {
                                        /* *i and *j are the same file; break out of the loop early */
@@ -305,8 +306,9 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist
                        /* no match: error */
 
                         if (must_exist) {
-                                error << string_compose(
-                                        _("Filesource: cannot find required file (%1)"), path) << endmsg;
+                               /* do not generate an error here, leave that to
+                                  whoever deals with the false return value.
+                               */
                                 goto out;
                         } else {
                                 isnew = true;
@@ -314,19 +316,20 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist
                 } else {
 
                        /* only one match: happy days */
-                       
+
                        keeppath = de_duped_hits[0];
                }
-                                                 
-        } else {
+
+       } else {
                 keeppath = path;
         }
 
         /* Current find() is unable to parse relative path names to yet non-existant
            sources. QuickFix(tm)
         */
-        if (keeppath == "") {
-                if (must_exist) {
+
+       if (keeppath.empty()) {
+               if (must_exist) {
                         error << "FileSource::find(), keeppath = \"\", but the file must exist" << endl;
                 } else {
                         keeppath = path;
@@ -334,7 +337,6 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist
         }
 
         found_path = keeppath;
-
         ret = true;
 
   out:
@@ -436,8 +438,7 @@ FileSource::find_2X (Session& s, DataType type, const string& path, bool must_ex
 
                        if (must_exist) {
                                error << string_compose(
-                                               _("Filesource: cannot find required file (%1): while searching")
-                                               , pathstr) << endmsg;
+                                               _("Filesource: cannot find required file (%1)"), pathstr) << endmsg;
                                goto out;
                        } else {
                                isnew = true;
@@ -510,35 +511,6 @@ out:
        return ret;
 }
 
-int
-FileSource::set_source_name (const string& newname, bool destructive)
-{
-       Glib::Threads::Mutex::Lock lm (_lock);
-       string oldpath = _path;
-       string 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;
-               return -1;
-       }
-
-       // Test whether newpath exists, if yes notify the user but continue.
-       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;
-
-       return 0;
-}
-
 void
 FileSource::mark_immutable ()
 {
@@ -572,7 +544,14 @@ FileSource::set_within_session_from_path (const std::string& path)
 void
 FileSource::set_path (const std::string& newpath)
 {
+       close ();
         _path = newpath;
+       set_within_session_from_path (newpath);
+       if (_within_session) {
+               _origin = Glib::path_get_basename (newpath);
+       } else {
+               _origin = newpath;
+       }
 }
 
 void
@@ -581,3 +560,48 @@ FileSource::inc_use_count ()
         Source::inc_use_count ();
 }
 
+bool
+FileSource::is_stub () const
+{
+       if (!empty()) {
+               return false;
+       }
+
+       if (!removable()) {
+               return false;
+       }
+
+       if (Glib::file_test (_path, Glib::FILE_TEST_EXISTS)) {
+               return false;
+       }
+
+       return true;
+}
+
+int
+FileSource::rename (const string& newpath)
+{
+       Glib::Threads::Mutex::Lock lm (_lock);
+       string oldpath = _path;
+
+       // Test whether newpath exists, if yes notify the user but continue.
+       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 (Glib::file_test (oldpath.c_str(), Glib::FILE_TEST_EXISTS)) {
+               /* rename only needed if file exists on disk */
+               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;
+
+       return 0;
+}
+
+