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