Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[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 <sigc++/sigc++.h>
22 #include <boost/shared_ptr.hpp>
23
24 #include <pbd/file_utils.h>
25
26 #include "ardour/session.h"
27 #include "ardour/session_directory.h"
28 #include "ardour/midi_patch_manager.h"
29
30 using namespace std;
31 using namespace sigc;
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 void
41 MidiPatchManager::set_session (Session& s)
42 {
43         _session = &s;
44         _session->GoingAway.connect (mem_fun (*this, &MidiPatchManager::drop_session));
45         
46         refresh();
47 }
48
49 void
50 MidiPatchManager::refresh()
51 {
52         _documents.clear();
53         _master_devices_by_model.clear();
54         _all_models.clear();
55         
56         path path_to_patches = _session->session_directory().midi_patch_path();
57         
58         cerr << "Path to patches: " << path_to_patches.to_string() << endl;
59         
60         if(!exists(path_to_patches)) {
61                 return;
62         }
63         cerr << "Path to patches: " << path_to_patches.to_string() << " exists" << endl;
64         
65         assert(is_directory(path_to_patches));
66         
67         Glib::PatternSpec pattern(Glib::ustring("*.midnam"));
68         vector<path> result;
69         
70         find_matching_files_in_directory(path_to_patches, pattern, result);
71
72         cerr << "patchfiles result contains " << result.size() << " elements" << endl;
73         
74         for(vector<path>::iterator i = result.begin(); i != result.end(); ++i) {
75                 cerr << "processing patchfile " << i->to_string() << endl;      
76                 
77                 boost::shared_ptr<MIDINameDocument> document(new MIDINameDocument(i->to_string()));
78                 for(MIDINameDocument::MasterDeviceNamesList::const_iterator device = 
79                           document->master_device_names_by_model().begin();
80                     device != document->master_device_names_by_model().end();
81                     ++device) {
82                         cerr << "got model " << device->first << endl;
83                         // have access to the documents by model name
84                         _documents[device->first] = document;
85                         // build a list of all master devices from all documents
86                         _master_devices_by_model[device->first] = device->second;
87                         _all_models.push_back(device->first);
88                         
89                         // make sure there are no double model names
90                         // TODO: handle this gracefully.
91                         assert(_documents.count(device->first) == 1);
92                         assert(_master_devices_by_model.count(device->first) == 1);
93                 }
94         }
95 }
96
97 void
98 MidiPatchManager::drop_session ()
99 {
100         _session = 0;
101         _documents.clear();
102         _master_devices_by_model.clear();
103         _all_models.clear();
104 }