efd636e7f4825b8353d05c5509c0f339f813b3fd
[ardour.git] / libs / ardour / named_selection.cc
1 /*
2     Copyright (C) 2003 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
20 #include "pbd/failed_constructor.h"
21 #include "pbd/error.h"
22
23 #include "ardour/session.h"
24 #include "ardour/utils.h"
25 #include "ardour/playlist.h"
26 #include "ardour/named_selection.h"
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 sigc::signal<void,NamedSelection*> NamedSelection::NamedSelectionCreated;
34
35 typedef std::list<boost::shared_ptr<Playlist> > PlaylistList;
36
37 NamedSelection::NamedSelection (string n, PlaylistList& l) 
38         : name (n)
39 {
40         playlists = l;
41         for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
42                 string new_name;
43
44                 /* rename playlists to reflect our ownership */
45
46                 new_name = name;
47                 new_name += '/';
48                 new_name += (*i)->name();
49
50                 (*i)->set_name (new_name);
51                 (*i)->use();
52         }
53
54         NamedSelectionCreated (this);
55 }
56
57 NamedSelection::NamedSelection (Session& session, const XMLNode& node)
58 {
59         XMLNode* lists_node;
60         const XMLProperty* property;
61
62         if ((property = node.property ("name")) == 0) {
63                 throw failed_constructor();
64         }
65
66         name = property->value();
67         
68         if ((lists_node = find_named_node (node, "Playlists")) == 0) {
69                 return;
70         }
71
72         XMLNodeList nlist = lists_node->children();
73         XMLNodeConstIterator niter;
74
75         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
76
77                 const XMLNode* plnode;
78                 string playlist_name;
79                 boost::shared_ptr<Playlist> playlist;
80
81                 plnode = *niter;
82
83                 if ((property = plnode->property ("name")) != 0) {
84                         if ((playlist = session.playlist_by_name (property->value())) != 0) {
85                                 playlist->use();
86                                 playlists.push_back (playlist);
87                         } else {
88                                 warning << string_compose (_("Chunk %1 uses an unknown playlist \"%2\""), name, property->value()) << endmsg;
89                         }
90                 } else {
91                         error << string_compose (_("Chunk %1 contains misformed playlist information"), name) << endmsg;
92                         throw failed_constructor();
93                 }
94         }
95
96         NamedSelectionCreated (this);
97 }
98
99 NamedSelection::~NamedSelection ()
100 {
101         for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
102                 (*i)->release ();
103                 (*i)->GoingAway ();
104         }
105 }
106
107 int
108 NamedSelection::set_state (const XMLNode& node)
109 {
110         return 0;
111 }
112
113 XMLNode&
114 NamedSelection::get_state ()
115 {
116         XMLNode* root = new XMLNode ("NamedSelection");
117         XMLNode* child;
118
119         root->add_property ("name", name);
120         child = root->add_child ("Playlists");
121
122         for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
123                 XMLNode* plnode = new XMLNode ("Playlist");
124
125                 plnode->add_property ("name", (*i)->name());
126                 child->add_child_nocopy (*plnode);
127         }
128         
129         return *root;
130 }