From 4dc4aa33ac949cfb0a9faae58119cf49c17dd24e Mon Sep 17 00:00:00 2001 From: David Sansome Date: Mon, 28 Nov 2011 14:18:20 +0000 Subject: [PATCH] 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 --- src/globalsearch/globalsearch.cpp | 36 ++++++++++++---------- src/globalsearch/globalsearch.h | 7 ++--- src/globalsearch/icecastsearchprovider.cpp | 2 +- src/globalsearch/librarysearchprovider.cpp | 11 +++++-- src/globalsearch/librarysearchprovider.h | 3 +- src/globalsearch/searchprovider.h | 9 +++++- src/internet/groovesharkservice.cpp | 2 +- src/internet/icecastservice.cpp | 3 +- src/internet/jamendoservice.cpp | 4 +-- src/internet/magnatuneservice.cpp | 1 + src/internet/spotifyservice.cpp | 2 +- src/translations/translations.pot | 18 +++++------ src/ui/mainwindow.cpp | 2 +- 13 files changed, 59 insertions(+), 41 deletions(-) diff --git a/src/globalsearch/globalsearch.cpp b/src/globalsearch/globalsearch.cpp index b3618d61b..f3ab685fd 100644 --- a/src/globalsearch/globalsearch.cpp +++ b/src/globalsearch/globalsearch.cpp @@ -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(); } } diff --git a/src/globalsearch/globalsearch.h b/src/globalsearch/globalsearch.h index 2d56226c5..28b957d5c 100644 --- a/src/globalsearch/globalsearch.h +++ b/src/globalsearch/globalsearch.h @@ -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 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 pending_art_searches_; - QMap providers_state_preference_; - // Used for providers with ArtIsInSongMetadata set. BackgroundThread* cover_loader_; QMap cover_loader_tasks_; diff --git a/src/globalsearch/icecastsearchprovider.cpp b/src/globalsearch/icecastsearchprovider.cpp index da4a75088..5cbb730a5 100644 --- a/src/globalsearch/icecastsearchprovider.cpp +++ b/src/globalsearch/icecastsearchprovider.cpp @@ -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) { diff --git a/src/globalsearch/librarysearchprovider.cpp b/src/globalsearch/librarysearchprovider.cpp index c84f2b433..4d82b719f 100644 --- a/src/globalsearch/librarysearchprovider.cpp +++ b/src/globalsearch/librarysearchprovider.cpp @@ -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) { diff --git a/src/globalsearch/librarysearchprovider.h b/src/globalsearch/librarysearchprovider.h index bbf534d01..65bb1d34e 100644 --- a/src/globalsearch/librarysearchprovider.h +++ b/src/globalsearch/librarysearchprovider.h @@ -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); diff --git a/src/globalsearch/searchprovider.h b/src/globalsearch/searchprovider.h index 9080b9064..39d5060a7 100644 --- a/src/globalsearch/searchprovider.h +++ b/src/globalsearch/searchprovider.h @@ -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. diff --git a/src/internet/groovesharkservice.cpp b/src/internet/groovesharkservice.cpp index e736a978e..417261800 100644 --- a/src/internet/groovesharkservice.cpp +++ b/src/internet/groovesharkservice.cpp @@ -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()); diff --git a/src/internet/icecastservice.cpp b/src/internet/icecastservice.cpp index 2cfec0e27..c36a87f04 100644 --- a/src/internet/icecastservice.cpp +++ b/src/internet/icecastservice.cpp @@ -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() { diff --git a/src/internet/jamendoservice.cpp b/src/internet/jamendoservice.cpp index 05efc8dac..9d9fe469e 100644 --- a/src/internet/jamendoservice.cpp +++ b/src/internet/jamendoservice.cpp @@ -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() { diff --git a/src/internet/magnatuneservice.cpp b/src/internet/magnatuneservice.cpp index 8754fd6e7..7893743e2 100644 --- a/src/internet/magnatuneservice.cpp +++ b/src/internet/magnatuneservice.cpp @@ -100,6 +100,7 @@ MagnatuneService::MagnatuneService(InternetModel* parent) tr("Magnatune"), "magnatune", QIcon(":/providers/magnatune.png"), + true, this)); } diff --git a/src/internet/spotifyservice.cpp b/src/internet/spotifyservice.cpp index 89df9ebd7..7e28ee86b 100644 --- a/src/internet/spotifyservice.cpp +++ b/src/internet/spotifyservice.cpp @@ -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); diff --git a/src/translations/translations.pot b/src/translations/translations.pot index f29734523..ecfbe382b 100644 --- a/src/translations/translations.pot +++ b/src/translations/translations.pot @@ -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 "" diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index ebb6fd77d..4d511867c 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -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();