2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2007-02-24 04:14:00 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2007-02-24 04:14:00 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2007-02-24 01:54:54 +01:00
|
|
|
#include "locate.h"
|
2008-04-12 06:19:26 +02:00
|
|
|
#include "path.h"
|
2008-09-06 20:28:31 +02:00
|
|
|
#include "tag.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2008-10-15 19:36:37 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2009-01-02 16:24:16 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-03-20 21:22:23 +01:00
|
|
|
#define LOCATE_TAG_FILE_KEY "file"
|
|
|
|
#define LOCATE_TAG_FILE_KEY_OLD "filename"
|
2007-02-24 01:54:54 +01:00
|
|
|
#define LOCATE_TAG_ANY_KEY "any"
|
|
|
|
|
2009-01-24 15:26:59 +01:00
|
|
|
int
|
|
|
|
locate_parse_type(const char *str)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2009-04-28 09:31:44 +02:00
|
|
|
if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_FILE_KEY) ||
|
|
|
|
0 == g_ascii_strcasecmp(str, LOCATE_TAG_FILE_KEY_OLD))
|
2007-02-24 01:54:54 +01:00
|
|
|
return LOCATE_TAG_FILE_TYPE;
|
|
|
|
|
2009-04-28 09:31:44 +02:00
|
|
|
if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_ANY_KEY))
|
2007-02-24 01:54:54 +01:00
|
|
|
return LOCATE_TAG_ANY_TYPE;
|
|
|
|
|
2012-06-27 09:36:02 +02:00
|
|
|
enum tag_type i = tag_name_parse_i(str);
|
2009-11-04 18:47:42 +01:00
|
|
|
if (i != TAG_NUM_OF_ITEM_TYPES)
|
|
|
|
return i;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
static bool
|
2009-01-24 15:26:59 +01:00
|
|
|
locate_item_init(struct locate_item *item,
|
2012-08-07 23:25:42 +02:00
|
|
|
const char *type_string, const char *needle,
|
|
|
|
bool fold_case)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2009-01-24 15:26:59 +01:00
|
|
|
item->tag = locate_parse_type(type_string);
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:26:59 +01:00
|
|
|
if (item->tag < 0)
|
2009-01-24 15:27:05 +01:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-08-07 23:25:42 +02:00
|
|
|
item->needle = fold_case
|
|
|
|
? g_utf8_casefold(needle, -1)
|
|
|
|
: g_strdup(needle);
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 15:26:59 +01:00
|
|
|
void
|
2009-01-24 15:56:30 +01:00
|
|
|
locate_item_list_free(struct locate_item_list *list)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2009-01-24 15:56:30 +01:00
|
|
|
for (unsigned i = 0; i < list->length; ++i)
|
|
|
|
g_free(list->items[i].needle);
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:56:30 +01:00
|
|
|
g_free(list);
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 15:56:30 +01:00
|
|
|
struct locate_item_list *
|
|
|
|
locate_item_list_new(unsigned length)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2012-06-27 09:36:02 +02:00
|
|
|
struct locate_item_list *list =
|
|
|
|
g_malloc0(sizeof(*list) - sizeof(list->items[0]) +
|
|
|
|
length * sizeof(list->items[0]));
|
2009-01-24 15:56:30 +01:00
|
|
|
list->length = length;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:56:30 +01:00
|
|
|
return list;
|
|
|
|
}
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:56:30 +01:00
|
|
|
struct locate_item_list *
|
2012-08-07 23:37:28 +02:00
|
|
|
locate_item_list_parse(char *argv[], unsigned argc, bool fold_case)
|
2009-01-24 15:56:30 +01:00
|
|
|
{
|
2012-08-07 23:37:38 +02:00
|
|
|
if (argc == 0 || argc % 2 != 0)
|
2009-01-24 15:56:30 +01:00
|
|
|
return NULL;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-06-27 09:36:02 +02:00
|
|
|
struct locate_item_list *list = locate_item_list_new(argc / 2);
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:56:30 +01:00
|
|
|
for (unsigned i = 0; i < list->length; ++i) {
|
|
|
|
if (!locate_item_init(&list->items[i], argv[i * 2],
|
2012-08-07 23:25:42 +02:00
|
|
|
argv[i * 2 + 1], fold_case)) {
|
2009-01-24 15:56:30 +01:00
|
|
|
locate_item_list_free(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 15:56:30 +01:00
|
|
|
return list;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-08-07 23:43:36 +02:00
|
|
|
gcc_pure
|
2009-01-24 15:27:05 +01:00
|
|
|
static bool
|
2009-01-24 15:26:59 +01:00
|
|
|
locate_tag_search(const struct song *song, enum tag_type type, const char *str)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2009-01-24 15:27:05 +01:00
|
|
|
bool ret = false;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
|
|
|
if (type == LOCATE_TAG_FILE_TYPE || type == LOCATE_TAG_ANY_TYPE) {
|
2012-06-27 09:36:02 +02:00
|
|
|
char *uri = song_get_uri(song);
|
|
|
|
char *p = g_utf8_casefold(uri, -1);
|
2009-01-04 19:09:34 +01:00
|
|
|
g_free(uri);
|
2007-12-28 03:56:25 +01:00
|
|
|
|
2008-12-13 01:54:11 +01:00
|
|
|
if (strstr(p, str))
|
2009-01-24 15:27:05 +01:00
|
|
|
ret = true;
|
2008-10-15 19:36:37 +02:00
|
|
|
g_free(p);
|
2007-12-28 03:56:25 +01:00
|
|
|
if (ret == 1 || type == LOCATE_TAG_FILE_TYPE)
|
2007-02-24 01:54:54 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!song->tag)
|
2009-01-24 15:27:05 +01:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-06-27 09:36:02 +02:00
|
|
|
bool visited_types[TAG_NUM_OF_ITEM_TYPES];
|
2009-01-24 15:27:09 +01:00
|
|
|
memset(visited_types, 0, sizeof(visited_types));
|
|
|
|
|
2009-02-27 09:02:13 +01:00
|
|
|
for (unsigned i = 0; i < song->tag->num_items && !ret; i++) {
|
2009-01-24 15:27:05 +01:00
|
|
|
visited_types[song->tag->items[i]->type] = true;
|
2007-02-24 01:54:54 +01:00
|
|
|
if (type != LOCATE_TAG_ANY_TYPE &&
|
2008-08-29 09:38:29 +02:00
|
|
|
song->tag->items[i]->type != type) {
|
2007-02-24 01:54:54 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-06-27 09:36:02 +02:00
|
|
|
char *duplicate = g_utf8_casefold(song->tag->items[i]->value, -1);
|
2008-09-29 13:18:49 +02:00
|
|
|
if (*str && strstr(duplicate, str))
|
2009-01-24 15:27:05 +01:00
|
|
|
ret = true;
|
2008-10-15 19:36:37 +02:00
|
|
|
g_free(duplicate);
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2008-09-29 13:18:49 +02:00
|
|
|
/** 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
|
2009-01-24 15:27:05 +01:00
|
|
|
* we should return true.
|
2008-09-29 13:18:49 +02:00
|
|
|
*/
|
2009-01-24 15:26:59 +01:00
|
|
|
if (!*str && !visited_types[type])
|
2009-01-24 15:27:05 +01:00
|
|
|
return true;
|
2008-09-29 13:18:49 +02:00
|
|
|
|
2007-02-24 01:54:54 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
bool
|
2009-01-24 15:56:34 +01:00
|
|
|
locate_song_search(const struct song *song,
|
|
|
|
const struct locate_item_list *criteria)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2009-01-24 15:56:34 +01:00
|
|
|
for (unsigned i = 0; i < criteria->length; i++)
|
|
|
|
if (!locate_tag_search(song, criteria->items[i].tag,
|
|
|
|
criteria->items[i].needle))
|
2009-01-24 15:27:05 +01:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-08-07 23:43:36 +02:00
|
|
|
gcc_pure
|
2009-01-24 15:27:05 +01:00
|
|
|
static bool
|
2009-01-24 15:26:59 +01:00
|
|
|
locate_tag_match(const struct song *song, enum tag_type type, const char *str)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
|
|
|
if (type == LOCATE_TAG_FILE_TYPE || type == LOCATE_TAG_ANY_TYPE) {
|
2009-01-04 19:09:34 +01:00
|
|
|
char *uri = song_get_uri(song);
|
2009-01-05 12:50:04 +01:00
|
|
|
bool matches = strcmp(str, uri) == 0;
|
2009-01-04 19:09:34 +01:00
|
|
|
g_free(uri);
|
|
|
|
|
|
|
|
if (matches)
|
2009-01-24 15:27:05 +01:00
|
|
|
return true;
|
2009-01-04 19:09:34 +01:00
|
|
|
|
2007-02-24 01:54:54 +01:00
|
|
|
if (type == LOCATE_TAG_FILE_TYPE)
|
2009-01-24 15:27:05 +01:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!song->tag)
|
2009-01-24 15:27:05 +01:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-06-27 09:36:02 +02:00
|
|
|
bool visited_types[TAG_NUM_OF_ITEM_TYPES];
|
2009-01-24 15:27:09 +01:00
|
|
|
memset(visited_types, 0, sizeof(visited_types));
|
|
|
|
|
2009-02-27 09:01:55 +01:00
|
|
|
for (unsigned i = 0; i < song->tag->num_items; i++) {
|
2009-01-24 15:27:05 +01:00
|
|
|
visited_types[song->tag->items[i]->type] = true;
|
2007-02-24 01:54:54 +01:00
|
|
|
if (type != LOCATE_TAG_ANY_TYPE &&
|
2008-08-29 09:38:29 +02:00
|
|
|
song->tag->items[i]->type != type) {
|
2007-02-24 01:54:54 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:29 +02:00
|
|
|
if (0 == strcmp(str, song->tag->items[i]->value))
|
2009-01-24 15:27:05 +01:00
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2008-09-29 13:18:49 +02:00
|
|
|
/** 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
|
2009-01-24 15:27:05 +01:00
|
|
|
* we should return true.
|
2008-09-29 13:18:49 +02:00
|
|
|
*/
|
2009-01-24 15:26:59 +01:00
|
|
|
if (!*str && !visited_types[type])
|
2009-01-24 15:27:05 +01:00
|
|
|
return true;
|
2008-09-29 13:18:49 +02:00
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
bool
|
2009-01-24 15:56:34 +01:00
|
|
|
locate_song_match(const struct song *song,
|
|
|
|
const struct locate_item_list *criteria)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2009-01-24 15:56:34 +01:00
|
|
|
for (unsigned i = 0; i < criteria->length; i++)
|
|
|
|
if (!locate_tag_match(song, criteria->items[i].tag,
|
|
|
|
criteria->items[i].needle))
|
2009-01-24 15:27:05 +01:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|