rename join regions op as combine regions; save and restore nested playlists, sources...
[ardour.git] / libs / ardour / playlist_source.cc
1 /*
2     Copyright (C) 2011 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #ifdef WAF_BUILD
20 #include "libardour-config.h"
21 #endif
22
23 #include <vector>
24 #include <cstdio>
25
26 #include <glibmm/fileutils.h>
27 #include <glibmm/miscutils.h>
28
29 #include "pbd/error.h"
30 #include "pbd/convert.h"
31 #include "pbd/enumwriter.h"
32
33 #include "ardour/playlist.h"
34 #include "ardour/playlist_source.h"
35 #include "ardour/playlist_factory.h"
36 #include "ardour/session.h"
37 #include "ardour/session_playlists.h"
38 #include "ardour/source_factory.h"
39
40 #include "i18n.h"
41
42 using namespace std;
43 using namespace ARDOUR;
44 using namespace PBD;
45
46 PlaylistSource::PlaylistSource (Session& s, const std::string& name, boost::shared_ptr<Playlist> p, DataType type,
47                                 frameoffset_t begin, framecnt_t len, Source::Flag flags)
48         : Source (s, type, name)
49         , _playlist (p)
50 {
51         /* PlaylistSources are never writable, renameable, removable or destructive */
52         _flags = Flag (_flags & ~(Writable|CanRename|Removable|RemovableIfEmpty|RemoveAtDestroy|Destructive));
53
54         _playlist = p;
55         _playlist_offset = begin;
56         _playlist_length = len;
57
58         _level = _playlist->max_source_level () + 1;
59 }
60
61 PlaylistSource::PlaylistSource (Session& s, const XMLNode& node)
62         : Source (s, DataType::AUDIO, "toBeRenamed")
63 {
64         /* PlaylistSources are never writable, renameable, removable or destructive */
65         _flags = Flag (_flags & ~(Writable|CanRename|Removable|RemovableIfEmpty|RemoveAtDestroy|Destructive));
66         
67
68         if (set_state (node, Stateful::loading_state_version)) {
69                 throw failed_constructor ();
70         }
71 }
72
73 PlaylistSource::~PlaylistSource ()
74 {
75 }
76
77 void
78 PlaylistSource::add_state (XMLNode& node)
79 {
80         char buf[64];
81
82         _playlist->id().print (buf, sizeof (buf));
83         node.add_property ("playlist", buf);
84         snprintf (buf, sizeof (buf), "%" PRIi64, _playlist_offset);
85         node.add_property ("offset", buf);
86         snprintf (buf, sizeof (buf), "%" PRIu64, _playlist_length);
87         node.add_property ("length", buf);
88         
89         node.add_child_nocopy (_playlist->get_state());
90 }
91
92 int
93 PlaylistSource::set_state (const XMLNode& node, int version) 
94 {
95         /* check that we have a playlist ID */
96
97         const XMLProperty *prop = node.property (X_("playlist"));
98
99         if (!prop) {
100                 error << _("No playlist ID in PlaylistSource XML!") << endmsg;
101                 throw failed_constructor ();
102         }
103
104         /* create playlist from child node */
105
106         XMLNodeList nlist;
107         XMLNodeConstIterator niter;
108
109         nlist = node.children();
110
111         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
112                 if ((*niter)->name() == "Playlist") {
113                         _playlist = PlaylistFactory::create (_session, **niter, true, false);
114                         break;
115                 }
116         }
117
118         if (!_playlist) {
119                 error << _("No playlist node in PlaylistSource XML!") << endmsg;
120                 throw failed_constructor ();
121         }
122
123         /* other properties */
124
125         if ((prop = node.property (X_("name"))) == 0) {
126                 throw failed_constructor ();
127         }
128         
129         set_name (prop->value());
130
131         if ((prop = node.property (X_("offset"))) == 0) {
132                 throw failed_constructor ();
133         }
134         sscanf (prop->value().c_str(), "%" PRIi64, &_playlist_offset);
135
136         if ((prop = node.property (X_("length"))) == 0) {
137                 throw failed_constructor ();
138         }
139
140         sscanf (prop->value().c_str(), "%" PRIu64, &_playlist_length);
141
142         _level = _playlist->max_source_level () + 1;
143
144         return 0;
145 }