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