2011-09-24 21:40:50 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "simplesearchprovider.h"
|
2011-09-24 23:49:04 +02:00
|
|
|
#include "core/logging.h"
|
2011-09-24 21:40:50 +02:00
|
|
|
#include "playlist/songmimedata.h"
|
|
|
|
|
|
|
|
const int SimpleSearchProvider::kDefaultResultLimit = 6;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
SimpleSearchProvider::Item::Item(const QString& title, const QUrl& url,
|
|
|
|
const QString& keyword)
|
|
|
|
: keyword_(keyword) {
|
2011-09-24 21:40:50 +02:00
|
|
|
metadata_.set_title(title);
|
|
|
|
metadata_.set_url(url);
|
|
|
|
}
|
|
|
|
|
2011-09-24 23:49:04 +02:00
|
|
|
SimpleSearchProvider::Item::Item(const Song& song, const QString& keyword)
|
2014-02-07 16:34:20 +01:00
|
|
|
: keyword_(keyword), metadata_(song) {}
|
2011-09-24 21:40:50 +02:00
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
SimpleSearchProvider::SimpleSearchProvider(Application* app, QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: BlockingSearchProvider(app, parent),
|
|
|
|
result_limit_(kDefaultResultLimit),
|
|
|
|
max_suggestion_count_(-1),
|
|
|
|
items_dirty_(true),
|
|
|
|
has_searched_before_(false) {}
|
2011-09-24 21:40:50 +02:00
|
|
|
|
2011-10-01 18:41:28 +02:00
|
|
|
void SimpleSearchProvider::MaybeRecreateItems() {
|
|
|
|
if (has_searched_before_) {
|
|
|
|
RecreateItems();
|
|
|
|
} else {
|
|
|
|
items_dirty_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
SearchProvider::ResultList SimpleSearchProvider::Search(int id,
|
|
|
|
const QString& query) {
|
2011-09-24 21:40:50 +02:00
|
|
|
Q_UNUSED(id)
|
|
|
|
|
2011-10-01 18:41:28 +02:00
|
|
|
if (items_dirty_) {
|
|
|
|
RecreateItems();
|
|
|
|
items_dirty_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
has_searched_before_ = true;
|
|
|
|
|
2011-09-24 21:40:50 +02:00
|
|
|
ResultList ret;
|
|
|
|
const QStringList tokens = TokenizeQuery(query);
|
|
|
|
|
|
|
|
QMutexLocker l(&items_mutex_);
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Item& item : items_) {
|
2012-06-04 19:18:37 +02:00
|
|
|
bool matched = true;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& token : tokens) {
|
2012-06-04 19:18:37 +02:00
|
|
|
if (!item.keyword_.contains(token, Qt::CaseInsensitive) &&
|
2012-06-10 18:36:40 +02:00
|
|
|
!item.metadata_.title().contains(token, Qt::CaseInsensitive) &&
|
2012-06-04 19:18:37 +02:00
|
|
|
!safe_words_.contains(token, Qt::CaseInsensitive)) {
|
|
|
|
matched = false;
|
2011-09-24 21:40:50 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
if (matched) {
|
2012-06-10 18:36:40 +02:00
|
|
|
Result result(this);
|
2012-06-04 19:18:37 +02:00
|
|
|
result.group_automatically_ = false;
|
2011-09-24 21:40:50 +02:00
|
|
|
result.metadata_ = item.metadata_;
|
|
|
|
ret << result;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (ret.count() >= result_limit_) break;
|
2011-09-24 21:40:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleSearchProvider::SetItems(const ItemList& items) {
|
|
|
|
QMutexLocker l(&items_mutex_);
|
|
|
|
items_ = items;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (ItemList::iterator it = items_.begin(); it != items_.end(); ++it) {
|
2012-06-10 21:55:51 +02:00
|
|
|
it->metadata_.set_filetype(Song::Type_Stream);
|
|
|
|
}
|
2011-09-24 21:40:50 +02:00
|
|
|
}
|
2011-11-06 17:29:09 +01:00
|
|
|
|
2012-06-11 00:05:30 +02:00
|
|
|
QStringList SimpleSearchProvider::GetSuggestions(int count) {
|
|
|
|
if (max_suggestion_count_ != -1) {
|
|
|
|
count = qMin(max_suggestion_count_, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList ret;
|
2011-11-06 17:29:09 +01:00
|
|
|
QMutexLocker l(&items_mutex_);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (items_.isEmpty()) return ret;
|
2012-06-11 00:05:30 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int attempt = 0; attempt < count * 5; ++attempt) {
|
2012-06-11 00:05:30 +02:00
|
|
|
if (ret.count() >= count) {
|
|
|
|
break;
|
|
|
|
}
|
2011-11-06 17:29:09 +01:00
|
|
|
|
|
|
|
const Item& item = items_[qrand() % items_.count()];
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!item.keyword_.isEmpty()) ret << item.keyword_;
|
|
|
|
if (!item.metadata_.title().isEmpty()) ret << item.metadata_.title();
|
2011-11-06 17:29:09 +01:00
|
|
|
}
|
|
|
|
|
2012-06-11 00:05:30 +02:00
|
|
|
return ret;
|
2011-11-06 17:29:09 +01:00
|
|
|
}
|