Use ISC license for RDFF (same idea, MIT style, just prettier).
[ardour.git] / libs / ardour / export_filename.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
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.
9
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.
14
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.
18
19 */
20
21 #include <string>
22 #include "ardour/export_filename.h"
23
24 #include "pbd/xml++.h"
25 #include "pbd/convert.h"
26 #include "pbd/enumwriter.h"
27
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"
34
35 #include "i18n.h"
36
37 using namespace PBD;
38 using namespace Glib;
39 using std::string;
40
41 namespace ARDOUR
42 {
43
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)
55 {
56         time_t rawtime;
57         std::time (&rawtime);
58         time_struct = localtime (&rawtime);
59
60         folder = session.session_directory().export_path().to_string();
61
62         XMLNode * instant_node = session.instant_xml ("ExportFilename");
63         if (instant_node) {
64                 set_state (*instant_node);
65         }
66 }
67
68 XMLNode &
69 ExportFilename::get_state ()
70 {
71         XMLNode * node = new XMLNode ("ExportFilename");
72         XMLNode * child;
73
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);
78
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));
84
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);
88
89         return *node;
90 }
91
92 int
93 ExportFilename::set_state (const XMLNode & node)
94 {
95         XMLNode * child;
96         XMLProperty * prop;
97         FieldPair pair;
98
99         child = node.child ("Folder");
100         if (!child) { return -1; }
101
102         folder = "";
103
104         if ((prop = child->property ("relative"))) {
105                 if (!prop->value().compare ("true")) {
106                         folder = session.session_directory().root_path().to_string();
107                 }
108         }
109
110         if ((prop = child->property ("path"))) {
111                 folder += prop->value();
112         }
113
114
115         pair = get_field (node, "label");
116         include_label = pair.first;
117         label = pair.second;
118
119         pair = get_field (node, "session");
120         include_session = pair.first;
121
122         pair = get_field (node, "revision");
123         include_revision = pair.first;
124
125         pair = get_field (node, "time");
126         include_time = pair.first;
127         time_format = (TimeFormat) string_2_enum (pair.second, time_format);
128
129         pair = get_field (node, "date");
130         include_date = pair.first;
131         date_format = (DateFormat) string_2_enum (pair.second, date_format);
132
133         XMLNode * instant_node = session.instant_xml ("ExportRevision");
134         if (instant_node && (prop = instant_node->property ("revision"))) {
135                 revision = atoi (prop->value());
136         }
137
138         return 0;
139 }
140
141 string
142 ExportFilename::get_path (FormatPtr format) const
143 {
144         string path = folder;
145         bool filename_empty = true;
146
147         path += "/";
148
149         if (include_session) {
150                 path += filename_empty ? "" : "_";
151                 path += session.name();
152                 filename_empty = false;
153         }
154
155         if (include_label) {
156                 path += filename_empty ? "" : "_";
157                 path += label;
158                 filename_empty = false;
159         }
160
161         if (include_revision) {
162                 path += filename_empty ? "" : "_";
163                 path += "r";
164                 path += to_string (revision, std::dec);
165                 filename_empty = false;
166         }
167
168         if (include_timespan && timespan) {
169                 path += filename_empty ? "" : "_";
170                 path += timespan->name();
171                 filename_empty = false;
172         }
173
174         if (include_channel_config && channel_config) {
175                 path += filename_empty ? "" : "_";
176                 path += channel_config->name();
177                 filename_empty = false;
178         }
179
180         if (include_channel) {
181                 path += filename_empty ? "" : "_";
182                 path += "channel";
183                 path += to_string (channel, std::dec);
184                 filename_empty = false;
185         }
186
187         if (include_date) {
188                 path += filename_empty ? "" : "_";
189                 path += get_date_format_str (date_format);
190                 filename_empty = false;
191         }
192
193         if (include_time) {
194                 path += filename_empty ? "" : "_";
195                 path += get_time_format_str (time_format);
196                 filename_empty = false;
197         }
198
199         path += ".";
200         path += format->extension ();
201
202         return path;
203 }
204
205 string
206 ExportFilename::get_time_format_str (TimeFormat format) const
207 {
208         switch ( format ) {
209           case T_None:
210                 return _("No Time");
211
212           case T_NoDelim:
213                 return get_formatted_time ("%H%M");
214
215           case T_Delim:
216                 return get_formatted_time ("%H.%M");
217
218           default:
219                 return _("Invalid time format");
220         }
221 }
222
223 string
224 ExportFilename::get_date_format_str (DateFormat format) const
225 {
226         switch (format) {
227           case D_None:
228                 return _("No Date");
229
230           case D_BE:
231                 return get_formatted_time ("%Y%m%d");
232
233           case D_ISO:
234                 return get_formatted_time ("%Y-%m-%d");
235
236           case D_BEShortY:
237                 return get_formatted_time ("%y%m%d");
238
239           case D_ISOShortY:
240                 return get_formatted_time ("%y-%m-%d");
241
242           default:
243                 return _("Invalid date format");
244         }
245 }
246
247 void
248 ExportFilename::set_time_format (TimeFormat format)
249 {
250         time_format = format;
251
252         if (format == T_None) {
253                 include_time = false;
254         } else {
255                 include_time = true;
256         }
257 }
258
259 void
260 ExportFilename::set_date_format (DateFormat format)
261 {
262         date_format = format;
263
264         if (format == D_None) {
265                 include_date = false;
266         } else {
267                 include_date = true;
268         }
269 }
270
271 void
272 ExportFilename::set_label (string value)
273 {
274         label = value;
275         include_label = !value.compare ("");
276 }
277
278 bool
279 ExportFilename::set_folder (string path)
280 {
281         // TODO check folder existence
282         folder = path;
283         return true;
284 }
285
286 string
287 ExportFilename::get_formatted_time (string const & format) const
288 {
289         char buffer [80];
290         strftime (buffer, 80, format.c_str(), time_struct);
291
292         string return_value (buffer);
293         return return_value;
294 }
295
296 void
297 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
298 {
299         XMLNode * child = node->add_child ("Field");
300
301         if (!child) {
302                 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
303                 return;
304         }
305
306         child->add_property ("name", name);
307         child->add_property ("enabled", enabled ? "true" : "false");
308         if (!value.empty()) {
309                 child->add_property ("value", value);
310         }
311 }
312
313 ExportFilename::FieldPair
314 ExportFilename::get_field (XMLNode const & node, string const & name)
315 {
316         FieldPair pair;
317         pair.first = false;
318
319         XMLNodeList children = node.children();
320
321         for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
322                 XMLProperty * prop = (*it)->property ("name");
323                 if (prop && !prop->value().compare (name)) {
324
325                         prop = (*it)->property ("enabled");
326                         if (prop && !prop->value().compare ("true")) {
327                                 pair.first = true;
328                         } else {
329                                 pair.first = false;
330                         }
331
332                         prop = (*it)->property ("value");
333                         if (prop) {
334                                 pair.second = prop->value();
335                         }
336
337                         return pair;
338                 }
339         }
340
341         return pair;
342 }
343
344 ExportFilename::FieldPair
345 ExportFilename::analyse_folder ()
346 {
347         FieldPair pair;
348
349         string session_dir = session.session_directory().root_path().to_string();
350         string::size_type session_dir_len = session_dir.length();
351
352         string folder_beginning = folder.substr (0, session_dir_len);
353
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;
360         }
361
362         return pair;
363 }
364
365 } // namespace ARDOUR