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