2 * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
4 * Copyright (C) 2011-2013 Sakari Bergen <sakari.bergen@beatwaves.net>
5 * Copyright (C) 2012-2016 Tim Mayberry <mojofunk@gmail.com>
6 * Copyright (C) 2014 Nick Mainsbridge <mainsbridge@gmail.com>
7 * Copyright (C) 2015-2018 Robin Gareus <robin@gareus.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <glibmm/miscutils.h>
27 #include <glibmm/fileutils.h>
29 #include "pbd/xml++.h"
30 #include "pbd/string_convert.h"
31 #include "pbd/enumwriter.h"
32 #include "pbd/localtime_r.h"
34 #include "ardour/libardour_visibility.h"
35 #include "ardour/session.h"
36 #include "ardour/session_directory.h"
37 #include "ardour/export_filename.h"
38 #include "ardour/export_format_specification.h"
39 #include "ardour/export_channel_configuration.h"
40 #include "ardour/export_timespan.h"
41 #include "ardour/utils.h"
52 ExportFilename::ExportFilename (Session & session) :
53 include_label (false),
54 include_session (false),
55 use_session_snapshot_name (false),
56 include_revision (false),
57 include_channel_config (false),
58 include_format_name (false),
59 include_channel (false),
60 include_timespan (true), // Include timespan name always
70 localtime_r (&rawtime, &time_struct);
72 folder = session.session_directory().export_path();
74 XMLNode * extra_node = session.extra_xml ("ExportFilename");
75 /* Legacy sessions used Session instant.xml for this */
77 session.instant_xml ("ExportFilename");
81 set_state (*extra_node);
86 ExportFilename::get_state ()
88 XMLNode * node = new XMLNode ("ExportFilename");
91 FieldPair dir = analyse_folder();
92 child = node->add_child ("Folder");
93 child->set_property ("relative", dir.first);
94 child->set_property ("path", dir.second);
96 add_field (node, "label", include_label, label);
97 add_field (node, "session", include_session);
98 add_field (node, "snapshot", use_session_snapshot_name);
99 add_field (node, "timespan", include_timespan);
100 add_field (node, "revision", include_revision);
101 add_field (node, "time", include_time, enum_2_string (time_format));
102 add_field (node, "date", include_date, enum_2_string (date_format));
104 XMLNode * extra_node = new XMLNode ("ExportRevision");
105 extra_node->set_property ("revision", revision);
106 session.add_extra_xml (*extra_node);
112 ExportFilename::set_state (const XMLNode & node)
117 child = node.child ("Folder");
118 if (!child) { return -1; }
123 if (child->get_property ("relative", is_relative) && is_relative) {
124 folder = session.session_directory ().root_path ();
128 if (child->get_property ("path", tmp)) {
129 tmp = Glib::build_filename (folder, tmp);
130 if (!Glib::file_test (tmp, Glib::FILE_TEST_EXISTS)) {
131 warning << string_compose (_("Existing export folder for this session (%1) does not exist - ignored"), tmp) << endmsg;
137 if (folder.empty() || !Glib::file_test (folder, FileTest (FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
138 folder = session.session_directory().export_path();
141 pair = get_field (node, "label");
142 include_label = pair.first;
145 pair = get_field (node, "session");
146 include_session = pair.first;
148 pair = get_field (node, "snapshot");
149 use_session_snapshot_name = pair.first;
151 pair = get_field (node, "timespan");
152 include_timespan = pair.first;
154 pair = get_field (node, "revision");
155 include_revision = pair.first;
157 pair = get_field (node, "time");
158 include_time = pair.first;
159 time_format = (TimeFormat) string_2_enum (pair.second, time_format);
161 pair = get_field (node, "date");
162 include_date = pair.first;
163 date_format = (DateFormat) string_2_enum (pair.second, date_format);
165 XMLNode * extra_node = session.extra_xml ("ExportRevision");
166 /* Legacy sessions used Session instant.xml for this */
168 extra_node = session.instant_xml ("ExportRevision");
172 extra_node->get_property ("revision", revision);
179 ExportFilename::get_path (ExportFormatSpecPtr format) const
182 bool filename_empty = true;
183 bool with_timespan = include_timespan;
189 && !include_channel_config
192 with_timespan = true;
195 if (include_session) {
196 path += filename_empty ? "" : "_";
197 if (use_session_snapshot_name) {
198 path += session.snap_name();
200 path += session.name();
202 filename_empty = false;
206 path += filename_empty ? "" : "_";
208 filename_empty = false;
211 if (include_revision) {
212 path += filename_empty ? "" : "_";
214 path += to_string (revision);
215 filename_empty = false;
218 if (with_timespan && timespan) {
219 path += filename_empty ? "" : "_";
220 path += timespan->name();
221 filename_empty = false;
224 if (include_channel_config && channel_config) {
225 path += filename_empty ? "" : "_";
226 path += channel_config->name();
227 filename_empty = false;
230 if (include_channel) {
231 path += filename_empty ? "" : "_";
233 path += to_string (channel);
234 filename_empty = false;
238 path += filename_empty ? "" : "_";
239 path += get_date_format_str (date_format);
240 filename_empty = false;
244 path += filename_empty ? "" : "_";
245 path += get_time_format_str (time_format);
246 filename_empty = false;
249 if (include_format_name && format) {
250 path += filename_empty ? "" : "_";
251 path += format->name();
252 filename_empty = false;
261 path += format->extension ();
264 path = legalize_for_universal_path (path);
266 return Glib::build_filename (folder, path);
270 ExportFilename::get_time_format_str (TimeFormat format) const
277 return get_formatted_time ("%H%M");
280 return get_formatted_time ("%H.%M");
283 return _("Invalid time format");
288 ExportFilename::get_date_format_str (DateFormat format) const
295 return get_formatted_time ("%Y%m%d");
298 return get_formatted_time ("%Y-%m-%d");
301 return get_formatted_time ("%y%m%d");
304 return get_formatted_time ("%y-%m-%d");
307 return _("Invalid date format");
312 ExportFilename::set_time_format (TimeFormat format)
314 time_format = format;
316 if (format == T_None) {
317 include_time = false;
324 ExportFilename::set_date_format (DateFormat format)
326 date_format = format;
328 if (format == D_None) {
329 include_date = false;
336 ExportFilename::set_label (string value)
339 include_label = !value.compare ("");
343 ExportFilename::set_folder (string path)
345 // TODO check folder existence
351 ExportFilename::get_formatted_time (string const & format) const
354 strftime (buffer, 80, format.c_str(), &time_struct);
356 string return_value (buffer);
361 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
363 XMLNode * child = node->add_child ("Field");
366 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
370 child->set_property ("name", name);
371 child->set_property ("enabled", enabled);
372 if (!value.empty()) {
373 child->set_property ("value", value);
377 ExportFilename::FieldPair
378 ExportFilename::get_field (XMLNode const & node, string const & name)
383 XMLNodeList children = node.children();
385 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
387 if ((*it)->get_property ("name", str) && name == str) {
389 (*it)->get_property ("enabled", pair.first);
390 (*it)->get_property ("value", pair.second);
399 ExportFilename::FieldPair
400 ExportFilename::analyse_folder ()
404 string session_dir = session.session_directory().root_path();
405 string::size_type session_dir_len = session_dir.length();
407 string folder_beginning = folder.substr (0, session_dir_len);
409 if (!folder_beginning.compare (session_dir)) {
411 pair.second = folder.substr (session_dir_len);
414 pair.second = folder;
420 } // namespace ARDOUR