NO-OP: whitespace/comments
[ardour.git] / libs / ardour / midi_patch_manager.cc
1 /*
2  * Copyright (C) 2008 Hans Baier <hansfbaier@googlemail.com>
3  * Copyright (C) 2009-2014 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2016 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2012-2015 Tim Mayberry <mojofunk@gmail.com>
6  * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <boost/shared_ptr.hpp>
24
25 #include <glibmm/fileutils.h>
26
27 #include "pbd/file_utils.h"
28 #include "pbd/error.h"
29
30 #include "ardour/midi_patch_manager.h"
31
32 #include "ardour/search_paths.h"
33
34 #include "pbd/i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace MIDI;
39 using namespace MIDI::Name;
40 using namespace PBD;
41
42 MidiPatchManager* MidiPatchManager::_manager = 0;
43
44 MidiPatchManager::MidiPatchManager ()
45 {
46         add_search_path(midi_patch_search_path ());
47 }
48
49 void
50 MidiPatchManager::add_search_path (const Searchpath& search_path)
51 {
52         for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
53
54                 if (_search_path.contains(*i)) {
55                         // already processed files from this path
56                         continue;
57                 }
58
59                 if (!Glib::file_test (*i, Glib::FILE_TEST_EXISTS)) {
60                         continue;
61                 }
62
63                 if (!Glib::file_test (*i, Glib::FILE_TEST_IS_DIR)) {
64                         continue;
65                 }
66
67                 add_midnam_files_from_directory (*i);
68
69                 _search_path.add_directory (*i);
70         }
71 }
72
73 bool
74 MidiPatchManager::add_custom_midnam (const std::string& id, const std::string& midnam)
75 {
76         boost::shared_ptr<MIDINameDocument> document;
77         document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument());
78         XMLTree mxml;
79         if (mxml.read_buffer (midnam, true)) {
80                 if (0 == document->set_state (mxml, *mxml.root())) {
81                         document->set_file_path ("custom:" + id);
82                         add_midi_name_document (document);
83                         return true;
84                 }
85         }
86         return false;
87 }
88
89 bool
90 MidiPatchManager::remove_custom_midnam (const std::string& id)
91 {
92         return remove_midi_name_document ("custom:" + id);
93 }
94
95 bool
96 MidiPatchManager::update_custom_midnam (const std::string& id, const std::string& midnam)
97 {
98         remove_midi_name_document ("custom:" + id, false);
99         return add_custom_midnam (id, midnam);
100 }
101
102 void
103 MidiPatchManager::add_midnam_files_from_directory(const std::string& directory_path)
104 {
105         vector<std::string> result;
106         find_files_matching_pattern (result, directory_path, "*.midnam");
107
108         info << string_compose(
109                         P_("Loading %1 MIDI patch from %2", "Loading %1 MIDI patches from %2", result.size()),
110                         result.size(), directory_path)
111              << endmsg;
112
113         for (vector<std::string>::const_iterator i = result.begin(); i != result.end(); ++i) {
114                 load_midi_name_document (*i);
115         }
116 }
117
118 void
119 MidiPatchManager::remove_search_path (const Searchpath& search_path)
120 {
121         for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
122
123                 if (!_search_path.contains(*i)) {
124                         continue;
125                 }
126
127                 remove_midnam_files_from_directory(*i);
128
129                 _search_path.remove_directory (*i);
130         }
131 }
132
133 void
134 MidiPatchManager::remove_midnam_files_from_directory(const std::string& directory_path)
135 {
136         vector<std::string> result;
137         find_files_matching_pattern (result, directory_path, "*.midnam");
138
139         info << string_compose(
140                         P_("Unloading %1 MIDI patch from %2", "Unloading %1 MIDI patches from %2", result.size()),
141                         result.size(), directory_path)
142              << endmsg;
143
144         for (vector<std::string>::const_iterator i = result.begin(); i != result.end(); ++i) {
145                 remove_midi_name_document (*i);
146         }
147 }
148
149 bool
150 MidiPatchManager::load_midi_name_document (const std::string& file_path)
151 {
152         boost::shared_ptr<MIDINameDocument> document;
153         try {
154                 document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument(file_path));
155         }
156         catch (...) {
157                 error << string_compose(_("Error parsing MIDI patch file %1"), file_path)
158                       << endmsg;
159                 return false;
160         }
161         return add_midi_name_document (document);
162 }
163
164 boost::shared_ptr<MIDINameDocument>
165 MidiPatchManager::document_by_model(std::string model_name) const
166 {
167         MidiNameDocuments::const_iterator i = _documents.find (model_name);
168         if (i != _documents.end ()) {
169                 return i->second;
170         }
171         return boost::shared_ptr<MIDINameDocument> ();
172 }
173
174 bool
175 MidiPatchManager::add_midi_name_document (boost::shared_ptr<MIDINameDocument> document)
176 {
177         bool added = false;
178         for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
179                  document->master_device_names_by_model().begin();
180              device != document->master_device_names_by_model().end();
181              ++device) {
182                 if (_documents.find(device->first) != _documents.end()) {
183                         warning << string_compose(_("Duplicate MIDI device `%1' in `%2' ignored"),
184                                                   device->first,
185                                                   document->file_path()) << endmsg;
186                         continue;
187                 }
188
189                 _documents[device->first] = document;
190                 _master_devices_by_model[device->first] = device->second;
191
192                 _all_models.insert(device->first);
193                 const std::string& manufacturer = device->second->manufacturer();
194                 if (_devices_by_manufacturer.find(manufacturer) ==
195                     _devices_by_manufacturer.end()) {
196                         MIDINameDocument::MasterDeviceNamesList empty;
197                         _devices_by_manufacturer.insert(std::make_pair(manufacturer, empty));
198                 }
199                 _devices_by_manufacturer[manufacturer].insert(
200                     std::make_pair(device->first, device->second));
201
202                 added = true;
203                 // TODO: handle this gracefully.
204                 assert(_documents.count(device->first) == 1);
205                 assert(_master_devices_by_model.count(device->first) == 1);
206         }
207
208         if (added) {
209                 PatchesChanged(); /* EMIT SIGNAL */
210         }
211         return added;
212 }
213
214 bool
215 MidiPatchManager::remove_midi_name_document (const std::string& file_path, bool emit_signal)
216 {
217         bool removed = false;
218         for (MidiNameDocuments::iterator i = _documents.begin(); i != _documents.end();) {
219                 if (i->second->file_path() == file_path) {
220
221                         boost::shared_ptr<MIDINameDocument> document = i->second;
222
223                         info << string_compose(_("Removing MIDI patch file %1"), file_path) << endmsg;
224
225                         _documents.erase(i++);
226
227                         for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
228                                  document->master_device_names_by_model().begin();
229                              device != document->master_device_names_by_model().end();
230                              ++device) {
231
232                                 _master_devices_by_model.erase(device->first);
233
234                                 _all_models.erase(device->first);
235
236                                 const std::string& manufacturer = device->second->manufacturer();
237
238                                 _devices_by_manufacturer[manufacturer].erase(device->first);
239                         }
240                         removed = true;
241                 } else {
242                         ++i;
243                 }
244         }
245         if (removed && emit_signal) {
246                 PatchesChanged(); /* EMIT SIGNAL */
247         }
248         return removed;
249 }