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.
22 #include "ardour/export_filename.h"
24 #include "pbd/xml++.h"
25 #include "pbd/convert.h"
26 #include "pbd/enumwriter.h"
28 #include "ardour/session.h"
29 #include "ardour/session_directory.h"
30 #include "ardour/export_timespan.h"
31 #include "ardour/export_format_specification.h"
32 #include "ardour/export_channel_configuration.h"
43 ExportFilename::ExportFilename (Session & session) :
44 include_label (false),
45 include_session (false),
46 include_revision (false),
47 include_channel_config (false),
48 include_channel (false),
49 include_timespan (true), // Include timespan name always
57 time_struct = localtime (&rawtime);
59 folder = session.session_directory().export_path();
61 XMLNode * instant_node = session.instant_xml ("ExportFilename");
63 set_state (*instant_node);
68 ExportFilename::get_state ()
70 XMLNode * node = new XMLNode ("ExportFilename");
73 FieldPair dir = analyse_folder();
74 child = node->add_child ("Folder");
75 child->add_property ("relative", dir.first ? "true" : "false");
76 child->add_property ("path", dir.second);
78 add_field (node, "label", include_label, label);
79 add_field (node, "session", include_session);
80 add_field (node, "revision", include_revision);
81 add_field (node, "time", include_time, enum_2_string (time_format));
82 add_field (node, "date", include_date, enum_2_string (date_format));
84 XMLNode * instant_node = new XMLNode ("ExportRevision");
85 instant_node->add_property ("revision", to_string (revision, std::dec));
86 session.add_instant_xml (*instant_node);
92 ExportFilename::set_state (const XMLNode & node)
98 child = node.child ("Folder");
99 if (!child) { return -1; }
103 if ((prop = child->property ("relative"))) {
104 if (!prop->value().compare ("true")) {
105 folder = session.session_directory().root_path();
109 if ((prop = child->property ("path"))) {
110 folder += prop->value();
114 pair = get_field (node, "label");
115 include_label = pair.first;
118 pair = get_field (node, "session");
119 include_session = pair.first;
121 pair = get_field (node, "revision");
122 include_revision = pair.first;
124 pair = get_field (node, "time");
125 include_time = pair.first;
126 time_format = (TimeFormat) string_2_enum (pair.second, time_format);
128 pair = get_field (node, "date");
129 include_date = pair.first;
130 date_format = (DateFormat) string_2_enum (pair.second, date_format);
132 XMLNode * instant_node = session.instant_xml ("ExportRevision");
133 if (instant_node && (prop = instant_node->property ("revision"))) {
134 revision = atoi (prop->value());
141 ExportFilename::get_path (ExportFormatSpecPtr format) const
143 string path = folder;
144 bool filename_empty = true;
148 if (include_session) {
149 path += filename_empty ? "" : "_";
150 path += session.name();
151 filename_empty = false;
155 path += filename_empty ? "" : "_";
157 filename_empty = false;
160 if (include_revision) {
161 path += filename_empty ? "" : "_";
163 path += to_string (revision, std::dec);
164 filename_empty = false;
167 if (include_timespan && timespan) {
168 path += filename_empty ? "" : "_";
169 path += timespan->name();
170 filename_empty = false;
173 if (include_channel_config && channel_config) {
174 path += filename_empty ? "" : "_";
175 path += channel_config->name();
176 filename_empty = false;
179 if (include_channel) {
180 path += filename_empty ? "" : "_";
182 path += to_string (channel, std::dec);
183 filename_empty = false;
187 path += filename_empty ? "" : "_";
188 path += get_date_format_str (date_format);
189 filename_empty = false;
193 path += filename_empty ? "" : "_";
194 path += get_time_format_str (time_format);
195 filename_empty = false;
199 path += format->extension ();
205 ExportFilename::get_time_format_str (TimeFormat format) const
212 return get_formatted_time ("%H%M");
215 return get_formatted_time ("%H.%M");
218 return _("Invalid time format");
223 ExportFilename::get_date_format_str (DateFormat format) const
230 return get_formatted_time ("%Y%m%d");
233 return get_formatted_time ("%Y-%m-%d");
236 return get_formatted_time ("%y%m%d");
239 return get_formatted_time ("%y-%m-%d");
242 return _("Invalid date format");
247 ExportFilename::set_time_format (TimeFormat format)
249 time_format = format;
251 if (format == T_None) {
252 include_time = false;
259 ExportFilename::set_date_format (DateFormat format)
261 date_format = format;
263 if (format == D_None) {
264 include_date = false;
271 ExportFilename::set_label (string value)
274 include_label = !value.compare ("");
278 ExportFilename::set_folder (string path)
280 // TODO check folder existence
286 ExportFilename::get_formatted_time (string const & format) const
289 strftime (buffer, 80, format.c_str(), time_struct);
291 string return_value (buffer);
296 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
298 XMLNode * child = node->add_child ("Field");
301 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
305 child->add_property ("name", name);
306 child->add_property ("enabled", enabled ? "true" : "false");
307 if (!value.empty()) {
308 child->add_property ("value", value);
312 ExportFilename::FieldPair
313 ExportFilename::get_field (XMLNode const & node, string const & name)
318 XMLNodeList children = node.children();
320 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
321 XMLProperty * prop = (*it)->property ("name");
322 if (prop && !prop->value().compare (name)) {
324 prop = (*it)->property ("enabled");
325 if (prop && !prop->value().compare ("true")) {
331 prop = (*it)->property ("value");
333 pair.second = prop->value();
343 ExportFilename::FieldPair
344 ExportFilename::analyse_folder ()
348 string session_dir = session.session_directory().root_path();
349 string::size_type session_dir_len = session_dir.length();
351 string folder_beginning = folder.substr (0, session_dir_len);
353 if (!folder_beginning.compare (session_dir)) {
355 pair.second = folder.substr (session_dir_len);
358 pair.second = folder;
364 } // namespace ARDOUR