Decouple Session from MidiPatchManager and reduce parsing of midnam xml files
[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 "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         bool do_refresh = false;
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                 _search_path.add_directory (*i);
68                 do_refresh = true;
69         }
70
71         if (do_refresh) {
72                 refresh();
73         }
74 }
75
76 void
77 MidiPatchManager::remove_search_path (const Searchpath& search_path)
78 {
79         bool do_refresh = false;
80
81         for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
82
83                 if (!_search_path.contains(*i)) {
84                         continue;
85                 }
86
87                 _search_path.remove_directory (*i);
88                 do_refresh = true;
89         }
90
91         if (do_refresh) {
92                 refresh();
93         }
94 }
95
96 bool
97 MidiPatchManager::add_midi_name_document (const std::string& file_path)
98 {
99         boost::shared_ptr<MIDINameDocument> document;
100         try {
101                 document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument(file_path));
102         }
103         catch (...) {
104                 error << "Error parsing MIDI patch file " << file_path << endmsg;
105                 return false;
106         }
107         for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
108                  document->master_device_names_by_model().begin();
109              device != document->master_device_names_by_model().end();
110              ++device) {
111                 if (_documents.find(device->first) != _documents.end()) {
112                         warning << string_compose(_("Duplicate MIDI device `%1' in `%2' ignored"),
113                                                   device->first,
114                                                   file_path) << endmsg;
115                         continue;
116                 }
117
118                 _documents[device->first] = document;
119                 _master_devices_by_model[device->first] = device->second;
120
121                 _all_models.insert(device->first);
122                 const std::string& manufacturer = device->second->manufacturer();
123                 if (_devices_by_manufacturer.find(manufacturer) ==
124                     _devices_by_manufacturer.end()) {
125                         MIDINameDocument::MasterDeviceNamesList empty;
126                         _devices_by_manufacturer.insert(std::make_pair(manufacturer, empty));
127                 }
128                 _devices_by_manufacturer[manufacturer].insert(
129                     std::make_pair(device->first, device->second));
130
131                 // TODO: handle this gracefully.
132                 assert(_documents.count(device->first) == 1);
133                 assert(_master_devices_by_model.count(device->first) == 1);
134         }
135         return true;
136 }
137
138 void
139 MidiPatchManager::refresh()
140 {
141         _documents.clear();
142         _master_devices_by_model.clear();
143         _all_models.clear();
144         _devices_by_manufacturer.clear();
145
146         vector<std::string> result;
147
148         find_files_matching_pattern (result, _search_path, "*.midnam");
149
150         info << "Loading " << result.size() << " MIDI patches from "
151              << _search_path.to_string() << endmsg;
152
153         for (vector<std::string>::iterator i = result.begin(); i != result.end(); ++i) {
154                 add_midi_name_document (*i);
155         }
156 }