fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / ardour / export_profile_manager.h
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 #ifndef __ardour_export_profile_manager_h__
22 #define __ardour_export_profile_manager_h__
23
24 #include <list>
25 #include <vector>
26 #include <map>
27 #include <stdexcept>
28 #include <string>
29
30 #include <boost/shared_ptr.hpp>
31
32 #include "pbd/uuid.h"
33 #include "pbd/file_utils.h"
34 #include "pbd/xml++.h"
35
36 #include "ardour/filesystem_paths.h"
37 #include "ardour/location.h"
38 #include "ardour/libardour_visibility.h"
39 #include "ardour/types.h"
40 #include "ardour/export_handler.h"
41
42 namespace ARDOUR
43 {
44
45 class ExportHandler;
46 class Location;
47 class Session;
48
49 /// Manages (de)serialization of export profiles and related classes
50 class LIBARDOUR_API ExportProfileManager
51 {
52   public:
53
54         enum ExportType {
55                 RegularExport,
56                 RangeExport,
57                 SelectionExport,
58                 RegionExport,
59                 StemExport
60         };
61
62         ExportProfileManager (Session & s, ExportType type);
63         ~ExportProfileManager ();
64
65         void load_profile ();
66         void prepare_for_export ();
67
68         ExportType type () const { return _type; }
69
70         typedef std::list<ExportPresetPtr> PresetList;
71
72         PresetList const & get_presets () { return preset_list; }
73         bool load_preset (ExportPresetPtr preset);
74         ExportPresetPtr new_preset (std::string const & name);
75         ExportPresetPtr save_preset (std::string const & name);
76         void remove_preset ();
77
78   private:
79         typedef boost::shared_ptr<ExportHandler> HandlerPtr;
80
81         typedef std::pair<PBD::UUID, std::string> FilePair;
82         typedef std::map<PBD::UUID, std::string> FileMap;
83
84         ExportType  _type;
85         std::string xml_node_name;
86         HandlerPtr  handler;
87         Session &   session;
88
89         std::string preset_filename (std::string const & preset_name);
90         void load_presets ();
91         void load_preset_from_disk (std::string const & path);
92
93         bool set_state (XMLNode const & root);
94         bool set_global_state (XMLNode const & root);
95         bool set_local_state (XMLNode const & root);
96
97         void serialize_profile (XMLNode & root);
98         void serialize_global_profile (XMLNode & root);
99         void serialize_local_profile (XMLNode & root);
100
101         PresetList      preset_list;
102         ExportPresetPtr current_preset;
103         FileMap         preset_file_map;
104
105         std::vector<std::string> find_file (std::string const & pattern);
106
107         std::string  export_config_dir;
108         PBD::Searchpath search_path;
109
110 /* Timespans */
111   public:
112
113         typedef std::list<ExportTimespanPtr> TimespanList;
114         typedef boost::shared_ptr<TimespanList> TimespanListPtr;
115         typedef std::list<Location *> LocationList;
116
117         enum TimeFormat {
118                 Timecode,
119                 BBT,
120                 MinSec,
121                 Frames,
122         };
123
124         struct TimespanState {
125                 TimespanListPtr timespans;
126                 TimeFormat      time_format;
127
128                 boost::shared_ptr<Location> selection_range;
129                 boost::shared_ptr<LocationList> ranges;
130
131                 TimespanState (boost::shared_ptr<Location> selection_range,
132                                boost::shared_ptr<LocationList> ranges)
133                   : timespans (new TimespanList ())
134                   , time_format (Timecode)
135                   , selection_range (selection_range)
136                   , ranges (ranges)
137                 {}
138         };
139
140         typedef boost::shared_ptr<TimespanState> TimespanStatePtr;
141         typedef std::list<TimespanStatePtr> TimespanStateList;
142
143         void set_selection_range (framepos_t start = 0, framepos_t end = 0);
144         std::string set_single_range (framepos_t start, framepos_t end, std::string name);
145         TimespanStateList const & get_timespans () { return check_list (timespans); }
146
147   private:
148
149         TimespanStateList timespans;
150
151         bool init_timespans (XMLNodeList nodes);
152
153         TimespanStatePtr deserialize_timespan (XMLNode & root);
154         XMLNode & serialize_timespan (TimespanStatePtr state);
155
156         /* Locations */
157
158         void update_ranges ();
159
160         boost::shared_ptr<Location>     selection_range;
161         boost::shared_ptr<LocationList> ranges;
162
163         bool                            single_range_mode;
164         boost::shared_ptr<Location>     single_range;
165
166 /* Channel Configs */
167   public:
168
169         struct ChannelConfigState {
170                 ExportChannelConfigPtr config;
171
172                 ChannelConfigState (ExportChannelConfigPtr ptr) : config (ptr) {}
173         };
174         typedef boost::shared_ptr<ChannelConfigState> ChannelConfigStatePtr;
175         typedef std::list<ChannelConfigStatePtr> ChannelConfigStateList;
176
177         ChannelConfigStateList const & get_channel_configs () { return check_list (channel_configs); }
178         void clear_channel_configs () { channel_configs.clear(); }
179         ChannelConfigStatePtr add_channel_config ();
180
181   private:
182
183         ChannelConfigStateList channel_configs;
184
185         bool init_channel_configs (XMLNodeList nodes);
186
187 /* Formats */
188   public:
189
190         typedef std::list<ExportFormatSpecPtr> FormatList;
191
192         struct FormatState {
193                 boost::shared_ptr<FormatList const> list;
194                 ExportFormatSpecPtr                     format;
195
196                 FormatState (boost::shared_ptr<FormatList const> list, ExportFormatSpecPtr format) :
197                   list (list), format (format) {}
198         };
199         typedef boost::shared_ptr<FormatState> FormatStatePtr;
200         typedef std::list<FormatStatePtr> FormatStateList;
201
202         FormatStateList const & get_formats () { return check_list (formats); }
203         FormatStatePtr duplicate_format_state (FormatStatePtr state);
204         void remove_format_state (FormatStatePtr state);
205
206         std::string save_format_to_disk (ExportFormatSpecPtr format);
207         void remove_format_profile (ExportFormatSpecPtr format);
208         ExportFormatSpecPtr get_new_format (ExportFormatSpecPtr original);
209
210         PBD::Signal0<void> FormatListChanged;
211
212   private:
213
214         FormatStateList formats;
215
216         bool init_formats (XMLNodeList nodes);
217         FormatStatePtr deserialize_format (XMLNode & root);
218         XMLNode & serialize_format (FormatStatePtr state);
219
220         void load_formats ();
221
222         ExportFormatSpecPtr load_format (XMLNode & node);
223         void load_format_from_disk (std::string const & path);
224
225         boost::shared_ptr<FormatList> format_list;
226         FileMap                       format_file_map;
227
228 /* Filenames */
229   public:
230
231         struct FilenameState {
232                 ExportFilenamePtr  filename;
233
234                 FilenameState (ExportFilenamePtr ptr) : filename (ptr) {}
235         };
236         typedef boost::shared_ptr<FilenameState> FilenameStatePtr;
237         typedef std::list<FilenameStatePtr> FilenameStateList;
238
239         FilenameStateList const & get_filenames () { return check_list (filenames); }
240         FilenameStatePtr duplicate_filename_state (FilenameStatePtr state);
241         void remove_filename_state (FilenameStatePtr state);
242
243         std::string get_sample_filename_for_format (ExportFilenamePtr filename, ExportFormatSpecPtr format);
244
245   private:
246
247         FilenameStateList filenames;
248
249         bool init_filenames (XMLNodeList nodes);
250         ExportFilenamePtr load_filename (XMLNode & node);
251
252 /* Warnings */
253   public:
254         struct Warnings {
255                 std::list<std::string> errors;
256                 std::list<std::string> warnings;
257                 std::list<std::string> conflicting_filenames;
258         };
259
260         boost::shared_ptr<Warnings> get_warnings ();
261
262   private:
263         void check_config (boost::shared_ptr<Warnings> warnings,
264                            TimespanStatePtr timespan_state,
265                            ChannelConfigStatePtr channel_config_state,
266                            FormatStatePtr format_state,
267                            FilenameStatePtr filename_state);
268
269         bool check_format (ExportFormatSpecPtr format, uint32_t channels);
270         bool check_sndfile_format (ExportFormatSpecPtr format, unsigned int channels);
271
272  /* Utilities */
273
274         void build_filenames(std::list<std::string> & result, ExportFilenamePtr filename,
275                              TimespanListPtr timespans, ExportChannelConfigPtr channel_config,
276                              ExportFormatSpecPtr format);
277
278         /* Element state lists should never be empty, this is used to check them */
279         template<typename T>
280         std::list<T> const &
281         check_list (std::list<T> const & list)
282         {
283                 if (list.empty()) {
284                         throw std::runtime_error ("Programming error: Uninitialized list in ExportProfileManager");
285                 }
286                 return list;
287         }
288
289 };
290
291
292 } // namespace ARDOUR
293
294 #endif /* __ardour_export_profile_manager_h__ */