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"
42 ExportFilename::ExportFilename (Session
& session
) :
43 include_label (false),
44 include_session (false),
45 include_revision (false),
46 include_channel_config (false),
47 include_channel (false),
48 include_timespan (true), // Include timespan name always
56 time_struct
= localtime (&rawtime
);
58 folder
= session
.session_directory().export_path().to_string();
60 XMLNode
* instant_node
= session
.instant_xml ("ExportFilename");
62 set_state (*instant_node
);
67 ExportFilename::get_state ()
69 XMLNode
* node
= new XMLNode ("ExportFilename");
72 FieldPair dir
= analyse_folder();
73 child
= node
->add_child ("Folder");
74 child
->add_property ("relative", dir
.first
? "true" : "false");
75 child
->add_property ("path", dir
.second
);
77 add_field (node
, "label", include_label
, label
);
78 add_field (node
, "session", include_session
);
79 add_field (node
, "revision", include_revision
);
80 add_field (node
, "time", include_time
, enum_2_string (time_format
));
81 add_field (node
, "date", include_date
, enum_2_string (date_format
));
83 XMLNode
* instant_node
= new XMLNode ("ExportRevision");
84 instant_node
->add_property ("revision", to_string (revision
, std::dec
));
85 session
.add_instant_xml (*instant_node
);
91 ExportFilename::set_state (const XMLNode
& node
)
97 child
= node
.child ("Folder");
98 if (!child
) { return -1; }
102 if ((prop
= child
->property ("relative"))) {
103 if (!prop
->value().compare ("true")) {
104 folder
= session
.session_directory().root_path().to_string();
108 if ((prop
= child
->property ("path"))) {
109 folder
+= prop
->value();
113 pair
= get_field (node
, "label");
114 include_label
= pair
.first
;
117 pair
= get_field (node
, "session");
118 include_session
= pair
.first
;
120 pair
= get_field (node
, "revision");
121 include_revision
= pair
.first
;
123 pair
= get_field (node
, "time");
124 include_time
= pair
.first
;
125 time_format
= (TimeFormat
) string_2_enum (pair
.second
, time_format
);
127 pair
= get_field (node
, "date");
128 include_date
= pair
.first
;
129 date_format
= (DateFormat
) string_2_enum (pair
.second
, date_format
);
131 XMLNode
* instant_node
= session
.instant_xml ("ExportRevision");
132 if (instant_node
&& (prop
= instant_node
->property ("revision"))) {
133 revision
= atoi (prop
->value());
140 ExportFilename::get_path (FormatPtr format
) const
142 ustring path
= folder
;
143 bool filename_empty
= true;
147 if (include_session
) {
148 path
+= filename_empty
? "" : "_";
149 path
+= session
.name();
150 filename_empty
= false;
154 path
+= filename_empty
? "" : "_";
156 filename_empty
= false;
159 if (include_revision
) {
160 path
+= filename_empty
? "" : "_";
162 path
+= to_string (revision
, std::dec
);
163 filename_empty
= false;
166 if (include_timespan
&& timespan
) {
167 path
+= filename_empty
? "" : "_";
168 path
+= timespan
->name();
169 filename_empty
= false;
172 if (include_channel_config
&& channel_config
) {
173 path
+= filename_empty
? "" : "_";
174 path
+= channel_config
->name();
175 filename_empty
= false;
178 if (include_channel
) {
179 path
+= filename_empty
? "" : "_";
181 path
+= to_string (channel
, std::dec
);
182 filename_empty
= false;
186 path
+= filename_empty
? "" : "_";
187 path
+= get_date_format_str (date_format
);
188 filename_empty
= false;
192 path
+= filename_empty
? "" : "_";
193 path
+= get_time_format_str (time_format
);
194 filename_empty
= false;
198 path
+= format
->extension ();
204 ExportFilename::get_time_format_str (TimeFormat format
) const
211 return get_formatted_time ("%H%M");
214 return get_formatted_time ("%H.%M");
217 return _("Invalid time format");
222 ExportFilename::get_date_format_str (DateFormat format
) const
229 return get_formatted_time ("%Y%m%d");
232 return get_formatted_time ("%Y-%m-%d");
235 return get_formatted_time ("%y%m%d");
238 return get_formatted_time ("%y-%m-%d");
241 return _("Invalid date format");
246 ExportFilename::set_time_format (TimeFormat format
)
248 time_format
= format
;
250 if (format
== T_None
) {
251 include_time
= false;
258 ExportFilename::set_date_format (DateFormat format
)
260 date_format
= format
;
262 if (format
== D_None
) {
263 include_date
= false;
270 ExportFilename::set_label (ustring value
)
273 include_label
= !value
.compare ("");
277 ExportFilename::set_folder (ustring path
)
279 // TODO check folder existence
285 ExportFilename::get_formatted_time (ustring
const & format
) const
288 strftime (buffer
, 80, format
.c_str(), time_struct
);
290 ustring
return_value (buffer
);
295 ExportFilename::add_field (XMLNode
* node
, ustring
const & name
, bool enabled
, ustring
const & value
)
297 XMLNode
* child
= node
->add_child ("Field");
300 std::cerr
<< "Error adding a field to ExportFilename XML-tree" << std::endl
;
304 child
->add_property ("name", name
);
305 child
->add_property ("enabled", enabled
? "true" : "false");
306 if (!value
.empty()) {
307 child
->add_property ("value", value
);
311 ExportFilename::FieldPair
312 ExportFilename::get_field (XMLNode
const & node
, ustring
const & name
)
317 XMLNodeList children
= node
.children();
319 for (XMLNodeList::iterator it
= children
.begin(); it
!= children
.end(); ++it
) {
320 XMLProperty
* prop
= (*it
)->property ("name");
321 if (prop
&& !prop
->value().compare (name
)) {
323 prop
= (*it
)->property ("enabled");
324 if (prop
&& !prop
->value().compare ("true")) {
330 prop
= (*it
)->property ("value");
332 pair
.second
= prop
->value();
342 ExportFilename::FieldPair
343 ExportFilename::analyse_folder ()
347 ustring session_dir
= session
.session_directory().root_path().to_string();
348 ustring::size_type session_dir_len
= session_dir
.length();
350 ustring folder_beginning
= folder
.substr (0, session_dir_len
);
352 if (!folder_beginning
.compare (session_dir
)) {
354 pair
.second
= folder
.substr (session_dir_len
);
357 pair
.second
= folder
;
363 } // namespace ARDOUR