fix thinko when dealing with non-MIDI tracks
[ardour.git] / libs / ardour / session_command.cc
1 /*
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>
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <string>
23
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"
38 #include "pbd/id.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"
43
44 class Command;
45
46 using namespace PBD;
47 using namespace ARDOUR;
48
49 #include "pbd/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     /* XXX: HACK! */
64     bool have_id = n->get_property ("obj-id", id);
65
66     /* get before/after */
67
68     if (n->name() == "MementoCommand") {
69             before = new XMLNode(*n->children().front());
70             after = new XMLNode(*n->children().back());
71             child = before;
72     } else if (n->name() == "MementoUndoCommand") {
73             before = new XMLNode(*n->children().front());
74             child = before;
75     } else if (n->name() == "MementoRedoCommand") {
76             after = new XMLNode(*n->children().front());
77             child = after;
78     } else if (n->name() == "PlaylistCommand") {
79             before = new XMLNode(*n->children().front());
80             after = new XMLNode(*n->children().back());
81             child = before;
82     }
83
84     if (!child) {
85             info << string_compose (_("Tried to reconstitute a MementoCommand with no contents, failing. id=%1"), id.to_s()) << endmsg;
86             return 0;
87     }
88
89     /* create command */
90     std::string type_name;
91     n->get_property ("type-name", type_name);
92
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);
95             if (r) {
96                     return new MementoCommand<Region>(*r, before, after);
97             }
98
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);
102
103     } else if (type_name == "ARDOUR::Location") {
104             Location* loc = _locations->get_location_by_id(id);
105             if (loc) {
106                     return new MementoCommand<Location>(*loc, before, after);
107             }
108
109     } else if (type_name == "ARDOUR::Locations") {
110             return new MementoCommand<Locations>(*_locations, before, after);
111
112     } else if (type_name == "ARDOUR::TempoMap") {
113             return new MementoCommand<TempoMap>(*_tempo_map, before, after);
114
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);
118             }
119
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);
123                 } else {
124                         error << string_compose (X_("Route %1 not found in session"), id) << endmsg;
125                 }
126
127     } else if (type_name == "Evoral::Curve" || type_name == "ARDOUR::AutomationList") {
128             if (have_id) {
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);
132                     }
133             } else {
134                     return new MementoCommand<AutomationList> (
135                             new MidiAutomationListBinder (n, sources),
136                             before, after
137                             );
138             }
139
140             std::cerr << "Alist " << id << " not found\n";
141
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);
144     }
145
146     /* we failed */
147     info << string_compose (_("Could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), type_name, id.to_s()) << endmsg;
148
149     return 0 ;
150 }
151
152 Command *
153 Session::stateful_diff_command_factory (XMLNode* n)
154 {
155         PBD::ID id;
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.")
159                       << endmsg;
160                       return 0;
161         }
162
163         if ((type_name == "ARDOUR::AudioRegion" || type_name == "ARDOUR::MidiRegion")) {
164                 boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
165                 if (r) {
166                         return new StatefulDiffCommand (r, *n);
167                 }
168
169         } else if (type_name == "ARDOUR::AudioPlaylist" ||  type_name == "ARDOUR::MidiPlaylist") {
170                 boost::shared_ptr<Playlist> p = _playlists->by_id (id);
171                 if (p) {
172                         return new StatefulDiffCommand (p, *n);
173                 } else {
174                         std::cerr << "Playlist with ID = " << id << " not found\n";
175                 }
176         }
177
178         /* we failed */
179
180         info << string_compose (
181                 _("Could not reconstitute StatefulDiffCommand from XMLNode. object type = %1 id = %2"), type_name, id.to_s())
182               << endmsg;
183
184         return 0;
185 }