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