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