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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SEARCHPROVIDER_H
|
|
|
|
#define SEARCHPROVIDER_H
|
|
|
|
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QMetaType>
|
|
|
|
#include <QObject>
|
|
|
|
|
|
|
|
#include "core/song.h"
|
|
|
|
|
2011-08-29 03:37:55 +02:00
|
|
|
class MimeData;
|
|
|
|
|
|
|
|
|
2011-08-28 22:33:59 +02:00
|
|
|
class SearchProvider : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2011-08-29 01:13:36 +02:00
|
|
|
SearchProvider(QObject* parent = 0);
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
static const int kArtHeight;
|
|
|
|
|
|
|
|
struct Result {
|
|
|
|
Result(SearchProvider* provider = 0)
|
|
|
|
: provider_(provider), album_size_(0) {}
|
|
|
|
|
2011-08-28 23:20:22 +02:00
|
|
|
// The order of types here is the order they'll appear in the UI.
|
2011-08-28 22:33:59 +02:00
|
|
|
enum Type {
|
2011-08-28 23:20:22 +02:00
|
|
|
Type_Track = 0,
|
2011-09-24 18:01:18 +02:00
|
|
|
Type_Stream,
|
2011-08-28 23:20:22 +02:00
|
|
|
Type_Album
|
2011-08-28 22:33:59 +02:00
|
|
|
};
|
|
|
|
|
2011-08-29 00:24:54 +02:00
|
|
|
enum MatchQuality {
|
|
|
|
// A token in the search string matched at the beginning of the song
|
|
|
|
// metadata.
|
|
|
|
Quality_AtStart = 0,
|
|
|
|
|
|
|
|
// A token matched somewhere else.
|
|
|
|
Quality_Middle,
|
|
|
|
|
|
|
|
Quality_None
|
|
|
|
};
|
|
|
|
|
2011-09-20 09:57:18 +02:00
|
|
|
// This must be set by the provider using the constructor.
|
2011-08-28 22:33:59 +02:00
|
|
|
SearchProvider* provider_;
|
|
|
|
|
2011-08-28 23:52:24 +02:00
|
|
|
// These must be set explicitly by the provider.
|
2011-08-28 22:33:59 +02:00
|
|
|
Type type_;
|
|
|
|
Song metadata_;
|
2011-08-29 00:24:54 +02:00
|
|
|
MatchQuality match_quality_;
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
// How many songs in the album - valid only if type == Type_Album.
|
|
|
|
int album_size_;
|
2011-08-28 23:52:24 +02:00
|
|
|
|
2011-09-18 01:06:07 +02:00
|
|
|
// Songs in the album - valid only if type == Type_Album. This is only
|
|
|
|
// used for display in the tooltip, so it's fine not to provide it.
|
|
|
|
SongList album_songs_;
|
|
|
|
|
2011-08-28 23:52:24 +02:00
|
|
|
QString pixmap_cache_key_;
|
2011-08-28 22:33:59 +02:00
|
|
|
};
|
|
|
|
typedef QList<Result> ResultList;
|
|
|
|
|
|
|
|
const QString& name() const { return name_; }
|
2011-09-17 18:42:14 +02:00
|
|
|
const QString& id() const { return id_; }
|
2011-08-28 22:33:59 +02:00
|
|
|
const QIcon& icon() const { return icon_; }
|
2011-08-29 01:32:45 +02:00
|
|
|
const bool wants_delayed_queries() const { return delay_searches_; }
|
|
|
|
const bool wants_serialised_art() const { return serialised_art_; }
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
// Starts a search. Must emit ResultsAvailable zero or more times and then
|
|
|
|
// SearchFinished exactly once, using this ID.
|
|
|
|
virtual void SearchAsync(int id, const QString& query) = 0;
|
|
|
|
|
|
|
|
// Starts loading an icon for a result that was previously emitted by
|
|
|
|
// ResultsAvailable. Must emit ArtLoaded exactly once with this ID.
|
|
|
|
virtual void LoadArtAsync(int id, const Result& result) = 0;
|
|
|
|
|
|
|
|
// Starts loading tracks for a result that was previously emitted by
|
|
|
|
// ResultsAvailable. Must emit TracksLoaded exactly once with this ID.
|
|
|
|
virtual void LoadTracksAsync(int id, const Result& result) = 0;
|
|
|
|
|
2011-08-29 00:59:18 +02:00
|
|
|
static QImage ScaleAndPad(const QImage& image);
|
|
|
|
|
2011-08-28 22:33:59 +02:00
|
|
|
signals:
|
|
|
|
void ResultsAvailable(int id, const SearchProvider::ResultList& results);
|
|
|
|
void SearchFinished(int id);
|
|
|
|
|
|
|
|
void ArtLoaded(int id, const QImage& image);
|
|
|
|
|
2011-08-29 03:37:55 +02:00
|
|
|
void TracksLoaded(int id, MimeData* mime_data);
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// These functions treat queries in the same way as LibraryQuery. They're
|
|
|
|
// useful for figuring out whether you got a result because it matched in
|
|
|
|
// the song title or the artist/album name.
|
|
|
|
static QStringList TokenizeQuery(const QString& query);
|
2011-08-29 00:24:54 +02:00
|
|
|
static Result::MatchQuality MatchQuality(const QStringList& tokens, const QString& string);
|
2011-08-28 22:33:59 +02:00
|
|
|
|
2011-09-18 01:06:07 +02:00
|
|
|
// Sorts a list of songs by disc, then by track.
|
|
|
|
static void SortSongs(SongList* list);
|
|
|
|
|
2011-09-24 18:01:18 +02:00
|
|
|
// Subclasses must call this from their constructors.
|
2011-09-17 18:42:14 +02:00
|
|
|
void Init(const QString& name, const QString& id, const QIcon& icon,
|
2011-08-29 01:32:45 +02:00
|
|
|
bool delay_searches, bool serialised_art);
|
2011-08-29 01:13:36 +02:00
|
|
|
|
2011-08-28 22:33:59 +02:00
|
|
|
private:
|
|
|
|
QString name_;
|
2011-09-17 18:42:14 +02:00
|
|
|
QString id_;
|
2011-08-28 22:33:59 +02:00
|
|
|
QIcon icon_;
|
2011-08-29 01:32:45 +02:00
|
|
|
bool delay_searches_;
|
|
|
|
bool serialised_art_;
|
2011-08-28 22:33:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(SearchProvider::Result)
|
2011-09-17 18:42:14 +02:00
|
|
|
Q_DECLARE_METATYPE(SearchProvider::ResultList)
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BlockingSearchProvider : public SearchProvider {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2011-08-29 01:13:36 +02:00
|
|
|
BlockingSearchProvider(QObject* parent = 0);
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
void SearchAsync(int id, const QString& query);
|
|
|
|
virtual ResultList Search(int id, const QString& query) = 0;
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void BlockingSearchFinished();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SEARCHPROVIDER_H
|