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"
33 #include "ardour/export_failed.h"
44 ExportFilename::ExportFilename (Session
& session
) :
45 include_label (false),
46 include_session (false),
47 include_revision (false),
48 include_channel_config (false),
49 include_channel (false),
50 include_timespan (true), // Include timespan name always
58 time_struct
= localtime (&rawtime
);
60 folder
= session
.session_directory().export_path().to_string();
62 XMLNode
* instant_node
= session
.instant_xml ("ExportFilename");
64 set_state (*instant_node
);
69 ExportFilename::get_state ()
71 XMLNode
* node
= new XMLNode ("ExportFilename");
74 FieldPair dir
= analyse_folder();
75 child
= node
->add_child ("Folder");
76 child
->add_property ("relative", dir
.first
? "true" : "false");
77 child
->add_property ("path", dir
.second
);
79 add_field (node
, "label", include_label
, label
);
80 add_field (node
, "session", include_session
);
81 add_field (node
, "revision", include_revision
);
82 add_field (node
, "time", include_time
, enum_2_string (time_format
));
83 add_field (node
, "date", include_date
, enum_2_string (date_format
));
85 XMLNode
* instant_node
= new XMLNode ("ExportRevision");
86 instant_node
->add_property ("revision", to_string (revision
, std::dec
));
87 session
.add_instant_xml (*instant_node
);
93 ExportFilename::set_state (const XMLNode
& node
)
99 child
= node
.child ("Folder");
100 if (!child
) { return -1; }
104 if ((prop
= child
->property ("relative"))) {
105 if (!prop
->value().compare ("true")) {
106 folder
= session
.session_directory().root_path().to_string();
110 if ((prop
= child
->property ("path"))) {
111 folder
+= prop
->value();
115 pair
= get_field (node
, "label");
116 include_label
= pair
.first
;
119 pair
= get_field (node
, "session");
120 include_session
= pair
.first
;
122 pair
= get_field (node
, "revision");
123 include_revision
= pair
.first
;
125 pair
= get_field (node
, "time");
126 include_time
= pair
.first
;
127 time_format
= (TimeFormat
) string_2_enum (pair
.second
, time_format
);
129 pair
= get_field (node
, "date");
130 include_date
= pair
.first
;
131 date_format
= (DateFormat
) string_2_enum (pair
.second
, date_format
);
133 XMLNode
* instant_node
= session
.instant_xml ("ExportRevision");
134 if (instant_node
&& (prop
= instant_node
->property ("revision"))) {
135 revision
= atoi (prop
->value());
142 ExportFilename::get_path (FormatPtr format
) const
144 string path
= folder
;
145 bool filename_empty
= true;
149 if (include_session
) {
150 path
+= filename_empty
? "" : "_";
151 path
+= session
.name();
152 filename_empty
= false;
156 path
+= filename_empty
? "" : "_";
158 filename_empty
= false;
161 if (include_revision
) {
162 path
+= filename_empty
? "" : "_";
164 path
+= to_string (revision
, std::dec
);
165 filename_empty
= false;
168 if (include_timespan
&& timespan
) {
169 path
+= filename_empty
? "" : "_";
170 path
+= timespan
->name();
171 filename_empty
= false;
174 if (include_channel_config
&& channel_config
) {
175 path
+= filename_empty
? "" : "_";
176 path
+= channel_config
->name();
177 filename_empty
= false;
180 if (include_channel
) {
181 path
+= filename_empty
? "" : "_";
183 path
+= to_string (channel
, std::dec
);
184 filename_empty
= false;
188 path
+= filename_empty
? "" : "_";
189 path
+= get_date_format_str (date_format
);
190 filename_empty
= false;
194 path
+= filename_empty
? "" : "_";
195 path
+= get_time_format_str (time_format
);
196 filename_empty
= false;
200 path
+= format
->extension ();
206 ExportFilename::get_time_format_str (TimeFormat format
) const
213 return get_formatted_time ("%H%M");
216 return get_formatted_time ("%H.%M");
219 return _("Invalid time format");
224 ExportFilename::get_date_format_str (DateFormat format
) const
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 get_formatted_time ("%y-%m-%d");
243 return _("Invalid date format");
248 ExportFilename::set_time_format (TimeFormat format
)
250 time_format
= format
;
252 if (format
== T_None
) {
253 include_time
= false;
260 ExportFilename::set_date_format (DateFormat format
)
262 date_format
= format
;
264 if (format
== D_None
) {
265 include_date
= false;
272 ExportFilename::set_label (string value
)
275 include_label
= !value
.compare ("");
279 ExportFilename::set_folder (string path
)
281 // TODO check folder existence
287 ExportFilename::get_formatted_time (string
const & format
) const
290 strftime (buffer
, 80, format
.c_str(), time_struct
);
292 string
return_value (buffer
);
297 ExportFilename::add_field (XMLNode
* node
, string
const & name
, bool enabled
, string
const & value
)
299 XMLNode
* child
= node
->add_child ("Field");
302 std::cerr
<< "Error adding a field to ExportFilename XML-tree" << std::endl
;
306 child
->add_property ("name", name
);
307 child
->add_property ("enabled", enabled
? "true" : "false");
308 if (!value
.empty()) {
309 child
->add_property ("value", value
);
313 ExportFilename::FieldPair
314 ExportFilename::get_field (XMLNode
const & node
, string
const & name
)
319 XMLNodeList children
= node
.children();
321 for (XMLNodeList::iterator it
= children
.begin(); it
!= children
.end(); ++it
) {
322 XMLProperty
* prop
= (*it
)->property ("name");
323 if (prop
&& !prop
->value().compare (name
)) {
325 prop
= (*it
)->property ("enabled");
326 if (prop
&& !prop
->value().compare ("true")) {
332 prop
= (*it
)->property ("value");
334 pair
.second
= prop
->value();
344 ExportFilename::FieldPair
345 ExportFilename::analyse_folder ()
349 string session_dir
= session
.session_directory().root_path().to_string();
350 string::size_type session_dir_len
= session_dir
.length();
352 string folder_beginning
= folder
.substr (0, session_dir_len
);
354 if (!folder_beginning
.compare (session_dir
)) {
356 pair
.second
= folder
.substr (session_dir_len
);
359 pair
.second
= folder
;
365 } // namespace ARDOUR