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
65 time_struct = localtime (&rawtime);
67 folder = session.session_directory().export_path();
69 XMLNode * extra_node = session.extra_xml ("ExportFilename");
70 /* Legacy sessions used Session instant.xml for this */
72 session.instant_xml ("ExportFilename");
76 set_state (*extra_node);
81 ExportFilename::get_state ()
83 XMLNode * node = new XMLNode ("ExportFilename");
86 FieldPair dir = analyse_folder();
87 child = node->add_child ("Folder");
88 child->add_property ("relative", dir.first ? "true" : "false");
89 child->add_property ("path", dir.second);
91 add_field (node, "label", include_label, label);
92 add_field (node, "session", include_session);
93 add_field (node, "revision", include_revision);
94 add_field (node, "time", include_time, enum_2_string (time_format));
95 add_field (node, "date", include_date, enum_2_string (date_format));
97 XMLNode * extra_node = new XMLNode ("ExportRevision");
98 extra_node->add_property ("revision", to_string (revision, std::dec));
99 session.add_extra_xml (*extra_node);
105 ExportFilename::set_state (const XMLNode & node)
111 child = node.child ("Folder");
112 if (!child) { return -1; }
116 if ((prop = child->property ("relative"))) {
117 if (string_is_affirmative (prop->value())) {
118 folder = session.session_directory().root_path();
122 if ((prop = child->property ("path"))) {
124 tmp = Glib::build_filename (folder, prop->value());
125 if (!Glib::file_test (tmp, Glib::FILE_TEST_EXISTS)) {
126 warning << string_compose (_("Existing export folder for this session (%1) does not exist - ignored"), tmp) << endmsg;
132 if (folder.empty()) {
133 folder = session.session_directory().export_path();
136 pair = get_field (node, "label");
137 include_label = pair.first;
140 pair = get_field (node, "session");
141 include_session = pair.first;
143 pair = get_field (node, "revision");
144 include_revision = pair.first;
146 pair = get_field (node, "time");
147 include_time = pair.first;
148 time_format = (TimeFormat) string_2_enum (pair.second, time_format);
150 pair = get_field (node, "date");
151 include_date = pair.first;
152 date_format = (DateFormat) string_2_enum (pair.second, date_format);
154 XMLNode * extra_node = session.extra_xml ("ExportRevision");
155 /* Legacy sessions used Session instant.xml for this */
157 extra_node = session.instant_xml ("ExportRevision");
160 if (extra_node && (prop = extra_node->property ("revision"))) {
161 revision = atoi (prop->value());
168 ExportFilename::get_path (ExportFormatSpecPtr format) const
171 bool filename_empty = true;
173 if (include_session) {
174 path += filename_empty ? "" : "_";
175 path += session.name();
176 filename_empty = false;
180 path += filename_empty ? "" : "_";
182 filename_empty = false;
185 if (include_revision) {
186 path += filename_empty ? "" : "_";
188 path += to_string (revision, std::dec);
189 filename_empty = false;
192 if (include_timespan && timespan) {
193 path += filename_empty ? "" : "_";
194 path += timespan->name();
195 filename_empty = false;
198 if (include_channel_config && channel_config) {
199 path += filename_empty ? "" : "_";
200 path += channel_config->name();
201 filename_empty = false;
204 if (include_channel) {
205 path += filename_empty ? "" : "_";
207 path += to_string (channel, std::dec);
208 filename_empty = false;
212 path += filename_empty ? "" : "_";
213 path += get_date_format_str (date_format);
214 filename_empty = false;
218 path += filename_empty ? "" : "_";
219 path += get_time_format_str (time_format);
220 filename_empty = false;
223 if (include_format_name) {
224 path += filename_empty ? "" : "_";
225 path += format->name();
226 filename_empty = false;
230 path += format->extension ();
232 path = legalize_for_universal_path (path);
234 return Glib::build_filename (folder, path);
238 ExportFilename::get_time_format_str (TimeFormat format) const
245 return get_formatted_time ("%H%M");
248 return get_formatted_time ("%H.%M");
251 return _("Invalid time format");
256 ExportFilename::get_date_format_str (DateFormat format) const
263 return get_formatted_time ("%Y%m%d");
266 return get_formatted_time ("%Y-%m-%d");
269 return get_formatted_time ("%y%m%d");
272 return get_formatted_time ("%y-%m-%d");
275 return _("Invalid date format");
280 ExportFilename::set_time_format (TimeFormat format)
282 time_format = format;
284 if (format == T_None) {
285 include_time = false;
292 ExportFilename::set_date_format (DateFormat format)
294 date_format = format;
296 if (format == D_None) {
297 include_date = false;
304 ExportFilename::set_label (string value)
307 include_label = !value.compare ("");
311 ExportFilename::set_folder (string path)
313 // TODO check folder existence
319 ExportFilename::get_formatted_time (string const & format) const
322 strftime (buffer, 80, format.c_str(), time_struct);
324 string return_value (buffer);
329 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
331 XMLNode * child = node->add_child ("Field");
334 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
338 child->add_property ("name", name);
339 child->add_property ("enabled", enabled ? "true" : "false");
340 if (!value.empty()) {
341 child->add_property ("value", value);
345 ExportFilename::FieldPair
346 ExportFilename::get_field (XMLNode const & node, string const & name)
351 XMLNodeList children = node.children();
353 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
354 XMLProperty * prop = (*it)->property ("name");
355 if (prop && !prop->value().compare (name)) {
357 prop = (*it)->property ("enabled");
358 if (prop && !prop->value().compare ("true")) {
364 prop = (*it)->property ("value");
366 pair.second = prop->value();
376 ExportFilename::FieldPair
377 ExportFilename::analyse_folder ()
381 string session_dir = session.session_directory().root_path();
382 string::size_type session_dir_len = session_dir.length();
384 string folder_beginning = folder.substr (0, session_dir_len);
386 if (!folder_beginning.compare (session_dir)) {
388 pair.second = folder.substr (session_dir_len);
391 pair.second = folder;
397 } // namespace ARDOUR