xref: /haiku/src/preferences/media/MidiSettingsView.cpp (revision a3e794ae459fec76826407f8ba8c94cd3535f128)
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 
26 #include <stdio.h>
27 
28 
29 #undef B_TRANSLATION_CONTEXT
30 #define B_TRANSLATION_CONTEXT "Midi View"
31 
32 const static uint32 kSelectSoundFont = 'SeSf';
33 
34 
35 MidiSettingsView::MidiSettingsView()
36 	:
37 	SettingsView()
38 {
39 	BBox* defaultsBox = new BBox("SoundFont");
40 	defaultsBox->SetLabel(B_TRANSLATE("Available SoundFonts"));
41 	BGridView* defaultsGridView = new BGridView();
42 
43 	fListView = new BListView(B_SINGLE_SELECTION_LIST);
44 	fListView->SetSelectionMessage(new BMessage(kSelectSoundFont));
45 
46 	BScrollView *scrollView = new BScrollView("ScrollView", fListView,
47 			0, false, true);
48 	BLayoutBuilder::Grid<>(defaultsGridView)
49 		.SetInsets(B_USE_DEFAULT_SPACING, 0, B_USE_DEFAULT_SPACING,
50 				B_USE_DEFAULT_SPACING)
51 		.Add(scrollView, 0, 0);
52 
53 	defaultsBox->AddChild(defaultsGridView);
54 
55 	BLayoutBuilder::Group<>(this)
56 		.SetInsets(0, 0, 0, 0)
57 		.Add(defaultsBox)
58 		.AddGlue();
59 }
60 
61 
62 /* virtual */
63 void
64 MidiSettingsView::AttachedToWindow()
65 {
66 	SettingsView::AttachedToWindow();
67 
68 	fListView->SetTarget(this);
69 	_RetrieveSoftSynthList();
70 
71 	_LoadSettings();
72 }
73 
74 
75 /* virtual */
76 void
77 MidiSettingsView::MessageReceived(BMessage* message)
78 {
79 	switch (message->what) {
80 		case kSelectSoundFont:
81 			_SaveSettings();
82 			break;
83 
84 		default:
85 			SettingsView::MessageReceived(message);
86 			break;
87 	}
88 }
89 
90 
91 void
92 MidiSettingsView::_RetrieveSoftSynthList()
93 {
94 	// TODO: Duplicated code between here
95 	// and BSoftSynth::SetDefaultInstrumentsFile
96 	BStringList paths;
97 	status_t status = BPathFinder::FindPaths(B_FIND_PATH_DATA_DIRECTORY,
98 			"synth", paths);
99 	if (status != B_OK)
100 		return;
101 
102 	fListView->MakeEmpty();
103 
104 	for (int32 i = 0; i < paths.CountStrings(); i++) {
105 		BDirectory directory(paths.StringAt(i).String());
106 		BEntry entry;
107 		if (directory.InitCheck() != B_OK)
108 			continue;
109 		while (directory.GetNextEntry(&entry) == B_OK) {
110 			BNode node(&entry);
111 			BNodeInfo nodeInfo(&node);
112 			char mimeType[B_MIME_TYPE_LENGTH];
113 			// TODO: For some reason the mimetype check fails.
114 			// maybe because the file hasn't yet been sniffed and recognized?
115 			if (nodeInfo.GetType(mimeType) == B_OK
116 				/*&& !strcmp(mimeType, "audio/x-soundfont")*/) {
117 				BPath fullPath = paths.StringAt(i).String();
118 				fullPath.Append(entry.Name());
119 				fListView->AddItem(new BStringItem(fullPath.Path()));
120 			}
121 		}
122 	}
123 }
124 
125 
126 void
127 MidiSettingsView::_LoadSettings()
128 {
129 	struct BPrivate::midi_settings settings;
130 	if (BPrivate::read_midi_settings(&settings) == B_OK) {
131 		for (int32 i = 0; i < fListView->CountItems(); i++) {
132 			BStringItem* item = (BStringItem*)fListView->ItemAt(i);
133 			if (!strcmp(item->Text(), settings.soundfont_file)) {
134 				fListView->Select(i);
135 				break;
136 			}
137 		}
138 	}
139 }
140 
141 
142 void
143 MidiSettingsView::_SaveSettings()
144 {
145 	int32 selection = fListView->CurrentSelection();
146 	if (selection < 0)
147 		return;
148 
149 	BStringItem* item = (BStringItem*)fListView->ItemAt(selection);
150 	if (item == NULL)
151 		return;
152 
153 	struct BPrivate::midi_settings settings;
154 	strlcpy(settings.soundfont_file, item->Text(), sizeof(settings.soundfont_file));
155 	BPrivate::write_midi_settings(settings);
156 }
157 
158