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, "timespan", include_timespan);
94 add_field (node, "revision", include_revision);
95 add_field (node, "time", include_time, enum_2_string (time_format));
96 add_field (node, "date", include_date, enum_2_string (date_format));
98 XMLNode * extra_node = new XMLNode ("ExportRevision");
99 extra_node->add_property ("revision", to_string (revision, std::dec));
100 session.add_extra_xml (*extra_node);
106 ExportFilename::set_state (const XMLNode & node)
112 child = node.child ("Folder");
113 if (!child) { return -1; }
117 if ((prop = child->property ("relative"))) {
118 if (string_is_affirmative (prop->value())) {
119 folder = session.session_directory().root_path();
123 if ((prop = child->property ("path"))) {
125 tmp = Glib::build_filename (folder, prop->value());
126 if (!Glib::file_test (tmp, Glib::FILE_TEST_EXISTS)) {
127 warning << string_compose (_("Existing export folder for this session (%1) does not exist - ignored"), tmp) << endmsg;
133 if (folder.empty()) {
134 folder = session.session_directory().export_path();
137 pair = get_field (node, "label");
138 include_label = pair.first;
141 pair = get_field (node, "session");
142 include_session = pair.first;
144 pair = get_field (node, "timespan");
145 include_timespan = pair.first;
147 pair = get_field (node, "revision");
148 include_revision = pair.first;
150 pair = get_field (node, "time");
151 include_time = pair.first;
152 time_format = (TimeFormat) string_2_enum (pair.second, time_format);
154 pair = get_field (node, "date");
155 include_date = pair.first;
156 date_format = (DateFormat) string_2_enum (pair.second, date_format);
158 XMLNode * extra_node = session.extra_xml ("ExportRevision");
159 /* Legacy sessions used Session instant.xml for this */
161 extra_node = session.instant_xml ("ExportRevision");
164 if (extra_node && (prop = extra_node->property ("revision"))) {
165 revision = atoi (prop->value());
172 ExportFilename::get_path (ExportFormatSpecPtr format) const
175 bool filename_empty = true;
176 bool with_timespan = include_timespan;
182 && !include_channel_config
185 && !include_format_name) {
186 with_timespan = true;
189 if (include_session) {
190 path += filename_empty ? "" : "_";
191 path += session.name();
192 filename_empty = false;
196 path += filename_empty ? "" : "_";
198 filename_empty = false;
201 if (include_revision) {
202 path += filename_empty ? "" : "_";
204 path += to_string (revision, std::dec);
205 filename_empty = false;
208 if (with_timespan && timespan) {
209 path += filename_empty ? "" : "_";
210 path += timespan->name();
211 filename_empty = false;
214 if (include_channel_config && channel_config) {
215 path += filename_empty ? "" : "_";
216 path += channel_config->name();
217 filename_empty = false;
220 if (include_channel) {
221 path += filename_empty ? "" : "_";
223 path += to_string (channel, std::dec);
224 filename_empty = false;
228 path += filename_empty ? "" : "_";
229 path += get_date_format_str (date_format);
230 filename_empty = false;
234 path += filename_empty ? "" : "_";
235 path += get_time_format_str (time_format);
236 filename_empty = false;
239 if (include_format_name) {
240 path += filename_empty ? "" : "_";
241 path += format->name();
242 filename_empty = false;
250 path += format->extension ();
252 path = legalize_for_universal_path (path);
254 return Glib::build_filename (folder, path);
258 ExportFilename::get_time_format_str (TimeFormat format) const
265 return get_formatted_time ("%H%M");
268 return get_formatted_time ("%H.%M");
271 return _("Invalid time format");
276 ExportFilename::get_date_format_str (DateFormat format) const
283 return get_formatted_time ("%Y%m%d");
286 return get_formatted_time ("%Y-%m-%d");
289 return get_formatted_time ("%y%m%d");
292 return get_formatted_time ("%y-%m-%d");
295 return _("Invalid date format");
300 ExportFilename::set_time_format (TimeFormat format)
302 time_format = format;
304 if (format == T_None) {
305 include_time = false;
312 ExportFilename::set_date_format (DateFormat format)
314 date_format = format;
316 if (format == D_None) {
317 include_date = false;
324 ExportFilename::set_label (string value)
327 include_label = !value.compare ("");
331 ExportFilename::set_folder (string path)
333 // TODO check folder existence
339 ExportFilename::get_formatted_time (string const & format) const
342 strftime (buffer, 80, format.c_str(), time_struct);
344 string return_value (buffer);
349 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
351 XMLNode * child = node->add_child ("Field");
354 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
358 child->add_property ("name", name);
359 child->add_property ("enabled", enabled ? "true" : "false");
360 if (!value.empty()) {
361 child->add_property ("value", value);
365 ExportFilename::FieldPair
366 ExportFilename::get_field (XMLNode const & node, string const & name)
371 XMLNodeList children = node.children();
373 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
374 XMLProperty * prop = (*it)->property ("name");
375 if (prop && !prop->value().compare (name)) {
377 prop = (*it)->property ("enabled");
378 if (prop && !prop->value().compare ("true")) {
384 prop = (*it)->property ("value");
386 pair.second = prop->value();
396 ExportFilename::FieldPair
397 ExportFilename::analyse_folder ()
401 string session_dir = session.session_directory().root_path();
402 string::size_type session_dir_len = session_dir.length();
404 string folder_beginning = folder.substr (0, session_dir_len);
406 if (!folder_beginning.compare (session_dir)) {
408 pair.second = folder.substr (session_dir_len);
411 pair.second = folder;
417 } // namespace ARDOUR