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 <glibmm/miscutils.h>
25 #include <glibmm/fileutils.h>
27 #include "pbd/xml++.h"
28 #include "pbd/convert.h"
29 #include "pbd/enumwriter.h"
31 #include "ardour/session.h"
32 #include "ardour/session_directory.h"
33 #include "ardour/export_timespan.h"
34 #include "ardour/export_format_specification.h"
35 #include "ardour/export_channel_configuration.h"
36 #include "ardour/utils.h"
47 ExportFilename::ExportFilename (Session & session) :
48 include_label (false),
49 include_session (false),
50 include_revision (false),
51 include_channel_config (false),
52 include_format_name (false),
53 include_channel (false),
54 include_timespan (true), // Include timespan name always
62 time_struct = localtime (&rawtime);
64 folder = session.session_directory().export_path();
66 XMLNode * instant_node = session.instant_xml ("ExportFilename");
68 set_state (*instant_node);
73 ExportFilename::get_state ()
75 XMLNode * node = new XMLNode ("ExportFilename");
78 FieldPair dir = analyse_folder();
79 child = node->add_child ("Folder");
80 child->add_property ("relative", dir.first ? "true" : "false");
81 child->add_property ("path", dir.second);
83 add_field (node, "label", include_label, label);
84 add_field (node, "session", include_session);
85 add_field (node, "revision", include_revision);
86 add_field (node, "time", include_time, enum_2_string (time_format));
87 add_field (node, "date", include_date, enum_2_string (date_format));
89 XMLNode * instant_node = new XMLNode ("ExportRevision");
90 instant_node->add_property ("revision", to_string (revision, std::dec));
91 session.add_instant_xml (*instant_node);
97 ExportFilename::set_state (const XMLNode & node)
103 child = node.child ("Folder");
104 if (!child) { return -1; }
108 if ((prop = child->property ("relative"))) {
109 if (string_is_affirmative (prop->value())) {
110 folder = session.session_directory().root_path();
114 if ((prop = child->property ("path"))) {
116 tmp = Glib::build_filename (folder, prop->value());
117 if (!Glib::file_test (tmp, Glib::FILE_TEST_EXISTS)) {
118 warning << string_compose (_("Existing export folder for this session (%1) does not exist - ignored"), tmp) << endmsg;
124 if (folder.empty()) {
125 folder = session.session_directory().export_path();
128 pair = get_field (node, "label");
129 include_label = pair.first;
132 pair = get_field (node, "session");
133 include_session = pair.first;
135 pair = get_field (node, "revision");
136 include_revision = pair.first;
138 pair = get_field (node, "time");
139 include_time = pair.first;
140 time_format = (TimeFormat) string_2_enum (pair.second, time_format);
142 pair = get_field (node, "date");
143 include_date = pair.first;
144 date_format = (DateFormat) string_2_enum (pair.second, date_format);
146 XMLNode * instant_node = session.instant_xml ("ExportRevision");
147 if (instant_node && (prop = instant_node->property ("revision"))) {
148 revision = atoi (prop->value());
155 ExportFilename::get_path (ExportFormatSpecPtr format) const
158 bool filename_empty = true;
160 if (include_session) {
161 path += filename_empty ? "" : "_";
162 path += session.name();
163 filename_empty = false;
167 path += filename_empty ? "" : "_";
169 filename_empty = false;
172 if (include_revision) {
173 path += filename_empty ? "" : "_";
175 path += to_string (revision, std::dec);
176 filename_empty = false;
179 if (include_timespan && timespan) {
180 path += filename_empty ? "" : "_";
181 path += timespan->name();
182 filename_empty = false;
185 if (include_channel_config && channel_config) {
186 path += filename_empty ? "" : "_";
187 path += channel_config->name();
188 filename_empty = false;
191 if (include_channel) {
192 path += filename_empty ? "" : "_";
194 path += to_string (channel, std::dec);
195 filename_empty = false;
199 path += filename_empty ? "" : "_";
200 path += get_date_format_str (date_format);
201 filename_empty = false;
205 path += filename_empty ? "" : "_";
206 path += get_time_format_str (time_format);
207 filename_empty = false;
210 if (include_format_name) {
211 path += filename_empty ? "" : "_";
212 path += format->name();
213 filename_empty = false;
217 path += format->extension ();
219 path = legalize_for_universal_path (path);
221 return Glib::build_filename (folder, path);
225 ExportFilename::get_time_format_str (TimeFormat format) const
232 return get_formatted_time ("%H%M");
235 return get_formatted_time ("%H.%M");
238 return _("Invalid time format");
243 ExportFilename::get_date_format_str (DateFormat format) const
250 return get_formatted_time ("%Y%m%d");
253 return get_formatted_time ("%Y-%m-%d");
256 return get_formatted_time ("%y%m%d");
259 return get_formatted_time ("%y-%m-%d");
262 return _("Invalid date format");
267 ExportFilename::set_time_format (TimeFormat format)
269 time_format = format;
271 if (format == T_None) {
272 include_time = false;
279 ExportFilename::set_date_format (DateFormat format)
281 date_format = format;
283 if (format == D_None) {
284 include_date = false;
291 ExportFilename::set_label (string value)
294 include_label = !value.compare ("");
298 ExportFilename::set_folder (string path)
300 // TODO check folder existence
306 ExportFilename::get_formatted_time (string const & format) const
309 strftime (buffer, 80, format.c_str(), time_struct);
311 string return_value (buffer);
316 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
318 XMLNode * child = node->add_child ("Field");
321 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
325 child->add_property ("name", name);
326 child->add_property ("enabled", enabled ? "true" : "false");
327 if (!value.empty()) {
328 child->add_property ("value", value);
332 ExportFilename::FieldPair
333 ExportFilename::get_field (XMLNode const & node, string const & name)
338 XMLNodeList children = node.children();
340 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
341 XMLProperty * prop = (*it)->property ("name");
342 if (prop && !prop->value().compare (name)) {
344 prop = (*it)->property ("enabled");
345 if (prop && !prop->value().compare ("true")) {
351 prop = (*it)->property ("value");
353 pair.second = prop->value();
363 ExportFilename::FieldPair
364 ExportFilename::analyse_folder ()
368 string session_dir = session.session_directory().root_path();
369 string::size_type session_dir_len = session_dir.length();
371 string folder_beginning = folder.substr (0, session_dir_len);
373 if (!folder_beginning.compare (session_dir)) {
375 pair.second = folder.substr (session_dir_len);
378 pair.second = folder;
384 } // namespace ARDOUR