2 * Copyright 2014-2017, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 #include "MidiSettingsView.h"
8 #include <MidiSettings.h>
12 #include <Directory.h>
14 #include <FindDirectory.h>
16 #include <LayoutBuilder.h>
21 #include <NodeMonitor.h>
23 #include <PathFinder.h>
24 #include <ScrollView.h>
25 #include <StringList.h>
26 #include <StringView.h>
31 #undef B_TRANSLATION_CONTEXT
32 #define B_TRANSLATION_CONTEXT "Midi View"
34 const static uint32 kSelectSoundFont
= 'SeSf';
35 const static uint32 kDoubleClick
= 'DClk';
38 MidiSettingsView::MidiSettingsView()
42 BBox
* defaultsBox
= new BBox("SoundFonts");
43 defaultsBox
->SetLabel(B_TRANSLATE("SoundFonts"));
44 BGridView
* defaultsGridView
= new BGridView();
46 fListView
= new BListView(B_SINGLE_SELECTION_LIST
);
47 fListView
->SetSelectionMessage(new BMessage(kSelectSoundFont
));
48 fListView
->SetInvocationMessage(new BMessage(kDoubleClick
));
50 BScrollView
* scrollView
= new BScrollView("ScrollView", fListView
,
52 BLayoutBuilder::Grid
<>(defaultsGridView
)
53 .SetInsets(B_USE_DEFAULT_SPACING
, 0, B_USE_DEFAULT_SPACING
,
54 B_USE_DEFAULT_SPACING
)
55 .Add(scrollView
, 0, 0);
57 defaultsBox
->AddChild(defaultsGridView
);
58 fSoundFontStatus
= new BStringView("SoundFontStatus", "");
60 BLayoutBuilder::Group
<>(this)
61 .SetInsets(0, 0, 0, 0)
63 .AddGroup(B_HORIZONTAL
)
65 .Add(fSoundFontStatus
)
74 MidiSettingsView::AttachedToWindow()
76 SettingsView::AttachedToWindow();
78 fListView
->SetTarget(this);
80 _RetrieveSoundFontList();
81 _SelectActiveSoundFont();
82 _UpdateSoundFontStatus();
89 MidiSettingsView::DetachedFromWindow()
91 SettingsView::DetachedFromWindow();
99 MidiSettingsView::MessageReceived(BMessage
* message
)
101 switch (message
->what
) {
104 int32 selected
= fListView
->CurrentSelection();
105 BStringItem
* olditem
= (BStringItem
*)fListView
->ItemAt(selected
);
107 _RetrieveSoundFontList();
109 int32 count
= fListView
->CountItems();
111 fListView
->Select(0);
113 } else if (olditem
!= NULL
) {
114 for (int32 i
= 0; i
< fListView
->CountItems(); i
++) {
115 BStringItem
* item
= (BStringItem
*)fListView
->ItemAt(i
);
116 if (strcmp(item
->Text(), olditem
->Text()) == 0) {
117 fListView
->Select(i
);
122 _UpdateSoundFontStatus();
125 case kSelectSoundFont
:
127 int selection
= fListView
->CurrentSelection();
129 _SelectActiveSoundFont();
133 _UpdateSoundFontStatus();
138 int selection
= fListView
->CurrentSelection();
142 BStringItem
* item
= (BStringItem
*)fListView
->ItemAt(selection
);
144 BEntry
entry(item
->Text());
146 entry
.GetParent(&parent
);
148 parent
.GetRef(&folderRef
);
149 BMessenger
msgr("application/x-vnd.Be-TRAK");
150 BMessage
refMsg(B_REFS_RECEIVED
);
151 refMsg
.AddRef("refs",&folderRef
);
152 msgr
.SendMessage(&refMsg
);
157 SettingsView::MessageReceived(message
);
164 MidiSettingsView::_RetrieveSoundFontList()
166 // TODO: Duplicated code between here
167 // and BSoftSynth::SetDefaultInstrumentsFile
169 status_t status
= BPathFinder::FindPaths(B_FIND_PATH_DATA_DIRECTORY
,
174 fListView
->MakeEmpty();
176 for (int32 i
= 0; i
< paths
.CountStrings(); i
++) {
177 BDirectory
directory(paths
.StringAt(i
).String());
179 if (directory
.InitCheck() != B_OK
)
181 while (directory
.GetNextEntry(&entry
) == B_OK
) {
183 BNodeInfo
nodeInfo(&node
);
184 char mimeType
[B_MIME_TYPE_LENGTH
];
185 // TODO: For some reason the mimetype check fails.
186 // maybe because the file hasn't yet been sniffed and recognized?
187 if (nodeInfo
.GetType(mimeType
) == B_OK
188 /*&& !strcmp(mimeType, "audio/x-soundfont")*/) {
189 BPath fullPath
= paths
.StringAt(i
).String();
190 fullPath
.Append(entry
.Name());
191 fListView
->AddItem(new BStringItem(fullPath
.Path()));
199 MidiSettingsView::_LoadSettings()
201 struct BPrivate::midi_settings settings
;
202 if (BPrivate::read_midi_settings(&settings
) == B_OK
)
203 fActiveSoundFont
.SetTo(settings
.soundfont_file
);
208 MidiSettingsView::_SaveSettings()
210 fActiveSoundFont
= _SelectedSoundFont();
211 if (fActiveSoundFont
.Length() > 0) {
212 struct BPrivate::midi_settings settings
;
213 strlcpy(settings
.soundfont_file
, fActiveSoundFont
.String(),
214 sizeof(settings
.soundfont_file
));
215 BPrivate::write_midi_settings(settings
);
221 MidiSettingsView::_SelectActiveSoundFont()
223 int32 count
= fListView
->CountItems();
225 fListView
->Select(0);
229 for (int32 i
= 0; i
< fListView
->CountItems(); i
++) {
230 BStringItem
* item
= (BStringItem
*)fListView
->ItemAt(i
);
231 if (strcmp(item
->Text(), fActiveSoundFont
.String()) == 0) {
232 fListView
->Select(i
);
240 MidiSettingsView::_SelectedSoundFont() const
243 int32 selection
= fListView
->CurrentSelection();
244 if (selection
>= 0) {
245 BStringItem
* item
= (BStringItem
*)fListView
->ItemAt(selection
);
247 string
= item
->Text();
255 MidiSettingsView::_UpdateSoundFontStatus()
257 if (fListView
->IsEmpty()) {
258 fSoundFontStatus
->SetText(
259 B_TRANSLATE("There are no SoundFont installed."));
262 int32 selection
= fListView
->CurrentSelection();
264 fSoundFontStatus
->SetText(
265 B_TRANSLATE("Please select a SoundFont."));
268 fSoundFontStatus
->SetText("");
273 MidiSettingsView::_WatchFolders()
276 BPathFinder().FindPaths(B_FIND_PATH_DATA_DIRECTORY
, "synth", paths
);
277 for (int32 i
= 0; i
< paths
.CountStrings(); i
++) {
278 BEntry
entry(paths
.StringAt(i
));
280 entry
.GetNodeRef(&nref
);
282 watch_node(&nref
, B_WATCH_ALL
, this);