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