Refactor common code in MidiPatchManager class into new method
[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 #include "ardour/search_paths.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace MIDI;
39 using namespace MIDI::Name;
40 using namespace PBD;
41
42 MidiPatchManager* MidiPatchManager::_manager = 0;
43
44 MidiPatchManager::MidiPatchManager ()
45 {
46 }
47
48 void
49 MidiPatchManager::set_session (Session* s)
50 {
51         SessionHandlePtr::set_session (s);
52         refresh ();
53         add_session_patches ();
54 }
55
56 bool
57 MidiPatchManager::add_midi_name_document (const std::string& file_path)
58 {
59         boost::shared_ptr<MIDINameDocument> document;
60         try {
61                 document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument(file_path));
62         }
63         catch (...) {
64                 error << "Error parsing MIDI patch file " << file_path << endmsg;
65                 return false;
66         }
67         for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
68                  document->master_device_names_by_model().begin();
69              device != document->master_device_names_by_model().end();
70              ++device) {
71                 if (_documents.find(device->first) != _documents.end()) {
72                         warning << string_compose(_("Duplicate MIDI device `%1' in `%2' ignored"),
73                                                   device->first,
74                                                   file_path) << endmsg;
75                         continue;
76                 }
77
78                 _documents[device->first] = document;
79                 _master_devices_by_model[device->first] = device->second;
80
81                 _all_models.insert(device->first);
82                 const std::string& manufacturer = device->second->manufacturer();
83                 if (_devices_by_manufacturer.find(manufacturer) ==
84                     _devices_by_manufacturer.end()) {
85                         MIDINameDocument::MasterDeviceNamesList empty;
86                         _devices_by_manufacturer.insert(std::make_pair(manufacturer, empty));
87                 }
88                 _devices_by_manufacturer[manufacturer].insert(
89                     std::make_pair(device->first, device->second));
90
91                 // TODO: handle this gracefully.
92                 assert(_documents.count(device->first) == 1);
93                 assert(_master_devices_by_model.count(device->first) == 1);
94         }
95         return true;
96 }
97
98 void
99 MidiPatchManager::add_session_patches ()
100 {
101         if (!_session) {
102                 return;
103         }
104
105         std::string path_to_patches = _session->session_directory().midi_patch_path();
106
107         if (!Glib::file_test (path_to_patches, Glib::FILE_TEST_EXISTS)) {
108                 return;
109         }
110
111         assert (Glib::file_test (path_to_patches, Glib::FILE_TEST_IS_DIR));
112
113         vector<std::string> result;
114
115         find_files_matching_pattern (result, path_to_patches, "*.midnam");
116
117         info << "Loading " << result.size() << " MIDI patches from " << path_to_patches << endmsg;
118
119         for (vector<std::string>::iterator i = result.begin(); i != result.end(); ++i) {
120                 add_midi_name_document(*i);
121         }
122 }
123
124 void
125 MidiPatchManager::refresh()
126 {
127         _documents.clear();
128         _master_devices_by_model.clear();
129         _all_models.clear();
130         _devices_by_manufacturer.clear();
131
132         Searchpath search_path = midi_patch_search_path ();
133         vector<std::string> result;
134
135         find_files_matching_pattern (result, search_path, "*.midnam");
136
137         info << "Loading " << result.size() << " MIDI patches from " << search_path.to_string() << endmsg;
138
139         for (vector<std::string>::iterator i = result.begin(); i != result.end(); ++i) {
140                 add_midi_name_document (*i);
141         }
142
143         if (_session) {
144                 add_session_patches ();
145         }
146 }
147
148 void
149 MidiPatchManager::session_going_away ()
150 {
151         SessionHandlePtr::session_going_away ();
152         _documents.clear();
153         _master_devices_by_model.clear();
154         _all_models.clear();
155         _devices_by_manufacturer.clear();
156 }