Make the MementoCommand and StatefulDiffCommand factories look up region IDs in the...
[ardour.git] / libs / ardour / session_command.cc
1 /*
2     Copyright (C) 2000-2007 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 "ardour/session.h"
21 #include "ardour/route.h"
22 #include "pbd/memento_command.h"
23 #include "ardour/diskstream.h"
24 #include "ardour/playlist.h"
25 #include "ardour/audioplaylist.h"
26 #include "ardour/audio_track.h"
27 #include "ardour/midi_playlist.h"
28 #include "ardour/midi_track.h"
29 #include "ardour/tempo.h"
30 #include "ardour/audiosource.h"
31 #include "ardour/audioregion.h"
32 #include "ardour/midi_source.h"
33 #include "ardour/midi_region.h"
34 #include "ardour/session_playlists.h"
35 #include "ardour/region_factory.h"
36 #include "pbd/error.h"
37 #include "pbd/id.h"
38 #include "pbd/statefuldestructible.h"
39 #include "pbd/failed_constructor.h"
40 #include "pbd/stateful_diff_command.h"
41 #include "evoral/Curve.hpp"
42
43 using namespace PBD;
44 using namespace ARDOUR;
45
46 #include "i18n.h"
47
48 void Session::register_with_memento_command_factory(PBD::ID id, PBD::StatefulDestructible *ptr)
49 {
50     registry[id] = ptr;
51 }
52
53 Command *
54 Session::memento_command_factory(XMLNode *n)
55 {
56     PBD::ID id;
57     XMLNode *before = 0, *after = 0;
58     XMLNode *child = 0;
59
60     /* get id */
61     id = PBD::ID(n->property("obj-id")->value());
62
63     /* get before/after */
64
65     if (n->name() == "MementoCommand") {
66             before = new XMLNode(*n->children().front());
67             after = new XMLNode(*n->children().back());
68             child = before;
69     } else if (n->name() == "MementoUndoCommand") {
70             before = new XMLNode(*n->children().front());
71             child = before;
72     } else if (n->name() == "MementoRedoCommand") {
73             after = new XMLNode(*n->children().front());
74             child = after;
75     } else if (n->name() == "PlaylistCommand") {
76             before = new XMLNode(*n->children().front());
77             after = new XMLNode(*n->children().back());
78             child = before;
79     }
80
81     if (!child) {
82         error << _("Tried to reconstitute a MementoCommand with no contents, failing. id=") << id.to_s() << endmsg;
83         return 0;
84     }
85
86     /* create command */
87     string obj_T = n->property ("type-name")->value();
88
89     if (obj_T == typeid (AudioRegion).name() || obj_T == typeid (MidiRegion).name() || obj_T == typeid (Region).name()) {
90             boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
91             if (r) {
92                     return new MementoCommand<Region>(*r, before, after);
93             }
94
95     } else if (obj_T == typeid (AudioSource).name() || obj_T == typeid (MidiSource).name()) {
96             if (sources.count(id))
97                     return new MementoCommand<Source>(*sources[id], before, after);
98
99     } else if (obj_T == typeid (Location).name()) {
100             Location* loc = _locations.get_location_by_id(id);
101             if (loc) {
102                     return new MementoCommand<Location>(*loc, before, after);
103             }
104
105     } else if (obj_T == typeid (Locations).name()) {
106             return new MementoCommand<Locations>(_locations, before, after);
107
108     } else if (obj_T == typeid (TempoMap).name()) {
109             return new MementoCommand<TempoMap>(*_tempo_map, before, after);
110
111     } else if (obj_T == typeid (Playlist).name() || obj_T == typeid (AudioPlaylist).name() || obj_T == typeid (MidiPlaylist).name()) {
112             if (boost::shared_ptr<Playlist> pl = playlists->by_name(child->property("name")->value())) {
113                     return new MementoCommand<Playlist>(*(pl.get()), before, after);
114             }
115
116     } else if (obj_T == typeid (Route).name() || obj_T == typeid (AudioTrack).name() || obj_T == typeid(MidiTrack).name()) {
117                 if (boost::shared_ptr<Route> r = route_by_id(id)) {
118                         return new MementoCommand<Route>(*r, before, after);
119                 } else {
120                         error << string_compose (X_("Route %1 not found in session"), id) << endmsg;
121                 }
122
123     } else if (obj_T == typeid (Evoral::Curve).name() || obj_T == typeid (AutomationList).name()) {
124                 std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
125                 if (i != automation_lists.end()) {
126                     return new MementoCommand<AutomationList>(*i->second, before, after);
127                 }
128     } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits herea
129             return new MementoCommand<PBD::StatefulDestructible>(*registry[id], before, after);
130     }
131
132     /* we failed */
133     error << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
134
135     return 0 ;
136 }
137
138 Command *
139 Session::stateful_diff_command_factory (XMLNode* n)
140 {
141         PBD::ID const id (n->property("obj-id")->value ());
142
143         string const obj_T = n->property ("type-name")->value ();
144         if ((obj_T == typeid (AudioRegion).name() || obj_T == typeid (MidiRegion).name())) {
145                 boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
146                 if (r) {
147                         return new StatefulDiffCommand (r.get(), *n);
148                 }
149         }
150
151         /* we failed */
152         
153         error << string_compose (
154                 _("could not reconstitute StatefulDiffCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s())
155               << endmsg;
156         
157         return 0;
158 }