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