Fix importing to a fixed-point format with resampling
[ardour.git] / libs / ardour / file_source.cc
index 109539ce2d7539fe242861a0d189b6e55e5f701b..37af5b8c38233a85726acb9fd60e6074b81283e0 100644 (file)
 
 #include <sys/time.h>
 #include <sys/stat.h>
-#include <stdio.h> // for rename(), sigh
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
 
+#include <glib.h>
+#include <glib/gstdio.h>
+
 #include "pbd/convert.h"
 #include "pbd/basename.h"
 #include "pbd/stl_delete.h"
@@ -44,7 +46,7 @@
 #include "ardour/source.h"
 #include "ardour/utils.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
@@ -56,10 +58,10 @@ 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)
+       , _gain (1.f)
 {
        set_within_session_from_path (path);
 }
@@ -67,6 +69,8 @@ FileSource::FileSource (Session& session, DataType type, const string& path, con
 FileSource::FileSource (Session& session, const XMLNode& node, bool /*must_exist*/)
        : Source (session, node)
        , _file_is_new (false)
+       , _channel (0)
+       , _gain (1.f)
 {
        /* this setting of _path is temporary - we expect derived classes
           to call ::init() which will actually locate the file
@@ -140,17 +144,15 @@ FileSource::init (const string& pathstr, bool must_exist)
 int
 FileSource::set_state (const XMLNode& node, int /*version*/)
 {
-       const XMLProperty* prop;
-
-       if ((prop = node.property (X_("channel"))) != 0) {
-               _channel = atoi (prop->value());
-       } else {
+       if (!node.get_property (X_("channel"), _channel)) {
                _channel = 0;
        }
 
-        if ((prop = node.property (X_("origin"))) != 0) {
-                _origin = prop->value();
-        }
+       node.get_property (X_("origin"), _origin);
+
+       if (!node.get_property (X_("gain"), _gain)) {
+               _gain = 1.f;
+       }
 
        return 0;
 }
@@ -205,16 +207,16 @@ FileSource::move_to_trash (const string& trash_dir_name)
                }
        }
 
-       if (::rename (_path.c_str(), newpath.c_str()) != 0) {
+       if (::g_rename (_path.c_str(), newpath.c_str()) != 0) {
                PBD::error << string_compose (
                                _("cannot rename file source from %1 to %2 (%3)"),
-                               _path, newpath, strerror (errno)) << endmsg;
+                               _path, newpath, g_strerror (errno)) << endmsg;
                return -1;
        }
 
        if (move_dependents_to_trash() != 0) {
                /* try to back out */
-               rename (newpath.c_str(), _path.c_str());
+               ::g_rename (newpath.c_str(), _path.c_str());
                return -1;
        }
 
@@ -228,10 +230,10 @@ FileSource::move_to_trash (const string& trash_dir_name)
 
 /** Find the actual source file based on \a filename.
  *
- * 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.
+ * If the source is within the session tree, \a path should be a simple filename (no slashes).
+ * If the source is external, \a path 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.
+ * \return true if the file was found.
  */
 bool
 FileSource::find (Session& s, DataType type, const string& path, bool must_exist,
@@ -252,8 +254,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);
@@ -276,7 +276,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 */
@@ -308,8 +308,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;
@@ -317,19 +318,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;
@@ -337,7 +339,6 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist
         }
 
         found_path = keeppath;
-
         ret = true;
 
   out:
@@ -432,7 +433,7 @@ 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\n\t"), pathstr) << endmsg;
+                                       _("FileSource: \"%1\" is ambiguous when searching\n\t"), pathstr) << endmsg;
                        goto out;
 
                } else if (cnt == 0) {
@@ -485,7 +486,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): %2"),
-                                               path, strerror (errno)) << endmsg;
+                                               path, g_strerror (errno)) << endmsg;
                                goto out;
                        }
 
@@ -493,7 +494,7 @@ FileSource::find_2X (Session& s, DataType type, const string& path, bool must_ex
                        if (errno != ENOENT) {
                                error << string_compose(
                                                _("Filesource: cannot check for existing file (%1): %2"),
-                                               path, strerror (errno)) << endmsg;
+                                               path, g_strerror (errno)) << endmsg;
                                goto out;
                        }
 #endif
@@ -518,6 +519,7 @@ FileSource::mark_immutable ()
        /* destructive sources stay writable, and their other flags don't change.  */
        if (!(_flags & Destructive)) {
                _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
+               close();
        }
 }
 
@@ -545,7 +547,23 @@ 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
+FileSource::replace_file (const std::string& newpath)
+{
+       close ();
+       _path = newpath;
+       _name = Glib::path_get_basename (newpath);
 }
 
 void
@@ -560,7 +578,7 @@ FileSource::is_stub () const
        if (!empty()) {
                return false;
        }
-       
+
        if (!removable()) {
                return false;
        }
@@ -571,4 +589,31 @@ FileSource::is_stub () const
 
        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 (::g_rename (oldpath.c_str(), newpath.c_str()) != 0) {
+                       error << string_compose (_("cannot rename file %1 to %2 (%3)"), oldpath, newpath, g_strerror(errno)) << endmsg;
+                       return -1;
+               }
+       }
+
+       _name = Glib::path_get_basename (newpath);
+       _path = newpath;
+
+       return 0;
+}
+
+