merge (w/fix) with master
[ardour.git] / libs / ardour / file_source.cc
index 2f7ad2caa870b9f2739ae0c451619849d87a5135..e06b3d624ef09adadc3b7f3131be8074d9fbe4bf 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"
@@ -51,12 +51,12 @@ using namespace ARDOUR;
 using namespace PBD;
 using namespace Glib;
 
-PBD::Signal3<int,std::string,std::string,std::vector<std::string> > FileSource::AmbiguousFileName;
+PBD::Signal2<int,std::string,std::vector<std::string> > FileSource::AmbiguousFileName;
 
 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)
@@ -111,22 +111,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;
@@ -238,19 +240,15 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist
        isnew = false;
 
         if (!Glib::path_is_absolute (path)) {
-                vector<string> dirs;
                 vector<string> hits;
                 string fullpath;
+               std::vector<std::string> dirs = s.source_search_path (type);
 
-                string search_path = s.source_search_path (type);
-
-                if (search_path.length() == 0) {
+                if (dirs.size() == 0) {
                         error << _("FileSource: search path not set") << endmsg;
                         goto out;
                 }
 
-                split (search_path, dirs, ':');
-
                 hits.clear ();
 
                 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
@@ -294,7 +292,7 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist
 
                        /* more than one match: ask the user */
 
-                        int which = FileSource::AmbiguousFileName (path, search_path, de_duped_hits).get_value_or (-1);
+                        int which = FileSource::AmbiguousFileName (path, de_duped_hits).get_value_or (-1);
 
                         if (which < 0) {
                                 goto out;
@@ -308,8 +306,7 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist
 
                         if (must_exist) {
                                 error << string_compose(
-                                        _("Filesource: cannot find required file (%1): while searching %2"),
-                                        path, search_path) << endmsg;
+                                        _("Filesource: cannot find required file (%1)"), path) << endmsg;
                                 goto out;
                         } else {
                                 isnew = true;
@@ -355,8 +352,6 @@ bool
 FileSource::find_2X (Session& s, DataType type, const string& path, bool must_exist,
                      bool& isnew, uint16_t& chan, string& found_path)
 {
-       string search_path = s.source_search_path (type);
-
        string pathstr = path;
        string::size_type pos;
        bool ret = false;
@@ -367,18 +362,17 @@ FileSource::find_2X (Session& s, DataType type, const string& path, bool must_ex
 
                /* non-absolute pathname: find pathstr in search path */
 
-               vector<string> dirs;
+               vector<string> dirs = s.source_search_path (type);
+
                int cnt;
                string fullpath;
                string keeppath;
 
-               if (search_path.length() == 0) {
+               if (dirs.size() == 0) {
                        error << _("FileSource: search path not set") << endmsg;
                        goto out;
                }
 
-               split (search_path, dirs, ':');
-
                cnt = 0;
 
                for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
@@ -435,16 +429,15 @@ FileSource::find_2X (Session& s, DataType type, const string& path, bool must_ex
                if (cnt > 1) {
 
                        error << string_compose (
-                                       _("FileSource: \"%1\" is ambigous when searching %2\n\t"),
-                                       pathstr, search_path) << endmsg;
+                                       _("FileSource: \"%1\" is ambigous when searching\n\t"), pathstr) << endmsg;
                        goto out;
 
                } else if (cnt == 0) {
 
                        if (must_exist) {
                                error << string_compose(
-                                               _("Filesource: cannot find required file (%1): while searching %2"),
-                                               pathstr, search_path) << endmsg;
+                                               _("Filesource: cannot find required file (%1): while searching")
+                                               , pathstr) << endmsg;
                                goto out;
                        } else {
                                isnew = true;
@@ -494,13 +487,14 @@ FileSource::find_2X (Session& s, DataType type, const string& path, bool must_ex
                                goto out;
                        }
 
+#ifndef PLATFORM_WINDOWS
                        if (errno != ENOENT) {
                                error << string_compose(
                                                _("Filesource: cannot check for existing file (%1): %2"),
                                                path, strerror (errno)) << endmsg;
                                goto out;
                        }
-
+#endif
                        /* a new file */
                        isnew = true;
                        ret = true;
@@ -519,7 +513,7 @@ out:
 int
 FileSource::set_source_name (const string& newname, bool destructive)
 {
-       Glib::Mutex::Lock lm (_lock);
+       Glib::Threads::Mutex::Lock lm (_lock);
        string oldpath = _path;
        string newpath = _session.change_source_path_by_name (oldpath, _name, newname, destructive);
 
@@ -554,6 +548,15 @@ FileSource::mark_immutable ()
        }
 }
 
+void
+FileSource::mark_immutable_except_write ()
+{
+       /* destructive sources stay writable, and their other flags don't change.  */
+       if (!(_flags & Destructive)) {
+               _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
+       }
+}
+
 void
 FileSource::mark_nonremovable ()
 {