Make the enabled/disabled state of a global search provider separate from its logged in state - means that now Grooveshark and Spotify providers will be enabled as soon as you login

This commit is contained in:
David Sansome 2011-11-28 14:18:20 +00:00
parent b873209d39
commit 4dc4aa33ac
13 changed files with 59 additions and 41 deletions

View File

@ -61,16 +61,25 @@ void GlobalSearch::ConnectProvider(SearchProvider* provider) {
SLOT(ProviderDestroyedSlot(QObject*)));
}
void GlobalSearch::AddProvider(SearchProvider* provider, bool enable_by_default) {
void GlobalSearch::AddProvider(SearchProvider* provider) {
Q_ASSERT(!provider->name().isEmpty());
ConnectProvider(provider);
bool enabled = provider->is_enabled_by_default();
// Check if there is saved enabled/disabled state for this provider.
QSettings s;
s.beginGroup(kSettingsGroup);
QVariant enabled_variant = s.value("enabled_" + provider->id());
if (enabled_variant.isValid()) {
enabled = enabled_variant.toBool();
}
// Add data
ProviderData data;
data.enabled_ = providers_state_preference_.contains(provider->id()) ?
providers_state_preference_[provider->id()] : enable_by_default;
data.enabled_ = enabled;
providers_[provider] = data;
ConnectProvider(provider);
emit ProviderAdded(provider);
}
@ -84,7 +93,7 @@ int GlobalSearch::SearchAsync(const QString& query) {
url_provider_->SearchAsync(id, query);
} else {
foreach (SearchProvider* provider, providers_.keys()) {
if (!providers_[provider].enabled_)
if (!is_provider_usable(provider))
continue;
pending_search_providers_[id] ++;
@ -196,7 +205,7 @@ int GlobalSearch::LoadArtAsync(const SearchProvider::Result& result) {
pending_art_searches_[id] = result.pixmap_cache_key_;
if (providers_.contains(result.provider_) &&
!providers_[result.provider_].enabled_) {
!is_provider_usable(result.provider_)) {
emit ArtLoaded(id, QPixmap());
return id;
}
@ -289,7 +298,6 @@ bool GlobalSearch::SetProviderEnabled(const SearchProvider* const_provider,
return false;
} else {
providers_[provider].enabled_ = enabled;
emit ProviderToggled(provider, enabled);
SaveProvidersSettings();
return true;
}
@ -305,6 +313,10 @@ bool GlobalSearch::is_provider_enabled(const SearchProvider* const_provider) con
return providers_[provider].enabled_;
}
bool GlobalSearch::is_provider_usable(SearchProvider* provider) const {
return is_provider_enabled(provider) && provider->IsLoggedIn();
}
void GlobalSearch::ReloadSettings() {
QSettings s;
s.beginGroup(kSettingsGroup);
@ -314,15 +326,7 @@ void GlobalSearch::ReloadSettings() {
if (!value.isValid())
continue;
providers_state_preference_.insert(provider->id(), value.toBool());
const bool old_state = providers_[provider].enabled_;
const bool new_state = value.toBool() && provider->IsLoggedIn();
if (old_state != new_state) {
providers_[provider].enabled_ = new_state;
emit ProviderToggled(provider, new_state);
}
providers_[provider].enabled_ = value.toBool();
}
}

View File

@ -38,7 +38,7 @@ public:
static const char* kSettingsGroup;
static const int kMaxResultsPerEmission;
void AddProvider(SearchProvider* provider, bool enable_by_default = true);
void AddProvider(SearchProvider* provider);
// Try to change provider state. Returns false if we can't (e.g. we can't
// enable a provider because it requires the user to be logged-in)
bool SetProviderEnabled(const SearchProvider* provider, bool enabled);
@ -53,8 +53,10 @@ public:
bool FindCachedPixmap(const SearchProvider::Result& result, QPixmap* pixmap) const;
// "enabled" is the user preference. "usable" is enabled AND logged in.
QList<SearchProvider*> providers() const;
bool is_provider_enabled(const SearchProvider* provider) const;
bool is_provider_usable(SearchProvider* provider) const;
static bool HideOtherSearchBoxes();
@ -72,7 +74,6 @@ signals:
void ProviderAdded(const SearchProvider* provider);
void ProviderRemoved(const SearchProvider* provider);
void ProviderToggled(const SearchProvider* provider, bool enabled);
protected:
void timerEvent(QTimerEvent* e);
@ -121,8 +122,6 @@ private:
QPixmapCache pixmap_cache_;
QMap<int, QString> pending_art_searches_;
QMap<QString, bool> providers_state_preference_;
// Used for providers with ArtIsInSongMetadata set.
BackgroundThread<AlbumCoverLoader>* cover_loader_;
QMap<quint64, int> cover_loader_tasks_;

View File

@ -22,7 +22,7 @@ IcecastSearchProvider::IcecastSearchProvider(IcecastBackend* backend, QObject* p
: BlockingSearchProvider(parent),
backend_(backend)
{
Init("Icecast", "icecast", QIcon(":last.fm/icon_radio.png"));
Init("Icecast", "icecast", QIcon(":last.fm/icon_radio.png"), DisabledByDefault);
}
SearchProvider::ResultList IcecastSearchProvider::Search(int id, const QString& query) {

View File

@ -28,12 +28,19 @@ LibrarySearchProvider::LibrarySearchProvider(LibraryBackendInterface* backend,
const QString& name,
const QString& id,
const QIcon& icon,
bool enabled_by_default,
QObject* parent)
: BlockingSearchProvider(parent),
backend_(backend)
{
Init(name, id, icon, WantsSerialisedArtQueries | ArtIsInSongMetadata |
CanGiveSuggestions);
Hints hints = WantsSerialisedArtQueries | ArtIsInSongMetadata |
CanGiveSuggestions;
if (!enabled_by_default) {
hints |= DisabledByDefault;
}
Init(name, id, icon, hints);
}
SearchProvider::ResultList LibrarySearchProvider::Search(int id, const QString& query) {

View File

@ -26,7 +26,8 @@ class LibraryBackendInterface;
class LibrarySearchProvider : public BlockingSearchProvider {
public:
LibrarySearchProvider(LibraryBackendInterface* backend, const QString& name,
const QString& id, const QIcon& icon, QObject* parent = 0);
const QString& id, const QIcon& icon,
bool enabled_by_default, QObject* parent = 0);
ResultList Search(int id, const QString& query);
void LoadTracksAsync(int id, const Result& result);

View File

@ -90,7 +90,12 @@ public:
// This provider can provide some example search strings to display in the
// UI.
CanGiveSuggestions = 0x20
CanGiveSuggestions = 0x20,
// Normally providers get enabled unless the user chooses otherwise.
// Setting this flag indicates that this provider is disabled by default
// instead.
DisabledByDefault = 0x40
};
Q_DECLARE_FLAGS(Hints, Hint)
@ -105,6 +110,8 @@ public:
bool art_is_in_song_metadata() const { return hints() & ArtIsInSongMetadata; }
bool can_show_config() const { return hints() & CanShowConfig; }
bool can_give_suggestions() const { return hints() & CanGiveSuggestions; }
bool is_disabled_by_default() const { return hints() & DisabledByDefault; }
bool is_enabled_by_default() const { return !is_disabled_by_default(); }
// Starts a search. Must emit ResultsAvailable zero or more times and then
// SearchFinished exactly once, using this ID.

View File

@ -113,7 +113,7 @@ GroovesharkService::GroovesharkService(InternetModel *parent)
GroovesharkSearchProvider* search_provider = new GroovesharkSearchProvider(this);
search_provider->Init(this);
model()->global_search()->AddProvider(search_provider, false);
model()->global_search()->AddProvider(search_provider);
// Init secret: this code is ugly, but that's good as nobody is supposed to wonder what it does
QByteArray ba = QByteArray::fromBase64(QCoreApplication::applicationName().toLatin1());

View File

@ -61,8 +61,7 @@ IcecastService::IcecastService(InternetModel* parent)
model_ = new IcecastModel(backend_, this);
filter_->SetIcecastModel(model_);
model()->global_search()->AddProvider(
new IcecastSearchProvider(backend_, this), false);
model()->global_search()->AddProvider(new IcecastSearchProvider(backend_, this));
}
IcecastService::~IcecastService() {

View File

@ -124,8 +124,8 @@ JamendoService::JamendoService(InternetModel* parent)
tr("Jamendo"),
"jamendo",
QIcon(":/providers/jamendo.png"),
this),
false /* disabled Jamendo by default */);
false,
this));
}
JamendoService::~JamendoService() {

View File

@ -100,6 +100,7 @@ MagnatuneService::MagnatuneService(InternetModel* parent)
tr("Magnatune"),
"magnatune",
QIcon(":/providers/magnatune.png"),
true,
this));
}

View File

@ -73,7 +73,7 @@ SpotifyService::SpotifyService(InternetModel* parent)
model()->player()->playlists()->RegisterSpecialPlaylistType(
new SpotifySearchPlaylistType(this));
model()->global_search()->AddProvider(new SpotifySearchProvider(this), false);
model()->global_search()->AddProvider(new SpotifySearchProvider(this));
search_delay_->setInterval(kSearchDelayMsec);
search_delay_->setSingleShot(true);

View File

@ -1000,7 +1000,7 @@ msgstr ""
msgid "Configure Last.fm..."
msgstr ""
#: internet/magnatuneservice.cpp:274
#: internet/magnatuneservice.cpp:275
msgid "Configure Magnatune..."
msgstr ""
@ -1434,7 +1434,7 @@ msgstr ""
msgid "Download membership"
msgstr ""
#: internet/magnatuneservice.cpp:270
#: internet/magnatuneservice.cpp:271
msgid "Download this album"
msgstr ""
@ -1446,7 +1446,7 @@ msgstr ""
msgid "Download..."
msgstr ""
#: internet/icecastservice.cpp:98
#: internet/icecastservice.cpp:97
msgid "Downloading Icecast directory"
msgstr ""
@ -1454,7 +1454,7 @@ msgstr ""
msgid "Downloading Jamendo catalogue"
msgstr ""
#: internet/magnatuneservice.cpp:155
#: internet/magnatuneservice.cpp:156
msgid "Downloading Magnatune catalogue"
msgstr ""
@ -2772,8 +2772,8 @@ msgid "Only show the first"
msgstr ""
#: internet/digitallyimportedservicebase.cpp:178
#: internet/groovesharkservice.cpp:503 internet/icecastservice.cpp:300
#: internet/jamendoservice.cpp:415 internet/magnatuneservice.cpp:272
#: internet/groovesharkservice.cpp:503 internet/icecastservice.cpp:299
#: internet/jamendoservice.cpp:415 internet/magnatuneservice.cpp:273
#: internet/somafmservice.cpp:84
#, qt-format
msgid "Open %1 in browser"
@ -3161,7 +3161,7 @@ msgstr ""
msgid "Really cancel?"
msgstr ""
#: internet/jamendoservice.cpp:416 internet/magnatuneservice.cpp:273
#: internet/jamendoservice.cpp:416 internet/magnatuneservice.cpp:274
msgid "Refresh catalogue"
msgstr ""
@ -3173,7 +3173,7 @@ msgstr ""
msgid "Refresh friends list"
msgstr ""
#: internet/icecastservice.cpp:301
#: internet/icecastservice.cpp:300
msgid "Refresh station list"
msgstr ""
@ -3399,7 +3399,7 @@ msgstr ""
msgid "Search Jamendo"
msgstr ""
#: internet/magnatuneservice.cpp:279
#: internet/magnatuneservice.cpp:280
msgid "Search Magnatune"
msgstr ""

View File

@ -239,7 +239,7 @@ MainWindow::MainWindow(
// Add global search providers
global_search->AddProvider(new LibrarySearchProvider(
library_->backend(), tr("Library"), "library",
IconLoader::Load("folder-sound"), global_search));
IconLoader::Load("folder-sound"), true, this));
global_search->ReloadSettings();