2 Copyright (C) 2008 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <glibmm/miscutils.h>
24 #include <glibmm/fileutils.h>
26 #include "pbd/xml++.h"
27 #include "pbd/convert.h"
28 #include "pbd/enumwriter.h"
30 #include "ardour/libardour_visibility.h"
31 #include "ardour/session.h"
32 #include "ardour/session_directory.h"
33 #include "ardour/export_filename.h"
34 #include "ardour/export_format_specification.h"
35 #include "ardour/export_channel_configuration.h"
36 #include "ardour/export_timespan.h"
37 #include "ardour/utils.h"
48 ExportFilename::ExportFilename (Session & session) :
49 include_label (false),
50 include_session (false),
51 include_revision (false),
52 include_channel_config (false),
53 include_format_name (false),
54 include_channel (false),
55 include_timespan (true), // Include timespan name always
67 time_struct = localtime (&rawtime);
69 folder = session.session_directory().export_path();
71 XMLNode * extra_node = session.extra_xml ("ExportFilename");
72 /* Legacy sessions used Session instant.xml for this */
74 session.instant_xml ("ExportFilename");
78 set_state (*extra_node);
83 ExportFilename::get_state ()
85 XMLNode * node = new XMLNode ("ExportFilename");
88 FieldPair dir = analyse_folder();
89 child = node->add_child ("Folder");
90 child->add_property ("relative", dir.first ? "true" : "false");
91 child->add_property ("path", dir.second);
93 add_field (node, "label", include_label, label);
94 add_field (node, "session", include_session);
95 add_field (node, "revision", include_revision);
96 add_field (node, "time", include_time, enum_2_string (time_format));
97 add_field (node, "date", include_date, enum_2_string (date_format));
99 XMLNode * extra_node = new XMLNode ("ExportRevision");
100 extra_node->add_property ("revision", to_string (revision, std::dec));
101 session.add_extra_xml (*extra_node);
107 ExportFilename::set_state (const XMLNode & node)
113 child = node.child ("Folder");
114 if (!child) { return -1; }
118 if ((prop = child->property ("relative"))) {
119 if (string_is_affirmative (prop->value())) {
120 folder = session.session_directory().root_path();
124 if ((prop = child->property ("path"))) {
126 tmp = Glib::build_filename (folder, prop->value());
127 if (!Glib::file_test (tmp, Glib::FILE_TEST_EXISTS)) {
128 warning << string_compose (_("Existing export folder for this session (%1) does not exist - ignored"), tmp) << endmsg;
134 if (folder.empty()) {
135 folder = session.session_directory().export_path();
138 pair = get_field (node, "label");
139 include_label = pair.first;
142 pair = get_field (node, "session");
143 include_session = pair.first;
145 pair = get_field (node, "revision");
146 include_revision = pair.first;
148 pair = get_field (node, "time");
149 include_time = pair.first;
150 time_format = (TimeFormat) string_2_enum (pair.second, time_format);
152 pair = get_field (node, "date");
153 include_date = pair.first;
154 date_format = (DateFormat) string_2_enum (pair.second, date_format);
156 XMLNode * extra_node = session.extra_xml ("ExportRevision");
157 /* Legacy sessions used Session instant.xml for this */
159 extra_node = session.instant_xml ("ExportRevision");
162 if (extra_node && (prop = extra_node->property ("revision"))) {
163 revision = atoi (prop->value());
170 ExportFilename::get_path (ExportFormatSpecPtr format) const
173 bool filename_empty = true;
175 if (include_session) {
176 path += filename_empty ? "" : "_";
177 path += session.name();
178 filename_empty = false;
182 path += filename_empty ? "" : "_";
184 filename_empty = false;
187 if (include_revision) {
188 path += filename_empty ? "" : "_";
190 path += to_string (revision, std::dec);
191 filename_empty = false;
194 if (include_timespan && timespan) {
195 path += filename_empty ? "" : "_";
196 path += timespan->name();
197 filename_empty = false;
200 if (include_channel_config && channel_config) {
201 path += filename_empty ? "" : "_";
202 path += channel_config->name();
203 filename_empty = false;
206 if (include_channel) {
207 path += filename_empty ? "" : "_";
209 path += to_string (channel, std::dec);
210 filename_empty = false;
214 path += filename_empty ? "" : "_";
215 path += get_date_format_str (date_format);
216 filename_empty = false;
220 path += filename_empty ? "" : "_";
221 path += get_time_format_str (time_format);
222 filename_empty = false;
225 if (include_format_name) {
226 path += filename_empty ? "" : "_";
227 path += format->name();
228 filename_empty = false;
232 path += format->extension ();
234 path = legalize_for_universal_path (path);
236 return Glib::build_filename (folder, path);
240 ExportFilename::get_time_format_str (TimeFormat format) const
247 return get_formatted_time ("%H%M");
250 return get_formatted_time ("%H.%M");
253 return _("Invalid time format");
258 ExportFilename::get_date_format_str (DateFormat format) const
265 return get_formatted_time ("%Y%m%d");
268 return get_formatted_time ("%Y-%m-%d");
271 return get_formatted_time ("%y%m%d");
274 return get_formatted_time ("%y-%m-%d");
277 return _("Invalid date format");
282 ExportFilename::set_time_format (TimeFormat format)
284 time_format = format;
286 if (format == T_None) {
287 include_time = false;
294 ExportFilename::set_date_format (DateFormat format)
296 date_format = format;
298 if (format == D_None) {
299 include_date = false;
306 ExportFilename::set_label (string value)
309 include_label = !value.compare ("");
313 ExportFilename::set_folder (string path)
315 // TODO check folder existence
321 ExportFilename::get_formatted_time (string const & format) const
324 strftime (buffer, 80, format.c_str(), time_struct);
326 string return_value (buffer);
331 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
333 XMLNode * child = node->add_child ("Field");
336 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
340 child->add_property ("name", name);
341 child->add_property ("enabled", enabled ? "true" : "false");
342 if (!value.empty()) {
343 child->add_property ("value", value);
347 ExportFilename::FieldPair
348 ExportFilename::get_field (XMLNode const & node, string const & name)
353 XMLNodeList children = node.children();
355 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
356 XMLProperty * prop = (*it)->property ("name");
357 if (prop && !prop->value().compare (name)) {
359 prop = (*it)->property ("enabled");
360 if (prop && !prop->value().compare ("true")) {
366 prop = (*it)->property ("value");
368 pair.second = prop->value();
378 ExportFilename::FieldPair
379 ExportFilename::analyse_folder ()
383 string session_dir = session.session_directory().root_path();
384 string::size_type session_dir_len = session_dir.length();
386 string folder_beginning = folder.substr (0, session_dir_len);
388 if (!folder_beginning.compare (session_dir)) {
390 pair.second = folder.substr (session_dir_len);
393 pair.second = folder;
399 } // namespace ARDOUR