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