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.
21 #include "ardour/export_filename.h"
23 #include "pbd/xml++.h"
24 #include "pbd/convert.h"
25 #include "pbd/enumwriter.h"
27 #include "ardour/session.h"
28 #include "ardour/session_directory.h"
29 #include "ardour/export_timespan.h"
30 #include "ardour/export_format_specification.h"
31 #include "ardour/export_channel_configuration.h"
32 #include "ardour/export_failed.h"
41 ExportFilename::ExportFilename (Session & session) :
42 include_label (false),
43 include_session (false),
44 include_revision (false),
45 include_channel_config (false),
46 include_channel (false),
47 include_timespan (true), // Include timespan name always
55 time_struct = localtime (&rawtime);
57 folder = session.session_directory().export_path().to_string();
59 XMLNode * instant_node = session.instant_xml ("ExportFilename");
61 set_state (*instant_node);
66 ExportFilename::get_state ()
68 XMLNode * node = new XMLNode ("ExportFilename");
71 FieldPair dir = analyse_folder();
72 child = node->add_child ("Folder");
73 child->add_property ("relative", dir.first ? "true" : "false");
74 child->add_property ("path", dir.second);
76 add_field (node, "label", include_label, label);
77 add_field (node, "session", include_session);
78 add_field (node, "revision", include_revision);
79 add_field (node, "time", include_time, enum_2_string (time_format));
80 add_field (node, "date", include_date, enum_2_string (date_format));
82 XMLNode * instant_node = new XMLNode ("ExportRevision");
83 instant_node->add_property ("revision", to_string (revision, std::dec));
84 session.add_instant_xml (*instant_node);
90 ExportFilename::set_state (const XMLNode & node)
96 child = node.child ("Folder");
97 if (!child) { return -1; }
101 if ((prop = child->property ("relative"))) {
102 if (!prop->value().compare ("true")) {
103 folder = session.session_directory().root_path().to_string();
107 if ((prop = child->property ("path"))) {
108 folder += prop->value();
112 pair = get_field (node, "label");
113 include_label = pair.first;
116 pair = get_field (node, "session");
117 include_session = pair.first;
119 pair = get_field (node, "revision");
120 include_revision = pair.first;
122 pair = get_field (node, "time");
123 include_time = pair.first;
124 time_format = (TimeFormat) string_2_enum (pair.second, time_format);
126 pair = get_field (node, "date");
127 include_date = pair.first;
128 date_format = (DateFormat) string_2_enum (pair.second, date_format);
130 XMLNode * instant_node = session.instant_xml ("ExportRevision");
131 if (instant_node && (prop = instant_node->property ("revision"))) {
132 revision = atoi (prop->value());
139 ExportFilename::get_path (FormatPtr format) const
141 ustring path = folder;
142 bool filename_empty = true;
146 if (include_session) {
147 path += filename_empty ? "" : "_";
148 path += session.name();
149 filename_empty = false;
153 path += filename_empty ? "" : "_";
155 filename_empty = false;
158 if (include_revision) {
159 path += filename_empty ? "" : "_";
161 path += to_string (revision, std::dec);
162 filename_empty = false;
165 if (include_timespan && timespan) {
166 path += filename_empty ? "" : "_";
167 path += timespan->name();
168 filename_empty = false;
171 if (include_channel_config && channel_config) {
172 path += filename_empty ? "" : "_";
173 path += channel_config->name();
174 filename_empty = false;
177 if (include_channel) {
178 path += filename_empty ? "" : "_";
180 path += to_string (channel, std::dec);
181 filename_empty = false;
185 path += filename_empty ? "" : "_";
186 path += get_date_format_str (date_format);
187 filename_empty = false;
191 path += filename_empty ? "" : "_";
192 path += get_time_format_str (time_format);
193 filename_empty = false;
197 path += format->extension ();
203 ExportFilename::get_time_format_str (TimeFormat format) const
210 return get_formatted_time ("%H%M");
213 return get_formatted_time ("%H.%M");
216 return _("Invalid time format");
221 ExportFilename::get_date_format_str (DateFormat format) const
228 return get_formatted_time ("%Y%m%d");
231 return get_formatted_time ("%Y-%m-%d");
234 return get_formatted_time ("%y%m%d");
237 return get_formatted_time ("%y-%m-%d");
240 return _("Invalid date format");
245 ExportFilename::set_time_format (TimeFormat format)
247 time_format = format;
249 if (format == T_None) {
250 include_time = false;
257 ExportFilename::set_date_format (DateFormat format)
259 date_format = format;
261 if (format == D_None) {
262 include_date = false;
269 ExportFilename::set_label (ustring value)
272 include_label = !value.compare ("");
276 ExportFilename::set_folder (ustring path)
278 // TODO check folder existence
284 ExportFilename::get_formatted_time (ustring const & format) const
287 strftime (buffer, 80, format.c_str(), time_struct);
289 ustring return_value (buffer);
294 ExportFilename::add_field (XMLNode * node, ustring const & name, bool enabled, ustring const & value)
296 XMLNode * child = node->add_child ("Field");
299 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
303 child->add_property ("name", name);
304 child->add_property ("enabled", enabled ? "true" : "false");
305 if (!value.empty()) {
306 child->add_property ("value", value);
310 ExportFilename::FieldPair
311 ExportFilename::get_field (XMLNode const & node, ustring const & name)
316 XMLNodeList children = node.children();
318 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
319 XMLProperty * prop = (*it)->property ("name");
320 if (prop && !prop->value().compare (name)) {
322 prop = (*it)->property ("enabled");
323 if (prop && !prop->value().compare ("true")) {
329 prop = (*it)->property ("value");
331 pair.second = prop->value();
341 ExportFilename::FieldPair
342 ExportFilename::analyse_folder ()
346 ustring session_dir = session.session_directory().root_path().to_string();
347 ustring::size_type session_dir_len = session_dir.length();
349 ustring folder_beginning = folder.substr (0, session_dir_len);
351 if (!folder_beginning.compare (session_dir)) {
353 pair.second = folder.substr (session_dir_len);
356 pair.second = folder;
362 } // namespace ARDOUR