diff --git a/cockatrice/src/chatview.cpp b/cockatrice/src/chatview.cpp index 2b967ac46..cb84df83a 100644 --- a/cockatrice/src/chatview.cpp +++ b/cockatrice/src/chatview.cpp @@ -161,72 +161,115 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use } cursor.setCharFormat(messageFormat); - int index = -1, bracketFirstIndex = -1, mentionFirstIndex = -1, urlFirstIndex = -1; + int index = -1, bracketFirstIndex = -1, mentionFirstIndex = -1, urlFirstIndex = -1, highlightWordFirstIndex = -1; bool mentionEnabled = settingsCache->getChatMention(); const QRegExp urlStarter = QRegExp("https?://|\\bwww\\."); const QRegExp phraseEnder = QRegExp("\\s"); const QRegExp notALetterOrNumber = QRegExp("[^a-zA-Z0-9]"); + const QStringList highlightedWords = settingsCache->getHighlightWords(); while (message.size()) { - // search for the first [ or @ bracketFirstIndex = message.indexOf('['); mentionFirstIndex = message.indexOf('@'); urlFirstIndex = message.indexOf(urlStarter); + highlightWordFirstIndex = -1; + + foreach (QString word, message.simplified().split(" ")) + { + if (highlightedWords.contains(word, Qt::CaseInsensitive)) + { + highlightWordFirstIndex = message.indexOf(word); + break; + } + } bool startsWithBracket = (bracketFirstIndex != -1); bool startsWithAtSymbol = (mentionFirstIndex != -1); - bool startsWithUrl = (urlFirstIndex != -1); + bool startsWithUrl = (urlFirstIndex != -1); + bool startsWithHighlightWord = (highlightWordFirstIndex != -1); + - if (!startsWithBracket) + if (!startsWithBracket && !startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) { - if (!startsWithAtSymbol) - { - if (!startsWithUrl) - { - // No brackets, mentions, or urls. Send message as normal - cursor.insertText(message); - break; - } - else - { - // There's a URL, lets begin! - index = urlFirstIndex; - } - } - else - { - if (!startsWithUrl) - { - // There's an @ symbol, lets begin! - index = mentionFirstIndex; - } - else - { - // There's both an @ symbol and URL, pick the first one... lets begin! - index = std::min(urlFirstIndex, mentionFirstIndex); - } - } + // No functions need to be run. Send message as normal + cursor.insertText(message); + break; } - else + else if (startsWithBracket && !startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) { - if (!startsWithAtSymbol) - { - // There's a [, look down! - index = bracketFirstIndex; - } - else - { - // There's both a [ and @, pick the first one... look down! - index = std::min(bracketFirstIndex, mentionFirstIndex); - } - - if (startsWithUrl) - { - // If there's a URL, pick the first one... then lets begin! - // Otherwise, just "lets begin!" - index = std::min(index, urlFirstIndex); - } + // Contains a bracket + index = bracketFirstIndex; + } + else if (!startsWithBracket && startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) + { + // Contains an @ symbol + index = mentionFirstIndex; + } + else if (!startsWithBracket && !startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) + { + // Contains URL stuff (http or www.) + index = urlFirstIndex; + } + else if (!startsWithBracket && !startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) + { + // Contains a word the user wants highlighted + index = highlightWordFirstIndex; + } + else if (startsWithBracket && startsWithAtSymbol && !startsWithUrl && !startsWithHighlightWord) + { + // Contains both a bracket and an @ symbol + index = std::min(bracketFirstIndex, mentionFirstIndex); + } + else if (startsWithBracket && !startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) + { + // Contains both a bracket and URL stuff + index = std::min(bracketFirstIndex, urlFirstIndex); + } + else if (startsWithBracket && !startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) + { + // Contains both a bracket and a word the user wants highlighted + index = std::min(bracketFirstIndex, highlightWordFirstIndex); + } + else if (!startsWithBracket && startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) + { + // Contains both an @ symbol and URL stuff + index = std::min(mentionFirstIndex, urlFirstIndex); + } + else if (!startsWithBracket && startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) + { + // Contains both an @ symbol and a word the user wants highlighted + index = std::min(mentionFirstIndex, highlightWordFirstIndex); + } + else if (!startsWithBracket && !startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) + { + // Contains both URL stuff and a word the user wants highlighted + index = std::min(urlFirstIndex, highlightWordFirstIndex); + } + else if (!startsWithBracket && startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) + { + // Contains an @ symbol, URL stuff, and a word the user wants highlighted + index = std::min(mentionFirstIndex, std::min(urlFirstIndex, highlightWordFirstIndex)); + } + else if (startsWithBracket && !startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) + { + // Contains a bracket, URL stuff, and a word the user wants highlighted + index = std::min(bracketFirstIndex, std::min(urlFirstIndex, highlightWordFirstIndex)); + } + else if (startsWithBracket && startsWithAtSymbol && !startsWithUrl && startsWithHighlightWord) + { + // Contains a bracket, an @ symbol, and a word the user wants highlighted + index = std::min(bracketFirstIndex, std::min(mentionFirstIndex, highlightWordFirstIndex)); + } + else if (startsWithBracket && startsWithAtSymbol && startsWithUrl && !startsWithHighlightWord) + { + // Contains a bracket, an @ symbol, and URL stuff + index = std::min(bracketFirstIndex, std::min(mentionFirstIndex, urlFirstIndex)); + } + else if (startsWithBracket && startsWithAtSymbol && startsWithUrl && startsWithHighlightWord) + { + // Contains a bracket, an @ symbol, URL stuff, and a word the user wants highlighted + index = std::min(highlightWordFirstIndex, std::min(bracketFirstIndex, std::min(mentionFirstIndex, urlFirstIndex))); } if (index > 0) @@ -374,9 +417,27 @@ void ChatView::appendMessage(QString message, QString sender, UserLevelFlags use while (fullMentionUpToSpaceOrEnd.size()); } } + else if (index == highlightWordFirstIndex) + { + // You have received a valid mention of custom word!! + int firstSpace = (message.indexOf(" ") == -1 ? message.size() : message.indexOf(" ")); + mentionFormat.setBackground(QBrush(getCustomMentionColor())); + mentionFormat.setForeground(settingsCache->getChatMentionForeground() ? QBrush(Qt::white) : QBrush(Qt::black)); + cursor.insertText(message.mid(0, firstSpace), mentionFormat); + cursor.setCharFormat(defaultFormat); + message = message.mid(firstSpace); + QApplication::alert(this); + if (settingsCache->getShowMentionPopup() && shouldShowSystemPopup()) + { + QString ref = sender.left(sender.length() - 2); + showSystemPopup(ref); + } + } else { - message = message.mid(1); // Not certain when this would ever be reached, but just incase lets skip the character + // Not certain when this would ever be reached, but just incase lets skip the character + cursor.insertText(message.mid(0), defaultFormat); + message = message.mid(1); } } diff --git a/cockatrice/src/dlg_settings.cpp b/cockatrice/src/dlg_settings.cpp index 86a0fd4ae..3159f88d6 100644 --- a/cockatrice/src/dlg_settings.cpp +++ b/cockatrice/src/dlg_settings.cpp @@ -591,6 +591,11 @@ MessagesSettingsPage::MessagesSettingsPage() mentionPopups.setChecked(settingsCache->getShowMentionPopup()); connect(&mentionPopups, SIGNAL(stateChanged(int)), settingsCache, SLOT(setShowMentionPopups(int))); + customAlertString = new QLineEdit(); + customAlertString->setPlaceholderText("Word1, Word2, Word3"); + customAlertString->setText(settingsCache->getHighlightWords().join(", ")); + connect(customAlertString, SIGNAL(textChanged(QString)), settingsCache, SLOT(setHighlightWords(QString))); + QGridLayout *chatGrid = new QGridLayout; chatGrid->addWidget(&chatMentionCheckBox, 0, 0); chatGrid->addWidget(&invertMentionForeground, 0, 1); @@ -602,6 +607,12 @@ MessagesSettingsPage::MessagesSettingsPage() chatGrid->addWidget(&mentionPopups, 4, 0); chatGroupBox = new QGroupBox; chatGroupBox->setLayout(chatGrid); + + QGridLayout *highlightNotice = new QGridLayout; + highlightNotice->addWidget(customAlertString, 0, 0); + highlightNotice->addWidget(&customAlertStringLabel, 1, 0); + highlightGroupBox = new QGroupBox; + highlightGroupBox->setLayout(highlightNotice); QSettings settings; messageList = new QListWidget; @@ -628,11 +639,12 @@ MessagesSettingsPage::MessagesSettingsPage() messageShortcuts = new QGroupBox; messageShortcuts->setLayout(messageListLayout); - + QVBoxLayout *mainLayout = new QVBoxLayout; - + mainLayout->addWidget(messageShortcuts); mainLayout->addWidget(chatGroupBox); + mainLayout->addWidget(highlightGroupBox); setLayout(mainLayout); @@ -688,6 +700,7 @@ void MessagesSettingsPage::actRemove() void MessagesSettingsPage::retranslateUi() { chatGroupBox->setTitle(tr("Chat settings")); + highlightGroupBox->setTitle(tr("Custom alert words")); chatMentionCheckBox.setText(tr("Enable chat mentions")); messageShortcuts->setTitle(tr("In-game message macros")); ignoreUnregUsersMainChat.setText(tr("Ignore unregistered users in main chat")); @@ -697,6 +710,7 @@ void MessagesSettingsPage::retranslateUi() messagePopups.setText(tr("Enable desktop notifications for private messages.")); mentionPopups.setText(tr("Enable desktop notification for mentions.")); hexLabel.setText(tr("(Color is hexadecimal)")); + customAlertStringLabel.setText(tr("(Seperate each word with a comma; Words are case insensitive)")); } diff --git a/cockatrice/src/dlg_settings.h b/cockatrice/src/dlg_settings.h index 5b2f06e50..718114340 100644 --- a/cockatrice/src/dlg_settings.h +++ b/cockatrice/src/dlg_settings.h @@ -172,9 +172,12 @@ private: QCheckBox messagePopups; QCheckBox mentionPopups; QGroupBox *chatGroupBox; + QGroupBox *highlightGroupBox; QGroupBox *messageShortcuts; QLineEdit *mentionColor; + QLineEdit *customAlertString; QLabel hexLabel; + QLabel customAlertStringLabel; void storeSettings(); void updateMentionPreview(); diff --git a/cockatrice/src/settingscache.cpp b/cockatrice/src/settingscache.cpp index 202a03563..ab6a8b386 100644 --- a/cockatrice/src/settingscache.cpp +++ b/cockatrice/src/settingscache.cpp @@ -82,6 +82,8 @@ SettingsCache::SettingsCache() masterVolume = settings->value("sound/mastervolume", 100).toInt(); cardInfoViewMode = settings->value("cards/cardinfoviewmode", 0).toInt(); + + highlightWords = settings->value("personal/highlightWords", QStringList()).toStringList(); } void SettingsCache::setCardInfoViewMode(const int _viewMode) { @@ -89,6 +91,12 @@ void SettingsCache::setCardInfoViewMode(const int _viewMode) { settings->setValue("cards/cardinfoviewmode", cardInfoViewMode); } +void SettingsCache::setHighlightWords(const QString _highlightWords) { + // Words are seperated by a comma and you can not use spaces in words + highlightWords = _highlightWords.simplified().replace(" ", "").split(","); + settings->setValue("personal/highlightWords", highlightWords); +} + void SettingsCache::setMasterVolume(int _masterVolume) { masterVolume = _masterVolume; settings->setValue("sound/mastervolume", masterVolume); diff --git a/cockatrice/src/settingscache.h b/cockatrice/src/settingscache.h index c838f74e2..5e7b2a85d 100644 --- a/cockatrice/src/settingscache.h +++ b/cockatrice/src/settingscache.h @@ -85,6 +85,7 @@ private: bool leftJustified; int masterVolume; int cardInfoViewMode; + QStringList highlightWords; public: SettingsCache(); const QByteArray &getMainWindowGeometry() const { return mainWindowGeometry; } @@ -143,6 +144,7 @@ public: int getMasterVolume() const { return masterVolume; } int getCardInfoViewMode() const { return cardInfoViewMode; } QStringList getCountries() const; + QStringList getHighlightWords() const { return highlightWords; } public slots: void setMainWindowGeometry(const QByteArray &_mainWindowGeometry); void setLang(const QString &_lang); @@ -194,6 +196,7 @@ public slots: void setLeftJustified( const int _leftJustified); void setMasterVolume(const int _masterVolume); void setCardInfoViewMode(const int _viewMode); + void setHighlightWords(const QString _highlightWords); }; extern SettingsCache *settingsCache;