Collect plugin runtime profile statistics.
[ardour.git] / libs / ardour / midi_patch_manager.cc
1 /*
2     Copyright (C) 2008 Hans Baier
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     $Id$
19 */
20
21 #include <boost/shared_ptr.hpp>
22
23 #include <glibmm/fileutils.h>
24
25 #include "pbd/file_utils.h"
26 #include "pbd/error.h"
27
28 #include "ardour/midi_patch_manager.h"
29
30 #include "ardour/search_paths.h"
31
32 #include "pbd/i18n.h"
33
34 using namespace std;
35 using namespace ARDOUR;
36 using namespace MIDI;
37 using namespace MIDI::Name;
38 using namespace PBD;
39
40 MidiPatchManager* MidiPatchManager::_manager = 0;
41
42 MidiPatchManager::MidiPatchManager ()
43 {
44         add_search_path(midi_patch_search_path ());
45 }
46
47 void
48 MidiPatchManager::add_search_path (const Searchpath& search_path)
49 {
50         for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
51
52                 if (_search_path.contains(*i)) {
53                         // already processed files from this path
54                         continue;
55                 }
56
57                 if (!Glib::file_test (*i, Glib::FILE_TEST_EXISTS)) {
58                         continue;
59                 }
60
61                 if (!Glib::file_test (*i, Glib::FILE_TEST_IS_DIR)) {
62                         continue;
63                 }
64
65                 add_midnam_files_from_directory (*i);
66
67                 _search_path.add_directory (*i);
68         }
69 }
70
71 bool
72 MidiPatchManager::add_custom_midnam (const std::string& id, const std::string& midnam)
73 {
74         boost::shared_ptr<MIDINameDocument> document;
75         document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument());
76         XMLTree mxml;
77         if (mxml.read_buffer (midnam, true)) {
78                 if (0 == document->set_state (mxml, *mxml.root())) {
79                         document->set_file_path ("custom:" + id);
80                         add_midi_name_document (document);
81                         return true;
82                 }
83         }
84         return false;
85 }
86
87 bool
88 MidiPatchManager::remove_custom_midnam (const std::string& id)
89 {
90         return remove_midi_name_document ("custom:" + id);
91 }
92
93 bool
94 MidiPatchManager::update_custom_midnam (const std::string& id, const std::string& midnam)
95 {
96         remove_midi_name_document ("custom:" + id, false);
97         return add_custom_midnam (id, midnam);
98 }
99
100 void
101 MidiPatchManager::add_midnam_files_from_directory(const std::string& directory_path)
102 {
103         vector<std::string> result;
104         find_files_matching_pattern (result, directory_path, "*.midnam");
105
106         info << string_compose(
107                         P_("Loading %1 MIDI patch from %2", "Loading %1 MIDI patches from %2", result.size()),
108                         result.size(), directory_path)
109              << endmsg;
110
111         for (vector<std::string>::const_iterator i = result.begin(); i != result.end(); ++i) {
112                 load_midi_name_document (*i);
113         }
114 }
115
116 void
117 MidiPatchManager::remove_search_path (const Searchpath& search_path)
118 {
119         for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
120
121                 if (!_search_path.contains(*i)) {
122                         continue;
123                 }
124
125                 remove_midnam_files_from_directory(*i);
126
127                 _search_path.remove_directory (*i);
128         }
129 }
130
131 void
132 MidiPatchManager::remove_midnam_files_from_directory(const std::string& directory_path)
133 {
134         vector<std::string> result;
135         find_files_matching_pattern (result, directory_path, "*.midnam");
136
137         info << string_compose(
138                         P_("Unloading %1 MIDI patch from %2", "Unloading %1 MIDI patches from %2", result.size()),
139                         result.size(), directory_path)
140              << endmsg;
141
142         for (vector<std::string>::const_iterator i = result.begin(); i != result.end(); ++i) {
143                 remove_midi_name_document (*i);
144         }
145 }
146
147 bool
148 MidiPatchManager::load_midi_name_document (const std::string& file_path)
149 {
150         boost::shared_ptr<MIDINameDocument> document;
151         try {
152                 document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument(file_path));
153         }
154         catch (...) {
155                 error << string_compose(_("Error parsing MIDI patch file %1"), file_path)
156                       << endmsg;
157                 return false;
158         }
159         return add_midi_name_document (document);
160 }
161
162 boost::shared_ptr<MIDINameDocument>
163 MidiPatchManager::document_by_model(std::string model_name) const
164 {
165         MidiNameDocuments::const_iterator i = _documents.find (model_name);
166         if (i != _documents.end ()) {
167                 return i->second;
168         }
169         return boost::shared_ptr<MIDINameDocument> ();
170 }
171
172 bool
173 MidiPatchManager::add_midi_name_document (boost::shared_ptr<MIDINameDocument> document)
174 {
175         bool added = false;
176         for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
177                  document->master_device_names_by_model().begin();
178              device != document->master_device_names_by_model().end();
179              ++device) {
180                 if (_documents.find(device->first) != _documents.end()) {
181                         warning << string_compose(_("Duplicate MIDI device `%1' in `%2' ignored"),
182                                                   device->first,
183                                                   document->file_path()) << endmsg;
184                         continue;
185                 }
186
187                 _documents[device->first] = document;
188                 _master_devices_by_model[device->first] = device->second;
189
190                 _all_models.insert(device->first);
191                 const std::string& manufacturer = device->second->manufacturer();
192                 if (_devices_by_manufacturer.find(manufacturer) ==
193                     _devices_by_manufacturer.end()) {
194                         MIDINameDocument::MasterDeviceNamesList empty;
195                         _devices_by_manufacturer.insert(std::make_pair(manufacturer, empty));
196                 }
197                 _devices_by_manufacturer[manufacturer].insert(
198                     std::make_pair(device->first, device->second));
199
200                 added = true;
201                 // TODO: handle this gracefully.
202                 assert(_documents.count(device->first) == 1);
203                 assert(_master_devices_by_model.count(device->first) == 1);
204         }
205
206         if (added) {
207                 PatchesChanged(); /* EMIT SIGNAL */
208         }
209         return added;
210 }
211
212 bool
213 MidiPatchManager::remove_midi_name_document (const std::string& file_path, bool emit_signal)
214 {
215         bool removed = false;
216         for (MidiNameDocuments::iterator i = _documents.begin(); i != _documents.end();) {
217                 if (i->second->file_path() == file_path) {
218
219                         boost::shared_ptr<MIDINameDocument> document = i->second;
220
221                         info << string_compose(_("Removing MIDI patch file %1"), file_path) << endmsg;
222
223                         _documents.erase(i++);
224
225                         for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
226                                  document->master_device_names_by_model().begin();
227                              device != document->master_device_names_by_model().end();
228                              ++device) {
229
230                                 _master_devices_by_model.erase(device->first);
231
232                                 _all_models.erase(device->first);
233
234                                 const std::string& manufacturer = device->second->manufacturer();
235
236                                 _devices_by_manufacturer[manufacturer].erase(device->first);
237                         }
238                         removed = true;
239                 } else {
240                         ++i;
241                 }
242         }
243         if (removed && emit_signal) {
244                 PatchesChanged(); /* EMIT SIGNAL */
245         }
246         return removed;
247 }