ARD: proper cleanup
[ardour.git] / libs / ardour / export_filename.cc
index 51e32a4ca9aca2d48439ccad8d449c6d6a12d14a..f7fe22c3b48c5a37b72435f55f2f6ad5305033bf 100644 (file)
 
 */
 
-#include "ardour/export_filename.h"
+#include <string>
+
+#include <glibmm/miscutils.h>
+#include <glibmm/fileutils.h>
 
 #include "pbd/xml++.h"
 #include "pbd/convert.h"
 #include "pbd/enumwriter.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"
 
 using namespace PBD;
 using namespace Glib;
+using std::string;
 
 namespace ARDOUR
 {
@@ -44,6 +50,7 @@ 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),
@@ -55,7 +62,7 @@ ExportFilename::ExportFilename (Session & session) :
        std::time (&rawtime);
        time_struct = localtime (&rawtime);
 
-       folder = session.session_directory().export_path().to_string();
+       folder = session.session_directory().export_path();
 
        XMLNode * instant_node = session.instant_xml ("ExportFilename");
        if (instant_node) {
@@ -100,15 +107,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;
@@ -136,14 +152,12 @@ ExportFilename::set_state (const XMLNode & node)
        return 0;
 }
 
-ustring
-ExportFilename::get_path (FormatPtr format) const
+string
+ExportFilename::get_path (ExportFormatSpecPtr format) const
 {
-       ustring path = folder;
+       string path;
        bool filename_empty = true;
 
-       path += "/";
-
        if (include_session) {
                path += filename_empty ? "" : "_";
                path += session.name();
@@ -194,13 +208,21 @@ ExportFilename::get_path (FormatPtr format) const
                filename_empty = false;
        }
 
+       if (include_format_name) {
+               path += filename_empty ? "" : "_";
+               path += format->name();
+               filename_empty = false;
+       }
+
        path += ".";
        path += format->extension ();
 
-       return path;
+       path = legalize_for_universal_path (path);
+
+       return Glib::build_filename (folder, path);
 }
 
-ustring
+string
 ExportFilename::get_time_format_str (TimeFormat format) const
 {
        switch ( format ) {
@@ -218,7 +240,7 @@ ExportFilename::get_time_format_str (TimeFormat format) const
        }
 }
 
-ustring
+string
 ExportFilename::get_date_format_str (DateFormat format) const
 {
        switch (format) {
@@ -267,32 +289,32 @@ ExportFilename::set_date_format (DateFormat format)
 }
 
 void
-ExportFilename::set_label (ustring value)
+ExportFilename::set_label (string value)
 {
        label = value;
        include_label = !value.compare ("");
 }
 
 bool
-ExportFilename::set_folder (ustring path)
+ExportFilename::set_folder (string path)
 {
        // TODO check folder existence
        folder = path;
        return true;
 }
 
-ustring
-ExportFilename::get_formatted_time (ustring const & format) const
+string
+ExportFilename::get_formatted_time (string const & format) const
 {
        char buffer [80];
        strftime (buffer, 80, format.c_str(), time_struct);
 
-       ustring return_value (buffer);
+       string return_value (buffer);
        return return_value;
 }
 
 void
-ExportFilename::add_field (XMLNode * node, ustring const & name, bool enabled, ustring const & value)
+ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
 {
        XMLNode * child = node->add_child ("Field");
 
@@ -309,7 +331,7 @@ ExportFilename::add_field (XMLNode * node, ustring const & name, bool enabled, u
 }
 
 ExportFilename::FieldPair
-ExportFilename::get_field (XMLNode const & node, ustring const & name)
+ExportFilename::get_field (XMLNode const & node, string const & name)
 {
        FieldPair pair;
        pair.first = false;
@@ -344,10 +366,10 @@ ExportFilename::analyse_folder ()
 {
        FieldPair pair;
 
-       ustring session_dir = session.session_directory().root_path().to_string();
-       ustring::size_type session_dir_len = session_dir.length();
+       string session_dir = session.session_directory().root_path();
+       string::size_type session_dir_len = session_dir.length();
 
-       ustring folder_beginning = folder.substr (0, session_dir_len);
+       string folder_beginning = folder.substr (0, session_dir_len);
 
        if (!folder_beginning.compare (session_dir)) {
                pair.first = true;