From 27fa6eb642c076a6d299e0e1076e0cefc24a2fff Mon Sep 17 00:00:00 2001 From: Zach H Date: Thu, 18 Jun 2015 18:30:20 -0400 Subject: [PATCH] Fix False Notifications --- cockatrice/src/chatview.cpp | 179 +++++++++++++++++++++++++----------- cockatrice/src/chatview.h | 1 + 2 files changed, 127 insertions(+), 53 deletions(-) diff --git a/cockatrice/src/chatview.cpp b/cockatrice/src/chatview.cpp index d2861324..342dfe9d 100644 --- a/cockatrice/src/chatview.cpp +++ b/cockatrice/src/chatview.cpp @@ -165,6 +165,7 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use bool mentionEnabled = settingsCache->getChatMention(); const QRegExp urlStarter = QRegExp("https?://|\\bwww\\."); const QRegExp phraseEnder = QRegExp("\\s"); + const QRegExp notALetterOrNumber = QRegExp("[^a-zA-Z0-9]"); while (message.size()) { @@ -172,47 +173,72 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use bracketFirstIndex = message.indexOf('['); mentionFirstIndex = mentionEnabled ? message.indexOf('@') : -1; urlFirstIndex = message.indexOf(urlStarter); - if(bracketFirstIndex == -1) { - if(mentionFirstIndex == -1) { - if (urlFirstIndex == -1) { - // quick way out + + bool startsWithBracket = (bracketFirstIndex != -1); + bool startsWithAtSymbol = (mentionFirstIndex != -1); + bool startsWithUrl = (urlFirstIndex != -1); + + if (!startsWithBracket) + { + if (!startsWithAtSymbol) + { + if (!startsWithUrl) + { + // No brackets, mentions, or urls. Send message as normal cursor.insertText(message); break; - } else { - // url + } + else + { + // There's a URL, lets begin! index = urlFirstIndex; } - } else { - if (urlFirstIndex == -1) { - // mention + } + else + { + if (!startsWithUrl) + { + // There's an @ symbol, lets begin! index = mentionFirstIndex; - } else { + } + else + { + // There's both an @ symbol and URL, pick the first one... lets begin! index = std::min(urlFirstIndex, mentionFirstIndex); } } - } else { - if(mentionFirstIndex == -1) { - // bracket + } + else + { + if (!startsWithAtSymbol) + { + // There's a [, look down! index = bracketFirstIndex; - } else { - // both, pick up the first one + } + else + { + // There's both a [ and @, pick the first one... look down! index = std::min(bracketFirstIndex, mentionFirstIndex); } - if(urlFirstIndex != -1) { + + if (startsWithUrl) + { + // If there's a URL, pick the first one... then lets begin! + // Otherwise, just "lets begin!" index = std::min(index, urlFirstIndex); } } - // insert the message text up to the [ / @ / https:// - if(index > 0) + if (index > 0) { cursor.insertText(message.left(index), defaultFormat); - message = message.mid(index); + message = message.mid(index); } - if(index == bracketFirstIndex) + if (index == bracketFirstIndex) // The message now starts with a bracket ->> [ <<- that symbol { - if (message.startsWith("[card]")) { + if (message.startsWith("[card]")) + { message = message.mid(6); int closeTagIndex = message.indexOf("[/card]"); QString cardName = message.left(closeTagIndex); @@ -222,7 +248,9 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use message = message.mid(closeTagIndex + 7); appendCardTag(cursor, cardName); - } else if (message.startsWith("[[")) { + } + else if (message.startsWith("[[")) + { message = message.mid(2); int closeTagIndex = message.indexOf("]]"); QString cardName = message.left(closeTagIndex); @@ -232,7 +260,9 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use message = message.mid(closeTagIndex + 2); appendCardTag(cursor, cardName); - } else if (message.startsWith("[url]")) { + } + else if (message.startsWith("[url]")) + { message = message.mid(5); int closeTagIndex = message.indexOf("[/url]"); QString url = message.left(closeTagIndex); @@ -242,12 +272,16 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use message = message.mid(closeTagIndex + 6); appendUrlTag(cursor, url); - } else { - // not a recognized [tag] + } + else + { + // Not a valid tag cursor.insertText("[", defaultFormat); message = message.mid(1); } - } else if (index == urlFirstIndex) { + } + else if (index == urlFirstIndex) // The message now starts with either: www. , http:// , or https:// + { int urlEndIndex = message.indexOf(phraseEnder, 0); if (urlEndIndex == -1) urlEndIndex = message.size(); @@ -261,35 +295,62 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use message.clear(); else message = message.mid(urlEndIndex); - } else { - if (message.startsWith(mention, Qt::CaseInsensitive)) { - // you have been mentioned - mentionFormat.setBackground(QBrush(getCustomMentionColor())); - 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); + } + else if (index == mentionFirstIndex) + { + QMap userList = tabSupervisor->getUserListsTab()->getAllUsersList()->getUsers(); + + int firstSpace = message.indexOf(" "); + QString fullMentionUpToSpaceOrEnd = (firstSpace == -1) ? message.mid(1) : message.mid(1, firstSpace - 1); + QString mentionIntact = fullMentionUpToSpaceOrEnd; + + while (fullMentionUpToSpaceOrEnd.size()) + { + if (isFullMentionAValidUser(userList, fullMentionUpToSpaceOrEnd)) // Is there a user online named this? + { + if (userName.toLower() == fullMentionUpToSpaceOrEnd.toLower()) // Is this user you? + { + // You have received a valid mention!! + mentionFormat.setBackground(QBrush(getCustomMentionColor())); + 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); + } + } + else + { + QString correctUserName = getNameFromUserList(userList, fullMentionUpToSpaceOrEnd); + UserListTWI *vlu = userList.value(correctUserName); + mentionFormatOtherUser.setAnchorHref("user://" + QString::number(vlu->getUserInfo().user_level()) + "_" + correctUserName); + cursor.insertText("@" + correctUserName, mentionFormatOtherUser); + + message = message.mid(correctUserName.size() + 1); + } + + cursor.setCharFormat(defaultFormat); + break; + } + else if (fullMentionUpToSpaceOrEnd.right(1).indexOf(notALetterOrNumber) == -1) + { + cursor.insertText("@" + mentionIntact, defaultFormat); + message = message.mid(mentionIntact.size() + 1); + cursor.setCharFormat(defaultFormat); + break; + } + else + { + fullMentionUpToSpaceOrEnd = fullMentionUpToSpaceOrEnd.left(fullMentionUpToSpaceOrEnd.size() - 1); } - } else { - int mentionEndIndex = message.indexOf(phraseEnder, 1);// from 1 as @ is non-char - if (mentionEndIndex == -1) - mentionEndIndex = message.size(); // there is no text after the mention - QString userMention = message.left(mentionEndIndex); - QString userName = userMention.right(userMention.size()-1).normalized(QString::NormalizationForm_D); - QMap userList = tabSupervisor->getUserListsTab()->getAllUsersList()->getUsers(); - QString correctUserName = getNameFromUserList(userList, userName); - if (!correctUserName.isEmpty()) { - UserListTWI *vlu = userList.value(correctUserName); - mentionFormatOtherUser.setAnchorHref("user://" + QString::number(vlu->getUserInfo().user_level()) + "_" + correctUserName); - cursor.insertText("@" + correctUserName, mentionFormatOtherUser); - } else - cursor.insertText("@" + userName, defaultFormat); - message = message.mid(userName.size() + 1); } - cursor.setCharFormat(defaultFormat); // reset format after each iteration + } + else + { + message = message.mid(1); // Not certain when this would ever be reached, but just incase } } @@ -330,6 +391,18 @@ QString ChatView::getNameFromUserList(QMap &userList, QS 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.h index b7437f45..578da3b8 100644 --- a/cockatrice/src/chatview.h +++ b/cockatrice/src/chatview.h @@ -39,6 +39,7 @@ private: 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(); bool shouldShowSystemPopup(); void showSystemPopup(QString &sender);