xref: /haiku/src/preferences/media/MidiSettingsView.cpp (revision db5b5dfa1417cace160abbf2c554010476bf3416)
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 <Box.h>
9 #include <Catalog.h>
10 #include <Directory.h>
11 #include <Entry.h>
12 #include <FindDirectory.h>
13 #include <GridView.h>
14 #include <LayoutBuilder.h>
15 #include <ListView.h>
16 #include <Locale.h>
17 #include <Node.h>
18 #include <NodeInfo.h>
19 #include <Path.h>
20 #include <ScrollView.h>
21 
22 #include <stdio.h>
23 
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "Midi View"
26 
27 #define SETTINGS_FILE "midi"
28 
29 const static uint32 kSelectSoundFont = 'SeSf';
30 
31 
32 MidiSettingsView::MidiSettingsView()
33 	:
34 	SettingsView()
35 {
36 	BBox* defaultsBox = new BBox("SoundFont");
37 	defaultsBox->SetLabel(B_TRANSLATE("SoundFont"));
38 	BGridView* defaultsGridView = new BGridView();
39 
40 	fListView = new BListView(B_SINGLE_SELECTION_LIST);
41 	fListView->SetSelectionMessage(new BMessage(kSelectSoundFont));
42 
43 	BScrollView *scrollView = new BScrollView("ScrollView", fListView,
44 			0, false, true);
45 	BLayoutBuilder::Grid<>(defaultsGridView)
46 		.SetInsets(B_USE_DEFAULT_SPACING, 0, B_USE_DEFAULT_SPACING,
47 				B_USE_DEFAULT_SPACING)
48 		.Add(scrollView, 0, 0);
49 
50 	defaultsBox->AddChild(defaultsGridView);
51 
52 	BLayoutBuilder::Group<>(this)
53 		.SetInsets(0, 0, 0, 0)
54 		.Add(defaultsBox)
55 		.AddGlue();
56 }
57 
58 
59 /* virtual */
60 void
61 MidiSettingsView::AttachedToWindow()
62 {
63 	SettingsView::AttachedToWindow();
64 
65 	fListView->SetTarget(this);
66 	_RetrieveSoftSynthList();
67 
68 	_LoadSettings();
69 }
70 
71 
72 /* virtual */
73 void
74 MidiSettingsView::MessageReceived(BMessage* message)
75 {
76 	switch (message->what) {
77 		case kSelectSoundFont:
78 			_SaveSettings();
79 			break;
80 
81 		default:
82 			SettingsView::MessageReceived(message);
83 			break;
84 	}
85 }
86 
87 
88 void
89 MidiSettingsView::_RetrieveSoftSynthList()
90 {
91 	char **paths = NULL;
92 	size_t pathCount = 0;
93 	status_t status = find_paths(B_FIND_PATH_DATA_DIRECTORY, "synth",
94 			&paths, &pathCount);
95 	if (status != B_OK)
96 		return;
97 
98 	fListView->MakeEmpty();
99 
100 	for (size_t i = 0; i < pathCount; i++) {
101 		BDirectory directory(paths[i]);
102 		BEntry entry;
103 		if (directory.InitCheck() != B_OK)
104 			continue;
105 		while (directory.GetNextEntry(&entry) == B_OK) {
106 			// TODO: Get rid of this as soon as we update
107 			// the SoundFont package not to create the link
108 			// anymore
109 			if (!strcmp(entry.Name(), "big_synth.sy"))
110 				continue;
111 			BNode node(&entry);
112 			BNodeInfo nodeInfo(&node);
113 			char mimeType[B_MIME_TYPE_LENGTH];
114 			// TODO: For some reason this doesn't work
115 			if (nodeInfo.GetType(mimeType) == B_OK
116 					/*&& !strcmp(mimeType, "audio/x-soundfont")*/) {
117 				BPath fullPath = paths[i];
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 	// TODO: Duplicated code between here
130 	// and BSoftSynth::LoadDefaultInstruments
131 
132 	char buffer[512];
133 	BPath path;
134 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
135 		path.Append(SETTINGS_FILE);
136 		BFile file(path.Path(), B_READ_ONLY);
137 		if (file.InitCheck() == B_OK)
138 			file.Read(buffer, sizeof(buffer));
139 	}
140 
141 	char soundFont[512];
142 	sscanf(buffer, "# Midi Settings\n soundfont = %s\n",
143 		soundFont);
144 
145 	for (int32 i = 0; i < fListView->CountItems(); i++) {
146 		BStringItem* item = (BStringItem*)fListView->ItemAt(i);
147 		if (!strcmp(item->Text(), soundFont)) {
148 			fListView->Select(i);
149 			break;
150 		}
151 	}
152 }
153 
154 
155 void
156 MidiSettingsView::_SaveSettings()
157 {
158 	int32 selection = fListView->CurrentSelection();
159 	if (selection < 0)
160 		return;
161 	BStringItem* item = (BStringItem*)fListView->ItemAt(selection);
162 	if (item == NULL)
163 		return;
164 
165 	char buffer[512];
166 	snprintf(buffer, 512, "# Midi Settings\n soundfont = %s\n",
167 			item->Text());
168 
169 	BPath path;
170 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
171 		path.Append(SETTINGS_FILE);
172 		BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
173 		if (file.InitCheck() == B_OK)
174 			file.Write(buffer, strlen(buffer));
175 	}
176 }
177 
178