Fix yet another oversight for the windows icon file update
[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   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->add_property ("relative", dir.first ? "true" : "false");
91         child->add_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->add_property ("revision", to_string (revision, std::dec));
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         XMLProperty const * prop;
113         FieldPair pair;
114
115         child = node.child ("Folder");
116         if (!child) { return -1; }
117
118         folder = "";
119
120         if ((prop = child->property ("relative"))) {
121                 if (string_is_affirmative (prop->value())) {
122                         folder = session.session_directory().root_path();
123                 }
124         }
125
126         if ((prop = child->property ("path"))) {
127                 std::string tmp;
128                 tmp = Glib::build_filename (folder, prop->value());
129                 if (!Glib::file_test (tmp, Glib::FILE_TEST_EXISTS)) {
130                         warning << string_compose (_("Existing export folder for this session (%1) does not exist - ignored"), tmp) << endmsg;
131                 } else {
132                         folder = tmp;
133                 }
134         }
135
136         if (folder.empty()) {
137                 folder = session.session_directory().export_path();
138         }
139
140         pair = get_field (node, "label");
141         include_label = pair.first;
142         label = pair.second;
143
144         pair = get_field (node, "session");
145         include_session = pair.first;
146
147         pair = get_field (node, "snapshot");
148         use_session_snapshot_name = pair.first;
149
150         pair = get_field (node, "timespan");
151         include_timespan = pair.first;
152
153         pair = get_field (node, "revision");
154         include_revision = pair.first;
155
156         pair = get_field (node, "time");
157         include_time = pair.first;
158         time_format = (TimeFormat) string_2_enum (pair.second, time_format);
159
160         pair = get_field (node, "date");
161         include_date = pair.first;
162         date_format = (DateFormat) string_2_enum (pair.second, date_format);
163
164         XMLNode * extra_node = session.extra_xml ("ExportRevision");
165         /* Legacy sessions used Session instant.xml for this */
166         if (!extra_node) {
167                 extra_node = session.instant_xml ("ExportRevision");
168         }
169
170         if (extra_node && (prop = extra_node->property ("revision"))) {
171                 revision = atoi (prop->value());
172         }
173
174         return 0;
175 }
176
177 string
178 ExportFilename::get_path (ExportFormatSpecPtr format) const
179 {
180         string path;
181         bool filename_empty = true;
182         bool with_timespan = include_timespan;
183
184         if (!include_session
185                         && !include_label
186                         && !include_revision
187                         && !include_timespan
188                         && !include_channel_config
189                         && !include_channel
190                         && !include_date
191                         && !include_format_name) {
192                 with_timespan = true;
193         }
194
195         if (include_session) {
196                 path += filename_empty ? "" : "_";
197                 if (use_session_snapshot_name) {
198                         path += session.snap_name();
199                 } else {
200                         path += session.name();
201                 }
202                 filename_empty = false;
203         }
204
205         if (include_label) {
206                 path += filename_empty ? "" : "_";
207                 path += label;
208                 filename_empty = false;
209         }
210
211         if (include_revision) {
212                 path += filename_empty ? "" : "_";
213                 path += "r";
214                 path += to_string (revision, std::dec);
215                 filename_empty = false;
216         }
217
218         if (with_timespan && timespan) {
219                 path += filename_empty ? "" : "_";
220                 path += timespan->name();
221                 filename_empty = false;
222         }
223
224         if (include_channel_config && channel_config) {
225                 path += filename_empty ? "" : "_";
226                 path += channel_config->name();
227                 filename_empty = false;
228         }
229
230         if (include_channel) {
231                 path += filename_empty ? "" : "_";
232                 path += "channel";
233                 path += to_string (channel, std::dec);
234                 filename_empty = false;
235         }
236
237         if (include_date) {
238                 path += filename_empty ? "" : "_";
239                 path += get_date_format_str (date_format);
240                 filename_empty = false;
241         }
242
243         if (include_time) {
244                 path += filename_empty ? "" : "_";
245                 path += get_time_format_str (time_format);
246                 filename_empty = false;
247         }
248
249         if (include_format_name) {
250                 path += filename_empty ? "" : "_";
251                 path += format->name();
252                 filename_empty = false;
253         }
254
255         if (path.empty ()) {
256                 path = "export";
257         }
258
259         path += ".";
260         path += format->extension ();
261
262         path = legalize_for_universal_path (path);
263
264         return Glib::build_filename (folder, path);
265 }
266
267 string
268 ExportFilename::get_time_format_str (TimeFormat format) const
269 {
270         switch ( format ) {
271           case T_None:
272                 return _("No Time");
273
274           case T_NoDelim:
275                 return get_formatted_time ("%H%M");
276
277           case T_Delim:
278                 return get_formatted_time ("%H.%M");
279
280           default:
281                 return _("Invalid time format");
282         }
283 }
284
285 string
286 ExportFilename::get_date_format_str (DateFormat format) const
287 {
288         switch (format) {
289           case D_None:
290                 return _("No Date");
291
292           case D_BE:
293                 return get_formatted_time ("%Y%m%d");
294
295           case D_ISO:
296                 return get_formatted_time ("%Y-%m-%d");
297
298           case D_BEShortY:
299                 return get_formatted_time ("%y%m%d");
300
301           case D_ISOShortY:
302                 return get_formatted_time ("%y-%m-%d");
303
304           default:
305                 return _("Invalid date format");
306         }
307 }
308
309 void
310 ExportFilename::set_time_format (TimeFormat format)
311 {
312         time_format = format;
313
314         if (format == T_None) {
315                 include_time = false;
316         } else {
317                 include_time = true;
318         }
319 }
320
321 void
322 ExportFilename::set_date_format (DateFormat format)
323 {
324         date_format = format;
325
326         if (format == D_None) {
327                 include_date = false;
328         } else {
329                 include_date = true;
330         }
331 }
332
333 void
334 ExportFilename::set_label (string value)
335 {
336         label = value;
337         include_label = !value.compare ("");
338 }
339
340 bool
341 ExportFilename::set_folder (string path)
342 {
343         // TODO check folder existence
344         folder = path;
345         return true;
346 }
347
348 string
349 ExportFilename::get_formatted_time (string const & format) const
350 {
351         char buffer [80];
352         strftime (buffer, 80, format.c_str(), &time_struct);
353
354         string return_value (buffer);
355         return return_value;
356 }
357
358 void
359 ExportFilename::add_field (XMLNode * node, string const & name, bool enabled, string const & value)
360 {
361         XMLNode * child = node->add_child ("Field");
362
363         if (!child) {
364                 std::cerr << "Error adding a field to ExportFilename XML-tree" << std::endl;
365                 return;
366         }
367
368         child->add_property ("name", name);
369         child->add_property ("enabled", enabled ? "true" : "false");
370         if (!value.empty()) {
371                 child->add_property ("value", value);
372         }
373 }
374
375 ExportFilename::FieldPair
376 ExportFilename::get_field (XMLNode const & node, string const & name)
377 {
378         FieldPair pair;
379         pair.first = false;
380
381         XMLNodeList children = node.children();
382
383         for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
384                 XMLProperty const * prop = (*it)->property ("name");
385                 if (prop && !prop->value().compare (name)) {
386
387                         prop = (*it)->property ("enabled");
388                         if (prop && !prop->value().compare ("true")) {
389                                 pair.first = true;
390                         } else {
391                                 pair.first = false;
392                         }
393
394                         prop = (*it)->property ("value");
395                         if (prop) {
396                                 pair.second = prop->value();
397                         }
398
399                         return pair;
400                 }
401         }
402
403         return pair;
404 }
405
406 ExportFilename::FieldPair
407 ExportFilename::analyse_folder ()
408 {
409         FieldPair pair;
410
411         string session_dir = session.session_directory().root_path();
412         string::size_type session_dir_len = session_dir.length();
413
414         string folder_beginning = folder.substr (0, session_dir_len);
415
416         if (!folder_beginning.compare (session_dir)) {
417                 pair.first = true;
418                 pair.second = folder.substr (session_dir_len);
419         } else {
420                 pair.first = false;
421                 pair.second = folder;
422         }
423
424         return pair;
425 }
426
427 } // namespace ARDOUR