From 0f18fa9546db54bbadff100af2d8df614d5f3305 Mon Sep 17 00:00:00 2001 From: Phillip Wheatley Date: Mon, 13 Jan 2020 15:13:36 +0000 Subject: [PATCH] Hide games created/hosted by people on your Ignore List (#3883) * Implement filter for games created by ignored users. --- cockatrice/src/dlg_filter_games.cpp | 14 +++++++++ cockatrice/src/dlg_filter_games.h | 3 ++ cockatrice/src/gameselector.cpp | 31 ++++++++++++++++++- cockatrice/src/gameselector.h | 6 ++++ cockatrice/src/gamesmodel.cpp | 24 ++++++++++++-- cockatrice/src/gamesmodel.h | 11 ++++++- .../src/settings/gamefilterssettings.cpp | 11 +++++++ cockatrice/src/settings/gamefilterssettings.h | 2 ++ cockatrice/src/settingscache.cpp | 1 + 9 files changed, 99 insertions(+), 4 deletions(-) diff --git a/cockatrice/src/dlg_filter_games.cpp b/cockatrice/src/dlg_filter_games.cpp index aadcbe21..795f7def 100644 --- a/cockatrice/src/dlg_filter_games.cpp +++ b/cockatrice/src/dlg_filter_games.cpp @@ -25,6 +25,9 @@ DlgFilterGames::DlgFilterGames(const QMap &_allGameTypes, showPasswordProtectedGames = new QCheckBox(tr("Show &password protected games")); showPasswordProtectedGames->setChecked(gamesProxyModel->getShowPasswordProtectedGames()); + hideIgnoredUserGames = new QCheckBox(tr("Hide '&ignored user' games")); + hideIgnoredUserGames->setChecked(gamesProxyModel->getHideIgnoredUserGames()); + gameNameFilterEdit = new QLineEdit; gameNameFilterEdit->setText(gamesProxyModel->getGameNameFilter()); QLabel *gameNameFilterLabel = new QLabel(tr("Game &description:")); @@ -87,6 +90,7 @@ DlgFilterGames::DlgFilterGames(const QMap &_allGameTypes, restrictionsLayout->addWidget(unavailableGamesVisibleCheckBox, 0, 0); restrictionsLayout->addWidget(showPasswordProtectedGames, 1, 0); restrictionsLayout->addWidget(showBuddiesOnlyGames, 2, 0); + restrictionsLayout->addWidget(hideIgnoredUserGames, 3, 0); QGroupBox *restrictionsGroupBox = new QGroupBox(tr("Restrictions")); restrictionsGroupBox->setLayout(restrictionsLayout); @@ -154,6 +158,16 @@ void DlgFilterGames::setShowPasswordProtectedGames(bool _passwordProtectedGamesH showPasswordProtectedGames->setChecked(_passwordProtectedGamesHidden); } +bool DlgFilterGames::getHideIgnoredUserGames() const +{ + return hideIgnoredUserGames->isChecked(); +} + +void DlgFilterGames::setHideIgnoredUserGames(bool _hideIgnoredUserGames) +{ + hideIgnoredUserGames->setChecked(_hideIgnoredUserGames); +} + QString DlgFilterGames::getGameNameFilter() const { return gameNameFilterEdit->text(); diff --git a/cockatrice/src/dlg_filter_games.h b/cockatrice/src/dlg_filter_games.h index f494a9ca..7e0158f0 100644 --- a/cockatrice/src/dlg_filter_games.h +++ b/cockatrice/src/dlg_filter_games.h @@ -20,6 +20,7 @@ private: QCheckBox *showBuddiesOnlyGames; QCheckBox *unavailableGamesVisibleCheckBox; QCheckBox *showPasswordProtectedGames; + QCheckBox *hideIgnoredUserGames; QLineEdit *gameNameFilterEdit; QLineEdit *creatorNameFilterEdit; QMap gameTypeFilterCheckBoxes; @@ -43,6 +44,8 @@ public: void setShowPasswordProtectedGames(bool _passwordProtectedGamesHidden); bool getShowBuddiesOnlyGames() const; void setShowBuddiesOnlyGames(bool _showBuddiesOnlyGames); + bool getHideIgnoredUserGames() const; + void setHideIgnoredUserGames(bool _hideIgnoredUserGames); QString getGameNameFilter() const; void setGameNameFilter(const QString &_gameNameFilter); QString getCreatorNameFilter() const; diff --git a/cockatrice/src/gameselector.cpp b/cockatrice/src/gameselector.cpp index 1777830a..48c8e0b9 100644 --- a/cockatrice/src/gameselector.cpp +++ b/cockatrice/src/gameselector.cpp @@ -1,4 +1,5 @@ #include "gameselector.h" +#include "abstractclient.h" #include "dlg_creategame.h" #include "dlg_filter_games.h" #include "gamesmodel.h" @@ -8,6 +9,7 @@ #include "pending_command.h" #include "tab_room.h" #include "tab_supervisor.h" +#include "tab_userlists.h" #include #include #include @@ -32,7 +34,7 @@ GameSelector::GameSelector(AbstractClient *_client, gameListView = new QTreeView; gameListModel = new GamesModel(_rooms, _gameTypes, this); if (showfilters) { - gameListProxyModel = new GamesProxyModel(this, tabSupervisor->isOwnUserRegistered()); + gameListProxyModel = new GamesProxyModel(this, tabSupervisor); gameListProxyModel->setSourceModel(gameListModel); gameListProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); gameListView->setModel(gameListProxyModel); @@ -107,6 +109,32 @@ GameSelector::GameSelector(AbstractClient *_client, connect(gameListView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(actSelectedGameChanged(const QModelIndex &, const QModelIndex &))); connect(gameListView, SIGNAL(activated(const QModelIndex &)), this, SLOT(actJoin())); + + connect(client, SIGNAL(ignoreListReceived(const QList &)), this, + SLOT(ignoreListReceived(const QList &))); + connect(client, SIGNAL(addToListEventReceived(const Event_AddToList &)), this, + SLOT(processAddToListEvent(const Event_AddToList &))); + connect(client, SIGNAL(removeFromListEventReceived(const Event_RemoveFromList &)), this, + SLOT(processRemoveFromListEvent(const Event_RemoveFromList &))); +} + +void GameSelector::ignoreListReceived(const QList &) +{ + gameListProxyModel->refresh(); +} + +void GameSelector::processAddToListEvent(const Event_AddToList &event) +{ + if (event.list_name() == "ignore") { + gameListProxyModel->refresh(); + } +} + +void GameSelector::processRemoveFromListEvent(const Event_RemoveFromList &event) +{ + if (event.list_name() == "ignore") { + gameListProxyModel->refresh(); + } } void GameSelector::actSetFilter() @@ -121,6 +149,7 @@ void GameSelector::actSetFilter() gameListProxyModel->setShowBuddiesOnlyGames(dlg.getShowBuddiesOnlyGames()); gameListProxyModel->setUnavailableGamesVisible(dlg.getUnavailableGamesVisible()); gameListProxyModel->setShowPasswordProtectedGames(dlg.getShowPasswordProtectedGames()); + gameListProxyModel->setHideIgnoredUserGames(dlg.getHideIgnoredUserGames()); gameListProxyModel->setGameNameFilter(dlg.getGameNameFilter()); gameListProxyModel->setCreatorNameFilter(dlg.getCreatorNameFilter()); gameListProxyModel->setGameTypeFilter(dlg.getGameTypeFilter()); diff --git a/cockatrice/src/gameselector.h b/cockatrice/src/gameselector.h index beb92763..e723f139 100644 --- a/cockatrice/src/gameselector.h +++ b/cockatrice/src/gameselector.h @@ -3,6 +3,8 @@ #include "gametypemap.h" #include +#include +#include class QTreeView; class GamesModel; @@ -25,6 +27,10 @@ private slots: void actJoin(); void actSelectedGameChanged(const QModelIndex ¤t, const QModelIndex &previous); void checkResponse(const Response &response); + + void ignoreListReceived(const QList &_ignoreList); + void processAddToListEvent(const Event_AddToList &event); + void processRemoveFromListEvent(const Event_RemoveFromList &event); signals: void gameJoined(int gameId); diff --git a/cockatrice/src/gamesmodel.cpp b/cockatrice/src/gamesmodel.cpp index f5fea49c..6033df94 100644 --- a/cockatrice/src/gamesmodel.cpp +++ b/cockatrice/src/gamesmodel.cpp @@ -2,6 +2,8 @@ #include "pb/serverinfo_game.pb.h" #include "pixmapgenerator.h" #include "settingscache.h" +#include "tab_userlists.h" +#include "userlist.h" #include #include @@ -253,8 +255,9 @@ void GamesModel::updateGameList(const ServerInfo_Game &game) endInsertRows(); } -GamesProxyModel::GamesProxyModel(QObject *parent, bool _ownUserIsRegistered) - : QSortFilterProxyModel(parent), ownUserIsRegistered(_ownUserIsRegistered), showBuddiesOnlyGames(false), +GamesProxyModel::GamesProxyModel(QObject *parent, const TabSupervisor *_tabSupervisor) + : QSortFilterProxyModel(parent), ownUserIsRegistered(_tabSupervisor->isOwnUserRegistered()), + tabSupervisor(_tabSupervisor), showBuddiesOnlyGames(false), hideIgnoredUserGames(false), unavailableGamesVisible(false), showPasswordProtectedGames(true), maxPlayersFilterMin(-1), maxPlayersFilterMax(-1) { setSortRole(GamesModel::SORT_ROLE); @@ -267,6 +270,12 @@ void GamesProxyModel::setShowBuddiesOnlyGames(bool _showBuddiesOnlyGames) invalidateFilter(); } +void GamesProxyModel::setHideIgnoredUserGames(bool _hideIgnoredUserGames) +{ + hideIgnoredUserGames = _hideIgnoredUserGames; + invalidateFilter(); +} + void GamesProxyModel::setUnavailableGamesVisible(bool _unavailableGamesVisible) { unavailableGamesVisible = _unavailableGamesVisible; @@ -323,6 +332,7 @@ void GamesProxyModel::loadFilterParameters(const QMap &allGameType unavailableGamesVisible = settingsCache->gameFilters().isUnavailableGamesVisible(); showPasswordProtectedGames = settingsCache->gameFilters().isShowPasswordProtectedGames(); + hideIgnoredUserGames = settingsCache->gameFilters().isHideIgnoredUserGames(); gameNameFilter = settingsCache->gameFilters().getGameNameFilter(); maxPlayersFilterMin = settingsCache->gameFilters().getMinPlayers(); maxPlayersFilterMax = settingsCache->gameFilters().getMaxPlayers(); @@ -343,6 +353,7 @@ void GamesProxyModel::saveFilterParameters(const QMap &allGameType settingsCache->gameFilters().setShowBuddiesOnlyGames(showBuddiesOnlyGames); settingsCache->gameFilters().setUnavailableGamesVisible(unavailableGamesVisible); settingsCache->gameFilters().setShowPasswordProtectedGames(showPasswordProtectedGames); + settingsCache->gameFilters().setHideIgnoredUserGames(hideIgnoredUserGames); settingsCache->gameFilters().setGameNameFilter(gameNameFilter); QMapIterator gameTypeIterator(allGameTypes); @@ -367,6 +378,10 @@ bool GamesProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & /*sour if (!showBuddiesOnlyGames && game.only_buddies()) { return false; } + if (hideIgnoredUserGames && tabSupervisor->getUserListsTab()->getIgnoreList()->getUsers().contains( + QString::fromStdString(game.creator_info().name()))) { + return false; + } if (!unavailableGamesVisible) { if (game.player_count() == game.max_players()) return false; @@ -398,3 +413,8 @@ bool GamesProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & /*sour return true; } + +void GamesProxyModel::refresh() +{ + invalidateFilter(); +} diff --git a/cockatrice/src/gamesmodel.h b/cockatrice/src/gamesmodel.h index b5dcb331..b2c54eb4 100644 --- a/cockatrice/src/gamesmodel.h +++ b/cockatrice/src/gamesmodel.h @@ -3,6 +3,7 @@ #include "gametypemap.h" #include "pb/serverinfo_game.pb.h" +#include "tab_supervisor.h" #include #include #include @@ -65,7 +66,9 @@ class GamesProxyModel : public QSortFilterProxyModel Q_OBJECT private: bool ownUserIsRegistered; + const TabSupervisor *tabSupervisor; bool showBuddiesOnlyGames; + bool hideIgnoredUserGames; bool unavailableGamesVisible; bool showPasswordProtectedGames; QString gameNameFilter, creatorNameFilter; @@ -75,13 +78,18 @@ private: static const int DEFAULT_MAX_PLAYERS_MAX = 99; public: - GamesProxyModel(QObject *parent = nullptr, bool _ownUserIsRegistered = false); + GamesProxyModel(QObject *parent = nullptr, const TabSupervisor *_tabSupervisor = nullptr); bool getShowBuddiesOnlyGames() const { return showBuddiesOnlyGames; } void setShowBuddiesOnlyGames(bool _showBuddiesOnlyGames); + bool getHideIgnoredUserGames() const + { + return hideIgnoredUserGames; + } + void setHideIgnoredUserGames(bool _hideIgnoredUserGames); bool getUnavailableGamesVisible() const { return unavailableGamesVisible; @@ -119,6 +127,7 @@ public: void resetFilterParameters(); void loadFilterParameters(const QMap &allGameTypes); void saveFilterParameters(const QMap &allGameTypes); + void refresh(); protected: bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; diff --git a/cockatrice/src/settings/gamefilterssettings.cpp b/cockatrice/src/settings/gamefilterssettings.cpp index 33a035ab..20d8cc02 100644 --- a/cockatrice/src/settings/gamefilterssettings.cpp +++ b/cockatrice/src/settings/gamefilterssettings.cpp @@ -48,6 +48,17 @@ bool GameFiltersSettings::isShowPasswordProtectedGames() return previous == QVariant() ? true : previous.toBool(); } +void GameFiltersSettings::setHideIgnoredUserGames(bool hide) +{ + setValue(hide, "hide_ignored_user_games", "filter_games"); +} + +bool GameFiltersSettings::isHideIgnoredUserGames() +{ + QVariant previous = getValue("hide_ignored_user_games", "filter_games"); + return previous == QVariant() ? false : previous.toBool(); +} + void GameFiltersSettings::setGameNameFilter(QString gameName) { setValue(gameName, "game_name_filter", "filter_games"); diff --git a/cockatrice/src/settings/gamefilterssettings.h b/cockatrice/src/settings/gamefilterssettings.h index 1e5373ba..d4d52a0a 100644 --- a/cockatrice/src/settings/gamefilterssettings.h +++ b/cockatrice/src/settings/gamefilterssettings.h @@ -12,12 +12,14 @@ public: bool isShowBuddiesOnlyGames(); bool isUnavailableGamesVisible(); bool isShowPasswordProtectedGames(); + bool isHideIgnoredUserGames(); QString getGameNameFilter(); int getMinPlayers(); int getMaxPlayers(); bool isGameTypeEnabled(QString gametype); void setShowBuddiesOnlyGames(bool show); + void setHideIgnoredUserGames(bool hide); void setUnavailableGamesVisible(bool enabled); void setShowPasswordProtectedGames(bool show); void setGameNameFilter(QString gameName); diff --git a/cockatrice/src/settingscache.cpp b/cockatrice/src/settingscache.cpp index b9852b59..1ef51df4 100644 --- a/cockatrice/src/settingscache.cpp +++ b/cockatrice/src/settingscache.cpp @@ -95,6 +95,7 @@ void SettingsCache::translateLegacySettings() gameFilters().setShowPasswordProtectedGames(legacySetting.value("show_password_protected_games").toBool()); gameFilters().setGameNameFilter(legacySetting.value("game_name_filter").toString()); gameFilters().setShowBuddiesOnlyGames(legacySetting.value("show_buddies_only_games").toBool()); + gameFilters().setHideIgnoredUserGames(legacySetting.value("hide_ignored_user_games").toBool()); gameFilters().setMinPlayers(legacySetting.value("min_players").toInt()); if (legacySetting.value("max_players").toInt() > 1)