xref: /haiku/src/preferences/media/MidiSettingsView.cpp (revision 8a6724a0ee3803f1e9f487d8111bb3f6cb8d16db)
1 /*
2  * Copyright 2014, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "MidiSettingsView.h"
7 
8 #include <MidiSettings.h>
9 
10 #include <Box.h>
11 #include <Catalog.h>
12 #include <Directory.h>
13 #include <Entry.h>
14 #include <FindDirectory.h>
15 #include <GridView.h>
16 #include <LayoutBuilder.h>
17 #include <ListView.h>
18 #include <Locale.h>
19 #include <Node.h>
20 #include <NodeInfo.h>
21 #include <Path.h>
22 #include <PathFinder.h>
23 #include <ScrollView.h>
24 #include <StringList.h>
25 #include <StringView.h>
26 
27 #include <stdio.h>
28 
29 
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "Midi View"
32 
33 const static uint32 kSelectSoundFont = 'SeSf';
34 
35 
36 MidiSettingsView::MidiSettingsView()
37 	:
38 	SettingsView()
39 {
40 	BBox* defaultsBox = new BBox("SoundFont");
41 	defaultsBox->SetLabel(B_TRANSLATE("Available SoundFonts"));
42 	BGridView* defaultsGridView = new BGridView();
43 
44 	fListView = new BListView(B_SINGLE_SELECTION_LIST);
45 	fListView->SetSelectionMessage(new BMessage(kSelectSoundFont));
46 
47 	BScrollView *scrollView = new BScrollView("ScrollView", fListView,
48 			0, false, true);
49 	BLayoutBuilder::Grid<>(defaultsGridView)
50 		.SetInsets(B_USE_DEFAULT_SPACING, 0, B_USE_DEFAULT_SPACING,
51 				B_USE_DEFAULT_SPACING)
52 		.Add(scrollView, 0, 0);
53 
54 	defaultsBox->AddChild(defaultsGridView);
55 
56 	BLayoutBuilder::Group<>(this)
57 		.SetInsets(0, 0, 0, 0)
58 		.Add(defaultsBox)
59 		.Add(fActiveSoundFont = new BStringView("ActiveSoundFont",
60 				"Active Sound Font:"))
61 		.AddGlue();
62 }
63 
64 
65 /* virtual */
66 void
67 MidiSettingsView::AttachedToWindow()
68 {
69 	SettingsView::AttachedToWindow();
70 
71 	fListView->SetTarget(this);
72 	_RetrieveSoftSynthList();
73 
74 	_LoadSettings();
75 }
76 
77 
78 /* virtual */
79 void
80 MidiSettingsView::MessageReceived(BMessage* message)
81 {
82 	switch (message->what) {
83 		case kSelectSoundFont:
84 		{
85 			BString text = _SelectedSoundFont();
86 			if (text != "") {
87 				text.Prepend("Active Sound Font: ");
88 				fActiveSoundFont->SetText(text);
89 				_SaveSettings();
90 			}
91 			break;
92 		}
93 
94 		default:
95 			SettingsView::MessageReceived(message);
96 			break;
97 	}
98 }
99 
100 
101 void
102 MidiSettingsView::_RetrieveSoftSynthList()
103 {
104 	// TODO: Duplicated code between here
105 	// and BSoftSynth::SetDefaultInstrumentsFile
106 	BStringList paths;
107 	status_t status = BPathFinder::FindPaths(B_FIND_PATH_DATA_DIRECTORY,
108 			"synth", paths);
109 	if (status != B_OK)
110 		return;
111 
112 	fListView->MakeEmpty();
113 
114 	for (int32 i = 0; i < paths.CountStrings(); i++) {
115 		BDirectory directory(paths.StringAt(i).String());
116 		BEntry entry;
117 		if (directory.InitCheck() != B_OK)
118 			continue;
119 		while (directory.GetNextEntry(&entry) == B_OK) {
120 			BNode node(&entry);
121 			BNodeInfo nodeInfo(&node);
122 			char mimeType[B_MIME_TYPE_LENGTH];
123 			// TODO: For some reason the mimetype check fails.
124 			// maybe because the file hasn't yet been sniffed and recognized?
125 			if (nodeInfo.GetType(mimeType) == B_OK
126 				/*&& !strcmp(mimeType, "audio/x-soundfont")*/) {
127 				BPath fullPath = paths.StringAt(i).String();
128 				fullPath.Append(entry.Name());
129 				fListView->AddItem(new BStringItem(fullPath.Path()));
130 			}
131 		}
132 	}
133 }
134 
135 
136 void
137 MidiSettingsView::_LoadSettings()
138 {
139 	struct BPrivate::midi_settings settings;
140 	if (BPrivate::read_midi_settings(&settings) == B_OK) {
141 		for (int32 i = 0; i < fListView->CountItems(); i++) {
142 			BStringItem* item = (BStringItem*)fListView->ItemAt(i);
143 			if (!strcmp(item->Text(), settings.soundfont_file)) {
144 				fListView->Select(i);
145 				break;
146 			}
147 		}
148 	}
149 }
150 
151 
152 void
153 MidiSettingsView::_SaveSettings()
154 {
155 	BString soundFont = _SelectedSoundFont();
156 	if (soundFont.Length() > 0) {
157 		struct BPrivate::midi_settings settings;
158 		strlcpy(settings.soundfont_file, soundFont, sizeof(settings.soundfont_file));
159 		BPrivate::write_midi_settings(settings);
160 	}
161 }
162 
163 
164 BString
165 MidiSettingsView::_SelectedSoundFont() const
166 {
167 	BString string;
168 	int32 selection = fListView->CurrentSelection();
169 	if (selection >= 0) {
170 		BStringItem* item = (BStringItem*)fListView->ItemAt(selection);
171 		if (item != NULL)
172 			string = item->Text();
173 	}
174 
175 	return string;
176 }
177