xref: /haiku/src/kits/media/MediaFiles.cpp (revision 9eb55bc1d104b8fda80898f8b25c94d8000c8255)
1 /***********************************************************************
2  * AUTHOR: Marcus Overhagen, Jérôme Duval
3  *   FILE: MediaFiles.cpp
4  *  DESCR:
5  ***********************************************************************/
6 #include <MediaFiles.h>
7 #include "DataExchange.h"
8 #include "debug.h"
9 
10 /*************************************************************
11  * public BMediaFiles
12  *************************************************************/
13 
14 const char BMediaFiles::B_SOUNDS[] = "Sounds";	/* for "types" */
15 
16 BMediaFiles::BMediaFiles()
17 	: m_type_index(-1),
18 	m_cur_type(""),
19 	m_item_index(-1)
20 {
21 
22 }
23 
24 
25 /* virtual */
26 BMediaFiles::~BMediaFiles()
27 {
28 
29 }
30 
31 
32 /* virtual */ status_t
33 BMediaFiles::RewindTypes()
34 {
35 	CALLED();
36 	status_t rv;
37 	server_rewindtypes_request request;
38 	server_rewindtypes_reply reply;
39 
40 	for(int32 i = 0; i < m_types.CountItems(); i++)
41 		delete m_types.ItemAt(i);
42 
43 	m_types.MakeEmpty();
44 
45 	TRACE("BMediaFiles::RewindTypes: sending SERVER_REWINDTYPES\n");
46 
47 	rv = QueryServer(SERVER_REWINDTYPES, &request, sizeof(request), &reply, sizeof(reply));
48 	if (rv != B_OK) {
49 		ERROR("BMediaFiles::RewindTypes: failed to rewindtypes (error %#lx)\n", rv);
50 		return rv;
51 	}
52 
53 	char *types;
54 	area_id clone;
55 
56 	clone = clone_area("rewind types clone", reinterpret_cast<void **>(&types), B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, reply.area);
57 	if (clone < B_OK) {
58 		ERROR("BMediaFiles::RewindTypes failed to clone area, %#lx\n", clone);
59 		delete_area(reply.area);
60 		return B_ERROR;
61 	}
62 
63 	for (int32 i = 0; i < reply.count; i++) {
64 		m_types.AddItem(new BString(types + i * B_MEDIA_NAME_LENGTH));
65 	}
66 
67 	delete_area(clone);
68 	delete_area(reply.area);
69 
70 	m_type_index = 0;
71 
72 	return B_OK;
73 }
74 
75 
76 /* virtual */ status_t
77 BMediaFiles::GetNextType(BString *out_type)
78 {
79 	CALLED();
80 	if(m_type_index < 0 || m_type_index >= m_types.CountItems()) {
81 		m_type_index = -1;
82 		return B_BAD_INDEX;
83 	}
84 
85 	*out_type = *(BString*)m_types.ItemAt(m_type_index);
86 	m_type_index++;
87 
88 	return B_OK;
89 }
90 
91 
92 /* virtual */ status_t
93 BMediaFiles::RewindRefs(const char *type)
94 {
95 	CALLED();
96 	status_t rv;
97 	server_rewindrefs_request request;
98 	server_rewindrefs_reply reply;
99 
100 	for(int32 i = 0; i < m_items.CountItems(); i++)
101 		delete m_items.ItemAt(i);
102 
103 	m_items.MakeEmpty();
104 
105 	TRACE("BMediaFiles::RewindRefs: sending SERVER_REWINDREFS for type %s\n", type);
106 	strncpy(request.type, type, B_MEDIA_NAME_LENGTH);
107 
108 	rv = QueryServer(SERVER_REWINDREFS, &request, sizeof(request), &reply, sizeof(reply));
109 	if (rv != B_OK) {
110 		ERROR("BMediaFiles::RewindRefs: failed to rewindrefs (error %#lx)\n", rv);
111 		return rv;
112 	}
113 
114 	if(reply.count>0) {
115 		char *items;
116 		area_id clone;
117 
118 		clone = clone_area("rewind refs clone", reinterpret_cast<void **>(&items), B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, reply.area);
119 		if (clone < B_OK) {
120 			ERROR("BMediaFiles::RewindRefs failed to clone area, %#lx\n", clone);
121 			delete_area(reply.area);
122 			return B_ERROR;
123 		}
124 
125 		for (int32 i = 0; i < reply.count; i++) {
126 			m_items.AddItem(new BString(items + i * B_MEDIA_NAME_LENGTH));
127 		}
128 
129 		delete_area(clone);
130 		delete_area(reply.area);
131 	}
132 
133 	m_cur_type = BString(type);
134 	m_item_index = 0;
135 
136 	return B_OK;
137 }
138 
139 
140 /* virtual */ status_t
141 BMediaFiles::GetNextRef(BString *out_type,
142 						entry_ref *out_ref)
143 {
144 	CALLED();
145 	if(m_item_index < 0 || m_item_index >= m_items.CountItems()) {
146 		m_item_index = -1;
147 		return B_BAD_INDEX;
148 	}
149 
150 	*out_type = *(BString*)m_items.ItemAt(m_item_index);
151 	m_item_index++;
152 
153 	GetRefFor(m_cur_type.String(), out_type->String(), out_ref);
154 
155 	return B_OK;
156 }
157 
158 
159 /* virtual */ status_t
160 BMediaFiles::GetRefFor(const char *type,
161 					   const char *item,
162 					   entry_ref *out_ref)
163 {
164 	CALLED();
165 	status_t rv;
166 	server_getreffor_request request;
167 	server_getreffor_reply reply;
168 
169 	strncpy(request.type, type, B_MEDIA_NAME_LENGTH);
170 	strncpy(request.item, item, B_MEDIA_NAME_LENGTH);
171 
172 	TRACE("BMediaFiles::GetRefFor: sending SERVER_GETREFFOR\n");
173 
174 	rv = QueryServer(SERVER_GETREFFOR, &request, sizeof(request), &reply, sizeof(reply));
175 	if (rv != B_OK) {
176 		entry_ref ref;
177 		*out_ref = ref;
178 		ERROR("BMediaFiles::GetRefFor: failed to getreffor (error %#lx)\n", rv);
179 		return rv;
180 	}
181 
182 	*out_ref = reply.ref;
183 
184 	return B_OK;
185 }
186 
187 
188 /* virtual */ status_t
189 BMediaFiles::SetRefFor(const char *type,
190 					   const char *item,
191 					   const entry_ref &ref)
192 {
193 	CALLED();
194 	status_t rv;
195 	server_setreffor_request request;
196 	server_setreffor_reply reply;
197 
198 	strncpy(request.type, type, B_MEDIA_NAME_LENGTH);
199 	strncpy(request.item, item, B_MEDIA_NAME_LENGTH);
200 	request.ref = ref;
201 
202 	TRACE("BMediaFiles::SetRefFor: sending SERVER_SETREFFOR\n");
203 
204 	rv = QueryServer(SERVER_SETREFFOR, &request, sizeof(request), &reply, sizeof(reply));
205 	if (rv != B_OK) {
206 		ERROR("BMediaFiles::SetRefFor: failed to setreffor (error %#lx)\n", rv);
207 		return rv;
208 	}
209 
210 	return B_OK;
211 }
212 
213 
214 /* virtual */ status_t
215 BMediaFiles::RemoveRefFor(const char *type,
216 						  const char *item,
217 						  const entry_ref &ref)
218 {
219 	status_t rv;
220 	server_removereffor_request request;
221 	server_removereffor_reply reply;
222 
223 	strncpy(request.type, type, B_MEDIA_NAME_LENGTH);
224 	strncpy(request.item, item, B_MEDIA_NAME_LENGTH);
225 	request.ref = ref;
226 
227 	TRACE("BMediaFiles::RemoveRefFor: sending SERVER_REMOVEREFFOR\n");
228 
229 	rv = QueryServer(SERVER_REMOVEREFFOR, &request, sizeof(request), &reply, sizeof(reply));
230 	if (rv != B_OK) {
231 		ERROR("BMediaFiles::RemoveRefFor: failed to removereffor (error %#lx)\n", rv);
232 		return rv;
233 	}
234 
235 	return B_OK;
236 }
237 
238 
239 /* virtual */ status_t
240 BMediaFiles::RemoveItem(const char *type,
241 						const char *item)
242 {
243 	status_t rv;
244 	server_removeitem_request request;
245 	server_removeitem_reply reply;
246 
247 	strncpy(request.type, type, B_MEDIA_NAME_LENGTH);
248 	strncpy(request.item, item, B_MEDIA_NAME_LENGTH);
249 
250 	TRACE("BMediaFiles::RemoveItem: sending SERVER_REMOVEITEM\n");
251 
252 	rv = QueryServer(SERVER_REMOVEITEM, &request, sizeof(request), &reply, sizeof(reply));
253 	if (rv != B_OK) {
254 		ERROR("BMediaFiles::RemoveItem: failed to removeitem (error %#lx)\n", rv);
255 		return rv;
256 	}
257 
258 	return B_OK;
259 }
260 
261 /*************************************************************
262  * private BMediaFiles
263  *************************************************************/
264 
265 status_t BMediaFiles::_Reserved_MediaFiles_0(void *,...) { return B_ERROR; }
266 status_t BMediaFiles::_Reserved_MediaFiles_1(void *,...) { return B_ERROR; }
267 status_t BMediaFiles::_Reserved_MediaFiles_2(void *,...) { return B_ERROR; }
268 status_t BMediaFiles::_Reserved_MediaFiles_3(void *,...) { return B_ERROR; }
269 status_t BMediaFiles::_Reserved_MediaFiles_4(void *,...) { return B_ERROR; }
270 status_t BMediaFiles::_Reserved_MediaFiles_5(void *,...) { return B_ERROR; }
271 status_t BMediaFiles::_Reserved_MediaFiles_6(void *,...) { return B_ERROR; }
272 status_t BMediaFiles::_Reserved_MediaFiles_7(void *,...) { return B_ERROR; }
273 
274