Use a MementoCommandBinder for Crossfades so that the undo record can contain details...
[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 "ardour/midi_automation_list_binder.h"
37 #include "ardour/crossfade_binder.h"
38 #include "ardour/crossfade.h"
39 #include "pbd/error.h"
40 #include "pbd/id.h"
41 #include "pbd/statefuldestructible.h"
42 #include "pbd/failed_constructor.h"
43 #include "pbd/stateful_diff_command.h"
44 #include "evoral/Curve.hpp"
45
46 using namespace PBD;
47 using namespace ARDOUR;
48
49 #include "i18n.h"
50
51 void Session::register_with_memento_command_factory(PBD::ID id, PBD::StatefulDestructible *ptr)
52 {
53     registry[id] = ptr;
54 }
55
56 Command *
57 Session::memento_command_factory(XMLNode *n)
58 {
59     PBD::ID id;
60     XMLNode *before = 0, *after = 0;
61     XMLNode *child = 0;
62
63     /* get id */
64
65     /* XXX: HACK! */
66     bool have_id = n->property("obj-id") != 0;
67     if (have_id) {
68             id = PBD::ID(n->property("obj-id")->value());
69     }
70
71     /* get before/after */
72
73     if (n->name() == "MementoCommand") {
74             before = new XMLNode(*n->children().front());
75             after = new XMLNode(*n->children().back());
76             child = before;
77     } else if (n->name() == "MementoUndoCommand") {
78             before = new XMLNode(*n->children().front());
79             child = before;
80     } else if (n->name() == "MementoRedoCommand") {
81             after = new XMLNode(*n->children().front());
82             child = after;
83     } else if (n->name() == "PlaylistCommand") {
84             before = new XMLNode(*n->children().front());
85             after = new XMLNode(*n->children().back());
86             child = before;
87     }
88
89     if (!child) {
90         error << _("Tried to reconstitute a MementoCommand with no contents, failing. id=") << id.to_s() << endmsg;
91         return 0;
92     }
93
94     /* create command */
95     string obj_T = n->property ("type-name")->value();
96
97     if (obj_T == "ARDOUR::AudioRegion" || obj_T == "ARDOUR::MidiRegion" || obj_T == "ARDOUR::Region") {
98             boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
99             if (r) {
100                     return new MementoCommand<Region>(*r, before, after);
101             }
102
103     } else if (obj_T == "ARDOUR::AudioSource" || obj_T == "ARDOUR::MidiSource") {
104             if (sources.count(id))
105                     return new MementoCommand<Source>(*sources[id], before, after);
106
107     } else if (obj_T == "ARDOUR::Location") {
108             Location* loc = _locations->get_location_by_id(id);
109             if (loc) {
110                     return new MementoCommand<Location>(*loc, before, after);
111             }
112
113     } else if (obj_T == "ARDOUR::Locations") {
114             return new MementoCommand<Locations>(*_locations, before, after);
115
116     } else if (obj_T == "ARDOUR::TempoMap") {
117             return new MementoCommand<TempoMap>(*_tempo_map, before, after);
118
119     } else if (obj_T == "ARDOUR::Playlist" || obj_T == "ARDOUR::AudioPlaylist" || obj_T == "ARDOUR::MidiPlaylist") {
120             if (boost::shared_ptr<Playlist> pl = playlists->by_name(child->property("name")->value())) {
121                     return new MementoCommand<Playlist>(*(pl.get()), before, after);
122             }
123
124     } else if (obj_T == "ARDOUR::Route" || obj_T == "ARDOUR::AudioTrack" || obj_T == "ARDOUR::MidiTrack") {
125                 if (boost::shared_ptr<Route> r = route_by_id(id)) {
126                         return new MementoCommand<Route>(*r, before, after);
127                 } else {
128                         error << string_compose (X_("Route %1 not found in session"), id) << endmsg;
129                 }
130
131     } else if (obj_T == "Evoral::Curve" || obj_T == "ARDOUR::AutomationList") {
132             if (have_id) {
133                     std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
134                     if (i != automation_lists.end()) {
135                             return new MementoCommand<AutomationList>(*i->second, before, after);
136                     }
137             } else {
138                     return new MementoCommand<AutomationList> (
139                             new MidiAutomationListBinder (n, sources),
140                             before, after
141                             );
142             }
143
144             cerr << "Alist " << id << " not found\n";
145
146     } else if (obj_T == "ARDOUR::Crossfade") {
147             if (have_id) {
148                     boost::shared_ptr<Crossfade> c = playlists->find_crossfade (id);
149                     if (c) {
150                             return new MementoCommand<Crossfade> (*c.get(), before, after);
151                     }
152             } else {
153                     return new MementoCommand<Crossfade> (
154                             new CrossfadeBinder (n, playlists),
155                             before, after
156                             );
157             }
158
159     } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits herea
160             return new MementoCommand<PBD::StatefulDestructible>(*registry[id], before, after);
161     }
162
163     /* we failed */
164     error << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
165
166     return 0 ;
167 }
168
169 Command *
170 Session::stateful_diff_command_factory (XMLNode* n)
171 {
172         PBD::ID const id (n->property("obj-id")->value ());
173
174         string const obj_T = n->property ("type-name")->value ();
175         if ((obj_T == "ARDOUR::AudioRegion" || obj_T == "ARDOUR::MidiRegion")) {
176                 boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
177                 if (r) {
178                         return new StatefulDiffCommand (r, *n);
179                 }
180
181         } else if (obj_T == "ARDOUR::AudioPlaylist" ||  obj_T == "ARDOUR::MidiPlaylist") {
182                 boost::shared_ptr<Playlist> p = playlists->by_id (id);
183                 if (p) {
184                         return new StatefulDiffCommand (p, *n);
185                 } else {
186                         cerr << "Playlist with ID = " << id << " not found\n";
187                 }
188         }
189
190         /* we failed */
191
192         error << string_compose (
193                 _("could not reconstitute StatefulDiffCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s())
194               << endmsg;
195
196         return 0;
197 }