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");
69 set_state (*extra_node);
74 ExportFilename::get_state ()
76 XMLNode * node = new XMLNode ("ExportFilename");
79 FieldPair dir = analyse_folder();
80 child = node->add_child ("Folder");
81 child->add_property ("relative", dir.first ? "true" : "false");
82 child->add_property ("path", dir.second);
84 add_field (node, "label", include_label, label);
85 add_field (node, "session", include_session);
86 add_field (node, "revision", include_revision);
87 add_field (node, "time", include_time, enum_2_string (time_format));
88 add_field (node, "date", include_date, enum_2_string (date_format));
90 XMLNode * extra_node = new XMLNode ("ExportRevision");
91 extra_node->add_property ("revision", to_string (revision, std::dec));
92 session.add_extra_xml (*extra_node);
98 ExportFilename::set_state (const XMLNode & node)
104 child = node.child ("Folder");
105 if (!child) { return -1; }
109 if ((prop = child->property ("relative"))) {
110 if (string_is_affirmative (prop->value())) {
111 folder = session.session_directory().root_path();
115 if ((prop = child->property ("path"))) {
117 tmp = Glib::build_filename (folder, prop->value());
118 if (!Glib::file_test (tmp, Glib::FILE_TEST_EXISTS)) {
119 warning << string_compose (_("Existing export folder for this session (%1) does not exist - ignored"), tmp) << endmsg;
125 if (folder.empty()) {
126 folder = session.session_directory().export_path();
129 pair = get_field (node, "label");
130 include_label = pair.first;
133 pair = get_field (node, "session");
134 include_session = pair.first;
136 pair = get_field (node, "revision");
137 include_revision = pair.first;
139 pair = get_field (node, "time");
140 include_time = pair.first;
141 time_format = (TimeFormat) string_2_enum (pair.second, time_format);
143 pair = get_field (node, "date");
144 include_date = pair.first;
145 date_format = (DateFormat) string_2_enum (pair.second, date_format);
147 XMLNode * extra_node = session.extra_xml ("ExportRevision");
148 if (extra_node && (prop = extra_node->property ("revision"))) {
149 revision = atoi (prop->value());
156 ExportFilename::get_path (ExportFormatSpecPtr format) const
159 bool filename_empty = true;
161 if (include_session) {
162 path += filename_empty ? "" : "_";
163 path += session.name();
164 filename_empty = false;
168 path += filename_empty ? "" : "_";
170 filename_empty = false;
173 if (include_revision) {
174 path += filename_empty ? "" : "_";
176 path += to_string (revision, std::dec);
177 filename_empty = false;
180 if (include_timespan && timespan) {
181 path += filename_empty ? "" : "_";
182 path += timespan->name();
183 filename_empty = false;
186 if (include_channel_config && channel_config) {
187 path += filename_empty ? "" : "_";
188 path += channel_config->name();
189 filename_empty = false;
192 if (include_channel) {
193 path += filename_empty ? "" : "_";
195 path += to_string (channel, std::dec);
196 filename_empty = false;
200 path += filename_empty ? "" : "_";
201 path += get_date_format_str (date_format);
202 filename_empty = false;
206 path += filename_empty ? "" : "_";
207 path += get_time_format_str (time_format);
208 filename_empty = false;
211 if (include_format_name) {
212 path += filename_empty ? "" : "_";
213 path += format->name();
214 filename_empty = false;
218 path += format->extension ();
220 path = legalize_for_universal_path (path);
222 return Glib::build_filename (folder, path);
226 ExportFilename::get_time_format_str (TimeFormat format) const
233 return get_formatted_time ("%H%M");
236 return get_formatted_time ("%H.%M");
239 return _("Invalid time format");
244 ExportFilename::get_date_format_str (DateFormat format) const
251 return get_formatted_time ("%Y%m%d");
254 return get_formatted_time ("%Y-%m-%d");
257 return get_formatted_time ("%y%m%d");
260 return get_formatted_time ("%y-%m-%d");
263 return _("Invalid date format");
268 ExportFilename::set_time_format (TimeFormat format)
270 time_format = format;
272 if (format == T_None) {
273 include_time = false;
280 ExportFilename::set_date_format (DateFormat format)
282 date_format = format;
284 if (format == D_None) {
285 include_date = false;
292 ExportFilename::set_label (string value)
295 include_label = !value.compare ("");
299 ExportFilename::set_folder (string path)
301 // TODO check folder existence
307 ExportFilename::get_formatted_time (string const & format) const
310 strftime (buffer, 80, format.c_str(), time_struct);
312 string return_value (buffer);
317 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
319 XMLNode * child = node->add_child ("Field");
322 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
326 child->add_property ("name", name);
327 child->add_property ("enabled", enabled ? "true" : "false");
328 if (!value.empty()) {
329 child->add_property ("value", value);
333 ExportFilename::FieldPair
334 ExportFilename::get_field (XMLNode const & node, string const & name)
339 XMLNodeList children = node.children();
341 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
342 XMLProperty * prop = (*it)->property ("name");
343 if (prop && !prop->value().compare (name)) {
345 prop = (*it)->property ("enabled");
346 if (prop && !prop->value().compare ("true")) {
352 prop = (*it)->property ("value");
354 pair.second = prop->value();
364 ExportFilename::FieldPair
365 ExportFilename::analyse_folder ()
369 string session_dir = session.session_directory().root_path();
370 string::size_type session_dir_len = session_dir.length();
372 string folder_beginning = folder.substr (0, session_dir_len);
374 if (!folder_beginning.compare (session_dir)) {
376 pair.second = folder.substr (session_dir_len);
379 pair.second = folder;
385 } // namespace ARDOUR