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