2007-02-24 04:14:00 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* (c)2007 by Warren Dukes (warren.dukes@gmail.com)
|
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (0 == strcasecmp(str, LOCATE_TAG_FILE_KEY) ||
|
2009-01-24 15:26:59 +01:00
|
|
|
0 == strcasecmp(str, LOCATE_TAG_FILE_KEY_OLD))
|
2007-02-24 01:54:54 +01:00
|
|
|
return LOCATE_TAG_FILE_TYPE;
|
|
|
|
|
2009-01-24 15:26:59 +01:00
|
|
|
if (0 == strcasecmp(str, LOCATE_TAG_ANY_KEY))
|
2007-02-24 01:54:54 +01:00
|
|
|
return LOCATE_TAG_ANY_TYPE;
|
|
|
|
|
2009-01-24 15:26:59 +01:00
|
|
|
for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++)
|
2007-02-24 01:54:54 +01:00
|
|
|
if (0 == strcasecmp(str, mpdTagItemKeys[i]))
|
|
|
|
return i;
|
|
|
|
|
|
|
|
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,
|
|
|
|
const char *type_string, const char *needle)
|
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
|
|
|
|
2009-01-02 16:24:16 +01:00
|
|
|
item->needle = 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 14:52:05 +01:00
|
|
|
struct locate_item *
|
2009-01-24 15:26:59 +01:00
|
|
|
locate_item_new(const char *type_string, const char *needle)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2009-01-24 14:52:05 +01:00
|
|
|
struct locate_item *ret = g_new(struct locate_item, 1);
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
if (!locate_item_init(ret, type_string, needle)) {
|
2009-01-25 18:47:21 +01:00
|
|
|
g_free(ret);
|
2007-02-24 01:54:54 +01:00
|
|
|
ret = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2009-01-24 15:56:30 +01:00
|
|
|
struct locate_item_list *list;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:56:30 +01:00
|
|
|
list = g_malloc0(sizeof(*list) - sizeof(list->items[0]) +
|
|
|
|
length * sizeof(list->items[0]));
|
|
|
|
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 *
|
|
|
|
locate_item_list_parse(char *argv[], int argc)
|
|
|
|
{
|
|
|
|
struct locate_item_list *list;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:56:30 +01:00
|
|
|
if (argc % 2 != 0)
|
|
|
|
return NULL;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:56:30 +01:00
|
|
|
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],
|
|
|
|
argv[i * 2 + 1])) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-01-24 15:56:34 +01:00
|
|
|
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);
|
|
|
|
|
2009-01-25 00:39:00 +01:00
|
|
|
for (unsigned i = 0; i < list->length; i++){
|
2009-01-24 15:56:34 +01:00
|
|
|
new_list->items[i].needle =
|
|
|
|
g_utf8_casefold(list->items[i].needle, -1);
|
2009-01-25 00:39:00 +01:00
|
|
|
new_list->items[i].tag = list->items[i].tag;
|
|
|
|
}
|
2009-01-24 15:56:34 +01:00
|
|
|
|
|
|
|
return new_list;
|
|
|
|
}
|
|
|
|
|
2009-01-24 15:26:59 +01:00
|
|
|
void
|
|
|
|
locate_item_free(struct locate_item *item)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2009-01-25 18:47:21 +01:00
|
|
|
g_free(item->needle);
|
|
|
|
g_free(item);
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int i;
|
2008-01-26 13:46:21 +01:00
|
|
|
char *duplicate;
|
2009-01-24 15:27:05 +01:00
|
|
|
bool ret = false;
|
2009-01-24 15:27:09 +01:00
|
|
|
bool visited_types[TAG_NUM_OF_ITEM_TYPES];
|
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, *p;
|
|
|
|
|
|
|
|
uri = song_get_uri(song);
|
|
|
|
p = g_utf8_casefold(uri, -1);
|
|
|
|
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
|
|
|
|
2009-01-24 15:27:09 +01:00
|
|
|
memset(visited_types, 0, sizeof(visited_types));
|
|
|
|
|
2007-02-24 01:54:54 +01:00
|
|
|
for (i = 0; i < song->tag->numOfItems && !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;
|
|
|
|
}
|
|
|
|
|
2008-10-15 19:36:37 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int i;
|
2009-01-24 15:27:09 +01:00
|
|
|
bool visited_types[TAG_NUM_OF_ITEM_TYPES];
|
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
|
|
|
|
2009-01-24 15:27:09 +01:00
|
|
|
memset(visited_types, 0, sizeof(visited_types));
|
|
|
|
|
2007-02-24 01:54:54 +01:00
|
|
|
for (i = 0; i < song->tag->numOfItems; 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
|
|
|
}
|