Remove most using declarations from header files.
[ardour.git] / libs / ardour / export_profile_manager.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 "ardour/export_profile_manager.h"
22
23 #include <cassert>
24 #include <stdexcept>
25
26 #include <glibmm/fileutils.h>
27
28 #include "pbd/enumwriter.h"
29 #include "pbd/xml++.h"
30 #include "pbd/convert.h"
31
32 #include "ardour/export_failed.h"
33 #include "ardour/export_file_io.h"
34 #include "ardour/export_format_specification.h"
35 #include "ardour/export_timespan.h"
36 #include "ardour/export_channel_configuration.h"
37 #include "ardour/export_filename.h"
38 #include "ardour/export_preset.h"
39 #include "ardour/export_handler.h"
40 #include "ardour/filename_extensions.h"
41 #include "ardour/session.h"
42
43 #include "i18n.h"
44
45 using namespace std;
46 using namespace Glib;
47 using namespace PBD;
48
49 namespace ARDOUR
50 {
51
52 ExportProfileManager::ExportProfileManager (Session & s) :
53   handler (s.get_export_handler()),
54   session (s),
55
56   session_range (new Location ()),
57   ranges (new LocationList ()),
58   single_range_mode (false),
59
60   format_list (new FormatList ())
61 {
62
63         /* Initialize path variables */
64
65         export_config_dir = user_config_directory();
66         export_config_dir /= "export";
67         search_path += export_config_dir;
68         
69         search_path += ardour_search_path().add_subdirectory_to_paths("export");
70         search_path += system_config_search_path().add_subdirectory_to_paths("export");;
71         
72         /* create export config directory if necessary */
73
74         if (!sys::exists (export_config_dir)) {
75                 sys::create_directory (export_config_dir);
76         }
77         
78         load_presets ();
79         load_formats ();
80         
81         /* Initialize all lists with an empty config */
82         
83         XMLNodeList dummy;
84         init_timespans (dummy);
85         init_channel_configs (dummy);
86         init_formats (dummy);
87         init_filenames (dummy);
88 }
89
90 ExportProfileManager::~ExportProfileManager ()
91 {
92         if (single_range_mode) { return; }
93         
94         XMLNode * instant_xml (new XMLNode ("ExportProfile"));
95         serialize_profile (*instant_xml);
96         session.add_instant_xml (*instant_xml, false);
97 }
98
99 void
100 ExportProfileManager::load_profile ()
101 {
102         XMLNode * instant_node = session.instant_xml ("ExportProfile");
103         if (instant_node) {
104                 set_state (*instant_node);
105         } else {
106                 XMLNode empty_node ("ExportProfile");
107                 set_state (empty_node);
108         }
109 }
110
111 void
112 ExportProfileManager::prepare_for_export ()
113 {
114         ChannelConfigPtr channel_config = channel_configs.front()->config;
115         TimespanListPtr ts_list = timespans.front()->timespans;
116         
117         FormatStateList::const_iterator format_it;
118         FilenameStateList::const_iterator filename_it;
119         
120         for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
121                 for (format_it = formats.begin(), filename_it = filenames.begin();
122                      format_it != formats.end() && filename_it != filenames.end();
123                      ++format_it, ++filename_it) {
124                 
125 //                      filename->include_timespan = (ts_list->size() > 1); Disabled for now...
126                         handler->add_export_config (*ts_it, channel_config, (*format_it)->format, (*filename_it)->filename);
127                 }
128         }
129 }
130
131 bool
132 ExportProfileManager::load_preset (PresetPtr preset)
133 {
134         bool ok = true;
135
136         current_preset = preset;
137         if (!preset) { return false; }
138
139         XMLNode const * state;
140         if ((state = preset->get_local_state())) {
141                 set_local_state (*state);
142         } else { ok = false; }
143         
144         if ((state = preset->get_global_state())) {
145                 if (!set_global_state (*state)) {
146                         ok = false;
147                 }
148         } else { ok = false; }
149         
150         return ok;
151 }
152
153 void
154 ExportProfileManager::load_presets ()
155 {
156         vector<sys::path> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
157
158         for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
159                 load_preset_from_disk (*it);
160         }
161 }
162
163 ExportProfileManager::PresetPtr
164 ExportProfileManager::save_preset (string const & name)
165 {
166         string filename = export_config_dir.to_string() + "/" + name + export_preset_suffix;
167
168         if (!current_preset) {
169                 current_preset.reset (new ExportPreset (filename, session));
170                 preset_list.push_back (current_preset);
171         }
172         
173         XMLNode * global_preset = new XMLNode ("ExportPreset");
174         XMLNode * local_preset = new XMLNode ("ExportPreset");
175
176         serialize_global_profile (*global_preset);
177         serialize_local_profile (*local_preset);
178
179         current_preset->set_name (name);
180         current_preset->set_global_state (*global_preset);
181         current_preset->set_local_state (*local_preset);
182         
183         current_preset->save (filename);
184         
185         return current_preset;
186 }
187
188 void
189 ExportProfileManager::remove_preset ()
190 {
191         if (!current_preset) { return; }
192
193         for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
194                 if (*it == current_preset) {
195                         preset_list.erase (it);
196                         break;
197                 }
198         }
199
200         FileMap::iterator it = preset_file_map.find (current_preset->id());
201         if (it != preset_file_map.end()) {
202                 sys::remove (it->second);
203                 preset_file_map.erase (it);
204         }
205         
206         current_preset->remove_local();
207         current_preset.reset();
208 }
209
210 void
211 ExportProfileManager::load_preset_from_disk (PBD::sys::path const & path)
212 {
213         PresetPtr preset (new ExportPreset (path.to_string(), session));
214         
215         /* Handle id to filename mapping and don't add duplicates to list */
216         
217         FilePair pair (preset->id(), path);
218         if (preset_file_map.insert (pair).second) {
219                 preset_list.push_back (preset);
220         }
221 }
222
223 bool
224 ExportProfileManager::set_state (XMLNode const & root)
225 {
226         return set_global_state (root) && set_local_state (root);
227 }
228
229 bool
230 ExportProfileManager::set_global_state (XMLNode const & root)
231 {
232         return init_filenames (root.children ("ExportFilename")) &&
233                init_formats (root.children ("ExportFormat"));
234 }
235
236 bool
237 ExportProfileManager::set_local_state (XMLNode const & root)
238 {
239         return init_timespans (root.children ("ExportTimespan")) &&
240                init_channel_configs (root.children ("ExportChannelConfiguration"));
241 }
242
243 void
244 ExportProfileManager::serialize_profile (XMLNode & root)
245 {
246         serialize_local_profile (root);
247         serialize_global_profile (root);
248 }
249
250 void
251 ExportProfileManager::serialize_global_profile (XMLNode & root)
252 {
253         for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
254                 root.add_child_nocopy (serialize_format (*it));
255         }
256
257         for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
258                 root.add_child_nocopy ((*it)->filename->get_state());
259         }
260 }
261
262 void
263 ExportProfileManager::serialize_local_profile (XMLNode & root)
264 {
265         for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
266                 root.add_child_nocopy (serialize_timespan (*it));
267         }
268         
269         for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
270                 root.add_child_nocopy ((*it)->config->get_state());
271         }
272 }
273
274 std::vector<sys::path>
275 ExportProfileManager::find_file (std::string const & pattern)
276 {
277         vector<sys::path> found;
278
279         Glib::PatternSpec pattern_spec (pattern);
280         find_matching_files_in_search_path (search_path, pattern_spec, found);
281
282         return found;
283 }
284
285 void
286 ExportProfileManager::set_selection_range (nframes_t start, nframes_t end)
287 {
288
289         if (start || end) {
290                 selection_range.reset (new Location());
291                 selection_range->set_name (_("Selection"));
292                 selection_range->set (start, end);
293         } else {
294                 selection_range.reset();
295         }
296         
297         for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
298                 (*it)->selection_range = selection_range;
299         }
300 }
301
302 std::string
303 ExportProfileManager::set_single_range (nframes_t start, nframes_t end, Glib::ustring name)
304 {
305         single_range_mode = true;
306         
307         single_range.reset (new Location());
308         single_range->set_name (name);
309         single_range->set (start, end);
310         
311         update_ranges ();
312         
313         return single_range->id().to_s();
314 }
315
316 bool
317 ExportProfileManager::init_timespans (XMLNodeList nodes)
318 {
319         timespans.clear ();
320         update_ranges ();
321         
322         bool ok = true;
323         for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
324                 TimespanStatePtr span = deserialize_timespan (**it);
325                 if (span) {
326                         timespans.push_back (span);
327                 } else { ok = false; }
328         }
329         
330         if (timespans.empty()) {
331                 TimespanStatePtr timespan (new TimespanState (session_range, selection_range, ranges));
332                 timespans.push_back (timespan);
333                 return false;
334         }
335         
336         return ok;
337 }
338
339 ExportProfileManager::TimespanStatePtr
340 ExportProfileManager::deserialize_timespan (XMLNode & root)
341 {
342         TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
343         XMLProperty const * prop;
344         
345         XMLNodeList spans = root.children ("Range");
346         for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
347                 
348                 prop = (*node_it)->property ("id");
349                 if (!prop) { continue; }
350                 ustring id = prop->value();
351         
352                 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
353                         if ((!id.compare ("session") && *it == session_range.get()) ||
354                             (!id.compare ("selection") && *it == selection_range.get()) ||
355                             (!id.compare ((*it)->id().to_s()))) {
356                                 TimespanPtr timespan = handler->add_timespan();
357                                 timespan->set_name ((*it)->name());
358                                 timespan->set_range_id (id);
359                                 timespan->set_range ((*it)->start(), (*it)->end());
360                                 state->timespans->push_back (timespan);
361                         }
362                 }
363         }
364         
365         if ((prop = root.property ("format"))) {
366                 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
367         }
368         
369         return state;
370 }
371
372 XMLNode &
373 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
374 {
375         XMLNode & root = *(new XMLNode ("ExportTimespan"));
376         XMLNode * span;
377         
378         update_ranges ();
379         
380         for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
381                 if ((span = root.add_child ("Range"))) {
382                         span->add_property ("id", (*it)->range_id());
383                 }
384         }
385         
386         root.add_property ("format", enum_2_string (state->time_format));
387         
388         return root;
389 }
390
391 void
392 ExportProfileManager::update_ranges () {
393         ranges->clear();
394         
395         if (single_range_mode) {
396                 ranges->push_back (single_range.get());
397                 return;
398         }
399
400         /* Session */
401
402         session_range->set_name (_("Session"));
403         session_range->set (session.current_start_frame(), session.current_end_frame());
404         ranges->push_back (session_range.get());
405         
406         /* Selection */
407         
408         if (selection_range) {
409                 ranges->push_back (selection_range.get());
410         }
411         
412         /* ranges */
413         
414         LocationList const & list (session.locations()->list());
415         for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
416                 if ((*it)->is_range_marker()) {
417                         ranges->push_back (*it);
418                 }
419         }
420 }
421
422 bool
423 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
424 {
425         channel_configs.clear();
426
427         if (nodes.empty()) {    
428                 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
429                 channel_configs.push_back (config);
430                 return false;
431         }
432         
433         for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
434                 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
435                 config->config->set_state (**it);
436                 channel_configs.push_back (config);
437         }
438         
439         return true;
440 }
441
442 ExportProfileManager::FormatStatePtr
443 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
444 {
445         /* Note: The pointer in the new FormatState should point to the same format spec
446                  as the original state's pointer. The spec itself should not be copied!   */
447
448         FormatStatePtr format (new FormatState (format_list, state->format));
449         formats.push_back (format);
450         return format;
451 }
452
453 void
454 ExportProfileManager::remove_format_state (FormatStatePtr state)
455 {
456         for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
457                 if (*it == state) {
458                         formats.erase (it);
459                         return;
460                 }
461         }
462 }
463
464 sys::path
465 ExportProfileManager::save_format_to_disk (FormatPtr format)
466 {
467         // TODO filename character stripping
468
469         /* Get filename for file */
470
471         Glib::ustring new_name = format->name();
472         new_name += export_format_suffix;
473         
474         sys::path new_path (export_config_dir);
475         new_path /= new_name;
476
477         /* Check if format is on disk already */
478         FileMap::iterator it;
479         if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
480                 
481                 /* Check if config is not in user config dir */
482                 if (it->second.branch_path().to_string().compare (export_config_dir.to_string())) {
483                 
484                         /* Write new file */
485                 
486                         XMLTree tree (new_path.to_string());
487                         tree.set_root (&format->get_state());
488                         tree.write();
489                 
490                 } else {
491                 
492                         /* Update file and rename if necessary */
493                 
494                         XMLTree tree (it->second.to_string());
495                         tree.set_root (&format->get_state());
496                         tree.write();
497                         
498                         if (new_name.compare (it->second.leaf())) {
499                                 sys::rename (it->second, new_path);
500                         }
501                 }
502                 
503                 it->second = new_path;
504                 
505         } else {
506                 /* Write new file */
507                 
508                 XMLTree tree (new_path.to_string());
509                 tree.set_root (&format->get_state());
510                 tree.write();
511         }
512         
513         FormatListChanged ();
514         return new_path;
515 }
516
517 void
518 ExportProfileManager::remove_format_profile (FormatPtr format)
519 {
520         for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
521                 if (*it == format) {
522                         format_list->erase (it);
523                         break;
524                 }
525         }
526
527         FileMap::iterator it = format_file_map.find (format->id());
528         if (it != format_file_map.end()) {
529                 sys::remove (it->second);
530                 format_file_map.erase (it);
531         }
532         
533         FormatListChanged ();
534 }
535
536 ExportProfileManager::FormatPtr
537 ExportProfileManager::get_new_format (FormatPtr original)
538 {       
539         FormatPtr format;
540         if (original) {
541                 format.reset (new ExportFormatSpecification (*original));
542         } else {
543                 format = handler->add_format();
544                 format->set_name ("empty format");
545         }
546         
547         sys::path path = save_format_to_disk (format);
548         FilePair pair (format->id(), path);
549         format_file_map.insert (pair);
550         
551         format_list->push_back (format);
552         FormatListChanged ();
553         
554         return format;
555 }
556
557 bool
558 ExportProfileManager::init_formats (XMLNodeList nodes)
559 {
560         formats.clear();
561
562         bool ok = true;
563         for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
564                 FormatStatePtr format = deserialize_format (**it);
565                 if (format) {
566                         formats.push_back (format);
567                 } else { ok = false; }
568         }
569         
570         if (formats.empty ()) {
571                 FormatStatePtr format (new FormatState (format_list, FormatPtr ()));
572                 formats.push_back (format);
573                 return false;
574         }
575         
576         return ok;
577 }
578
579 ExportProfileManager::FormatStatePtr
580 ExportProfileManager::deserialize_format (XMLNode & root)
581 {
582         XMLProperty * prop;
583         UUID id;
584         
585         if ((prop = root.property ("id"))) {
586                 id = prop->value();
587         }
588         
589         for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
590                 if ((*it)->id() == id) {
591                         return FormatStatePtr (new FormatState (format_list, *it));
592                 }
593         }
594
595         return FormatStatePtr ();
596 }
597
598 XMLNode &
599 ExportProfileManager::serialize_format (FormatStatePtr state)
600 {
601         XMLNode * root = new XMLNode ("ExportFormat");
602         
603         string id = state->format ? state->format->id().to_s() : "";
604         root->add_property ("id", id);
605         
606         return *root;
607 }
608
609 void
610 ExportProfileManager::load_formats ()
611 {
612         vector<sys::path> found = find_file (string_compose ("*%1", export_format_suffix));
613
614         for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
615                 load_format_from_disk (*it);
616         }
617 }
618
619 void
620 ExportProfileManager::load_format_from_disk (PBD::sys::path const & path)
621 {
622         XMLTree const tree (path.to_string());
623         FormatPtr format = handler->add_format (*tree.root());
624         
625         /* Handle id to filename mapping and don't add duplicates to list */
626         
627         FilePair pair (format->id(), path);
628         if (format_file_map.insert (pair).second) {
629                 format_list->push_back (format);
630         }
631         
632         FormatListChanged ();
633 }
634
635 ExportProfileManager::FilenameStatePtr
636 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
637 {
638         FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
639         filenames.push_back (filename);
640         return filename;
641 }
642
643 void
644 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
645 {
646         for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
647                 if (*it == state) {
648                         filenames.erase (it);
649                         return;
650                 }
651         }
652 }
653
654 bool
655 ExportProfileManager::init_filenames (XMLNodeList nodes)
656 {
657         filenames.clear ();
658
659         for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
660                 FilenamePtr filename = handler->add_filename();
661                 filename->set_state (**it);
662                 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
663         }
664         
665         if (filenames.empty()) {
666                 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
667                 filenames.push_back (filename);
668                 return false;
669         }
670         
671         return true;
672 }
673
674 boost::shared_ptr<ExportProfileManager::Warnings>
675 ExportProfileManager::get_warnings ()
676 {
677         boost::shared_ptr<Warnings> warnings (new Warnings ());
678
679         ChannelConfigStatePtr channel_config_state = channel_configs.front();
680         TimespanStatePtr timespan_state = timespans.front();
681         
682         /*** Check "global" config ***/
683         
684         TimespanListPtr timespans = timespan_state->timespans;
685         ChannelConfigPtr channel_config = channel_config_state->config;
686
687         /* Check Timespans are not empty */
688         
689         if (timespans->empty()) {
690                 warnings->errors.push_back (_("No timespan has been selected!"));
691         }
692
693         /* Check channel config ports */
694         
695         if (!channel_config->all_channels_have_ports ()) {
696                 warnings->warnings.push_back (_("Some channels are empty"));
697         }
698         
699         /*** Check files ***/
700         
701         FormatStateList::const_iterator format_it;
702         FilenameStateList::const_iterator filename_it;
703         for (format_it = formats.begin(), filename_it = filenames.begin();
704              format_it != formats.end() && filename_it != filenames.end();
705              ++format_it, ++filename_it) {
706                         check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
707         }
708         
709         return warnings;
710 }
711
712 void
713 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
714                                     TimespanStatePtr timespan_state,
715                                     ChannelConfigStatePtr channel_config_state,
716                                     FormatStatePtr format_state,
717                                     FilenameStatePtr filename_state)
718 {
719         TimespanListPtr timespans = timespan_state->timespans;
720         ChannelConfigPtr channel_config = channel_config_state->config;
721         FormatPtr format = format_state->format;
722         FilenamePtr filename = filename_state->filename;
723
724         /* Check format and maximum channel count */
725         if (!format || !format->type()) {
726                 warnings->errors.push_back (_("No format selected!"));
727         } else if (!channel_config->get_n_chans()) {
728                 warnings->errors.push_back (_("All channels are empty!"));
729         } else if (!ExportFileFactory::check (format, channel_config->get_n_chans())) {
730                 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
731         } else if (format->channel_limit() < channel_config->get_n_chans()) {
732                 warnings->errors.push_back
733                   (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
734                                      format->format_name(),
735                                      format->channel_limit(),
736                                      channel_config->get_n_chans()));
737         }
738         
739         if (!warnings->errors.empty()) { return; }
740         
741         /* Check filenames */
742         
743 //      filename->include_timespan = (timespans->size() > 1); Disabled for now...
744         
745         for (std::list<TimespanPtr>::iterator timespan_it = timespans->begin(); timespan_it != timespans->end(); ++timespan_it) {
746                 filename->set_timespan (*timespan_it);
747         
748                 if (channel_config->get_split()) {
749                         filename->include_channel = true;
750                         
751                         for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
752                                 filename->set_channel (chan);
753                                 
754                                 Glib::ustring path = filename->get_path (format);
755                                 
756                                 if (sys::exists (sys::path (path))) {
757                                         warnings->conflicting_filenames.push_back (path);
758                                 }
759                         }
760                         
761                 } else {
762                         Glib::ustring path = filename->get_path (format);
763                         
764                         if (sys::exists (sys::path (path))) {
765                                 warnings->conflicting_filenames.push_back (path);
766                         }
767                 }
768         }
769 }
770
771 }; // namespace ARDOUR