locate.c 5.9 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2010 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
21
#include "locate.h"
Max Kellermann's avatar
Max Kellermann committed
22
#include "path.h"
Max Kellermann's avatar
Max Kellermann committed
23
#include "tag.h"
24
#include "song.h"
25

26 27
#include <glib.h>

28 29
#include <stdlib.h>

30 31
#define LOCATE_TAG_FILE_KEY     "file"
#define LOCATE_TAG_FILE_KEY_OLD "filename"
32 33
#define LOCATE_TAG_ANY_KEY      "any"

Max Kellermann's avatar
Max Kellermann committed
34 35
int
locate_parse_type(const char *str)
36 37 38
{
	int i;

39 40
	if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_FILE_KEY) ||
	    0 == g_ascii_strcasecmp(str, LOCATE_TAG_FILE_KEY_OLD))
41 42
		return LOCATE_TAG_FILE_TYPE;

43
	if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_ANY_KEY))
44 45
		return LOCATE_TAG_ANY_TYPE;

46 47 48
	i = tag_name_parse_i(str);
	if (i != TAG_NUM_OF_ITEM_TYPES)
		return i;
49 50 51 52

	return -1;
}

53
static bool
Max Kellermann's avatar
Max Kellermann committed
54 55
locate_item_init(struct locate_item *item,
		 const char *type_string, const char *needle)
56
{
Max Kellermann's avatar
Max Kellermann committed
57
	item->tag = locate_parse_type(type_string);
58

Max Kellermann's avatar
Max Kellermann committed
59
	if (item->tag < 0)
60
		return false;
61

62
	item->needle = g_strdup(needle);
63

64
	return true;
65 66
}

67
struct locate_item *
Max Kellermann's avatar
Max Kellermann committed
68
locate_item_new(const char *type_string, const char *needle)
69
{
70
	struct locate_item *ret = g_new(struct locate_item, 1);
71

72
	if (!locate_item_init(ret, type_string, needle)) {
73
		g_free(ret);
74 75 76 77 78 79
		ret = NULL;
	}

	return ret;
}

Max Kellermann's avatar
Max Kellermann committed
80
void
81
locate_item_list_free(struct locate_item_list *list)
82
{
83 84
	for (unsigned i = 0; i < list->length; ++i)
		g_free(list->items[i].needle);
85

86
	g_free(list);
87 88
}

89 90
struct locate_item_list *
locate_item_list_new(unsigned length)
91
{
92
	struct locate_item_list *list;
93

94 95 96
	list = g_malloc0(sizeof(*list) - sizeof(list->items[0]) +
			 length * sizeof(list->items[0]));
	list->length = length;
97

98 99
	return list;
}
100

101 102 103 104
struct locate_item_list *
locate_item_list_parse(char *argv[], int argc)
{
	struct locate_item_list *list;
105

106 107
	if (argc % 2 != 0)
		return NULL;
108

109
	list = locate_item_list_new(argc / 2);
110

111 112 113 114 115 116
	for (unsigned i = 0; i < list->length; ++i) {
		if (!locate_item_init(&list->items[i], argv[i * 2],
				      argv[i * 2 + 1])) {
			locate_item_list_free(list);
			return NULL;
		}
117 118
	}

119
	return list;
120 121
}

122 123 124 125 126
struct locate_item_list *
locate_item_list_casefold(const struct locate_item_list *list)
{
	struct locate_item_list *new_list = locate_item_list_new(list->length);

127
	for (unsigned i = 0; i < list->length; i++){
128 129
		new_list->items[i].needle =
			g_utf8_casefold(list->items[i].needle, -1);
130 131
		new_list->items[i].tag = list->items[i].tag;
	}
132 133 134 135

	return new_list;
}

Max Kellermann's avatar
Max Kellermann committed
136 137
void
locate_item_free(struct locate_item *item)
138
{
139 140
	g_free(item->needle);
	g_free(item);
141 142
}

143
static bool
Max Kellermann's avatar
Max Kellermann committed
144
locate_tag_search(const struct song *song, enum tag_type type, const char *str)
145
{
Max Kellermann's avatar
Max Kellermann committed
146
	char *duplicate;
147
	bool ret = false;
148
	bool visited_types[TAG_NUM_OF_ITEM_TYPES];
149 150

	if (type == LOCATE_TAG_FILE_TYPE || type == LOCATE_TAG_ANY_TYPE) {
151 152 153 154 155
		char *uri, *p;

		uri = song_get_uri(song);
		p = g_utf8_casefold(uri, -1);
		g_free(uri);
Eric Wong's avatar
Eric Wong committed
156

157
		if (strstr(p, str))
158
			ret = true;
159
		g_free(p);
Eric Wong's avatar
Eric Wong committed
160
		if (ret == 1 || type == LOCATE_TAG_FILE_TYPE)
161 162 163 164
			return ret;
	}

	if (!song->tag)
165
		return false;
166

167 168
	memset(visited_types, 0, sizeof(visited_types));

169
	for (unsigned i = 0; i < song->tag->num_items && !ret; i++) {
170
		visited_types[song->tag->items[i]->type] = true;
171
		if (type != LOCATE_TAG_ANY_TYPE &&
172
		    song->tag->items[i]->type != type) {
173 174 175
			continue;
		}

176
		duplicate = g_utf8_casefold(song->tag->items[i]->value, -1);
177
		if (*str && strstr(duplicate, str))
178
			ret = true;
179
		g_free(duplicate);
180 181
	}

182 183 184 185
	/** If the search critieron was not visited during the sweep
	 * through the song's tag, it means this field is absent from
	 * the tag or empty. Thus, if the searched string is also
	 *  empty (first char is a \0), then it's a match as well and
186
	 *  we should return true.
187
	 */
Max Kellermann's avatar
Max Kellermann committed
188
	if (!*str && !visited_types[type])
189
		return true;
190

191 192 193
	return ret;
}

194
bool
195 196
locate_song_search(const struct song *song,
		   const struct locate_item_list *criteria)
197
{
198 199 200
	for (unsigned i = 0; i < criteria->length; i++)
		if (!locate_tag_search(song, criteria->items[i].tag,
				       criteria->items[i].needle))
201
			return false;
202

203
	return true;
204 205
}

206
static bool
Max Kellermann's avatar
Max Kellermann committed
207
locate_tag_match(const struct song *song, enum tag_type type, const char *str)
208
{
209
	bool visited_types[TAG_NUM_OF_ITEM_TYPES];
210 211

	if (type == LOCATE_TAG_FILE_TYPE || type == LOCATE_TAG_ANY_TYPE) {
212
		char *uri = song_get_uri(song);
213
		bool matches = strcmp(str, uri) == 0;
214 215 216
		g_free(uri);

		if (matches)
217
			return true;
218

219
		if (type == LOCATE_TAG_FILE_TYPE)
220
			return false;
221 222 223
	}

	if (!song->tag)
224
		return false;
225

226 227
	memset(visited_types, 0, sizeof(visited_types));

Max Kellermann's avatar
Max Kellermann committed
228
	for (unsigned i = 0; i < song->tag->num_items; i++) {
229
		visited_types[song->tag->items[i]->type] = true;
230
		if (type != LOCATE_TAG_ANY_TYPE &&
231
		    song->tag->items[i]->type != type) {
232 233 234
			continue;
		}

235
		if (0 == strcmp(str, song->tag->items[i]->value))
236
			return true;
237 238
	}

239 240 241 242
	/** If the search critieron was not visited during the sweep
	 * through the song's tag, it means this field is absent from
	 * the tag or empty. Thus, if the searched string is also
	 *  empty (first char is a \0), then it's a match as well and
243
	 *  we should return true.
244
	 */
Max Kellermann's avatar
Max Kellermann committed
245
	if (!*str && !visited_types[type])
246
		return true;
247

248
	return false;
249 250
}

251
bool
252 253
locate_song_match(const struct song *song,
		  const struct locate_item_list *criteria)
254
{
255 256 257
	for (unsigned i = 0; i < criteria->length; i++)
		if (!locate_tag_match(song, criteria->items[i].tag,
				      criteria->items[i].needle))
258
			return false;
259

260
	return true;
261
}