File: medialib-search.c

package info (click to toggle)
ocp 1%3A3.0.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,972 kB
  • sloc: ansic: 332,849; cpp: 68,319; makefile: 6,685; sh: 4,344; tcl: 1,040; xml: 436; perl: 320; ruby: 126
file content (314 lines) | stat: -rw-r--r-- 7,513 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* OpenCP Module Player
 * copyright (c) 2005-'24 Stian Skjelstad <stian.skjelstad@gmail.com>
 *
 * MEDIALIBRARY search dialog
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * revision history: (please note changes here)
 *  -ss050430   Stian Skjelstad <stian@nixia.no>
 *    -first release
 */

/* 0 = typing
 * 1 = query
 * 2 = ready
 */
static int mlSearchPerformed;
static char *mlSearchQuery = 0;
static struct ocpfile_t **mlSearchResult;
static int                mlSearchResultCount;
static int                mlSearchResultSize;
static int                mlSearchFirst = 1;
static uint32_t           mlSearchDirDbRef;

static void mlSearchClear (void)
{
	int i;
	mlSearchPerformed = 0;
	free (mlSearchQuery);
	      mlSearchQuery = 0;
	for (i=0; i < mlSearchResultCount; i++)
	{
		mlSearchResult[i]->unref (mlSearchResult[i]);
	}
	free (mlSearchResult);
	      mlSearchResult = 0;
	mlSearchResultCount = 0;
	mlSearchResultSize = 0;
	mlSearchFirst = 1;
}

static int mlSearchPerformQuery (void)
{
	struct dmDrive *drive = 0;
	struct ocpfile_t *file = 0;
	char *filename = 0;
	char *ptr, *ptr2;
	uint32_t mdb_ref;
	struct moduleinfostruct info;
	char buffer
	[
		MAX(sizeof(info.title),
		MAX(sizeof(info.composer),
		    sizeof(info.comment)))
	];

	if (!mlSearchQuery)
	{
		return 1;
	}

	while (1)
	{
		if (dirdbGetMdb(&mlSearchDirDbRef, &mdb_ref, &mlSearchFirst)) /* does not refcount.... */
		{
			return 1;
		}

		dirdbGetName_malloc (mlSearchDirDbRef, &filename);
		if (!filename)
		{ /* out of memory probably */
			return 1;
		}

		strupr(filename);

		if (strstr (filename, mlSearchQuery))
		{
			free (filename);
			      filename = 0;
			break; /* goto add; */
		}
		free (filename);
		      filename = 0;

		mdbGetModuleInfo(&info, mdb_ref);

		/* make each field upper case before trying to match */

		for (ptr=buffer, ptr2=info.title; *ptr2; ptr++, ptr2++)
		{
			*ptr = toupper (*ptr2);
		}
		if (strstr (buffer, mlSearchQuery))
		{
			break; /* goto add; */
		}

		for (ptr=buffer, ptr2=info.composer; *ptr2; ptr++, ptr2++)
		{
			*ptr = toupper (*ptr2);
		}
		if (strstr (buffer, mlSearchQuery))
		{
			break; /* goto add; */
		}

		for (ptr=buffer, ptr2=info.comment; *ptr2; ptr++, ptr2++)
		{
			*ptr = toupper (*ptr2);
		}
		if (strstr (buffer, mlSearchQuery))
		{
			break; /* goto add; */
		}

	}
//add:
	if (filesystem_resolve_dirdb_file (mlSearchDirDbRef, &drive, &file))
	{
		return 0;
	}

	if (mlSearchResultCount >= mlSearchResultSize)
	{
		struct ocpfile_t **temp = realloc (mlSearchResult, (mlSearchResultSize + 128) * sizeof (mlSearchResult[0]));
		if (!temp)
		{
			/* out of memory */
			file->unref (file);
			             file = 0;
			return 1;
		}
		mlSearchResult = temp;
		mlSearchResultSize += 128;
	}
	mlSearchResult[mlSearchResultCount] = file;
	mlSearchResultCount++;

	return 0;
}

static int mlSearchDraw(const char *title)
{
	unsigned int mlHeight;
	unsigned int mlTop;
	unsigned int mlLeft;
	unsigned int mlWidth;

	int i;

	/* SETUP the framesize */
	mlHeight = plScrHeight - 20;
	if (mlHeight < 20)
	{
		mlHeight = 20;
	}
	mlTop = (plScrHeight - mlHeight) / 2;

	mlLeft = 5;
	mlWidth = plScrWidth - 10;
	while (mlWidth < 72)
	{
		mlLeft--;
		mlWidth += 2;
	}

	/* Draw the main frame */
	for (i=1; i < (mlWidth - 1); i++)
	{
		displaystr (mlTop,     mlLeft + i, 0x04, "\xc4", 1);
		displaystr (mlTop + 2, mlLeft + i, 0x04, "\xc4", 1);
		displaystr (mlTop + 4, mlLeft + i, 0x04, "\xc4", 1);
	}

	displaystr (mlTop,     mlLeft,               0x04, "\xda", 1);
	displaystr (mlTop,     mlLeft + mlWidth - 1, 0x04, "\xbf", 1);
	displaystr (mlTop + 1, mlLeft,               0x04, "\xb3", 1);
	displaystr (mlTop + 1, mlLeft + mlWidth - 1, 0x04, "\xb3", 1);
	displaystr (mlTop + 2, mlLeft,               0x04, "\xc3", 1);
	displaystr (mlTop + 2, mlLeft + mlWidth - 1, 0x04, "\xb4", 1);
	displaystr (mlTop + 3, mlLeft,               0x04, "\xb3", 1);
	displaystr (mlTop + 3, mlLeft + mlWidth - 1, 0x04, "\xb3", 1);
	displaystr (mlTop + 4, mlLeft,               0x04, "\xc0", 1);
	displaystr (mlTop + 4, mlLeft + mlWidth - 1, 0x04, "\xd9", 1);

	do
	{
		int Left = 5 + (plScrWidth - strlen (title) - 2 - 10) / 2;
		displaystr (mlTop, Left,                      0x09, " ",   1);
		displaystr (mlTop, Left + 1,                  0x09, title, strlen (title));
		displaystr (mlTop, Left + 1 + strlen (title), 0x09, " ",   1);
	} while (0);

	displaystr (mlTop + 1, mlLeft + 1,  0x07, "Please type in something to search for, or press ", 49);
	displaystr (mlTop + 1, mlLeft + 50, 0x0f, "<esc>", 5);
	displaystr (mlTop + 1, mlLeft + 55, 0x07, " to abort", mlWidth - 56);

	/* Line 2: the current selected path */
	if (!mlSearchQuery)
	{
		mlSearchQuery = strdup ("");
	}
	return EditStringUTF8 (mlTop + 3, mlLeft + 1, mlWidth - 2, &mlSearchQuery);
}


static void ocpdir_search_ref (struct ocpdir_t *self)
{
	self->refcount++;
}

static void ocpdir_search_unref (struct ocpdir_t *self)
{
	self->refcount--;
	if (self->refcount <= 2)
	{
		mlSearchClear();
	}
}

struct search_readdir_handle_t
{
	struct ocpdir_t *self;
	void (*callback_file)(void *token, struct ocpfile_t *);
	void *token;
	int nextindex;
};

static ocpdirhandle_pt ocpdir_search_readdir_start (struct ocpdir_t *self, void(*callback_file)(void *token, struct ocpfile_t *), void(*callback_dir )(void *token, struct ocpdir_t *), void *token)
{
	struct search_readdir_handle_t *retval = calloc (1, sizeof (*retval));
	if (!retval)
	{
		return 0;
	}
	retval->self = self;
	retval->callback_file = callback_file;
	retval->token = token;
	retval->nextindex = 0;
	self->ref (self);
	return retval;
}

static void ocpdir_search_readdir_cancel (ocpdirhandle_pt _handle)
{
	struct search_readdir_handle_t *handle = (struct search_readdir_handle_t *)_handle;
	handle->self->unref (handle->self);
	free (handle);
}

static int ocpdir_search_readdir_iterate (ocpdirhandle_pt _handle)
{
	struct search_readdir_handle_t *handle = (struct search_readdir_handle_t *)_handle;
	int res;

	switch (mlSearchPerformed)
	{
		case 0:
			res = mlSearchDraw("medialib search");
			if (res < 0)
			{
				mlSearchPerformed = 2;
				return 0;
			} else if (res == 0)
			{/* make query upper-case */
				strupr(mlSearchQuery);
				mlSearchPerformed = 1;
				return 1;
			}
			return 1;
		case 1:
			res = mlSearchPerformQuery();
			if (res < 0)
			{
				mlSearchPerformed = 2;
				return 0;
			} else if (res > 0)
			{
				mlSearchPerformed = 2;
				return 1;
			}
			return 1;
		default:
		case 2:
			while (handle->nextindex < mlSearchResultCount)
			{
				handle->callback_file (handle->token, mlSearchResult[handle->nextindex++]);
			}
			return 0;
	}
}

static struct ocpdir_t  *ocpdir_search_readdir_dir  (struct ocpdir_t *self, uint32_t dirdb_ref)
{
	return 0;
}

static struct ocpfile_t *ocpdir_search_readdir_file (struct ocpdir_t *self, uint32_t dirdb_ref)
{
	return 0;
}