Factor out sequencing related things into an independant new library: "evoral".
[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 <set>
28
29 #include <boost/shared_ptr.hpp>
30 #include <boost/weak_ptr.hpp>
31 #include <sigc++/signal.h>
32 #include <glibmm/ustring.h>
33
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 using std::string;
42 using std::list;
43 using std::set;
44
45 namespace ARDOUR
46 {
47
48 class ExportHandler;
49 class ExportTimespan;
50 class ExportChannelConfiguration;
51 class ExportFormatSpecification;
52 class ExportFilename;
53 class Location;
54 class Session;
55
56 /// Manages (de)serialization of export profiles and related classes
57 class ExportProfileManager
58 {
59   public:
60         class Preset {
61           public:
62                 Preset (string filename, Session & s);
63                 ~Preset ();
64         
65                 uint32_t id () const { return _id; }
66                 string name () const { return _name; }
67         
68                 void set_name (string name);
69                 void set_id (uint32_t id);
70         
71                 // Note: The set_..._state functions take ownership of the XMLNode
72                 void set_global_state (XMLNode & state);
73                 void set_local_state (XMLNode & state);
74                 
75                 XMLNode const * get_global_state () const { return global.root(); }
76                 XMLNode const * get_local_state () const { return local; }
77                 
78                 void save () const;
79                 void remove_local () const;
80         
81           private:
82         
83                 XMLNode * get_instant_xml () const;
84                 void save_instant_xml () const;
85                 void remove_instant_xml () const;
86         
87                 uint32_t   _id;
88                 string     _name;
89         
90                 Session &   session;
91                 XMLTree     global;
92                 XMLNode *   local;
93                 
94         };
95
96         typedef boost::shared_ptr<Preset> PresetPtr;
97         typedef std::list<PresetPtr> PresetList;
98
99   public:
100
101         ExportProfileManager (Session & s);
102         ~ExportProfileManager ();
103
104         void load_profile ();
105         void prepare_for_export ();
106         
107         PresetList const & get_presets () { return preset_list; }
108         void load_preset (PresetPtr preset);
109         PresetPtr save_preset (string const & name);
110         void remove_preset ();
111
112   private:
113         typedef boost::shared_ptr<ExportHandler> HandlerPtr;
114
115         typedef std::pair<uint32_t, PBD::sys::path> FilePair;
116         typedef std::map<uint32_t, PBD::sys::path> FileMap;
117         
118         HandlerPtr  handler;
119         Session &   session;
120         
121         void load_presets ();
122         uint32_t load_preset_from_disk (PBD::sys::path const & path); // Returns preset id
123         
124         void set_state (XMLNode const & root);
125         void set_global_state (XMLNode const & root);
126         void set_local_state (XMLNode const & root);
127         
128         void serialize_profile (XMLNode & root);
129         void serialize_global_profile (XMLNode & root);
130         void serialize_local_profile (XMLNode & root);
131         
132         PresetList preset_list;
133         PresetPtr  current_preset;
134         uint32_t   preset_id_counter;
135         FileMap    preset_file_map;
136         
137         std::vector<PBD::sys::path> find_file (std::string const & pattern);
138         
139         PBD::sys::path  export_config_dir;
140         PBD::SearchPath search_path;
141
142 /* Timespans */
143   public:
144
145         typedef boost::shared_ptr<ExportTimespan> TimespanPtr;
146         typedef std::list<TimespanPtr> TimespanList;
147         typedef boost::shared_ptr<TimespanList> TimespanListPtr;
148         typedef std::list<Location *> LocationList;
149
150         enum TimeFormat {
151                 SMPTE,
152                 BBT,
153                 MinSec,
154                 Frames,
155                 Off
156         };
157
158         struct TimespanState {
159                 TimespanListPtr timespans;
160                 TimeFormat      time_format;
161                 
162                 boost::shared_ptr<Location> session_range;
163                 boost::shared_ptr<Location> selection_range;
164                 boost::shared_ptr<LocationList> ranges;
165                 
166                 TimespanState (boost::shared_ptr<Location> session_range,
167                                boost::shared_ptr<Location> selection_range,
168                                boost::shared_ptr<LocationList> ranges) :
169                   timespans (new TimespanList ()),
170                   time_format (SMPTE),
171                 
172                   session_range (session_range),
173                   selection_range (selection_range),
174                   ranges (ranges)
175                 {}
176         };
177         
178         typedef boost::shared_ptr<TimespanState> TimespanStatePtr;
179         typedef std::list<TimespanStatePtr> TimespanStateList;
180         
181         void set_selection_range (nframes_t start = 0, nframes_t end = 0);
182         TimespanStateList const & get_timespans () { return timespans; }
183         
184   private:
185
186         TimespanStateList timespans;
187
188         void init_timespans (XMLNodeList nodes);
189         
190         TimespanStatePtr deserialize_timespan (XMLNode & root);
191         XMLNode & serialize_timespan (TimespanStatePtr state);
192         
193         /* Locations */
194         
195         void update_ranges ();
196         
197         boost::shared_ptr<Location>     session_range;
198         boost::shared_ptr<Location>     selection_range;
199         boost::shared_ptr<LocationList> ranges;
200
201 /* Channel Configs */
202   public:
203
204         typedef boost::shared_ptr<ExportChannelConfiguration> ChannelConfigPtr;
205
206         struct ChannelConfigState {
207                 ChannelConfigPtr config;
208                 
209                 ChannelConfigState (ChannelConfigPtr ptr) : config (ptr) {}
210         };
211         typedef boost::shared_ptr<ChannelConfigState> ChannelConfigStatePtr;
212         typedef std::list<ChannelConfigStatePtr> ChannelConfigStateList;
213         
214         ChannelConfigStateList const & get_channel_configs () { return channel_configs; }
215
216   private:
217
218         ChannelConfigStateList channel_configs;
219
220         void init_channel_configs (XMLNodeList nodes);
221
222         ChannelConfigStatePtr deserialize_channel_config (XMLNode & root);
223         XMLNode & serialize_channel_config (ChannelConfigStatePtr state);
224
225 /* Formats */
226   public:
227
228         typedef boost::shared_ptr<ExportFormatSpecification> FormatPtr;
229         typedef std::list<FormatPtr> FormatList;
230
231         struct FormatState {
232                 boost::shared_ptr<FormatList const> list;
233                 FormatPtr                           format;
234                 
235                 FormatState (boost::shared_ptr<FormatList const> list, FormatPtr format) :
236                   list (list), format (format) {}
237         };
238         typedef boost::shared_ptr<FormatState> FormatStatePtr;
239         typedef std::list<FormatStatePtr> FormatStateList;
240         
241         FormatStateList const & get_formats () { return formats; }
242         FormatStatePtr duplicate_format_state (FormatStatePtr state);
243         void remove_format_state (FormatStatePtr state);
244         
245         PBD::sys::path save_format_to_disk (FormatPtr format);
246         void remove_format_profile (FormatPtr format);
247         FormatPtr get_new_format (FormatPtr original);
248         
249         sigc::signal<void> FormatListChanged;
250
251   private:
252
253         FormatStateList formats;
254
255         void init_formats (XMLNodeList nodes);
256         FormatStatePtr deserialize_format (XMLNode & root);
257         XMLNode & serialize_format (FormatStatePtr state);
258
259         void load_formats ();
260         
261         FormatPtr load_format (XMLNode & node);
262         void load_format_from_disk (PBD::sys::path const & path);
263
264         boost::shared_ptr<FormatList> format_list;
265         FileMap                       format_file_map;
266         
267 /* Filenames */
268   public:
269         
270         typedef boost::shared_ptr<ExportFilename> FilenamePtr;
271         
272         struct FilenameState {
273                 FilenamePtr  filename;
274                 
275                 FilenameState (FilenamePtr ptr) : filename (ptr) {}
276         };
277         typedef boost::shared_ptr<FilenameState> FilenameStatePtr;
278         typedef std::list<FilenameStatePtr> FilenameStateList;
279         
280         FilenameStateList const & get_filenames () { return filenames; }
281         FilenameStatePtr duplicate_filename_state (FilenameStatePtr state);
282         void remove_filename_state (FilenameStatePtr state);
283
284   private:
285
286         FilenameStateList filenames;
287         
288         void init_filenames (XMLNodeList nodes);
289         
290         FilenameStatePtr deserialize_filename (XMLNode & root);
291         XMLNode & serialize_filename (FilenameStatePtr state);
292
293         FilenamePtr load_filename (XMLNode & node);
294
295 /* Warnings */
296   public:
297         struct Warnings {
298                 std::list<Glib::ustring> errors;
299                 std::list<Glib::ustring> warnings;
300                 std::list<Glib::ustring> conflicting_filenames;
301         };
302         
303         boost::shared_ptr<Warnings> get_warnings ();
304         
305   private:
306         void check_config (boost::shared_ptr<Warnings> warnings,
307                            TimespanStatePtr timespan_state,
308                            ChannelConfigStatePtr channel_config_state,
309                            FormatStatePtr format_state,
310                            FilenameStatePtr filename_state);
311 };
312
313
314 } // namespace ARDOUR
315
316 #endif /* __ardour_export_profile_manager_h__ */