fix up fwd/reverse stuff in semitone mode
[ardour2.git] / libs / ardour / export_filename.cc
blob7034600c8a082ea139c659bd6aef4cc8a5d1e357
1 /*
2 Copyright (C) 2008 Paul Davis
3 Author: Sakari Bergen
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 <string>
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"
35 #include "i18n.h"
37 using namespace PBD;
38 using namespace Glib;
39 using std::string;
41 namespace ARDOUR
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
51 include_time (false),
52 include_date (false),
53 session (session),
54 revision (1)
56 time_t rawtime;
57 std::time (&rawtime);
58 time_struct = localtime (&rawtime);
60 folder = session.session_directory().export_path().to_string();
62 XMLNode * instant_node = session.instant_xml ("ExportFilename");
63 if (instant_node) {
64 set_state (*instant_node);
68 XMLNode &
69 ExportFilename::get_state ()
71 XMLNode * node = new XMLNode ("ExportFilename");
72 XMLNode * child;
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);
89 return *node;
92 int
93 ExportFilename::set_state (const XMLNode & node)
95 XMLNode * child;
96 XMLProperty * prop;
97 FieldPair pair;
99 child = node.child ("Folder");
100 if (!child) { return -1; }
102 folder = "";
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;
117 label = pair.second;
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());
138 return 0;
141 string
142 ExportFilename::get_path (FormatPtr format) const
144 string path = folder;
145 bool filename_empty = true;
147 path += "/";
149 if (include_session) {
150 path += filename_empty ? "" : "_";
151 path += session.name();
152 filename_empty = false;
155 if (include_label) {
156 path += filename_empty ? "" : "_";
157 path += label;
158 filename_empty = false;
161 if (include_revision) {
162 path += filename_empty ? "" : "_";
163 path += "r";
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 ? "" : "_";
182 path += "channel";
183 path += to_string (channel, std::dec);
184 filename_empty = false;
187 if (include_date) {
188 path += filename_empty ? "" : "_";
189 path += get_date_format_str (date_format);
190 filename_empty = false;
193 if (include_time) {
194 path += filename_empty ? "" : "_";
195 path += get_time_format_str (time_format);
196 filename_empty = false;
199 path += ".";
200 path += format->extension ();
202 return path;
205 string
206 ExportFilename::get_time_format_str (TimeFormat format) const
208 switch ( format ) {
209 case T_None:
210 return _("No Time");
212 case T_NoDelim:
213 return get_formatted_time ("%H%M");
215 case T_Delim:
216 return get_formatted_time ("%H.%M");
218 default:
219 return _("Invalid time format");
223 string
224 ExportFilename::get_date_format_str (DateFormat format) const
226 switch (format) {
227 case D_None:
228 return _("No Date");
230 case D_BE:
231 return get_formatted_time ("%Y%m%d");
233 case D_ISO:
234 return get_formatted_time ("%Y-%m-%d");
236 case D_BEShortY:
237 return get_formatted_time ("%y%m%d");
239 case D_ISOShortY:
240 return get_formatted_time ("%y-%m-%d");
242 default:
243 return _("Invalid date format");
247 void
248 ExportFilename::set_time_format (TimeFormat format)
250 time_format = format;
252 if (format == T_None) {
253 include_time = false;
254 } else {
255 include_time = true;
259 void
260 ExportFilename::set_date_format (DateFormat format)
262 date_format = format;
264 if (format == D_None) {
265 include_date = false;
266 } else {
267 include_date = true;
271 void
272 ExportFilename::set_label (string value)
274 label = value;
275 include_label = !value.compare ("");
278 bool
279 ExportFilename::set_folder (string path)
281 // TODO check folder existence
282 folder = path;
283 return true;
286 string
287 ExportFilename::get_formatted_time (string const & format) const
289 char buffer [80];
290 strftime (buffer, 80, format.c_str(), time_struct);
292 string return_value (buffer);
293 return return_value;
296 void
297 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
299 XMLNode * child = node->add_child ("Field");
301 if (!child) {
302 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
303 return;
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)
316 FieldPair pair;
317 pair.first = false;
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")) {
327 pair.first = true;
328 } else {
329 pair.first = false;
332 prop = (*it)->property ("value");
333 if (prop) {
334 pair.second = prop->value();
337 return pair;
341 return pair;
344 ExportFilename::FieldPair
345 ExportFilename::analyse_folder ()
347 FieldPair pair;
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)) {
355 pair.first = true;
356 pair.second = folder.substr (session_dir_len);
357 } else {
358 pair.first = false;
359 pair.second = folder;
362 return pair;
365 } // namespace ARDOUR