2 * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
3 * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2016-2019 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "ardour/automation_list.h"
25 #include "ardour/location.h"
26 #include "ardour/midi_automation_list_binder.h"
27 #include "ardour/playlist.h"
28 #include "ardour/region.h"
29 #include "ardour/region_factory.h"
30 #include "ardour/route.h"
31 #include "ardour/session.h"
32 #include "ardour/session_playlists.h"
33 #include "ardour/source.h"
34 #include "ardour/tempo.h"
35 #include "evoral/Curve.hpp"
36 #include "pbd/error.h"
37 #include "pbd/failed_constructor.h"
39 #include "pbd/memento_command.h"
40 #include "pbd/stateful_diff_command.h"
41 #include "pbd/statefuldestructible.h"
42 #include "pbd/types_convert.h"
47 using namespace ARDOUR;
51 void Session::register_with_memento_command_factory(PBD::ID id, PBD::StatefulDestructible *ptr)
57 Session::memento_command_factory(XMLNode *n)
60 XMLNode *before = 0, *after = 0;
64 bool have_id = n->get_property ("obj-id", id);
66 /* get before/after */
68 if (n->name() == "MementoCommand") {
69 before = new XMLNode(*n->children().front());
70 after = new XMLNode(*n->children().back());
72 } else if (n->name() == "MementoUndoCommand") {
73 before = new XMLNode(*n->children().front());
75 } else if (n->name() == "MementoRedoCommand") {
76 after = new XMLNode(*n->children().front());
78 } else if (n->name() == "PlaylistCommand") {
79 before = new XMLNode(*n->children().front());
80 after = new XMLNode(*n->children().back());
85 info << string_compose (_("Tried to reconstitute a MementoCommand with no contents, failing. id=%1"), id.to_s()) << endmsg;
90 std::string type_name;
91 n->get_property ("type-name", type_name);
93 if (type_name == "ARDOUR::AudioRegion" || type_name == "ARDOUR::MidiRegion" || type_name == "ARDOUR::Region") {
94 boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
96 return new MementoCommand<Region>(*r, before, after);
99 } else if (type_name == "ARDOUR::AudioSource" || type_name == "ARDOUR::MidiSource") {
100 if (sources.count(id))
101 return new MementoCommand<Source>(*sources[id], before, after);
103 } else if (type_name == "ARDOUR::Location") {
104 Location* loc = _locations->get_location_by_id(id);
106 return new MementoCommand<Location>(*loc, before, after);
109 } else if (type_name == "ARDOUR::Locations") {
110 return new MementoCommand<Locations>(*_locations, before, after);
112 } else if (type_name == "ARDOUR::TempoMap") {
113 return new MementoCommand<TempoMap>(*_tempo_map, before, after);
115 } else if (type_name == "ARDOUR::Playlist" || type_name == "ARDOUR::AudioPlaylist" || type_name == "ARDOUR::MidiPlaylist") {
116 if (boost::shared_ptr<Playlist> pl = _playlists->by_name(child->property("name")->value())) {
117 return new MementoCommand<Playlist>(*(pl.get()), before, after);
120 } else if (type_name == "ARDOUR::Route" || type_name == "ARDOUR::AudioTrack" || type_name == "ARDOUR::MidiTrack") {
121 if (boost::shared_ptr<Route> r = route_by_id(id)) {
122 return new MementoCommand<Route>(*r, before, after);
124 error << string_compose (X_("Route %1 not found in session"), id) << endmsg;
127 } else if (type_name == "Evoral::Curve" || type_name == "ARDOUR::AutomationList") {
129 std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
130 if (i != automation_lists.end()) {
131 return new MementoCommand<AutomationList>(*i->second, before, after);
134 return new MementoCommand<AutomationList> (
135 new MidiAutomationListBinder (n, sources),
140 std::cerr << "Alist " << id << " not found\n";
142 } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits herea
143 return new MementoCommand<PBD::StatefulDestructible>(*registry[id], before, after);
147 info << string_compose (_("Could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), type_name, id.to_s()) << endmsg;
153 Session::stateful_diff_command_factory (XMLNode* n)
156 std::string type_name;
157 if (!n->get_property ("obj-id", id) || !n->get_property ("type-name", type_name)) {
158 error << _("Could get object ID and type name for StatefulDiffCommand from XMLNode.")
163 if ((type_name == "ARDOUR::AudioRegion" || type_name == "ARDOUR::MidiRegion")) {
164 boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
166 return new StatefulDiffCommand (r, *n);
169 } else if (type_name == "ARDOUR::AudioPlaylist" || type_name == "ARDOUR::MidiPlaylist") {
170 boost::shared_ptr<Playlist> p = _playlists->by_id (id);
172 return new StatefulDiffCommand (p, *n);
174 std::cerr << "Playlist with ID = " << id << " not found\n";
180 info << string_compose (
181 _("Could not reconstitute StatefulDiffCommand from XMLNode. object type = %1 id = %2"), type_name, id.to_s())