include timespan-option with export preset
[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
23 #include <glibmm/miscutils.h>
24 #include <glibmm/fileutils.h>
25
26 #include "pbd/xml++.h"
27 #include "pbd/convert.h"
28 #include "pbd/enumwriter.h"
29
30 #include "ardour/libardour_visibility.h"
31 #include "ardour/session.h"
32 #include "ardour/session_directory.h"
33 #include "ardour/export_filename.h"
34 #include "ardour/export_format_specification.h"
35 #include "ardour/export_channel_configuration.h"
36 #include "ardour/export_timespan.h"
37 #include "ardour/utils.h"
38
39 #include "i18n.h"
40
41 using namespace PBD;
42 using namespace Glib;
43 using std::string;
44
45 namespace ARDOUR
46 {
47
48 ExportFilename::ExportFilename (Session & session) :
49   include_label (false),
50   include_session (false),
51   include_revision (false),
52   include_channel_config (false),
53   include_format_name (false),
54   include_channel (false),
55   include_timespan (true), // Include timespan name always
56   include_time (false),
57   include_date (false),
58   session (session),
59   revision (1),
60   date_format (D_None),
61   time_format (T_None)
62 {
63         time_t rawtime;
64         std::time (&rawtime);
65         time_struct = localtime (&rawtime);
66
67         folder = session.session_directory().export_path();
68
69         XMLNode * extra_node = session.extra_xml ("ExportFilename");
70         /* Legacy sessions used Session instant.xml for this */
71         if (!extra_node) {
72                 session.instant_xml ("ExportFilename");
73         }
74
75         if (extra_node) {
76                 set_state (*extra_node);
77         }
78 }
79
80 XMLNode &
81 ExportFilename::get_state ()
82 {
83         XMLNode * node = new XMLNode ("ExportFilename");
84         XMLNode * child;
85
86         FieldPair dir = analyse_folder();
87         child = node->add_child ("Folder");
88         child->add_property ("relative", dir.first ? "true" : "false");
89         child->add_property ("path", dir.second);
90
91         add_field (node, "label", include_label, label);
92         add_field (node, "session", include_session);
93         add_field (node, "timespan", include_timespan);
94         add_field (node, "revision", include_revision);
95         add_field (node, "time", include_time, enum_2_string (time_format));
96         add_field (node, "date", include_date, enum_2_string (date_format));
97
98         XMLNode * extra_node = new XMLNode ("ExportRevision");
99         extra_node->add_property ("revision", to_string (revision, std::dec));
100         session.add_extra_xml (*extra_node);
101
102         return *node;
103 }
104
105 int
106 ExportFilename::set_state (const XMLNode & node)
107 {
108         XMLNode * child;
109         XMLProperty * prop;
110         FieldPair pair;
111
112         child = node.child ("Folder");
113         if (!child) { return -1; }
114
115         folder = "";
116
117         if ((prop = child->property ("relative"))) {
118                 if (string_is_affirmative (prop->value())) {
119                         folder = session.session_directory().root_path();
120                 }
121         }
122
123         if ((prop = child->property ("path"))) {
124                 std::string tmp;
125                 tmp = Glib::build_filename (folder, prop->value());
126                 if (!Glib::file_test (tmp, Glib::FILE_TEST_EXISTS)) {
127                         warning << string_compose (_("Existing export folder for this session (%1) does not exist - ignored"), tmp) << endmsg;
128                 } else {
129                         folder = tmp;
130                 }
131         }
132
133         if (folder.empty()) {
134                 folder = session.session_directory().export_path();
135         }
136
137         pair = get_field (node, "label");
138         include_label = pair.first;
139         label = pair.second;
140
141         pair = get_field (node, "session");
142         include_session = pair.first;
143
144         pair = get_field (node, "timespan");
145         include_timespan = pair.first;
146
147         pair = get_field (node, "revision");
148         include_revision = pair.first;
149
150         pair = get_field (node, "time");
151         include_time = pair.first;
152         time_format = (TimeFormat) string_2_enum (pair.second, time_format);
153
154         pair = get_field (node, "date");
155         include_date = pair.first;
156         date_format = (DateFormat) string_2_enum (pair.second, date_format);
157
158         XMLNode * extra_node = session.extra_xml ("ExportRevision");
159         /* Legacy sessions used Session instant.xml for this */
160         if (!extra_node) {
161                 extra_node = session.instant_xml ("ExportRevision");
162         }
163
164         if (extra_node && (prop = extra_node->property ("revision"))) {
165                 revision = atoi (prop->value());
166         }
167
168         return 0;
169 }
170
171 string
172 ExportFilename::get_path (ExportFormatSpecPtr format) const
173 {
174         string path;
175         bool filename_empty = true;
176         bool with_timespan = include_timespan;
177
178         if (!include_session
179                         && !include_label
180                         && !include_revision
181                         && !include_timespan
182                         && !include_channel_config
183                         && !include_channel
184                         && !include_date
185                         && !include_format_name) {
186                 with_timespan = true;
187         }
188
189         if (include_session) {
190                 path += filename_empty ? "" : "_";
191                 path += session.name();
192                 filename_empty = false;
193         }
194
195         if (include_label) {
196                 path += filename_empty ? "" : "_";
197                 path += label;
198                 filename_empty = false;
199         }
200
201         if (include_revision) {
202                 path += filename_empty ? "" : "_";
203                 path += "r";
204                 path += to_string (revision, std::dec);
205                 filename_empty = false;
206         }
207
208         if (with_timespan && timespan) {
209                 path += filename_empty ? "" : "_";
210                 path += timespan->name();
211                 filename_empty = false;
212         }
213
214         if (include_channel_config && channel_config) {
215                 path += filename_empty ? "" : "_";
216                 path += channel_config->name();
217                 filename_empty = false;
218         }
219
220         if (include_channel) {
221                 path += filename_empty ? "" : "_";
222                 path += "channel";
223                 path += to_string (channel, std::dec);
224                 filename_empty = false;
225         }
226
227         if (include_date) {
228                 path += filename_empty ? "" : "_";
229                 path += get_date_format_str (date_format);
230                 filename_empty = false;
231         }
232
233         if (include_time) {
234                 path += filename_empty ? "" : "_";
235                 path += get_time_format_str (time_format);
236                 filename_empty = false;
237         }
238
239         if (include_format_name) {
240                 path += filename_empty ? "" : "_";
241                 path += format->name();
242                 filename_empty = false;
243         }
244
245         if (path.empty ()) {
246                 path = "export";
247         }
248
249         path += ".";
250         path += format->extension ();
251
252         path = legalize_for_universal_path (path);
253
254         return Glib::build_filename (folder, path);
255 }
256
257 string
258 ExportFilename::get_time_format_str (TimeFormat format) const
259 {
260         switch ( format ) {
261           case T_None:
262                 return _("No Time");
263
264           case T_NoDelim:
265                 return get_formatted_time ("%H%M");
266
267           case T_Delim:
268                 return get_formatted_time ("%H.%M");
269
270           default:
271                 return _("Invalid time format");
272         }
273 }
274
275 string
276 ExportFilename::get_date_format_str (DateFormat format) const
277 {
278         switch (format) {
279           case D_None:
280                 return _("No Date");
281
282           case D_BE:
283                 return get_formatted_time ("%Y%m%d");
284
285           case D_ISO:
286                 return get_formatted_time ("%Y-%m-%d");
287
288           case D_BEShortY:
289                 return get_formatted_time ("%y%m%d");
290
291           case D_ISOShortY:
292                 return get_formatted_time ("%y-%m-%d");
293
294           default:
295                 return _("Invalid date format");
296         }
297 }
298
299 void
300 ExportFilename::set_time_format (TimeFormat format)
301 {
302         time_format = format;
303
304         if (format == T_None) {
305                 include_time = false;
306         } else {
307                 include_time = true;
308         }
309 }
310
311 void
312 ExportFilename::set_date_format (DateFormat format)
313 {
314         date_format = format;
315
316         if (format == D_None) {
317                 include_date = false;
318         } else {
319                 include_date = true;
320         }
321 }
322
323 void
324 ExportFilename::set_label (string value)
325 {
326         label = value;
327         include_label = !value.compare ("");
328 }
329
330 bool
331 ExportFilename::set_folder (string path)
332 {
333         // TODO check folder existence
334         folder = path;
335         return true;
336 }
337
338 string
339 ExportFilename::get_formatted_time (string const & format) const
340 {
341         char buffer [80];
342         strftime (buffer, 80, format.c_str(), time_struct);
343
344         string return_value (buffer);
345         return return_value;
346 }
347
348 void
349 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
350 {
351         XMLNode * child = node->add_child ("Field");
352
353         if (!child) {
354                 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
355                 return;
356         }
357
358         child->add_property ("name", name);
359         child->add_property ("enabled", enabled ? "true" : "false");
360         if (!value.empty()) {
361                 child->add_property ("value", value);
362         }
363 }
364
365 ExportFilename::FieldPair
366 ExportFilename::get_field (XMLNode const & node, string const & name)
367 {
368         FieldPair pair;
369         pair.first = false;
370
371         XMLNodeList children = node.children();
372
373         for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
374                 XMLProperty * prop = (*it)->property ("name");
375                 if (prop && !prop->value().compare (name)) {
376
377                         prop = (*it)->property ("enabled");
378                         if (prop && !prop->value().compare ("true")) {
379                                 pair.first = true;
380                         } else {
381                                 pair.first = false;
382                         }
383
384                         prop = (*it)->property ("value");
385                         if (prop) {
386                                 pair.second = prop->value();
387                         }
388
389                         return pair;
390                 }
391         }
392
393         return pair;
394 }
395
396 ExportFilename::FieldPair
397 ExportFilename::analyse_folder ()
398 {
399         FieldPair pair;
400
401         string session_dir = session.session_directory().root_path();
402         string::size_type session_dir_len = session_dir.length();
403
404         string folder_beginning = folder.substr (0, session_dir_len);
405
406         if (!folder_beginning.compare (session_dir)) {
407                 pair.first = true;
408                 pair.second = folder.substr (session_dir_len);
409         } else {
410                 pair.first = false;
411                 pair.second = folder;
412         }
413
414         return pair;
415 }
416
417 } // namespace ARDOUR