2 * Copyright 2014, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 #include "MidiSettingsView.h"
10 #include <Directory.h>
12 #include <FindDirectory.h>
14 #include <LayoutBuilder.h>
20 #include <PathFinder.h>
21 #include <ScrollView.h>
22 #include <StringList.h>
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "Midi View"
30 #define SETTINGS_FILE "midi"
32 const static uint32 kSelectSoundFont
= 'SeSf';
35 MidiSettingsView::MidiSettingsView()
39 BBox
* defaultsBox
= new BBox("SoundFont");
40 defaultsBox
->SetLabel(B_TRANSLATE("SoundFont"));
41 BGridView
* defaultsGridView
= new BGridView();
43 fListView
= new BListView(B_SINGLE_SELECTION_LIST
);
44 fListView
->SetSelectionMessage(new BMessage(kSelectSoundFont
));
46 BScrollView
*scrollView
= new BScrollView("ScrollView", fListView
,
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);
53 defaultsBox
->AddChild(defaultsGridView
);
55 BLayoutBuilder::Group
<>(this)
56 .SetInsets(0, 0, 0, 0)
64 MidiSettingsView::AttachedToWindow()
66 SettingsView::AttachedToWindow();
68 fListView
->SetTarget(this);
69 _RetrieveSoftSynthList();
77 MidiSettingsView::MessageReceived(BMessage
* message
)
79 switch (message
->what
) {
80 case kSelectSoundFont
:
85 SettingsView::MessageReceived(message
);
92 MidiSettingsView::_RetrieveSoftSynthList()
95 status_t status
= BPathFinder::FindPaths(B_FIND_PATH_DATA_DIRECTORY
,
100 fListView
->MakeEmpty();
102 for (int32 i
= 0; i
< paths
.CountStrings(); i
++) {
103 BDirectory
directory(paths
.StringAt(i
).String());
105 if (directory
.InitCheck() != B_OK
)
107 while (directory
.GetNextEntry(&entry
) == B_OK
) {
109 BNodeInfo
nodeInfo(&node
);
110 char mimeType
[B_MIME_TYPE_LENGTH
];
111 // TODO: For some reason this doesn't work
112 if (nodeInfo
.GetType(mimeType
) == B_OK
113 /*&& !strcmp(mimeType, "audio/x-soundfont")*/) {
114 BPath fullPath
= paths
.StringAt(i
).String();
115 fullPath
.Append(entry
.Name());
116 fListView
->AddItem(new BStringItem(fullPath
.Path()));
124 MidiSettingsView::_LoadSettings()
126 // TODO: Duplicated code between here
127 // and BSoftSynth::SetDefaultInstrumentsFile
130 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &path
) == B_OK
) {
131 path
.Append(SETTINGS_FILE
);
132 BFile
file(path
.Path(), B_READ_ONLY
);
133 if (file
.InitCheck() == B_OK
) {
134 file
.Read(buffer
, sizeof(buffer
));
136 sscanf(buffer
, "# Midi Settings\n soundfont = %s\n",
139 for (int32 i
= 0; i
< fListView
->CountItems(); i
++) {
140 BStringItem
* item
= (BStringItem
*)fListView
->ItemAt(i
);
141 if (!strcmp(item
->Text(), soundFont
)) {
142 fListView
->Select(i
);
152 MidiSettingsView::_SaveSettings()
154 int32 selection
= fListView
->CurrentSelection();
158 BStringItem
* item
= (BStringItem
*)fListView
->ItemAt(selection
);
163 snprintf(buffer
, 512, "# Midi Settings\n soundfont = %s\n",
167 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &path
) == B_OK
) {
168 path
.Append(SETTINGS_FILE
);
169 BFile
file(path
.Path(), B_READ_WRITE
| B_CREATE_FILE
| B_ERASE_FILE
);
170 if (file
.InitCheck() == B_OK
)
171 file
.Write(buffer
, strlen(buffer
));