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