Cope with loading a film containing a missing DCP which itself contains subtitles.
[dcpomatic.git] / src / lib / state.cc
index dd48516f43fc05a384089c543a8b60a12896ea5f..a8230385d42e957653f2b32ad2a622a7096ff3db 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2018-2020 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
-#include "state.h"
+
 #include "cross.h"
-#include <glib.h>
+#include "state.h"
+#include "util.h"
+#include <dcp/filesystem.h>
+
 
 using std::string;
+using boost::optional;
+
 
 boost::optional<boost::filesystem::path> State::override_path;
 
-/** @param file State filename
- *  @return Full path to write @file to */
+
+/* List of config versions to look for in descending order of preference;
+ * i.e. look at the first one, and if that doesn't exist, try the second, etc.
+ */
+static std::vector<std::string> config_versions = { "2.16" };
+
+
+static
 boost::filesystem::path
-State::path (string file, bool create_directories)
+config_path_or_override (optional<string> version)
 {
-       boost::filesystem::path p;
-       if (override_path) {
-               p = *override_path;
-       } else {
-               p = config_path ();
+       if (State::override_path) {
+               auto p = *State::override_path;
+               if (version) {
+                       p /= *version;
+               }
+               return p;
        }
-       boost::system::error_code ec;
-       if (create_directories) {
-               boost::filesystem::create_directories (p, ec);
+
+       return config_path (version);
+}
+
+
+/** @param file State filename
+ *  @return Full path to read @file from.
+ */
+boost::filesystem::path
+State::read_path (string file)
+{
+       for (auto i: config_versions) {
+               auto full = config_path_or_override(i) / file;
+               if (dcp::filesystem::exists(full)) {
+                       return full;
+               }
        }
+
+       return config_path_or_override({}) / file;
+}
+
+
+/** @param file State filename
+ *  @return Full path to write @file to.
+ */
+boost::filesystem::path
+State::write_path (string file)
+{
+       auto p = config_path_or_override(config_versions.front());
+       dcp::filesystem::create_directories(p);
        p /= file;
        return p;
 }
+