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 BString text = "Active Sound Font: "; 85 text << _SelectedSoundFont(); 86 fActiveSoundFont->SetText(text); 87 _SaveSettings(); 88 break; 89 } 90 91 default: 92 SettingsView::MessageReceived(message); 93 break; 94 } 95 } 96 97 98 void 99 MidiSettingsView::_RetrieveSoftSynthList() 100 { 101 // TODO: Duplicated code between here 102 // and BSoftSynth::SetDefaultInstrumentsFile 103 BStringList paths; 104 status_t status = BPathFinder::FindPaths(B_FIND_PATH_DATA_DIRECTORY, 105 "synth", paths); 106 if (status != B_OK) 107 return; 108 109 fListView->MakeEmpty(); 110 111 for (int32 i = 0; i < paths.CountStrings(); i++) { 112 BDirectory directory(paths.StringAt(i).String()); 113 BEntry entry; 114 if (directory.InitCheck() != B_OK) 115 continue; 116 while (directory.GetNextEntry(&entry) == B_OK) { 117 BNode node(&entry); 118 BNodeInfo nodeInfo(&node); 119 char mimeType[B_MIME_TYPE_LENGTH]; 120 // TODO: For some reason the mimetype check fails. 121 // maybe because the file hasn't yet been sniffed and recognized? 122 if (nodeInfo.GetType(mimeType) == B_OK 123 /*&& !strcmp(mimeType, "audio/x-soundfont")*/) { 124 BPath fullPath = paths.StringAt(i).String(); 125 fullPath.Append(entry.Name()); 126 fListView->AddItem(new BStringItem(fullPath.Path())); 127 } 128 } 129 } 130 } 131 132 133 void 134 MidiSettingsView::_LoadSettings() 135 { 136 struct BPrivate::midi_settings settings; 137 if (BPrivate::read_midi_settings(&settings) == B_OK) { 138 for (int32 i = 0; i < fListView->CountItems(); i++) { 139 BStringItem* item = (BStringItem*)fListView->ItemAt(i); 140 if (!strcmp(item->Text(), settings.soundfont_file)) { 141 fListView->Select(i); 142 break; 143 } 144 } 145 } 146 } 147 148 149 void 150 MidiSettingsView::_SaveSettings() 151 { 152 BString soundFont = _SelectedSoundFont(); 153 if (soundFont.Length() > 0) { 154 struct BPrivate::midi_settings settings; 155 strlcpy(settings.soundfont_file, soundFont, sizeof(settings.soundfont_file)); 156 BPrivate::write_midi_settings(settings); 157 } 158 } 159 160 161 BString 162 MidiSettingsView::_SelectedSoundFont() const 163 { 164 BString string; 165 int32 selection = fListView->CurrentSelection(); 166 if (selection >= 0) { 167 BStringItem* item = (BStringItem*)fListView->ItemAt(selection); 168 if (item != NULL) 169 string = item->Text(); 170 } 171 172 return string; 173 } 174