fix minor problem with recent cherry-picking from cairocanvas
[ardour.git] / libs / ardour / file_source.cc
index 2f7ad2caa870b9f2739ae0c451619849d87a5135..de2783a1acc5cb908074b182f49f28ee220cf1cb 100644 (file)
@@ -36,7 +36,7 @@
 
 #include <glibmm/miscutils.h>
 #include <glibmm/fileutils.h>
-#include <glibmm/thread.h>
+#include <glibmm/threads.h>
 
 #include "ardour/data_type.h"
 #include "ardour/file_source.h"
@@ -55,15 +55,13 @@ PBD::Signal3<int,std::string,std::string,std::vector<std::string> > FileSource::
 
 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(true)
+       , _path (path)
+       , _file_is_new (!origin.empty()) // origin empty => new file VS. origin !empty => new file
        , _channel (0)
         , _origin (origin)
         , _open (false)
 {
        set_within_session_from_path (path);
-
-        prevent_deletion ();
 }
 
 FileSource::FileSource (Session& session, const XMLNode& node, bool /*must_exist*/)
@@ -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;
 }
@@ -111,22 +114,24 @@ FileSource::init (const string& pathstr, bool must_exist)
 {
        _timeline_position = 0;
 
-        if (Stateful::loading_state_version < 3000) {
-                if (!find_2X (_session, _type, pathstr, must_exist, _file_is_new, _channel, _path)) {
-                        throw MissingSource (pathstr, _type);
-                }
-        } else {
-                if (!find (_session, _type, pathstr, must_exist, _file_is_new, _channel, _path)) {
-                        throw MissingSource (pathstr, _type);
-                }
-        }
+       if (Stateful::loading_state_version < 3000) {
+               if (!find_2X (_session, _type, pathstr, must_exist, _file_is_new, _channel, _path)) {
+                       throw MissingSource (pathstr, _type);
+               }
+       } else {
+               if (!find (_session, _type, pathstr, must_exist, _file_is_new, _channel, _path)) {
+                       throw MissingSource (pathstr, _type);
+               }
+       }
 
        set_within_session_from_path (_path);
-       
+
         _name = Glib::path_get_basename (_path);
 
-       if (_file_is_new && must_exist) {
-               return -1;
+       if (must_exist) {
+               if (!Glib::file_test (_path, Glib::FILE_TEST_EXISTS)) {
+                       throw MissingSource (pathstr, _type);
+               }
        }
 
        return 0;
@@ -516,41 +521,21 @@ out:
        return ret;
 }
 
-int
-FileSource::set_source_name (const string& newname, bool destructive)
+void
+FileSource::mark_immutable ()
 {
-       Glib::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;
+       /* destructive sources stay writable, and their other flags don't change.  */
+       if (!(_flags & Destructive)) {
+               _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
        }
-
-        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 ()
+FileSource::mark_immutable_except_write ()
 {
        /* destructive sources stay writable, and their other flags don't change.  */
        if (!(_flags & Destructive)) {
-               _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
+               _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
        }
 }
 
@@ -578,3 +563,21 @@ 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;
+}
+