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
|
|
|
|
|
2015-11-27 15:22:59 +01:00
|
|
|
#include <QFuture>
|
2011-08-28 22:33:59 +02:00
|
|
|
#include <QIcon>
|
|
|
|
#include <QMetaType>
|
|
|
|
#include <QObject>
|
|
|
|
|
|
|
|
#include "core/song.h"
|
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
class Application;
|
2012-06-19 23:25:15 +02:00
|
|
|
class InternetService;
|
2012-06-19 23:32:15 +02:00
|
|
|
class MimeData;
|
2011-08-29 03:37:55 +02:00
|
|
|
|
2011-08-28 22:33:59 +02:00
|
|
|
class SearchProvider : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
public:
|
2014-02-10 16:03:54 +01:00
|
|
|
SearchProvider(Application* app, QObject* parent = nullptr);
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
static const int kArtHeight;
|
|
|
|
|
|
|
|
struct Result {
|
|
|
|
Result(SearchProvider* provider = 0)
|
2014-02-07 16:34:20 +01:00
|
|
|
: provider_(provider), group_automatically_(true) {}
|
2011-08-28 22:33:59 +02:00
|
|
|
|
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_;
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
// If this is set to true, the view will group this result into
|
|
|
|
// artist/album categories as appropriate.
|
|
|
|
bool group_automatically_;
|
2011-08-28 23:52:24 +02:00
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
// Must be set by the provider.
|
|
|
|
Song metadata_;
|
2011-09-18 01:06:07 +02:00
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
// This is set and used by the GlobalSearch engine itself.
|
2011-08-28 23:52:24 +02:00
|
|
|
QString pixmap_cache_key_;
|
2011-08-28 22:33:59 +02:00
|
|
|
};
|
|
|
|
typedef QList<Result> ResultList;
|
|
|
|
|
2011-10-20 00:01:10 +02:00
|
|
|
enum Hint {
|
|
|
|
NoHints = 0x00,
|
|
|
|
|
|
|
|
// Indicates that queries to this provider mean making requests to a third
|
|
|
|
// party. To be polite, queries should be buffered by a few milliseconds
|
|
|
|
// instead of executing them each time the user types a character.
|
|
|
|
WantsDelayedQueries = 0x01,
|
|
|
|
|
|
|
|
// Indicates that this provider wants to be given art queries one after the
|
|
|
|
// other (serially), instead of all at once (in parallel).
|
|
|
|
WantsSerialisedArtQueries = 0x02,
|
|
|
|
|
|
|
|
// Indicates that album cover art is probably going to be loaded remotely.
|
|
|
|
// If a third-party application is making art requests over dbus and has
|
|
|
|
// to get all the art it can before showing results to the user, it might
|
|
|
|
// not load art from this provider.
|
2011-11-04 23:31:19 +01:00
|
|
|
ArtIsProbablyRemote = 0x04,
|
|
|
|
|
|
|
|
// Indicates the art URL (or filename) for each result is stored in the
|
|
|
|
// normal place in the song metadata. LoadArtAsync will never be called and
|
|
|
|
// WantsSerialisedArtQueries and ArtIsProbablyRemote will be ignored if
|
|
|
|
// they are set as well. The GlobalSearch engine will load the art itself.
|
2011-11-05 18:11:02 +01:00
|
|
|
ArtIsInSongMetadata = 0x08,
|
|
|
|
|
|
|
|
// Indicates this provider has a config dialog that can be shown by calling
|
2012-06-04 19:18:37 +02:00
|
|
|
// ShowConfig. If this is not set then the button will be greyed out
|
2011-11-05 18:11:02 +01:00
|
|
|
// in the GUI.
|
2011-11-06 17:29:09 +01:00
|
|
|
CanShowConfig = 0x10,
|
|
|
|
|
|
|
|
// This provider can provide some example search strings to display in the
|
|
|
|
// UI.
|
2011-11-28 15:18:20 +01:00
|
|
|
CanGiveSuggestions = 0x20,
|
|
|
|
|
|
|
|
// Normally providers get enabled unless the user chooses otherwise.
|
|
|
|
// Setting this flag indicates that this provider is disabled by default
|
|
|
|
// instead.
|
2012-06-10 21:55:51 +02:00
|
|
|
DisabledByDefault = 0x40,
|
|
|
|
|
|
|
|
// The default implementation of LoadTracksAsync normally creates a
|
|
|
|
// SongMimeData containing the entire metadata for each result being loaded.
|
|
|
|
// Setting this flag will cause a plain MimeData to be created containing
|
|
|
|
// only the URLs of the results.
|
|
|
|
MimeDataContainsUrlsOnly = 0x80
|
2011-10-20 00:01:10 +02:00
|
|
|
};
|
|
|
|
Q_DECLARE_FLAGS(Hints, Hint)
|
|
|
|
|
2011-08-28 22:33:59 +02:00
|
|
|
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_; }
|
2015-05-31 12:08:33 +02:00
|
|
|
const QImage& icon_as_image() const { return icon_as_image_; }
|
2011-10-20 00:01:10 +02:00
|
|
|
|
|
|
|
Hints hints() const { return hints_; }
|
|
|
|
bool wants_delayed_queries() const { return hints() & WantsDelayedQueries; }
|
2014-02-07 16:34:20 +01:00
|
|
|
bool wants_serialised_art() const {
|
|
|
|
return hints() & WantsSerialisedArtQueries;
|
|
|
|
}
|
2011-10-20 00:01:10 +02:00
|
|
|
bool art_is_probably_remote() const { return hints() & ArtIsProbablyRemote; }
|
2011-11-04 23:31:19 +01:00
|
|
|
bool art_is_in_song_metadata() const { return hints() & ArtIsInSongMetadata; }
|
2011-11-05 18:11:02 +01:00
|
|
|
bool can_show_config() const { return hints() & CanShowConfig; }
|
2011-11-06 17:29:09 +01:00
|
|
|
bool can_give_suggestions() const { return hints() & CanGiveSuggestions; }
|
2011-11-28 15:18:20 +01:00
|
|
|
bool is_disabled_by_default() const { return hints() & DisabledByDefault; }
|
|
|
|
bool is_enabled_by_default() const { return !is_disabled_by_default(); }
|
2014-02-07 16:34:20 +01:00
|
|
|
bool mime_data_contains_urls_only() const {
|
|
|
|
return hints() & MimeDataContainsUrlsOnly;
|
|
|
|
}
|
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.
|
2011-11-04 23:31:19 +01:00
|
|
|
virtual void LoadArtAsync(int id, const Result& result);
|
2011-08-28 22:33:59 +02:00
|
|
|
|
2012-06-10 21:55:51 +02:00
|
|
|
// Loads tracks for results that were previously emitted by ResultsAvailable.
|
|
|
|
// The default implementation creates a SongMimeData with one Song for each
|
|
|
|
// Result, unless the MimeDataContainsUrlsOnly flag is set.
|
|
|
|
virtual MimeData* LoadTracks(const ResultList& results);
|
2011-08-28 22:33:59 +02:00
|
|
|
|
2012-06-11 00:05:30 +02:00
|
|
|
// Returns some example search strings to display in the UI. The provider
|
|
|
|
// should pick some of its items at random and return between 0 and count
|
|
|
|
// strings. Remember to set the CanGiveSuggestions hint.
|
|
|
|
virtual QStringList GetSuggestions(int count) { return QStringList(); }
|
2011-11-06 17:29:09 +01:00
|
|
|
|
2011-10-20 15:03:47 +02:00
|
|
|
// If provider needs user login to search and play songs, this method should
|
|
|
|
// be reimplemented
|
|
|
|
virtual bool IsLoggedIn() { return true; }
|
2014-02-07 16:34:20 +01:00
|
|
|
virtual void ShowConfig() {} // Remember to set the CanShowConfig hint
|
2015-11-26 19:51:32 +01:00
|
|
|
// Returns the Internet service in charge of this provider, or nullptr if
|
|
|
|
// there is none.
|
2014-02-21 17:24:49 +01:00
|
|
|
virtual InternetService* internet_service() { return nullptr; }
|
2011-10-20 15:03:47 +02:00
|
|
|
|
2011-08-29 00:59:18 +02:00
|
|
|
static QImage ScaleAndPad(const QImage& image);
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
signals:
|
2011-08-28 22:33:59 +02:00
|
|
|
void ResultsAvailable(int id, const SearchProvider::ResultList& results);
|
|
|
|
void SearchFinished(int id);
|
|
|
|
|
|
|
|
void ArtLoaded(int id, const QImage& image);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
protected:
|
2011-08-28 22:33:59 +02:00
|
|
|
// 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);
|
2012-06-04 19:18:37 +02:00
|
|
|
static bool Matches(const QStringList& tokens, const QString& string);
|
2011-09-18 01:06:07 +02:00
|
|
|
|
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-10-20 00:01:10 +02:00
|
|
|
Hints hints = NoHints);
|
2014-03-30 08:08:30 +02:00
|
|
|
void SetHint(Hint hint, bool set = true);
|
2011-08-29 01:13:36 +02:00
|
|
|
|
2011-11-01 13:00:27 +01:00
|
|
|
struct PendingState {
|
|
|
|
PendingState() : orig_id_(-1) {}
|
|
|
|
PendingState(int orig_id, QStringList tokens)
|
2014-02-07 16:34:20 +01:00
|
|
|
: orig_id_(orig_id), tokens_(tokens) {}
|
2011-11-01 13:00:27 +01:00
|
|
|
int orig_id_;
|
|
|
|
QStringList tokens_;
|
|
|
|
|
|
|
|
bool operator<(const PendingState& b) const {
|
|
|
|
return orig_id_ < b.orig_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const PendingState& b) const {
|
|
|
|
return orig_id_ == b.orig_id_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
protected:
|
2012-02-13 21:44:04 +01:00
|
|
|
Application* app_;
|
2011-11-01 13:00:27 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private:
|
2011-08-28 22:33:59 +02:00
|
|
|
QString name_;
|
2011-09-17 18:42:14 +02:00
|
|
|
QString id_;
|
2011-08-28 22:33:59 +02:00
|
|
|
QIcon icon_;
|
2011-10-20 00:01:10 +02:00
|
|
|
Hints hints_;
|
2015-05-31 12:08:33 +02:00
|
|
|
QImage icon_as_image_;
|
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-10-20 00:01:10 +02:00
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(SearchProvider::Hints)
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
class BlockingSearchProvider : public SearchProvider {
|
|
|
|
Q_OBJECT
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
public:
|
2014-02-10 16:03:54 +01:00
|
|
|
BlockingSearchProvider(Application* app, QObject* parent = nullptr);
|
2011-08-28 22:33:59 +02:00
|
|
|
|
|
|
|
void SearchAsync(int id, const QString& query);
|
|
|
|
virtual ResultList Search(int id, const QString& query) = 0;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private slots:
|
2015-11-26 19:51:32 +01:00
|
|
|
void BlockingSearchFinished(QFuture<ResultList> future, const int id);
|
2011-08-28 22:33:59 +02:00
|
|
|
};
|
|
|
|
|
2011-10-30 19:52:38 +01:00
|
|
|
Q_DECLARE_METATYPE(SearchProvider*)
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // SEARCHPROVIDER_H
|