Add 'midi_scene_change' sources to ardour.vcproj
[ardour.git] / libs / ardour / smf_source.cc
index dfaa51481d352a4e1fb32b407a76260640fd3d32..812e06c27b08d30252630d1bb776fc4178567a8c 100644 (file)
@@ -32,6 +32,7 @@
 
 #include <glib/gstdio.h>
 #include <glibmm/miscutils.h>
+#include <glibmm/fileutils.h>
 
 #include "evoral/Control.hpp"
 
@@ -62,9 +63,12 @@ SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
 {
        /* note that origin remains empty */
 
-       if (init(_path, false)) {
+       if (init (_path, false)) {
                throw failed_constructor ();
        }
+        assert (!Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
+       existence_check ();
 
        /* file is not opened until write */
 
@@ -72,9 +76,10 @@ SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
                return;
        }
 
-       if (open(_path)) {
+       if (open (_path)) {
                throw failed_constructor ();
        }
+
        _open = true;
 }
 
@@ -92,10 +97,13 @@ SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
                throw failed_constructor ();
        }
 
-       if (init(_path, true)) {
+       if (init (_path, true)) {
                throw failed_constructor ();
        }
 
+        assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
+       existence_check ();
+
        if (open(_path)) {
                throw failed_constructor ();
        }
@@ -464,7 +472,14 @@ SMFSource::safe_midi_file_extension (const string& file)
        const int nmatches = 2;
        regmatch_t matches[nmatches];
        
-       if (compile && regcomp (&compiled_pattern, "[mM][iI][dD][iI]?$", REG_EXTENDED)) {
+       if (Glib::file_test (file, Glib::FILE_TEST_EXISTS)) {
+               if (!Glib::file_test (file, Glib::FILE_TEST_IS_REGULAR)) {
+                       /* exists but is not a regular file */
+                       return false;
+               }
+       }
+
+       if (compile && regcomp (&compiled_pattern, "\\.[mM][iI][dD][iI]?$", REG_EXTENDED)) {
                return false;
        } else {
                compile = false;
@@ -650,3 +665,44 @@ SMFSource::ensure_disk_file ()
        }
 }
 
+void
+SMFSource::prevent_deletion ()
+{
+       /* Unlike the audio case, the MIDI file remains mutable (because we can
+          edit MIDI data)
+       */
+  
+       _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
+}
+
+int
+SMFSource::rename (const string& newname)
+{
+       Glib::Threads::Mutex::Lock lm (_lock);
+       string oldpath = _path;
+       string newpath = _session.new_source_path_from_name (DataType::MIDI, newname);
+
+       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 (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;
+}