2011-08-28 22:33:59 +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 "searchprovider.h"
|
|
|
|
#include "core/boundfuturewatcher.h"
|
2014-12-18 23:35:21 +01:00
|
|
|
#include "internet/core/internetsongmimedata.h"
|
2011-11-06 00:22:25 +01:00
|
|
|
#include "playlist/songmimedata.h"
|
2011-08-28 22:33:59 +02:00
|
|
|
|
2011-08-29 00:59:18 +02:00
|
|
|
#include <QPainter>
|
2012-06-10 21:55:51 +02:00
|
|
|
#include <QUrl>
|
2011-08-28 22:33:59 +02:00
|
|
|
#include <QtConcurrentRun>
|
|
|
|
|
|
|
|
const int SearchProvider::kArtHeight = 32;
|
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
SearchProvider::SearchProvider(Application* app, QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent), app_(app), hints_(0) {}
|
2011-08-28 22:33:59 +02:00
|
|
|
|
2011-10-20 00:01:10 +02:00
|
|
|
void SearchProvider::Init(const QString& name, const QString& id,
|
|
|
|
const QIcon& icon, Hints hints) {
|
2011-08-29 01:13:36 +02:00
|
|
|
name_ = name;
|
2011-09-17 18:42:14 +02:00
|
|
|
id_ = id;
|
2011-08-29 01:13:36 +02:00
|
|
|
icon_ = icon;
|
2011-10-20 00:01:10 +02:00
|
|
|
hints_ = hints;
|
2011-08-29 01:13:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-30 08:08:30 +02:00
|
|
|
void SearchProvider::SetHint(Hint hint, bool set) {
|
|
|
|
if (set) {
|
|
|
|
hints_ |= hint;
|
|
|
|
} else {
|
|
|
|
hints_ &= ~hint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-28 22:33:59 +02:00
|
|
|
QStringList SearchProvider::TokenizeQuery(const QString& query) {
|
|
|
|
QStringList tokens(query.split(QRegExp("\\s+")));
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (QStringList::iterator it = tokens.begin(); it != tokens.end(); ++it) {
|
2011-08-28 22:33:59 +02:00
|
|
|
(*it).remove('(');
|
|
|
|
(*it).remove(')');
|
|
|
|
(*it).remove('"');
|
|
|
|
|
|
|
|
const int colon = (*it).indexOf(":");
|
|
|
|
if (colon != -1) {
|
|
|
|
(*it).remove(0, colon + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
bool SearchProvider::Matches(const QStringList& tokens, const QString& string) {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& token : tokens) {
|
2012-06-04 19:18:37 +02:00
|
|
|
if (!string.contains(token, Qt::CaseInsensitive)) {
|
|
|
|
return false;
|
2011-08-28 22:33:59 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-29 00:24:54 +02:00
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
return true;
|
2011-08-28 22:33:59 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
BlockingSearchProvider::BlockingSearchProvider(Application* app,
|
|
|
|
QObject* parent)
|
|
|
|
: SearchProvider(app, parent) {}
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
void BlockingSearchProvider::SearchAsync(int id, const QString& query) {
|
2014-02-07 16:34:20 +01:00
|
|
|
QFuture<ResultList> future =
|
|
|
|
QtConcurrent::run(this, &BlockingSearchProvider::Search, id, query);
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
BoundFutureWatcher<ResultList, int>* watcher =
|
|
|
|
new BoundFutureWatcher<ResultList, int>(id);
|
|
|
|
watcher->setFuture(future);
|
|
|
|
connect(watcher, SIGNAL(finished()), SLOT(BlockingSearchFinished()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingSearchProvider::BlockingSearchFinished() {
|
|
|
|
BoundFutureWatcher<ResultList, int>* watcher =
|
|
|
|
static_cast<BoundFutureWatcher<ResultList, int>*>(sender());
|
|
|
|
watcher->deleteLater();
|
|
|
|
|
|
|
|
const int id = watcher->data();
|
|
|
|
emit ResultsAvailable(id, watcher->result());
|
|
|
|
emit SearchFinished(id);
|
|
|
|
}
|
2011-08-29 00:59:18 +02:00
|
|
|
|
|
|
|
QImage SearchProvider::ScaleAndPad(const QImage& image) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (image.isNull()) return QImage();
|
2011-08-29 00:59:18 +02:00
|
|
|
|
|
|
|
const QSize target_size = QSize(kArtHeight, kArtHeight);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (image.size() == target_size) return image;
|
2011-08-29 00:59:18 +02:00
|
|
|
|
|
|
|
// Scale the image down
|
|
|
|
QImage copy;
|
2014-02-07 16:34:20 +01:00
|
|
|
copy =
|
|
|
|
image.scaled(target_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
2011-08-29 00:59:18 +02:00
|
|
|
|
|
|
|
// Pad the image to kHeight x kHeight
|
2014-02-07 16:34:20 +01:00
|
|
|
if (copy.size() == target_size) return copy;
|
2011-08-29 00:59:18 +02:00
|
|
|
|
|
|
|
QImage padded_image(kArtHeight, kArtHeight, QImage::Format_ARGB32);
|
|
|
|
padded_image.fill(0);
|
|
|
|
|
|
|
|
QPainter p(&padded_image);
|
|
|
|
p.drawImage((kArtHeight - copy.width()) / 2, (kArtHeight - copy.height()) / 2,
|
|
|
|
copy);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
return padded_image;
|
|
|
|
}
|
2011-09-18 01:06:07 +02:00
|
|
|
|
2011-11-04 23:31:19 +01:00
|
|
|
void SearchProvider::LoadArtAsync(int id, const Result& result) {
|
|
|
|
emit ArtLoaded(id, QImage());
|
|
|
|
}
|
2011-11-06 00:22:25 +01:00
|
|
|
|
2012-06-10 21:55:51 +02:00
|
|
|
MimeData* SearchProvider::LoadTracks(const ResultList& results) {
|
2014-02-06 16:49:49 +01:00
|
|
|
MimeData* mime_data = nullptr;
|
2011-11-06 00:22:25 +01:00
|
|
|
|
2012-06-10 21:55:51 +02:00
|
|
|
if (mime_data_contains_urls_only()) {
|
|
|
|
mime_data = new MimeData;
|
|
|
|
} else {
|
2012-06-19 23:25:15 +02:00
|
|
|
SongList songs;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Result& result : results) {
|
|
|
|
songs << result.metadata_;
|
|
|
|
}
|
2012-06-19 23:25:15 +02:00
|
|
|
|
|
|
|
if (internet_service()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
InternetSongMimeData* internet_song_mime_data =
|
|
|
|
new InternetSongMimeData(internet_service());
|
2012-06-19 23:25:15 +02:00
|
|
|
internet_song_mime_data->songs = songs;
|
|
|
|
mime_data = internet_song_mime_data;
|
|
|
|
} else {
|
|
|
|
SongMimeData* song_mime_data = new SongMimeData;
|
|
|
|
song_mime_data->songs = songs;
|
|
|
|
mime_data = song_mime_data;
|
2012-06-10 21:55:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QUrl> urls;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Result& result : results) {
|
|
|
|
urls << result.metadata_.url();
|
|
|
|
}
|
2012-06-10 21:55:51 +02:00
|
|
|
mime_data->setUrls(urls);
|
|
|
|
|
|
|
|
return mime_data;
|
2011-11-06 00:22:25 +01:00
|
|
|
}
|