738441b63120a963b2f078deaab5c63790217149
[ardour.git] / gtk2_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 "midi_patch_manager.h"
25 #include "pbd/file_utils.h"
26 #include "ardour/session.h"
27 #include "ardour/session_directory.h"
28
29 using namespace std;
30 using namespace sigc;
31 using namespace ARDOUR;
32 using namespace MIDI;
33 using namespace MIDI::Name;
34 using namespace PBD;
35 using namespace PBD::sys;
36
37 MidiPatchManager* MidiPatchManager::_manager = 0;
38
39 void
40 MidiPatchManager::set_session (Session& s)
41 {
42         _session = &s;
43         _session->GoingAway.connect (mem_fun (*this, &MidiPatchManager::drop_session));
44         
45         refresh();
46 }
47
48 void
49 MidiPatchManager::refresh()
50 {
51         _documents.clear();
52         
53         path path_to_patches = _session->session_directory().midi_patch_path();
54         
55         if(!exists(path_to_patches)) {
56                 return;
57         }
58         
59         assert(is_directory(path_to_patches));
60         
61         Glib::PatternSpec pattern(Glib::ustring("*.midnam"));
62         vector<path> result;
63         
64         find_matching_files_in_directory(path_to_patches, pattern, result);
65         
66         for(vector<path>::iterator i = result.begin(); i != result.end(); ++i) {
67                 boost::shared_ptr<MIDINameDocument> document(new MIDINameDocument(i->to_string()));
68                 _documents.push_back(document);
69         }
70 }
71
72 void
73 MidiPatchManager::drop_session ()
74 {
75         _session = 0;
76         _documents.clear();
77 }