diff --git a/src/plugins/azoth/core.cpp b/src/plugins/azoth/core.cpp index ddf9e03..3ac8367 100644 --- a/src/plugins/azoth/core.cpp +++ b/src/plugins/azoth/core.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -827,7 +828,48 @@ namespace Azoth return string; } - QString Core::FormatBody (QString body, IMessage *msg) + namespace + { + void HighlightNicks (QString& body, IMessage *msg, const QList& colors) + { + const auto entry = qobject_cast (msg->ParentCLEntry ()); + if (!entry) + return; + + const auto& nicks = Util::Map (entry->GetParticipants (), + [] (QObject *obj) { return qobject_cast (obj)->GetEntryName (); }); + for (const auto& nick : nicks) + { + if (!body.contains (nick)) + continue; + + const auto& nickColor = GetNickColor (nick, colors); + if (nickColor.isNull ()) + continue; + + int pos = 0; + while ((pos = body.indexOf (nick, pos)) >= 0) + { + const auto nickEnd = pos + nick.size (); + if ((pos > 0 && !body.at (pos - 1).isSpace ()) || + (nickEnd + 1 < body.size () && !body.at (nickEnd + 1).isSpace ())) + { + pos += nick.size (); + continue; + } + + const auto& startStr = ""; + const QString endStr { "" }; + body.insert (nickEnd, endStr); + body.insert (pos, startStr); + + pos += nick.size () + startStr.size () + endStr.size (); + } + } + } + } + + QString Core::FormatBody (QString body, IMessage *msg, const QList& colors) { QObject *msgObj = msg->GetQObject (); @@ -851,6 +893,9 @@ namespace Azoth body = HandleSmiles (body); + if (msg->GetMessageType () == IMessage::Type::MUCMessage) + HighlightNicks (body, msg, colors); + proxy.reset (new Util::DefaultHookProxy); proxy->SetValue ("body", body); emit hookFormatBodyEnd (proxy, msgObj); diff --git a/src/plugins/azoth/core.h b/src/plugins/azoth/core.h index 6806a88..8d3166e 100644 --- a/src/plugins/azoth/core.h +++ b/src/plugins/azoth/core.h @@ -260,7 +260,7 @@ namespace Azoth QString FormatDate (QDateTime, IMessage*); QString FormatNickname (QString, IMessage*, const QString& color); - QString FormatBody (QString body, IMessage *msg); + QString FormatBody (QString body, IMessage *msg, const QList& coloring); QString HandleSmiles (QString body); /** This function increases the number of unread messages by diff --git a/src/plugins/azoth/interfaces/azoth/iproxyobject.h b/src/plugins/azoth/interfaces/azoth/iproxyobject.h index 30b5cd7..9fdfb0c 100644 --- a/src/plugins/azoth/interfaces/azoth/iproxyobject.h +++ b/src/plugins/azoth/interfaces/azoth/iproxyobject.h @@ -253,11 +253,17 @@ namespace Azoth * This function should be used to format the body of the given * message. * + * This function also accepts the list of colors used for + * nick coloring in the current chat window, since the + * \em body of the \em message may also be colored if it contains + * other participants' nicks. + * * @param[in] body The body to format. * @param[in] message The message object implementing IMessage. + * @param[in] coloring The set of colors used for nick coloring. * @return The formatted body. */ - virtual QString FormatBody (QString body, QObject *message) const = 0; + virtual QString FormatBody (QString body, QObject *message, const QList& coloring) const = 0; /** @brief Preprocesses the message before displaying it. * diff --git a/src/plugins/azoth/plugins/adiumstyles/adiumstylesource.cpp b/src/plugins/azoth/plugins/adiumstyles/adiumstylesource.cpp index ff98c1c..4679780 100644 --- a/src/plugins/azoth/plugins/adiumstyles/adiumstylesource.cpp +++ b/src/plugins/azoth/plugins/adiumstyles/adiumstylesource.cpp @@ -642,7 +642,8 @@ namespace AdiumStyles Coloring2Colors_ ["hash"] = Proxy_->GenerateColors ("hash", {}); // %senderColor% - const QString& nickColor = Proxy_->GetNickColor (senderNick, Coloring2Colors_ ["hash"]); + const auto& colors = Coloring2Colors_ ["hash"]; + const auto& nickColor = Proxy_->GetNickColor (senderNick, colors); templ.replace ("%senderColor%", nickColor); // %senderColor{N}% @@ -672,7 +673,7 @@ namespace AdiumStyles .arg (senderNick) .arg (body.mid (4)); - body = Proxy_->FormatBody (body, msgObj); + body = Proxy_->FormatBody (body, msgObj, colors); if (isHighlightMsg && !hasHighBackground) body = "GetEscapedBody (); - body = Proxy_->FormatBody (body, msg->GetQObject ()); + body = Proxy_->FormatBody (body, msg->GetQObject (), colors); const QString dateBegin (""); const QString dateEnd (""); diff --git a/src/plugins/azoth/proxyobject.cpp b/src/plugins/azoth/proxyobject.cpp index 4d39630..a529236 100644 --- a/src/plugins/azoth/proxyobject.cpp +++ b/src/plugins/azoth/proxyobject.cpp @@ -198,9 +198,9 @@ namespace Azoth return Core::Instance ().FormatNickname (nick, qobject_cast (obj), color); } - QString ProxyObject::FormatBody (QString body, QObject *obj) const + QString ProxyObject::FormatBody (QString body, QObject *obj, const QList& coloring) const { - return Core::Instance ().FormatBody (body, qobject_cast (obj)); + return Core::Instance ().FormatBody (body, qobject_cast (obj), coloring); } void ProxyObject::PreprocessMessage (QObject *msgObj) diff --git a/src/plugins/azoth/proxyobject.h b/src/plugins/azoth/proxyobject.h index f22b53b..42cdda4 100644 --- a/src/plugins/azoth/proxyobject.h +++ b/src/plugins/azoth/proxyobject.h @@ -68,7 +68,7 @@ namespace Azoth QString GetNickColor (const QString&, const QList&) const; QString FormatDate (QDateTime, QObject*) const; QString FormatNickname (QString, QObject*, const QString&) const; - QString FormatBody (QString, QObject*) const; + QString FormatBody (QString, QObject*, const QList&) const; void PreprocessMessage (QObject*); Util::ResourceLoader* GetResourceLoader (PublicResourceLoader) const; QIcon GetIconForState (State) const;