undef SearchPath where needed as it is defined via windows.h
[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/session.h"
29 #include "ardour/session_directory.h"
30 #include "ardour/midi_patch_manager.h"
31
32 #ifdef SearchPath
33 #undef SearchPath
34 #endif
35
36 #include "ardour/midi_patch_search_path.h"
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace ARDOUR;
42 using namespace MIDI;
43 using namespace MIDI::Name;
44 using namespace PBD;
45
46 MidiPatchManager* MidiPatchManager::_manager = 0;
47
48 MidiPatchManager::MidiPatchManager ()
49 {
50         refresh ();
51 }
52
53 void
54 MidiPatchManager::set_session (Session* s)
55 {
56         SessionHandlePtr::set_session (s);
57         add_session_patches ();
58 }
59
60 void
61 MidiPatchManager::add_session_patches ()
62 {
63         if (!_session) {
64                 return;
65         }
66         
67         std::string path_to_patches = _session->session_directory().midi_patch_path();
68
69         if (!Glib::file_test (path_to_patches, Glib::FILE_TEST_EXISTS)) {
70                 return;
71         }
72
73         assert (Glib::file_test (path_to_patches, Glib::FILE_TEST_IS_DIR));
74
75         Glib::PatternSpec pattern(string("*.midnam"));
76         vector<std::string> result;
77
78         find_matching_files_in_directory (path_to_patches, pattern, result);
79
80         info << "Loading " << result.size() << " MIDI patches from " << path_to_patches << endmsg;
81
82         for (vector<std::string>::iterator i = result.begin(); i != result.end(); ++i) {
83                 boost::shared_ptr<MIDINameDocument> document(new MIDINameDocument(*i));
84                 for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
85                                         document->master_device_names_by_model().begin();
86                                 device != document->master_device_names_by_model().end();
87                                 ++device) {
88                         //cerr << "got model " << device->first << endl;
89                         // have access to the documents by model name
90                         _documents[device->first] = document;
91                         // build a list of all master devices from all documents
92                         _master_devices_by_model[device->first] = device->second;
93                         _all_models.insert(device->first);
94
95                         // make sure there are no double model names
96                         // TODO: handle this gracefully.
97                         assert(_documents.count(device->first) == 1);
98                         assert(_master_devices_by_model.count(device->first) == 1);
99                 }
100         }
101 }
102
103 void
104 MidiPatchManager::refresh()
105 {
106         _documents.clear();
107         _master_devices_by_model.clear();
108         _all_models.clear();
109
110         SearchPath search_path = midi_patch_search_path ();
111         Glib::PatternSpec pattern (string("*.midnam"));
112         vector<std::string> result;
113
114         find_matching_files_in_search_path (search_path, pattern, result);
115
116         info << "Loading " << result.size() << " MIDI patches from " << search_path.to_string() << endmsg;
117
118         for (vector<std::string>::iterator i = result.begin(); i != result.end(); ++i) {
119                 boost::shared_ptr<MIDINameDocument> document;
120                 try {
121                         document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument(*i));
122                 } catch (...) {
123                         error << "Error parsing MIDI patch file " << *i << endmsg;
124                         continue;
125                 }
126                 for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
127                              document->master_device_names_by_model().begin();
128                      device != document->master_device_names_by_model().end();
129                      ++device) {
130                         if (_documents.find(device->first) != _documents.end()) {
131                                 warning << string_compose(_("Duplicate MIDI device `%1' in `%2' ignored"),
132                                                           device->first, *i)
133                                         << endmsg;
134                                 continue;
135                         }
136                                                       
137                         _documents[device->first]               = document;
138                         _master_devices_by_model[device->first] = device->second;
139
140                         _all_models.insert(device->first);
141                 }
142         }
143
144         if (_session) {
145                 add_session_patches ();
146         }
147 }
148
149 void
150 MidiPatchManager::session_going_away ()
151 {
152         SessionHandlePtr::session_going_away ();
153         refresh ();
154 }