Merging from trunk
[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 NamedSelection::NamedSelection (string n, list<Playlist*>& l) 
37         : name (n)
38 {
39         playlists = l;
40         for (list<Playlist*>::iterator i = playlists.begin(); i != playlists.end(); ++i) {
41                 (*i)->ref();
42         }
43         NamedSelectionCreated (this);
44 }
45
46 NamedSelection::NamedSelection (Session& session, const XMLNode& node)
47 {
48         XMLNode* lists_node;
49         const XMLProperty* property;
50
51         if ((property = node.property ("name")) == 0) {
52                 throw failed_constructor();
53         }
54
55         name = property->value();
56         
57         if ((lists_node = find_named_node (node, "Playlists")) == 0) {
58                 return;
59         }
60
61         XMLNodeList nlist = lists_node->children();
62         XMLNodeConstIterator niter;
63
64         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
65
66                 const XMLNode* plnode;
67                 string playlist_name;
68                 Playlist* playlist;
69
70                 plnode = *niter;
71
72                 if ((property = plnode->property ("name")) != 0) {
73                         if ((playlist = session.playlist_by_name (property->value())) != 0) {
74                                 playlist->ref();
75                                 playlists.push_back (playlist);
76                         } else {
77                                 warning << string_compose (_("Chunk %1 uses an unknown playlist \"%2\""), name, property->value()) << endmsg;
78                         }
79                 } else {
80                         error << string_compose (_("Chunk %1 contains misformed playlist information"), name) << endmsg;
81                         throw failed_constructor();
82                 }
83         }
84
85         NamedSelectionCreated (this);
86 }
87
88 NamedSelection::~NamedSelection ()
89 {
90         for (list<Playlist*>::iterator i = playlists.begin(); i != playlists.end(); ++i) {
91                 (*i)->unref();
92         }
93 }
94
95 int
96 NamedSelection::set_state (const XMLNode& node)
97 {
98         return 0;
99 }
100
101 XMLNode&
102 NamedSelection::get_state ()
103 {
104         XMLNode* root = new XMLNode ("NamedSelection");
105         XMLNode* child;
106
107         root->add_property ("name", name);
108         child = root->add_child ("Playlists");
109
110         for (list<Playlist*>::iterator i = playlists.begin(); i != playlists.end(); ++i) {
111                 XMLNode* plnode = new XMLNode ("Playlist");
112
113                 plnode->add_property ("name", (*i)->name());
114                 child->add_child_nocopy (*plnode);
115         }
116         
117         return *root;
118 }