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
63 time_struct = localtime (&rawtime);
65 folder = session.session_directory().export_path();
67 XMLNode * extra_node = session.extra_xml ("ExportFilename");
68 /* Legacy sessions used Session instant.xml for this */
70 session.instant_xml ("ExportFilename");
74 set_state (*extra_node);
79 ExportFilename::get_state ()
81 XMLNode * node = new XMLNode ("ExportFilename");
84 FieldPair dir = analyse_folder();
85 child = node->add_child ("Folder");
86 child->add_property ("relative", dir.first ? "true" : "false");
87 child->add_property ("path", dir.second);
89 add_field (node, "label", include_label, label);
90 add_field (node, "session", include_session);
91 add_field (node, "revision", include_revision);
92 add_field (node, "time", include_time, enum_2_string (time_format));
93 add_field (node, "date", include_date, enum_2_string (date_format));
95 XMLNode * extra_node = new XMLNode ("ExportRevision");
96 extra_node->add_property ("revision", to_string (revision, std::dec));
97 session.add_extra_xml (*extra_node);
103 ExportFilename::set_state (const XMLNode & node)
109 child = node.child ("Folder");
110 if (!child) { return -1; }
114 if ((prop = child->property ("relative"))) {
115 if (string_is_affirmative (prop->value())) {
116 folder = session.session_directory().root_path();
120 if ((prop = child->property ("path"))) {
122 tmp = Glib::build_filename (folder, prop->value());
123 if (!Glib::file_test (tmp, Glib::FILE_TEST_EXISTS)) {
124 warning << string_compose (_("Existing export folder for this session (%1) does not exist - ignored"), tmp) << endmsg;
130 if (folder.empty()) {
131 folder = session.session_directory().export_path();
134 pair = get_field (node, "label");
135 include_label = pair.first;
138 pair = get_field (node, "session");
139 include_session = pair.first;
141 pair = get_field (node, "revision");
142 include_revision = pair.first;
144 pair = get_field (node, "time");
145 include_time = pair.first;
146 time_format = (TimeFormat) string_2_enum (pair.second, time_format);
148 pair = get_field (node, "date");
149 include_date = pair.first;
150 date_format = (DateFormat) string_2_enum (pair.second, date_format);
152 XMLNode * extra_node = session.extra_xml ("ExportRevision");
153 /* Legacy sessions used Session instant.xml for this */
155 extra_node = session.instant_xml ("ExportRevision");
158 if (extra_node && (prop = extra_node->property ("revision"))) {
159 revision = atoi (prop->value());
166 ExportFilename::get_path (ExportFormatSpecPtr format) const
169 bool filename_empty = true;
171 if (include_session) {
172 path += filename_empty ? "" : "_";
173 path += session.name();
174 filename_empty = false;
178 path += filename_empty ? "" : "_";
180 filename_empty = false;
183 if (include_revision) {
184 path += filename_empty ? "" : "_";
186 path += to_string (revision, std::dec);
187 filename_empty = false;
190 if (include_timespan && timespan) {
191 path += filename_empty ? "" : "_";
192 path += timespan->name();
193 filename_empty = false;
196 if (include_channel_config && channel_config) {
197 path += filename_empty ? "" : "_";
198 path += channel_config->name();
199 filename_empty = false;
202 if (include_channel) {
203 path += filename_empty ? "" : "_";
205 path += to_string (channel, std::dec);
206 filename_empty = false;
210 path += filename_empty ? "" : "_";
211 path += get_date_format_str (date_format);
212 filename_empty = false;
216 path += filename_empty ? "" : "_";
217 path += get_time_format_str (time_format);
218 filename_empty = false;
221 if (include_format_name) {
222 path += filename_empty ? "" : "_";
223 path += format->name();
224 filename_empty = false;
228 path += format->extension ();
230 path = legalize_for_universal_path (path);
232 return Glib::build_filename (folder, path);
236 ExportFilename::get_time_format_str (TimeFormat format) const
243 return get_formatted_time ("%H%M");
246 return get_formatted_time ("%H.%M");
249 return _("Invalid time format");
254 ExportFilename::get_date_format_str (DateFormat format) const
261 return get_formatted_time ("%Y%m%d");
264 return get_formatted_time ("%Y-%m-%d");
267 return get_formatted_time ("%y%m%d");
270 return get_formatted_time ("%y-%m-%d");
273 return _("Invalid date format");
278 ExportFilename::set_time_format (TimeFormat format)
280 time_format = format;
282 if (format == T_None) {
283 include_time = false;
290 ExportFilename::set_date_format (DateFormat format)
292 date_format = format;
294 if (format == D_None) {
295 include_date = false;
302 ExportFilename::set_label (string value)
305 include_label = !value.compare ("");
309 ExportFilename::set_folder (string path)
311 // TODO check folder existence
317 ExportFilename::get_formatted_time (string const & format) const
320 strftime (buffer, 80, format.c_str(), time_struct);
322 string return_value (buffer);
327 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
329 XMLNode * child = node->add_child ("Field");
332 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
336 child->add_property ("name", name);
337 child->add_property ("enabled", enabled ? "true" : "false");
338 if (!value.empty()) {
339 child->add_property ("value", value);
343 ExportFilename::FieldPair
344 ExportFilename::get_field (XMLNode const & node, string const & name)
349 XMLNodeList children = node.children();
351 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
352 XMLProperty * prop = (*it)->property ("name");
353 if (prop && !prop->value().compare (name)) {
355 prop = (*it)->property ("enabled");
356 if (prop && !prop->value().compare ("true")) {
362 prop = (*it)->property ("value");
364 pair.second = prop->value();
374 ExportFilename::FieldPair
375 ExportFilename::analyse_folder ()
379 string session_dir = session.session_directory().root_path();
380 string::size_type session_dir_len = session_dir.length();
382 string folder_beginning = folder.substr (0, session_dir_len);
384 if (!folder_beginning.compare (session_dir)) {
386 pair.second = folder.substr (session_dir_len);
389 pair.second = folder;
395 } // namespace ARDOUR