Remove over 500 unnecessary includes (including 54 of session.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 "pbd/file_utils.h"
24 #include "pbd/error.h"
25
26 #include "ardour/session.h"
27 #include "ardour/session_directory.h"
28 #include "ardour/midi_patch_manager.h"
29 #include "ardour/midi_patch_search_path.h"
30
31 using namespace std;
32 using namespace ARDOUR;
33 using namespace MIDI;
34 using namespace MIDI::Name;
35 using namespace PBD;
36 using namespace PBD::sys;
37
38 MidiPatchManager* MidiPatchManager::_manager = 0;
39
40 MidiPatchManager::MidiPatchManager ()
41 {
42         refresh ();
43 }
44
45 void
46 MidiPatchManager::set_session (Session* s)
47 {
48         SessionHandlePtr::set_session (s);
49         add_session_patches ();
50 }
51
52 void
53 MidiPatchManager::add_session_patches ()
54 {
55         if (!_session) {
56                 return;
57         }
58         
59         path path_to_patches = _session->session_directory().midi_patch_path();
60
61         if (!exists (path_to_patches)) {
62                 return;
63         }
64
65         assert(is_directory(path_to_patches));
66
67         Glib::PatternSpec pattern(string("*.midnam"));
68         vector<path> result;
69
70         find_matching_files_in_directory (path_to_patches, pattern, result);
71
72         info << "Loading " << result.size() << " MIDI patches from " << path_to_patches.to_string() << endmsg;
73
74         for (vector<path>::iterator i = result.begin(); i != result.end(); ++i) {
75                 boost::shared_ptr<MIDINameDocument> document(new MIDINameDocument(i->to_string()));
76                 for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
77                                         document->master_device_names_by_model().begin();
78                                 device != document->master_device_names_by_model().end();
79                                 ++device) {
80                         //cerr << "got model " << device->first << endl;
81                         // have access to the documents by model name
82                         _documents[device->first] = document;
83                         // build a list of all master devices from all documents
84                         _master_devices_by_model[device->first] = device->second;
85                         _all_models.push_back(device->first);
86
87                         // make sure there are no double model names
88                         // TODO: handle this gracefully.
89                         assert(_documents.count(device->first) == 1);
90                         assert(_master_devices_by_model.count(device->first) == 1);
91                 }
92         }
93 }
94
95 void
96 MidiPatchManager::refresh()
97 {
98         _documents.clear();
99         _master_devices_by_model.clear();
100         _all_models.clear();
101
102         SearchPath search_path = midi_patch_search_path ();
103         Glib::PatternSpec pattern (string("*.midnam"));
104         vector<path> result;
105
106         find_matching_files_in_search_path (search_path, pattern, result);
107
108         info << "Loading " << result.size() << " MIDI patches from " << search_path.to_string() << endmsg;
109
110         for (vector<path>::iterator i = result.begin(); i != result.end(); ++i) {
111                 boost::shared_ptr<MIDINameDocument> document(new MIDINameDocument(i->to_string()));
112                 for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
113                                         document->master_device_names_by_model().begin();
114                                 device != document->master_device_names_by_model().end();
115                                 ++device) {
116                         //cerr << "got model " << device->first << endl;
117                         // have access to the documents by model name
118                         _documents[device->first] = document;
119                         // build a list of all master devices from all documents
120                         _master_devices_by_model[device->first] = device->second;
121                         _all_models.push_back(device->first);
122
123                         // make sure there are no double model names
124                         // TODO: handle this gracefully.
125                         assert(_documents.count(device->first) == 1);
126                         assert(_master_devices_by_model.count(device->first) == 1);
127                 }
128         }
129
130         if (_session) {
131                 add_session_patches ();
132         }
133 }
134
135 void
136 MidiPatchManager::session_going_away ()
137 {
138         SessionHandlePtr::session_going_away ();
139         refresh ();
140 }