Stop using static initialisation so that dcpomatic::write() can be called more than...
[dcpomatic.git] / src / lib / film.cc
index 7aeb6ab770d3eecddbffa8b2def6f2e9eed834e9..4084ca59c1700ec2d5ecae874b8eb5b0732b62b5 100644 (file)
@@ -29,7 +29,6 @@
 #include "audio_content.h"
 #include "audio_processor.h"
 #include "change_signaller.h"
-#include "check_content_change_job.h"
 #include "cinema.h"
 #include "compose.hpp"
 #include "config.h"
@@ -482,12 +481,12 @@ Film::write_metadata (boost::filesystem::path path) const
 
 /** Write state to our `metadata' file */
 void
-Film::write_metadata () const
+Film::write_metadata ()
 {
        DCPOMATIC_ASSERT (directory());
        boost::filesystem::create_directories (directory().get());
        metadata()->write_to_file_formatted(file(metadata_file).string());
-       _dirty = false;
+       set_dirty (false);
 }
 
 /** Write a template from this film */
@@ -535,6 +534,8 @@ Film::read_metadata (optional<boost::filesystem::path> path)
                }
        }
 
+       _last_written_by = f.optional_string_child("LastWrittenBy");
+
        _name = f.string_child ("Name");
        if (_state_version >= 9) {
                _use_isdcf_name = f.bool_child ("UseISDCFName");
@@ -705,7 +706,7 @@ Film::read_metadata (optional<boost::filesystem::path> path)
                set_backtrace_file (file ("backtrace.txt"));
        }
 
-       _dirty = false;
+       set_dirty (false);
        return notes;
 }
 
@@ -1063,7 +1064,7 @@ void
 Film::set_directory (boost::filesystem::path d)
 {
        _directory = d;
-       _dirty = true;
+       set_dirty (true);
 }
 
 void
@@ -1206,7 +1207,7 @@ void
 Film::signal_change (ChangeType type, Property p)
 {
        if (type == ChangeType::DONE) {
-               _dirty = true;
+               set_dirty (true);
 
                if (p == Property::CONTENT) {
                        if (!_user_explicit_video_frame_rate) {
@@ -1486,7 +1487,7 @@ Film::playlist_content_change (ChangeType type, weak_ptr<Content> c, int p, bool
                ContentChange (type, c, p, frequent);
        }
 
-       _dirty = true;
+       set_dirty (true);
 }
 
 void
@@ -1505,7 +1506,7 @@ Film::playlist_change (ChangeType type)
                check_settings_consistency ();
        }
 
-       _dirty = true;
+       set_dirty (true);
 }
 
 /** Check for (and if necessary fix) impossible settings combinations, like
@@ -1718,9 +1719,9 @@ Film::should_be_enough_disk_space (double& required, double& available, bool& ca
        boost::filesystem::path test = internal_video_asset_dir() / "test";
        boost::filesystem::path test2 = internal_video_asset_dir() / "test2";
        can_hard_link = true;
-       auto f = fopen_boost (test, "w");
+       dcp::File f(test, "w");
        if (f) {
-               fclose (f);
+               f.close();
                boost::system::error_code ec;
                boost::filesystem::create_hard_link (test, test2, ec);
                if (ec) {
@@ -2071,34 +2072,15 @@ Film::info_file_handle (DCPTimePeriod period, bool read) const
        return std::make_shared<InfoFileHandle>(_info_file_mutex, info_file(period), read);
 }
 
-InfoFileHandle::InfoFileHandle (boost::mutex& mutex, boost::filesystem::path file, bool read)
+InfoFileHandle::InfoFileHandle (boost::mutex& mutex, boost::filesystem::path path, bool read)
        : _lock (mutex)
-       , _file (file)
+       , _file (path, read ? "rb" : (boost::filesystem::exists(path) ? "r+b" : "wb"))
 {
-       if (read) {
-               _handle = fopen_boost (file, "rb");
-               if (!_handle) {
-                       throw OpenFileError (file, errno, OpenFileError::READ);
-               }
-       } else {
-               auto const exists = boost::filesystem::exists (file);
-               if (exists) {
-                       _handle = fopen_boost (file, "r+b");
-               } else {
-                       _handle = fopen_boost (file, "wb");
-               }
-
-               if (!_handle) {
-                       throw OpenFileError (file, errno, exists ? OpenFileError::READ_WRITE : OpenFileError::WRITE);
-               }
+       if (!_file) {
+               throw OpenFileError (path, errno, read ? OpenFileError::READ : (boost::filesystem::exists(path) ? OpenFileError::READ_WRITE : OpenFileError::WRITE));
        }
 }
 
-InfoFileHandle::~InfoFileHandle ()
-{
-       fclose (_handle);
-}
-
 
 /** Add FFOC and LFOC markers to a list if they are not already there */
 void
@@ -2176,3 +2158,52 @@ Film::set_sign_language_video_language (optional<dcp::LanguageTag> lang)
        _sign_language_video_language = lang;
 }
 
+
+void
+Film::set_dirty (bool dirty)
+{
+       auto const changed = dirty != _dirty;
+       _dirty = dirty;
+       if (changed) {
+               emit (boost::bind(boost::ref(DirtyChange), _dirty));
+       }
+}
+
+
+/** @return true if the metadata was (probably) last written by a version earlier
+ *  than the given one; false if it definitely was not.
+ */
+bool
+Film::last_written_by_earlier_than(int major, int minor, int micro) const
+{
+       if (!_last_written_by) {
+               return true;
+       }
+
+       vector<string> parts;
+       boost::split(parts, *_last_written_by, boost::is_any_of("."));
+
+       if (parts.size() != 3) {
+               /* Not sure what's going on, so let's say it was written by an old version */
+               return true;
+       }
+
+       if (boost::ends_with(parts[2], "pre")) {
+               parts[2] = parts[2].substr(0, parts[2].length() - 3);
+       }
+
+       int our_major = dcp::raw_convert<int>(parts[0]);
+       int our_minor = dcp::raw_convert<int>(parts[1]);
+       int our_micro = dcp::raw_convert<int>(parts[2]);
+
+       if (our_major != major) {
+               return our_major < major;
+       }
+
+       if (our_minor != minor) {
+               return our_minor < minor;
+       }
+
+       return our_micro < micro;
+}
+