From d65a444ac55eab27a413bdc5ed6fc295b90de580 Mon Sep 17 00:00:00 2001 From: Gavin Bisesi Date: Fri, 17 Mar 2017 23:13:35 -0400 Subject: [PATCH 1/5] Remove direct usages of TabSupervisor from Chatview - There still might be inherited usages - It's still used in the ctor Areas to test - Mentions - Chat notifications - Username clickable links --- cockatrice/CMakeLists.txt | 3 +- cockatrice/src/{ => chatview}/chatview.cpp | 103 ++++++--------------- cockatrice/src/{ => chatview}/chatview.h | 11 +-- cockatrice/src/chatview/userlistProxy.h | 17 ++++ cockatrice/src/messagelogwidget.cpp | 4 +- cockatrice/src/messagelogwidget.h | 4 +- cockatrice/src/tab_game.cpp | 2 +- cockatrice/src/tab_message.cpp | 6 +- cockatrice/src/tab_room.cpp | 16 ++-- cockatrice/src/tab_supervisor.cpp | 32 ++++++- cockatrice/src/tab_supervisor.h | 6 +- 11 files changed, 104 insertions(+), 100 deletions(-) rename cockatrice/src/{ => chatview}/chatview.cpp (83%) rename cockatrice/src/{ => chatview}/chatview.h (87%) create mode 100644 cockatrice/src/chatview/userlistProxy.h diff --git a/cockatrice/CMakeLists.txt b/cockatrice/CMakeLists.txt index 567ed668..186687e0 100644 --- a/cockatrice/CMakeLists.txt +++ b/cockatrice/CMakeLists.txt @@ -83,7 +83,8 @@ SET(cockatrice_SOURCES src/replay_timeline_widget.cpp src/deckstats_interface.cpp src/tappedout_interface.cpp - src/chatview.cpp + src/chatview/chatview.cpp + src/chatview/userlistProxy.h src/userlist.cpp src/userinfobox.cpp src/user_context_menu.cpp diff --git a/cockatrice/src/chatview.cpp b/cockatrice/src/chatview/chatview.cpp similarity index 83% rename from cockatrice/src/chatview.cpp rename to cockatrice/src/chatview/chatview.cpp index 8be83a63..f58be1a8 100644 --- a/cockatrice/src/chatview.cpp +++ b/cockatrice/src/chatview/chatview.cpp @@ -4,32 +4,27 @@ #include #include #include -#include #include "chatview.h" #include "user_level.h" -#include "user_context_menu.h" -#include "pixmapgenerator.h" -#include "settingscache.h" -#include "tab_userlists.h" -#include "soundengine.h" -#include "room_message_type.h" +#include "../user_context_menu.h" +#include "../pixmapgenerator.h" +#include "../settingscache.h" +#include "../tab_userlists.h" +#include "../soundengine.h" const QColor DEFAULT_MENTION_COLOR = QColor(194, 31, 47); const QColor OTHER_USER_COLOR = QColor(0, 65, 255); // dark blue const QString SERVER_MESSAGE_COLOR = "#851515"; -ChatView::ChatView(const TabSupervisor *_tabSupervisor, TabGame *_game, bool _showTimestamps, QWidget *parent) - : QTextBrowser(parent), tabSupervisor(_tabSupervisor), game(_game), evenNumber(true), showTimestamps(_showTimestamps), hoveredItemType(HoveredNothing) +ChatView::ChatView(const TabSupervisor *_tabSupervisor, const UserlistProxy *_userlistProxy, TabGame *_game, bool _showTimestamps, QWidget *parent) + : QTextBrowser(parent), tabSupervisor(_tabSupervisor), userlistProxy(_userlistProxy), game(_game), evenNumber(true), showTimestamps(_showTimestamps), hoveredItemType(HoveredNothing) { document()->setDefaultStyleSheet("a { text-decoration: none; color: blue; }"); userContextMenu = new UserContextMenu(tabSupervisor, this, game); connect(userContextMenu, SIGNAL(openMessageDialog(QString, bool)), this, SIGNAL(openMessageDialog(QString, bool))); - - if(tabSupervisor->getUserInfo()) - { - userName = QString::fromStdString(tabSupervisor->getUserInfo()->name()); - mention = "@" + userName; - } + + userName = userlistProxy->getOwnUsername(); + mention = "@" + userName; mentionFormat.setFontWeight(QFont::Bold); @@ -142,8 +137,7 @@ void ChatView::appendMessage(QString message, RoomMessageTypeFlags messageType, // nickname if (sender.toLower() != "servatrice") { QTextCharFormat senderFormat; - if (tabSupervisor && tabSupervisor->getUserInfo() && - (sender == QString::fromStdString(tabSupervisor->getUserInfo()->name()))) { + if (sender == userName) { senderFormat.setForeground(QBrush(getCustomMentionColor())); senderFormat.setFontWeight(QFont::Bold); } else { @@ -156,10 +150,10 @@ void ChatView::appendMessage(QString message, RoomMessageTypeFlags messageType, if (sameSender) { cursor.insertText(" "); } else { - if (!sender.isEmpty() && tabSupervisor->getUserListsTab()) { + if (!sender.isEmpty()) { const int pixelSize = QFontInfo(cursor.charFormat().font()).pixelSize(); - QMap buddyList = tabSupervisor->getUserListsTab()->getBuddyList()->getUsers(); - cursor.insertImage(UserLevelPixmapGenerator::generatePixmap(pixelSize, userLevel, buddyList.contains(sender), UserPrivLevel).toImage()); + bool isBuddy = userlistProxy->isUserBuddy(sender); + cursor.insertImage(UserLevelPixmapGenerator::generatePixmap(pixelSize, userLevel, isBuddy, UserPrivLevel).toImage()); cursor.insertText(" "); } cursor.setCharFormat(senderFormat); @@ -231,6 +225,7 @@ void ChatView::appendMessage(QString message, RoomMessageTypeFlags messageType, verticalScrollBar()->setValue(verticalScrollBar()->maximum()); } + void ChatView::checkTag(QTextCursor &cursor, QString &message) { if (message.startsWith("[card]")) @@ -287,11 +282,10 @@ void ChatView::checkMention(QTextCursor &cursor, QString &message, QString &send QString fullMentionUpToSpaceOrEnd = (firstSpace == -1) ? message.mid(1) : message.mid(1, firstSpace - 1); QString mentionIntact = fullMentionUpToSpaceOrEnd; - QMap userList = tabSupervisor->getUserListsTab()->getAllUsersList()->getUsers(); - while (fullMentionUpToSpaceOrEnd.size()) { - if (isFullMentionAValidUser(userList, fullMentionUpToSpaceOrEnd)) // Is there a user online named this? + const ServerInfo_User *onlineUser = userlistProxy->getOnlineUser(fullMentionUpToSpaceOrEnd); + if (onlineUser) // Is there a user online named this? { if (userName.toLower() == fullMentionUpToSpaceOrEnd.toLower()) // Is this user you? { @@ -301,16 +295,10 @@ void ChatView::checkMention(QTextCursor &cursor, QString &message, QString &send mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); cursor.insertText(mention, mentionFormat); message = message.mid(mention.size()); - QApplication::alert(this); - if (settingsCache->getShowMentionPopup() && shouldShowSystemPopup()) - { - QString ref = sender.left(sender.length() - 2); - showSystemPopup(ref); - } + showSystemPopup(sender); } else { - QString correctUserName = getNameFromUserList(userList, fullMentionUpToSpaceOrEnd); - UserListTWI *vlu = userList.value(correctUserName); - mentionFormatOtherUser.setAnchorHref("user://" + QString::number(vlu->getUserInfo().user_level()) + "_" + correctUserName); + QString correctUserName = QString::fromStdString(onlineUser->name()); + mentionFormatOtherUser.setAnchorHref("user://" + QString::number(onlineUser->user_level()) + "_" + correctUserName); cursor.insertText("@" + correctUserName, mentionFormatOtherUser); message = message.mid(correctUserName.size() + 1); @@ -327,12 +315,7 @@ void ChatView::checkMention(QTextCursor &cursor, QString &message, QString &send mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); cursor.insertText("@" + fullMentionUpToSpaceOrEnd, mentionFormat); message = message.mid(fullMentionUpToSpaceOrEnd.size() + 1); - QApplication::alert(this); - if (settingsCache->getShowMentionPopup() && shouldShowSystemPopup()) - { - QString ref = sender.left(sender.length() - 2); - showSystemPopup(ref); - } + showSystemPopup(sender); cursor.setCharFormat(defaultFormat); return; @@ -406,7 +389,7 @@ QString ChatView::extractNextWord(QString &message, QString &rest) message = message.mid(firstSpace); } - // remove any punctution from the end and pass it separately + // remove any punctuation from the end and pass it separately for (int len = word.size() - 1; len >= 0; --len) { if(word.at(len).isLetterOrNumber()) @@ -428,22 +411,22 @@ bool ChatView::isModeratorSendingGlobal(QFlags u QStringList getAttentionList; getAttentionList << "/all"; // Send a message to all users - if (getAttentionList.contains(message) && (userLevel & ServerInfo_User::IsModerator || userLevel & ServerInfo_User::IsAdmin)) - return true; + return (getAttentionList.contains(message) + && (userLevel & ServerInfo_User::IsModerator + || userLevel & ServerInfo_User::IsAdmin)); - return false; } void ChatView::actMessageClicked() { emit messageClickedSignal(); } -bool ChatView::shouldShowSystemPopup() { - return QApplication::activeWindow() == 0 || QApplication::focusWidget() == 0 ||tabSupervisor->currentIndex() != tabSupervisor->indexOf(this); -} - void ChatView::showSystemPopup(QString &sender) { - emit showMentionPopup(sender); + QApplication::alert(this); + if (settingsCache->getShowMentionPopup()) { + QString ref = sender.left(sender.length() - 2); + emit showMentionPopup(ref); + } } QColor ChatView::getCustomMentionColor() { @@ -458,32 +441,6 @@ QColor ChatView::getCustomHighlightColor() { return customColor.isValid() ? customColor : DEFAULT_MENTION_COLOR; } -/** - Returns the correct case version of the provided username, if no correct casing version - was found then the provided name is not available and will return an empty QString. - */ -QString ChatView::getNameFromUserList(QMap &userList, QString &userName) { - QMap::iterator i; - QString lowerUserName = userName.toLower(); - for (i = userList.begin(); i != userList.end(); ++i) { - if (i.key().toLower() == lowerUserName) - return i.key(); - } - return QString(); -} - -bool ChatView::isFullMentionAValidUser(QMap &userList, QString userNameToMatch) -{ - QString userNameToMatchLower = userNameToMatch.toLower(); - QMap::iterator i; - - for (i = userList.begin(); i != userList.end(); ++i) - if (i.key().toLower() == userNameToMatchLower) - return true; - - return false; -} - void ChatView::clearChat() { document()->clear(); lastSender = ""; diff --git a/cockatrice/src/chatview.h b/cockatrice/src/chatview/chatview.h similarity index 87% rename from cockatrice/src/chatview.h rename to cockatrice/src/chatview/chatview.h index cea9859b..dff82f3c 100644 --- a/cockatrice/src/chatview.h +++ b/cockatrice/src/chatview/chatview.h @@ -6,10 +6,11 @@ #include #include #include -#include "userlist.h" +#include "../userlist.h" #include "user_level.h" #include "room_message_type.h" -#include "tab_supervisor.h" +#include "../tab_supervisor.h" +#include "userlistProxy.h" class QTextTable; class QMouseEvent; @@ -23,6 +24,7 @@ protected: TabGame * const game; private: enum HoveredItemType { HoveredNothing, HoveredUrl, HoveredCard, HoveredUser }; + const UserlistProxy * const userlistProxy; UserContextMenu *userContextMenu; QString lastSender; QString userName; @@ -41,11 +43,8 @@ private: QTextCursor prepareBlock(bool same = false); void appendCardTag(QTextCursor &cursor, const QString &cardName); void appendUrlTag(QTextCursor &cursor, QString url); - QString getNameFromUserList(QMap &userList, QString &userName); - bool isFullMentionAValidUser(QMap &userList, QString userNameToMatch); QColor getCustomMentionColor(); QColor getCustomHighlightColor(); - bool shouldShowSystemPopup(); void showSystemPopup(QString &sender); bool isModeratorSendingGlobal(QFlags userLevelFlag, QString message); void checkTag(QTextCursor &cursor, QString &message); @@ -56,7 +55,7 @@ private slots: void openLink(const QUrl &link); void actMessageClicked(); public: - ChatView(const TabSupervisor *_tabSupervisor, TabGame *_game, bool _showTimestamps, QWidget *parent = 0); + ChatView(const TabSupervisor *_tabSupervisor, const UserlistProxy *_userlistProxy, TabGame *_game, bool _showTimestamps, QWidget *parent = 0); void retranslateUi(); void appendHtml(const QString &html); void appendHtmlServerMessage(const QString &html, bool optionalIsBold = false, QString optionalFontColor = QString()); diff --git a/cockatrice/src/chatview/userlistProxy.h b/cockatrice/src/chatview/userlistProxy.h new file mode 100644 index 00000000..7f917136 --- /dev/null +++ b/cockatrice/src/chatview/userlistProxy.h @@ -0,0 +1,17 @@ +#ifndef COCKATRICE_USERLISTPROXY_H +#define COCKATRICE_USERLISTPROXY_H + +class ServerInfo_User; + +/** + * Responsible for providing a bare-bones minimal interface into userlist information, + * including your current connection to the server as well as buddy/ignore/alluser lists. + */ +class UserlistProxy { +public: + virtual const QString getOwnUsername() const = 0; + virtual bool isUserBuddy(const QString &userName) const = 0; + virtual const ServerInfo_User* getOnlineUser(const QString &userName) const = 0; // Can return nullptr +}; + +#endif //COCKATRICE_USERLISTPROXY_H diff --git a/cockatrice/src/messagelogwidget.cpp b/cockatrice/src/messagelogwidget.cpp index 66f730b7..eaa6981a 100644 --- a/cockatrice/src/messagelogwidget.cpp +++ b/cockatrice/src/messagelogwidget.cpp @@ -636,7 +636,7 @@ void MessageLogWidget::connectToPlayer(Player *player) connect(player, SIGNAL(logAlwaysRevealTopCard(Player *, CardZone *, bool)), this, SLOT(logAlwaysRevealTopCard(Player *, CardZone *, bool))); } -MessageLogWidget::MessageLogWidget(const TabSupervisor *_tabSupervisor, TabGame *_game, QWidget *parent) - : ChatView(_tabSupervisor, _game, true, parent), currentContext(MessageContext_None) +MessageLogWidget::MessageLogWidget(const TabSupervisor *_tabSupervisor, const UserlistProxy *_userlistProxy, TabGame *_game, QWidget *parent) + : ChatView(_tabSupervisor, _userlistProxy, _game, true, parent), currentContext(MessageContext_None) { } diff --git a/cockatrice/src/messagelogwidget.h b/cockatrice/src/messagelogwidget.h index 72803e3b..da375928 100644 --- a/cockatrice/src/messagelogwidget.h +++ b/cockatrice/src/messagelogwidget.h @@ -1,7 +1,7 @@ #ifndef MESSAGELOGWIDGET_H #define MESSAGELOGWIDGET_H -#include "chatview.h" +#include "chatview/chatview.h" #include "translation.h" #include "user_level.h" @@ -83,7 +83,7 @@ public slots: void containerProcessingDone(); public: void connectToPlayer(Player *player); - MessageLogWidget(const TabSupervisor *_tabSupervisor, TabGame *_game, QWidget *parent = 0); + MessageLogWidget(const TabSupervisor *_tabSupervisor, const UserlistProxy *_userlistProxy, TabGame *_game, QWidget *parent = 0); }; #endif diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index 0ad2e32e..49dd70e9 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -1574,7 +1574,7 @@ void TabGame::createPlayerListDock(bool bReplay) void TabGame::createMessageDock(bool bReplay) { - messageLog = new MessageLogWidget(tabSupervisor, this); + messageLog = new MessageLogWidget(tabSupervisor, tabSupervisor, this); connect(messageLog, SIGNAL(cardNameHovered(QString)), cardInfo, SLOT(setCard(QString))); connect(messageLog, SIGNAL(showCardInfoPopup(QPoint, QString)), this, SLOT(showCardInfoPopup(QPoint, QString))); connect(messageLog, SIGNAL(deleteCardInfoPopup(QString)), this, SLOT(deleteCardInfoPopup(QString))); diff --git a/cockatrice/src/tab_message.cpp b/cockatrice/src/tab_message.cpp index 0bc15f6a..d8945479 100644 --- a/cockatrice/src/tab_message.cpp +++ b/cockatrice/src/tab_message.cpp @@ -1,14 +1,12 @@ #include #include -#include #include -#include #include #include #include #include "tab_message.h" #include "abstractclient.h" -#include "chatview.h" +#include "chatview/chatview.h" #include "main.h" #include "settingscache.h" #include "soundengine.h" @@ -21,7 +19,7 @@ TabMessage::TabMessage(TabSupervisor *_tabSupervisor, AbstractClient *_client, const ServerInfo_User &_ownUserInfo, const ServerInfo_User &_otherUserInfo) : Tab(_tabSupervisor), client(_client), ownUserInfo(new ServerInfo_User(_ownUserInfo)), otherUserInfo(new ServerInfo_User(_otherUserInfo)), userOnline(true) { - chatView = new ChatView(tabSupervisor, 0, true); + chatView = new ChatView(tabSupervisor, tabSupervisor, 0, true); connect(chatView, SIGNAL(showCardInfoPopup(QPoint, QString)), this, SLOT(showCardInfoPopup(QPoint, QString))); connect(chatView, SIGNAL(deleteCardInfoPopup(QString)), this, SLOT(deleteCardInfoPopup(QString))); connect(chatView, SIGNAL(addMentionTag(QString)), this, SLOT(addMentionTag(QString))); diff --git a/cockatrice/src/tab_room.cpp b/cockatrice/src/tab_room.cpp index a6adfb8d..781f4bbe 100644 --- a/cockatrice/src/tab_room.cpp +++ b/cockatrice/src/tab_room.cpp @@ -1,29 +1,24 @@ #include #include -#include #include -#include #include #include -#include #include #include #include #include #include #include -#include #include #include "tab_supervisor.h" #include "tab_room.h" #include "tab_userlists.h" #include "userlist.h" #include "abstractclient.h" -#include "chatview.h" +#include "chatview/chatview.h" #include "gameselector.h" #include "settingscache.h" #include "main.h" -#include "lineeditcompleter.h" #include "get_pb_extension.h" #include "pb/room_commands.pb.h" #include "pb/serverinfo_room.pb.h" @@ -48,7 +43,7 @@ TabRoom::TabRoom(TabSupervisor *_tabSupervisor, AbstractClient *_client, ServerI userList = new UserList(tabSupervisor, client, UserList::RoomList); connect(userList, SIGNAL(openMessageDialog(const QString &, bool)), this, SIGNAL(openMessageDialog(const QString &, bool))); - chatView = new ChatView(tabSupervisor, 0, true); + chatView = new ChatView(tabSupervisor, tabSupervisor, 0, true); connect(chatView, SIGNAL(showMentionPopup(QString&)), this, SLOT(actShowMentionPopup(QString&))); connect(chatView, SIGNAL(messageClickedSignal()), this, SLOT(focusTab())); connect(chatView, SIGNAL(openMessageDialog(QString, bool)), this, SIGNAL(openMessageDialog(QString, bool))); @@ -156,8 +151,11 @@ void TabRoom::focusTab() { } void TabRoom::actShowMentionPopup(QString &sender) { - if (trayIcon && (tabSupervisor->currentIndex() != tabSupervisor->indexOf(this) || QApplication::activeWindow() == 0 - || QApplication::focusWidget() == 0)) { + if (trayIcon + && (tabSupervisor->currentIndex() != tabSupervisor->indexOf(this) + || QApplication::activeWindow() == 0 + || QApplication::focusWidget() == 0) + ) { disconnect(trayIcon, SIGNAL(messageClicked()), 0, 0); trayIcon->showMessage(sender + tr(" mentioned you."), tr("Click to view")); connect(trayIcon, SIGNAL(messageClicked()), chatView, SLOT(actMessageClicked())); diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index c5ca20eb..ab49e3ba 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -604,4 +604,34 @@ void TabSupervisor::processNotifyUserEvent(const Event_NotifyUser &event) default: ; } -} \ No newline at end of file +} +const QString TabSupervisor::getOwnUsername() const +{ + return QString::fromStdString(userInfo->name()); +} + +bool TabSupervisor::isUserBuddy(const QString &userName) const +{ + if (!getUserListsTab()) return false; + if (!getUserListsTab()->getBuddyList()) return false; + QMap buddyList = getUserListsTab()->getBuddyList()->getUsers(); + bool senderIsBuddy = buddyList.contains(userName); + return senderIsBuddy; +} + +const ServerInfo_User * TabSupervisor::getOnlineUser(const QString &userName) const +{ + if (!getUserListsTab()) return nullptr; + if (!getUserListsTab()->getAllUsersList()) return nullptr; + QMap userList = getUserListsTab()->getAllUsersList()->getUsers(); + const QString &userNameToMatchLower = userName.toLower(); + QMap::iterator i; + + for (i = userList.begin(); i != userList.end(); ++i) + if (i.key().toLower() == userNameToMatchLower) { + const ServerInfo_User &userInfo = i.value()->getUserInfo(); + return &userInfo; + } + + return nullptr; +}; diff --git a/cockatrice/src/tab_supervisor.h b/cockatrice/src/tab_supervisor.h index 027ba64f..7f80aba9 100644 --- a/cockatrice/src/tab_supervisor.h +++ b/cockatrice/src/tab_supervisor.h @@ -5,6 +5,7 @@ #include #include #include "deck_loader.h" +#include "chatview/userlistProxy.h" class QMenu; class AbstractClient; @@ -41,7 +42,7 @@ protected: void paintEvent(QPaintEvent *event); }; -class TabSupervisor : public QTabWidget { +class TabSupervisor : public QTabWidget, public UserlistProxy { Q_OBJECT private: ServerInfo_User *userInfo; @@ -78,6 +79,9 @@ public: const QMap &getRoomTabs() const { return roomTabs; } bool getAdminLocked() const; bool closeRequest(); + const QString getOwnUsername() const; + bool isUserBuddy(const QString &userName) const; + const ServerInfo_User* getOnlineUser(const QString &userName) const; signals: void setMenu(const QList &newMenuList = QList()); void localGameEnded(); From c5aa75d4d1d4f40ca609788791306b462528fc92 Mon Sep 17 00:00:00 2001 From: Gavin Bisesi Date: Fri, 17 Mar 2017 23:29:42 -0400 Subject: [PATCH 2/5] Reduce data dependency for GamesProxyModel --- cockatrice/src/gameselector.cpp | 3 ++- cockatrice/src/gamesmodel.cpp | 6 +++--- cockatrice/src/gamesmodel.h | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/cockatrice/src/gameselector.cpp b/cockatrice/src/gameselector.cpp index 13989a61..b5f8ffc2 100644 --- a/cockatrice/src/gameselector.cpp +++ b/cockatrice/src/gameselector.cpp @@ -25,7 +25,8 @@ GameSelector::GameSelector(AbstractClient *_client, const TabSupervisor *_tabSup gameListModel = new GamesModel(_rooms, _gameTypes, this); if(showfilters) { - gameListProxyModel = new GamesProxyModel(this, tabSupervisor->getUserInfo()); + bool ownUserIsRegistered = (bool)(tabSupervisor->getUserInfo()->user_level() & ServerInfo_User::IsRegistered); + gameListProxyModel = new GamesProxyModel(this, ownUserIsRegistered); gameListProxyModel->setSourceModel(gameListModel); gameListProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); gameListView->setModel(gameListProxyModel); diff --git a/cockatrice/src/gamesmodel.cpp b/cockatrice/src/gamesmodel.cpp index 32dd64f0..1f3d222c 100644 --- a/cockatrice/src/gamesmodel.cpp +++ b/cockatrice/src/gamesmodel.cpp @@ -225,9 +225,9 @@ void GamesModel::updateGameList(const ServerInfo_Game &game) endInsertRows(); } -GamesProxyModel::GamesProxyModel(QObject *parent, ServerInfo_User *_ownUser) +GamesProxyModel::GamesProxyModel(QObject *parent, bool _ownUserIsRegistered) : QSortFilterProxyModel(parent), - ownUser(_ownUser), + ownUserIsRegistered(_ownUserIsRegistered), showBuddiesOnlyGames(false), unavailableGamesVisible(false), showPasswordProtectedGames(true), @@ -348,7 +348,7 @@ bool GamesProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &/*sourc return false; if (game.started()) return false; - if (!(ownUser->user_level() & ServerInfo_User::IsRegistered)) + if (!ownUserIsRegistered) if (game.only_registered()) return false; } diff --git a/cockatrice/src/gamesmodel.h b/cockatrice/src/gamesmodel.h index f84857e7..ef01e9ed 100644 --- a/cockatrice/src/gamesmodel.h +++ b/cockatrice/src/gamesmodel.h @@ -46,7 +46,7 @@ class ServerInfo_User; class GamesProxyModel : public QSortFilterProxyModel { Q_OBJECT private: - ServerInfo_User *ownUser; + bool ownUserIsRegistered; bool showBuddiesOnlyGames; bool unavailableGamesVisible; bool showPasswordProtectedGames; @@ -56,7 +56,7 @@ private: static const int DEFAULT_MAX_PLAYERS_MAX = 99; public: - GamesProxyModel(QObject *parent = 0, ServerInfo_User *_ownUser = 0); + GamesProxyModel(QObject *parent = 0, bool _ownUserIsRegistered = false); bool getShowBuddiesOnlyGames() const {return showBuddiesOnlyGames; } void setShowBuddiesOnlyGames(bool _showBuddiesOnlyGames); From a3f4012d1adf64139c0cce65ed964f0d98932aaa Mon Sep 17 00:00:00 2001 From: Gavin Bisesi Date: Fri, 17 Mar 2017 23:37:00 -0400 Subject: [PATCH 3/5] Reduce TabSupervisor direct usage in UserContextMenu --- cockatrice/src/chatview/userlistProxy.h | 2 ++ cockatrice/src/gameselector.cpp | 3 +-- cockatrice/src/tab_supervisor.cpp | 15 +++++++++++++++ cockatrice/src/tab_supervisor.h | 2 ++ cockatrice/src/user_context_menu.cpp | 10 +++++----- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/cockatrice/src/chatview/userlistProxy.h b/cockatrice/src/chatview/userlistProxy.h index 7f917136..14411982 100644 --- a/cockatrice/src/chatview/userlistProxy.h +++ b/cockatrice/src/chatview/userlistProxy.h @@ -9,8 +9,10 @@ class ServerInfo_User; */ class UserlistProxy { public: + virtual const bool isOwnUserRegistered() const = 0; virtual const QString getOwnUsername() const = 0; virtual bool isUserBuddy(const QString &userName) const = 0; + virtual bool isUserIgnored(const QString &userName) const = 0; virtual const ServerInfo_User* getOnlineUser(const QString &userName) const = 0; // Can return nullptr }; diff --git a/cockatrice/src/gameselector.cpp b/cockatrice/src/gameselector.cpp index b5f8ffc2..c73b224a 100644 --- a/cockatrice/src/gameselector.cpp +++ b/cockatrice/src/gameselector.cpp @@ -25,8 +25,7 @@ GameSelector::GameSelector(AbstractClient *_client, const TabSupervisor *_tabSup gameListModel = new GamesModel(_rooms, _gameTypes, this); if(showfilters) { - bool ownUserIsRegistered = (bool)(tabSupervisor->getUserInfo()->user_level() & ServerInfo_User::IsRegistered); - gameListProxyModel = new GamesProxyModel(this, ownUserIsRegistered); + gameListProxyModel = new GamesProxyModel(this, tabSupervisor->isOwnUserRegistered()); gameListProxyModel->setSourceModel(gameListModel); gameListProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); gameListView->setModel(gameListProxyModel); diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index ab49e3ba..63dc432e 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -605,6 +605,12 @@ void TabSupervisor::processNotifyUserEvent(const Event_NotifyUser &event) } } + +const bool TabSupervisor::isOwnUserRegistered() const +{ + return (bool) getUserInfo()->user_level() & ServerInfo_User::IsRegistered; +} + const QString TabSupervisor::getOwnUsername() const { return QString::fromStdString(userInfo->name()); @@ -619,6 +625,15 @@ bool TabSupervisor::isUserBuddy(const QString &userName) const return senderIsBuddy; } +bool TabSupervisor::isUserIgnored(const QString &userName) const +{ + if (!getUserListsTab()) return false; + if (!getUserListsTab()->getIgnoreList()) return false; + QMap buddyList = getUserListsTab()->getIgnoreList()->getUsers(); + bool senderIsBuddy = buddyList.contains(userName); + return senderIsBuddy; +} + const ServerInfo_User * TabSupervisor::getOnlineUser(const QString &userName) const { if (!getUserListsTab()) return nullptr; diff --git a/cockatrice/src/tab_supervisor.h b/cockatrice/src/tab_supervisor.h index 7f80aba9..cadc826c 100644 --- a/cockatrice/src/tab_supervisor.h +++ b/cockatrice/src/tab_supervisor.h @@ -79,8 +79,10 @@ public: const QMap &getRoomTabs() const { return roomTabs; } bool getAdminLocked() const; bool closeRequest(); + const bool isOwnUserRegistered() const; const QString getOwnUsername() const; bool isUserBuddy(const QString &userName) const; + bool isUserIgnored(const QString &userName) const; const ServerInfo_User* getOnlineUser(const QString &userName) const; signals: void setMenu(const QList &newMenuList = QList()); diff --git a/cockatrice/src/user_context_menu.cpp b/cockatrice/src/user_context_menu.cpp index aed3e59a..d839a551 100644 --- a/cockatrice/src/user_context_menu.cpp +++ b/cockatrice/src/user_context_menu.cpp @@ -245,7 +245,7 @@ void UserContextMenu::warnUser_dialogFinished() { WarningDialog *dlg = static_cast(sender()); - if (dlg->getName().isEmpty() || QString::fromStdString(tabSupervisor->getUserInfo()->name()).simplified().isEmpty()) + if (dlg->getName().isEmpty() || tabSupervisor->getOwnUsername().simplified().isEmpty()) return; Command_WarnUser cmd; @@ -267,13 +267,13 @@ void UserContextMenu::showContextMenu(const QPoint &pos, const QString &userName menu->addAction(aDetails); menu->addAction(aShowGames); menu->addAction(aChat); - if (userLevel.testFlag(ServerInfo_User::IsRegistered) && (tabSupervisor->getUserInfo()->user_level() & ServerInfo_User::IsRegistered)) { + if (userLevel.testFlag(ServerInfo_User::IsRegistered) && tabSupervisor->isOwnUserRegistered()) { menu->addSeparator(); - if (tabSupervisor->getUserListsTab()->getBuddyList()->getUsers().contains(userName)) + if (tabSupervisor->isUserBuddy(userName)) menu->addAction(aRemoveFromBuddyList); else menu->addAction(aAddToBuddyList); - if (tabSupervisor->getUserListsTab()->getIgnoreList()->getUsers().contains(userName)) + if (tabSupervisor->isUserIgnored(userName)) menu->addAction(aRemoveFromIgnoreList); else menu->addAction(aAddToIgnoreList); @@ -298,7 +298,7 @@ void UserContextMenu::showContextMenu(const QPoint &pos, const QString &userName menu->addAction(aPromoteToMod); } } - bool anotherUser = userName != QString::fromStdString(tabSupervisor->getUserInfo()->name()); + bool anotherUser = userName != tabSupervisor->getOwnUsername(); aDetails->setEnabled(true); aChat->setEnabled(anotherUser && online); aShowGames->setEnabled(online); From 8ac2a5870d91271be14ee8d780b80f2eb348e800 Mon Sep 17 00:00:00 2001 From: Gavin Bisesi Date: Wed, 22 Mar 2017 21:12:42 -0400 Subject: [PATCH 4/5] Un-const non-pointer return types --- cockatrice/src/chatview/userlistProxy.h | 4 ++-- cockatrice/src/tab_supervisor.cpp | 4 ++-- cockatrice/src/tab_supervisor.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cockatrice/src/chatview/userlistProxy.h b/cockatrice/src/chatview/userlistProxy.h index 14411982..febb7952 100644 --- a/cockatrice/src/chatview/userlistProxy.h +++ b/cockatrice/src/chatview/userlistProxy.h @@ -9,8 +9,8 @@ class ServerInfo_User; */ class UserlistProxy { public: - virtual const bool isOwnUserRegistered() const = 0; - virtual const QString getOwnUsername() const = 0; + virtual bool isOwnUserRegistered() const = 0; + virtual QString getOwnUsername() const = 0; virtual bool isUserBuddy(const QString &userName) const = 0; virtual bool isUserIgnored(const QString &userName) const = 0; virtual const ServerInfo_User* getOnlineUser(const QString &userName) const = 0; // Can return nullptr diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index 63dc432e..24bf6360 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -606,12 +606,12 @@ void TabSupervisor::processNotifyUserEvent(const Event_NotifyUser &event) } -const bool TabSupervisor::isOwnUserRegistered() const +bool TabSupervisor::isOwnUserRegistered() const { return (bool) getUserInfo()->user_level() & ServerInfo_User::IsRegistered; } -const QString TabSupervisor::getOwnUsername() const +QString TabSupervisor::getOwnUsername() const { return QString::fromStdString(userInfo->name()); } diff --git a/cockatrice/src/tab_supervisor.h b/cockatrice/src/tab_supervisor.h index cadc826c..6a7a4a15 100644 --- a/cockatrice/src/tab_supervisor.h +++ b/cockatrice/src/tab_supervisor.h @@ -79,8 +79,8 @@ public: const QMap &getRoomTabs() const { return roomTabs; } bool getAdminLocked() const; bool closeRequest(); - const bool isOwnUserRegistered() const; - const QString getOwnUsername() const; + bool isOwnUserRegistered() const; + QString getOwnUsername() const; bool isUserBuddy(const QString &userName) const; bool isUserIgnored(const QString &userName) const; const ServerInfo_User* getOnlineUser(const QString &userName) const; From f5a5ea043470b8f869df04465b908adba09b28f9 Mon Sep 17 00:00:00 2001 From: Gavin Bisesi Date: Wed, 22 Mar 2017 21:52:15 -0400 Subject: [PATCH 5/5] ChatView: Initialize fields in declared order --- cockatrice/src/chatview/chatview.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cockatrice/src/chatview/chatview.cpp b/cockatrice/src/chatview/chatview.cpp index f58be1a8..90919486 100644 --- a/cockatrice/src/chatview/chatview.cpp +++ b/cockatrice/src/chatview/chatview.cpp @@ -17,7 +17,7 @@ const QColor OTHER_USER_COLOR = QColor(0, 65, 255); // dark blue const QString SERVER_MESSAGE_COLOR = "#851515"; ChatView::ChatView(const TabSupervisor *_tabSupervisor, const UserlistProxy *_userlistProxy, TabGame *_game, bool _showTimestamps, QWidget *parent) - : QTextBrowser(parent), tabSupervisor(_tabSupervisor), userlistProxy(_userlistProxy), game(_game), evenNumber(true), showTimestamps(_showTimestamps), hoveredItemType(HoveredNothing) + : QTextBrowser(parent), tabSupervisor(_tabSupervisor), game(_game), userlistProxy(_userlistProxy), evenNumber(true), showTimestamps(_showTimestamps), hoveredItemType(HoveredNothing) { document()->setDefaultStyleSheet("a { text-decoration: none; color: blue; }"); userContextMenu = new UserContextMenu(tabSupervisor, this, game);