fix invalid mapping detection
[ardour.git] / libs / ardour / export_filename.cc
index d5309bd535d46a611a70b35e0f69b4933e693ded..201abb934be93a08344db3a5d86a410a43907dec 100644 (file)
 */
 
 #include <string>
-#include "ardour/export_filename.h"
+
+#include <glibmm/miscutils.h>
+#include <glibmm/fileutils.h>
 
 #include "pbd/xml++.h"
 #include "pbd/convert.h"
 #include "pbd/enumwriter.h"
+#include "pbd/localtime_r.h"
 
+#include "ardour/libardour_visibility.h"
 #include "ardour/session.h"
 #include "ardour/session_directory.h"
-#include "ardour/export_timespan.h"
+#include "ardour/export_filename.h"
 #include "ardour/export_format_specification.h"
 #include "ardour/export_channel_configuration.h"
-#include "ardour/export_failed.h"
+#include "ardour/export_timespan.h"
+#include "ardour/utils.h"
 
 #include "i18n.h"
 
@@ -46,22 +51,30 @@ ExportFilename::ExportFilename (Session & session) :
   include_session (false),
   include_revision (false),
   include_channel_config (false),
+  include_format_name (false),
   include_channel (false),
   include_timespan (true), // Include timespan name always
   include_time (false),
   include_date (false),
   session (session),
-  revision (1)
+  revision (1),
+  date_format (D_None),
+  time_format (T_None)
 {
        time_t rawtime;
        std::time (&rawtime);
-       time_struct = localtime (&rawtime);
+       localtime_r (&rawtime, &time_struct);
 
-       folder = session.session_directory().export_path().to_string();
+       folder = session.session_directory().export_path();
 
-       XMLNode * instant_node = session.instant_xml ("ExportFilename");
-       if (instant_node) {
-               set_state (*instant_node);
+       XMLNode * extra_node = session.extra_xml ("ExportFilename");
+       /* Legacy sessions used Session instant.xml for this */
+       if (!extra_node) {
+               session.instant_xml ("ExportFilename");
+       }
+
+       if (extra_node) {
+               set_state (*extra_node);
        }
 }
 
@@ -78,13 +91,14 @@ ExportFilename::get_state ()
 
        add_field (node, "label", include_label, label);
        add_field (node, "session", include_session);
+       add_field (node, "timespan", include_timespan);
        add_field (node, "revision", include_revision);
        add_field (node, "time", include_time, enum_2_string (time_format));
        add_field (node, "date", include_date, enum_2_string (date_format));
 
-       XMLNode * instant_node = new XMLNode ("ExportRevision");
-       instant_node->add_property ("revision", to_string (revision, std::dec));
-       session.add_instant_xml (*instant_node);
+       XMLNode * extra_node = new XMLNode ("ExportRevision");
+       extra_node->add_property ("revision", to_string (revision, std::dec));
+       session.add_extra_xml (*extra_node);
 
        return *node;
 }
@@ -102,15 +116,24 @@ ExportFilename::set_state (const XMLNode & node)
        folder = "";
 
        if ((prop = child->property ("relative"))) {
-               if (!prop->value().compare ("true")) {
-                       folder = session.session_directory().root_path().to_string();
+               if (string_is_affirmative (prop->value())) {
+                       folder = session.session_directory().root_path();
                }
        }
 
        if ((prop = child->property ("path"))) {
-               folder += prop->value();
+               std::string tmp;
+               tmp = Glib::build_filename (folder, prop->value());
+               if (!Glib::file_test (tmp, Glib::FILE_TEST_EXISTS)) {
+                       warning << string_compose (_("Existing export folder for this session (%1) does not exist - ignored"), tmp) << endmsg;
+               } else {
+                       folder = tmp;
+               }
        }
 
+       if (folder.empty()) {
+               folder = session.session_directory().export_path();
+       }
 
        pair = get_field (node, "label");
        include_label = pair.first;
@@ -119,6 +142,9 @@ ExportFilename::set_state (const XMLNode & node)
        pair = get_field (node, "session");
        include_session = pair.first;
 
+       pair = get_field (node, "timespan");
+       include_timespan = pair.first;
+
        pair = get_field (node, "revision");
        include_revision = pair.first;
 
@@ -130,8 +156,13 @@ ExportFilename::set_state (const XMLNode & node)
        include_date = pair.first;
        date_format = (DateFormat) string_2_enum (pair.second, date_format);
 
-       XMLNode * instant_node = session.instant_xml ("ExportRevision");
-       if (instant_node && (prop = instant_node->property ("revision"))) {
+       XMLNode * extra_node = session.extra_xml ("ExportRevision");
+       /* Legacy sessions used Session instant.xml for this */
+       if (!extra_node) {
+               extra_node = session.instant_xml ("ExportRevision");
+       }
+
+       if (extra_node && (prop = extra_node->property ("revision"))) {
                revision = atoi (prop->value());
        }
 
@@ -141,10 +172,20 @@ ExportFilename::set_state (const XMLNode & node)
 string
 ExportFilename::get_path (ExportFormatSpecPtr format) const
 {
-       string path = folder;
+       string path;
        bool filename_empty = true;
-
-       path += "/";
+       bool with_timespan = include_timespan;
+
+       if (!include_session
+                       && !include_label
+                       && !include_revision
+                       && !include_timespan
+                       && !include_channel_config
+                       && !include_channel
+                       && !include_date
+                       && !include_format_name) {
+               with_timespan = true;
+       }
 
        if (include_session) {
                path += filename_empty ? "" : "_";
@@ -165,7 +206,7 @@ ExportFilename::get_path (ExportFormatSpecPtr format) const
                filename_empty = false;
        }
 
-       if (include_timespan && timespan) {
+       if (with_timespan && timespan) {
                path += filename_empty ? "" : "_";
                path += timespan->name();
                filename_empty = false;
@@ -196,10 +237,22 @@ ExportFilename::get_path (ExportFormatSpecPtr format) const
                filename_empty = false;
        }
 
+       if (include_format_name) {
+               path += filename_empty ? "" : "_";
+               path += format->name();
+               filename_empty = false;
+       }
+
+       if (path.empty ()) {
+               path = "export";
+       }
+
        path += ".";
        path += format->extension ();
 
-       return path;
+       path = legalize_for_universal_path (path);
+
+       return Glib::build_filename (folder, path);
 }
 
 string
@@ -287,7 +340,7 @@ string
 ExportFilename::get_formatted_time (string const & format) const
 {
        char buffer [80];
-       strftime (buffer, 80, format.c_str(), time_struct);
+       strftime (buffer, 80, format.c_str(), &time_struct);
 
        string return_value (buffer);
        return return_value;
@@ -346,7 +399,7 @@ ExportFilename::analyse_folder ()
 {
        FieldPair pair;
 
-       string session_dir = session.session_directory().root_path().to_string();
+       string session_dir = session.session_directory().root_path();
        string::size_type session_dir_len = session_dir.length();
 
        string folder_beginning = folder.substr (0, session_dir_len);