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