enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 <string>
21
22 #include "ardour/automation_list.h"
23 #include "ardour/location.h"
24 #include "ardour/midi_automation_list_binder.h"
25 #include "ardour/playlist.h"
26 #include "ardour/region.h"
27 #include "ardour/region_factory.h"
28 #include "ardour/route.h"
29 #include "ardour/session.h"
30 #include "ardour/session_playlists.h"
31 #include "ardour/source.h"
32 #include "ardour/tempo.h"
33 #include "evoral/Curve.hpp"
34 #include "pbd/error.h"
35 #include "pbd/failed_constructor.h"
36 #include "pbd/id.h"
37 #include "pbd/memento_command.h"
38 #include "pbd/stateful_diff_command.h"
39 #include "pbd/statefuldestructible.h"
40
41 class Command;
42
43 using namespace PBD;
44 using namespace ARDOUR;
45
46 #include "pbd/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
62     /* XXX: HACK! */
63     bool have_id = n->property("obj-id") != 0;
64     if (have_id) {
65             id = PBD::ID(n->property("obj-id")->value());
66     }
67
68     /* get before/after */
69
70     if (n->name() == "MementoCommand") {
71             before = new XMLNode(*n->children().front());
72             after = new XMLNode(*n->children().back());
73             child = before;
74     } else if (n->name() == "MementoUndoCommand") {
75             before = new XMLNode(*n->children().front());
76             child = before;
77     } else if (n->name() == "MementoRedoCommand") {
78             after = new XMLNode(*n->children().front());
79             child = after;
80     } else if (n->name() == "PlaylistCommand") {
81             before = new XMLNode(*n->children().front());
82             after = new XMLNode(*n->children().back());
83             child = before;
84     }
85
86     if (!child) {
87             error << string_compose (_("Tried to reconstitute a MementoCommand with no contents, failing. id=%1"), id.to_s()) << endmsg;
88             return 0;
89     }
90
91     /* create command */
92     std::string obj_T = n->property ("type-name")->value();
93
94     if (obj_T == "ARDOUR::AudioRegion" || obj_T == "ARDOUR::MidiRegion" || obj_T == "ARDOUR::Region") {
95             boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
96             if (r) {
97                     return new MementoCommand<Region>(*r, before, after);
98             }
99
100     } else if (obj_T == "ARDOUR::AudioSource" || obj_T == "ARDOUR::MidiSource") {
101             if (sources.count(id))
102                     return new MementoCommand<Source>(*sources[id], before, after);
103
104     } else if (obj_T == "ARDOUR::Location") {
105             Location* loc = _locations->get_location_by_id(id);
106             if (loc) {
107                     return new MementoCommand<Location>(*loc, before, after);
108             }
109
110     } else if (obj_T == "ARDOUR::Locations") {
111             return new MementoCommand<Locations>(*_locations, before, after);
112
113     } else if (obj_T == "ARDOUR::TempoMap") {
114             return new MementoCommand<TempoMap>(*_tempo_map, before, after);
115
116     } else if (obj_T == "ARDOUR::Playlist" || obj_T == "ARDOUR::AudioPlaylist" || obj_T == "ARDOUR::MidiPlaylist") {
117             if (boost::shared_ptr<Playlist> pl = playlists->by_name(child->property("name")->value())) {
118                     return new MementoCommand<Playlist>(*(pl.get()), before, after);
119             }
120
121     } else if (obj_T == "ARDOUR::Route" || obj_T == "ARDOUR::AudioTrack" || obj_T == "ARDOUR::MidiTrack") {
122                 if (boost::shared_ptr<Route> r = route_by_id(id)) {
123                         return new MementoCommand<Route>(*r, before, after);
124                 } else {
125                         error << string_compose (X_("Route %1 not found in session"), id) << endmsg;
126                 }
127
128     } else if (obj_T == "Evoral::Curve" || obj_T == "ARDOUR::AutomationList") {
129             if (have_id) {
130                     std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
131                     if (i != automation_lists.end()) {
132                             return new MementoCommand<AutomationList>(*i->second, before, after);
133                     }
134             } else {
135                     return new MementoCommand<AutomationList> (
136                             new MidiAutomationListBinder (n, sources),
137                             before, after
138                             );
139             }
140
141             std::cerr << "Alist " << id << " not found\n";
142
143     } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits herea
144             return new MementoCommand<PBD::StatefulDestructible>(*registry[id], before, after);
145     }
146
147     /* we failed */
148     info << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
149
150     return 0 ;
151 }
152
153 Command *
154 Session::stateful_diff_command_factory (XMLNode* n)
155 {
156         PBD::ID const id (n->property("obj-id")->value ());
157
158         std::string const obj_T = n->property ("type-name")->value ();
159         if ((obj_T == "ARDOUR::AudioRegion" || obj_T == "ARDOUR::MidiRegion")) {
160                 boost::shared_ptr<Region> r = RegionFactory::region_by_id (id);
161                 if (r) {
162                         return new StatefulDiffCommand (r, *n);
163                 }
164
165         } else if (obj_T == "ARDOUR::AudioPlaylist" ||  obj_T == "ARDOUR::MidiPlaylist") {
166                 boost::shared_ptr<Playlist> p = playlists->by_id (id);
167                 if (p) {
168                         return new StatefulDiffCommand (p, *n);
169                 } else {
170                         std::cerr << "Playlist with ID = " << id << " not found\n";
171                 }
172         }
173
174         /* we failed */
175
176         error << string_compose (
177                 _("could not reconstitute StatefulDiffCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s())
178               << endmsg;
179
180         return 0;
181 }