2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 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"
|
2012-08-29 19:12:26 +02:00
|
|
|
#include "SongFilter.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/LightSong.hxx"
|
2014-01-07 21:39:47 +01:00
|
|
|
#include "DetachedSong.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2014-04-24 09:43:08 +02:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-10-20 23:09:51 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2013-10-29 18:54:34 +01:00
|
|
|
#include "util/UriUtil.hxx"
|
2014-02-23 19:27:08 +01:00
|
|
|
#include "lib/icu/Collate.hxx"
|
2008-10-15 19:36:37 +02:00
|
|
|
|
2012-08-07 23:59:17 +02:00
|
|
|
#include <assert.h>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.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"
|
|
|
|
|
2012-08-08 00:45:46 +02:00
|
|
|
unsigned
|
2009-01-24 15:26:59 +01:00
|
|
|
locate_parse_type(const char *str)
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2013-10-20 23:09:51 +02:00
|
|
|
if (StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY) ||
|
|
|
|
StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY_OLD))
|
2007-02-24 01:54:54 +01:00
|
|
|
return LOCATE_TAG_FILE_TYPE;
|
|
|
|
|
2013-10-20 23:09:51 +02:00
|
|
|
if (StringEqualsCaseASCII(str, LOCATE_TAG_ANY_KEY))
|
2007-02-24 01:54:54 +01:00
|
|
|
return LOCATE_TAG_ANY_TYPE;
|
|
|
|
|
2013-10-29 18:54:34 +01:00
|
|
|
if (strcmp(str, "base") == 0)
|
|
|
|
return LOCATE_TAG_BASE_TYPE;
|
|
|
|
|
2012-08-08 00:45:46 +02:00
|
|
|
return tag_name_parse_i(str);
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-29 19:39:17 +01:00
|
|
|
gcc_pure
|
|
|
|
static std::string
|
|
|
|
ImportString(const char *p, bool fold_case)
|
|
|
|
{
|
|
|
|
return fold_case
|
2014-02-23 19:27:08 +01:00
|
|
|
? IcuCaseFold(p)
|
2013-10-29 19:39:17 +01:00
|
|
|
: std::string(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
SongFilter::Item::Item(unsigned _tag, const char *_value, bool _fold_case)
|
|
|
|
:tag(_tag), fold_case(_fold_case),
|
|
|
|
value(ImportString(_value, _fold_case))
|
2009-01-24 15:56:30 +01:00
|
|
|
{
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
|
|
|
SongFilter::Item::StringMatch(const char *s) const
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
assert(s != nullptr);
|
2007-12-28 03:56:25 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
if (fold_case) {
|
2014-02-23 19:27:08 +01:00
|
|
|
const std::string folded = IcuCaseFold(s);
|
|
|
|
return folded.find(value) != folded.npos;
|
2012-08-07 23:59:17 +02:00
|
|
|
} else {
|
2013-10-29 19:39:17 +01:00
|
|
|
return s == value;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
2012-08-07 23:59:17 +02:00
|
|
|
}
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2013-07-30 20:11:57 +02:00
|
|
|
SongFilter::Item::Match(const TagItem &item) const
|
2012-08-07 23:59:17 +02:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
return (tag == LOCATE_TAG_ANY_TYPE || (unsigned)item.type == tag) &&
|
|
|
|
StringMatch(item.value);
|
|
|
|
}
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2013-07-30 20:11:57 +02:00
|
|
|
SongFilter::Item::Match(const Tag &_tag) const
|
2012-08-29 19:27:03 +02:00
|
|
|
{
|
2012-06-27 09:36:02 +02:00
|
|
|
bool visited_types[TAG_NUM_OF_ITEM_TYPES];
|
2014-01-14 22:40:07 +01:00
|
|
|
std::fill_n(visited_types, size_t(TAG_NUM_OF_ITEM_TYPES), false);
|
2009-01-24 15:27:09 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
for (unsigned i = 0; i < _tag.num_items; i++) {
|
|
|
|
visited_types[_tag.items[i]->type] = true;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
if (Match(*_tag.items[i]))
|
2012-08-07 23:59:17 +02:00
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2013-09-26 19:25:13 +02:00
|
|
|
if (tag < TAG_NUM_OF_ITEM_TYPES && !visited_types[tag]) {
|
|
|
|
/* 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
|
2013-10-29 19:39:17 +01:00
|
|
|
searched string is also empty
|
2013-09-26 19:25:13 +02:00
|
|
|
then it's a match as well and we should return
|
|
|
|
true. */
|
2013-10-29 19:39:17 +01:00
|
|
|
if (value.empty())
|
2013-09-26 19:25:13 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (tag == TAG_ALBUM_ARTIST && visited_types[TAG_ARTIST]) {
|
|
|
|
/* if we're looking for "album artist", but
|
|
|
|
only "artist" exists, use that */
|
|
|
|
for (unsigned i = 0; i < _tag.num_items; i++) {
|
|
|
|
const TagItem &item = *_tag.items[i];
|
|
|
|
if (item.type == TAG_ARTIST &&
|
|
|
|
StringMatch(item.value))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-29 13:18:49 +02:00
|
|
|
|
2012-08-07 23:59:17 +02:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2014-01-19 10:51:34 +01:00
|
|
|
SongFilter::Item::Match(const DetachedSong &song) const
|
|
|
|
{
|
|
|
|
if (tag == LOCATE_TAG_BASE_TYPE)
|
|
|
|
return uri_is_child_or_same(value.c_str(), song.GetURI());
|
|
|
|
|
|
|
|
if (tag == LOCATE_TAG_FILE_TYPE)
|
|
|
|
return StringMatch(song.GetURI());
|
|
|
|
|
|
|
|
return Match(song.GetTag());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SongFilter::Item::Match(const LightSong &song) const
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2013-10-29 18:54:34 +01:00
|
|
|
if (tag == LOCATE_TAG_BASE_TYPE) {
|
|
|
|
const auto uri = song.GetURI();
|
|
|
|
return uri_is_child_or_same(value.c_str(), uri.c_str());
|
|
|
|
}
|
|
|
|
|
2013-10-29 19:54:40 +01:00
|
|
|
if (tag == LOCATE_TAG_FILE_TYPE) {
|
2013-10-17 01:01:15 +02:00
|
|
|
const auto uri = song.GetURI();
|
2013-10-29 19:54:40 +01:00
|
|
|
return StringMatch(uri.c_str());
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
return Match(*song.tag);
|
2014-01-07 21:39:47 +01:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
SongFilter::SongFilter(unsigned tag, const char *value, bool fold_case)
|
|
|
|
{
|
|
|
|
items.push_back(Item(tag, value, fold_case));
|
|
|
|
}
|
|
|
|
|
|
|
|
SongFilter::~SongFilter()
|
|
|
|
{
|
|
|
|
/* this destructor exists here just so it won't get inlined */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SongFilter::Parse(const char *tag_string, const char *value, bool fold_case)
|
|
|
|
{
|
|
|
|
unsigned tag = locate_parse_type(tag_string);
|
|
|
|
if (tag == TAG_NUM_OF_ITEM_TYPES)
|
|
|
|
return false;
|
|
|
|
|
2013-10-29 18:54:34 +01:00
|
|
|
if (tag == LOCATE_TAG_BASE_TYPE) {
|
|
|
|
if (!uri_safe_local(value))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* case folding doesn't work with "base" */
|
|
|
|
fold_case = false;
|
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
items.push_back(Item(tag, value, fold_case));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-04-24 09:43:08 +02:00
|
|
|
SongFilter::Parse(ConstBuffer<const char *> args, bool fold_case)
|
2012-08-29 19:27:03 +02:00
|
|
|
{
|
2014-04-24 09:43:08 +02:00
|
|
|
if (args.size == 0 || args.size % 2 != 0)
|
2012-08-29 19:27:03 +02:00
|
|
|
return false;
|
|
|
|
|
2014-04-24 09:43:08 +02:00
|
|
|
for (unsigned i = 0; i < args.size; i += 2)
|
|
|
|
if (!Parse(args[i], args[i + 1], fold_case))
|
2012-08-29 19:27:03 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
bool
|
2014-01-19 10:51:34 +01:00
|
|
|
SongFilter::Match(const DetachedSong &song) const
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
for (const auto &i : items)
|
|
|
|
if (!i.Match(song))
|
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
|
|
|
}
|
2013-10-29 18:54:34 +01:00
|
|
|
|
2014-01-07 21:39:47 +01:00
|
|
|
bool
|
2014-01-19 10:51:34 +01:00
|
|
|
SongFilter::Match(const LightSong &song) const
|
2014-01-07 21:39:47 +01:00
|
|
|
{
|
|
|
|
for (const auto &i : items)
|
|
|
|
if (!i.Match(song))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-23 09:12:51 +02:00
|
|
|
bool
|
|
|
|
SongFilter::HasOtherThanBase() const
|
|
|
|
{
|
|
|
|
for (const auto &i : items)
|
|
|
|
if (i.GetTag() != LOCATE_TAG_BASE_TYPE)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-29 18:54:34 +01:00
|
|
|
std::string
|
|
|
|
SongFilter::GetBase() const
|
|
|
|
{
|
|
|
|
for (const auto &i : items)
|
|
|
|
if (i.GetTag() == LOCATE_TAG_BASE_TYPE)
|
|
|
|
return i.GetValue();
|
|
|
|
|
|
|
|
return std::string();
|
|
|
|
}
|