diff options
Diffstat (limited to 'Swift/Controllers/Chat')
-rw-r--r-- | Swift/Controllers/Chat/ChatMessageParser.cpp | 9 | ||||
-rw-r--r-- | Swift/Controllers/Chat/ChatMessageParser.h | 5 | ||||
-rw-r--r-- | Swift/Controllers/Chat/ChatsManager.cpp | 2 | ||||
-rw-r--r-- | Swift/Controllers/Chat/UnitTest/ChatMessageParserTest.cpp | 53 |
4 files changed, 37 insertions, 32 deletions
diff --git a/Swift/Controllers/Chat/ChatMessageParser.cpp b/Swift/Controllers/Chat/ChatMessageParser.cpp index de4abb1..a43ef0e 100644 --- a/Swift/Controllers/Chat/ChatMessageParser.cpp +++ b/Swift/Controllers/Chat/ChatMessageParser.cpp @@ -20,9 +20,9 @@ namespace Swift { - ChatMessageParser::ChatMessageParser(const std::map<std::string, std::string>& emoticons, const std::vector<HighlightRule>& highlightRules) - : emoticons_(emoticons), highlightRules_(highlightRules) { - + ChatMessageParser::ChatMessageParser(const std::map<std::string, std::string>& emoticons, HighlightRulesListPtr highlightRules) + : emoticons_(emoticons), highlightRules_(highlightRules) + { } typedef std::pair<std::string, std::string> StringPair; @@ -126,7 +126,8 @@ namespace Swift { { ChatWindow::ChatMessage parsedMessage = message; - foreach(const HighlightRule &rule, highlightRules_) { + for (size_t i = 0; i < highlightRules_->getSize(); ++i) { + const HighlightRule& rule = highlightRules_->getRule(i); foreach(const boost::regex ®ex, rule.getKeywordRegex()) { ChatWindow::ChatMessage newMessage; foreach (boost::shared_ptr<ChatWindow::ChatMessagePart> part, parsedMessage.getParts()) { diff --git a/Swift/Controllers/Chat/ChatMessageParser.h b/Swift/Controllers/Chat/ChatMessageParser.h index e899246..1acb9c3 100644 --- a/Swift/Controllers/Chat/ChatMessageParser.h +++ b/Swift/Controllers/Chat/ChatMessageParser.h @@ -14,13 +14,12 @@ namespace Swift { class ChatMessageParser { public: - ChatMessageParser(const std::map<std::string, std::string>& emoticons, const std::vector<HighlightRule>& highlightRules); + ChatMessageParser(const std::map<std::string, std::string>& emoticons, HighlightRulesListPtr highlightRules); ChatWindow::ChatMessage parseMessageBody(const std::string& body, bool senderIsSelf = false); private: ChatWindow::ChatMessage emoticonHighlight(const ChatWindow::ChatMessage& parsedMessage); ChatWindow::ChatMessage splitHighlight(const ChatWindow::ChatMessage& parsedMessage); std::map<std::string, std::string> emoticons_; - std::vector<HighlightRule> highlightRules_; - + HighlightRulesListPtr highlightRules_; }; } diff --git a/Swift/Controllers/Chat/ChatsManager.cpp b/Swift/Controllers/Chat/ChatsManager.cpp index 6c5d6d9..d56f6cc 100644 --- a/Swift/Controllers/Chat/ChatsManager.cpp +++ b/Swift/Controllers/Chat/ChatsManager.cpp @@ -155,7 +155,7 @@ ChatsManager::ChatsManager( uiEventStream_ = uiEventStream; mucBookmarkManager_ = NULL; profileSettings_ = profileSettings; - chatMessageParser_ = new ChatMessageParser(emoticons, highlightManager->getRules()); + chatMessageParser_ = new ChatMessageParser(emoticons, highlightManager_->getRules()); presenceOracle_->onPresenceChange.connect(boost::bind(&ChatsManager::handlePresenceChange, this, _1)); uiEventConnection_ = uiEventStream_->onUIEvent.connect(boost::bind(&ChatsManager::handleUIEvent, this, _1)); diff --git a/Swift/Controllers/Chat/UnitTest/ChatMessageParserTest.cpp b/Swift/Controllers/Chat/UnitTest/ChatMessageParserTest.cpp index 4a25b50..9207846 100644 --- a/Swift/Controllers/Chat/UnitTest/ChatMessageParserTest.cpp +++ b/Swift/Controllers/Chat/UnitTest/ChatMessageParserTest.cpp @@ -52,7 +52,7 @@ public: CPPUNIT_ASSERT_EQUAL(text, part->target); } - static std::vector<HighlightRule> ruleFromKeyword(const std::string& keyword, bool matchCase, bool matchWholeWord) + static HighlightRule ruleFromKeyword(const std::string& keyword, bool matchCase, bool matchWholeWord) { HighlightRule rule; std::vector<std::string> keywords; @@ -60,26 +60,31 @@ public: rule.setKeywords(keywords); rule.setMatchCase(matchCase); rule.setMatchWholeWords(matchWholeWord); - std::vector<HighlightRule> rules; - rules.push_back(rule); - return rules; + return rule; } - static std::vector<HighlightRule> ruleFromKeywords(const std::vector<HighlightRule>& rules1, const std::vector<HighlightRule>& rules2) + static const HighlightRulesListPtr ruleListFromKeyword(const std::string& keyword, bool matchCase, bool matchWholeWord) { - std::vector<HighlightRule> rules = rules1; - rules.insert(rules.end(), rules2.begin(), rules2.end()); - return rules; + boost::shared_ptr<HighlightManager::HighlightRulesList> list = boost::make_shared<HighlightManager::HighlightRulesList>(); + list->addRule(ruleFromKeyword(keyword, matchCase, matchWholeWord)); + return list; } - void testFullBody() { + static const HighlightRulesListPtr ruleListFromKeywords(const HighlightRule &rule1, const HighlightRule &rule2) + { + boost::shared_ptr<HighlightManager::HighlightRulesList> list = boost::make_shared<HighlightManager::HighlightRulesList>(); + list->addRule(rule1); + list->addRule(rule2); + return list; + } + void testFullBody() { const std::string no_special_message = "a message with no special content"; - ChatMessageParser testling(emoticons_, std::vector<HighlightRule>()); + ChatMessageParser testling(emoticons_, boost::make_shared<HighlightManager::HighlightRulesList>()); ChatWindow::ChatMessage result = testling.parseMessageBody(no_special_message); assertText(result, 0, no_special_message); - testling = ChatMessageParser(emoticons_, ruleFromKeyword("trigger", false, false)); + testling = ChatMessageParser(emoticons_, ruleListFromKeyword("trigger", false, false)); result = testling.parseMessageBody(":) shiny :( trigger :) http://wonderland.lit/blah http://denmark.lit boom boom"); assertEmoticon(result, 0, smile1_, smile1Path_); assertText(result, 1, " shiny "); @@ -94,72 +99,72 @@ public: assertURL(result, 10, "http://denmark.lit"); assertText(result, 11, " boom boom"); - testling = ChatMessageParser(emoticons_, ruleFromKeyword("trigger", false, false)); + testling = ChatMessageParser(emoticons_, ruleListFromKeyword("trigger", false, false)); result = testling.parseMessageBody("testtriggermessage"); assertText(result, 0, "test"); assertHighlight(result, 1, "trigger"); assertText(result, 2, "message"); - testling = ChatMessageParser(emoticons_, ruleFromKeyword("trigger", false, true)); + testling = ChatMessageParser(emoticons_, ruleListFromKeyword("trigger", false, true)); result = testling.parseMessageBody("testtriggermessage"); assertText(result, 0, "testtriggermessage"); - testling = ChatMessageParser(emoticons_, ruleFromKeyword("trigger", true, false)); + testling = ChatMessageParser(emoticons_, ruleListFromKeyword("trigger", true, false)); result = testling.parseMessageBody("TrIgGeR"); assertText(result, 0, "TrIgGeR"); - testling = ChatMessageParser(emoticons_, ruleFromKeyword("trigger", false, false)); + testling = ChatMessageParser(emoticons_, ruleListFromKeyword("trigger", false, false)); result = testling.parseMessageBody("TrIgGeR"); assertHighlight(result, 0, "TrIgGeR"); - testling = ChatMessageParser(emoticons_, ruleFromKeyword("trigger", false, false)); + testling = ChatMessageParser(emoticons_, ruleListFromKeyword("trigger", false, false)); result = testling.parseMessageBody("partialTrIgGeRmatch"); assertText(result, 0, "partial"); assertHighlight(result, 1, "TrIgGeR"); assertText(result, 2, "match"); - testling = ChatMessageParser(emoticons_, ruleFromKeywords(ruleFromKeyword("one", false, false), ruleFromKeyword("three", false, false))); + testling = ChatMessageParser(emoticons_, ruleListFromKeywords(ruleFromKeyword("one", false, false), ruleFromKeyword("three", false, false))); result = testling.parseMessageBody("zero one two three"); assertText(result, 0, "zero "); assertHighlight(result, 1, "one"); assertText(result, 2, " two "); assertHighlight(result, 3, "three"); - testling = ChatMessageParser(emoticons_, ruleFromKeywords(ruleFromKeyword("one", false, false), ruleFromKeyword("three", false, false))); + testling = ChatMessageParser(emoticons_, ruleListFromKeywords(ruleFromKeyword("one", false, false), ruleFromKeyword("three", false, false))); result = testling.parseMessageBody("zero oNe two tHrEe"); assertText(result, 0, "zero "); assertHighlight(result, 1, "oNe"); assertText(result, 2, " two "); assertHighlight(result, 3, "tHrEe"); - testling = ChatMessageParser(emoticons_, ruleFromKeywords(ruleFromKeyword("one", false, false), ruleFromKeyword("three", true, false))); + testling = ChatMessageParser(emoticons_, ruleListFromKeywords(ruleFromKeyword("one", false, false), ruleFromKeyword("three", true, false))); result = testling.parseMessageBody("zero oNe two tHrEe"); assertText(result, 0, "zero "); assertHighlight(result, 1, "oNe"); assertText(result, 2, " two tHrEe"); - testling = ChatMessageParser(emoticons_, ruleFromKeywords(ruleFromKeyword("one", true, false), ruleFromKeyword("three", true, false))); + testling = ChatMessageParser(emoticons_, ruleListFromKeywords(ruleFromKeyword("one", true, false), ruleFromKeyword("three", true, false))); result = testling.parseMessageBody("zero oNe two tHrEe"); assertText(result, 0, "zero oNe two tHrEe"); - testling = ChatMessageParser(emoticons_, ruleFromKeywords(ruleFromKeyword("one", false, false), ruleFromKeyword("three", false, false))); + testling = ChatMessageParser(emoticons_, ruleListFromKeywords(ruleFromKeyword("one", false, false), ruleFromKeyword("three", false, false))); result = testling.parseMessageBody("zeroonetwothree"); assertText(result, 0, "zero"); assertHighlight(result, 1, "one"); assertText(result, 2, "two"); assertHighlight(result, 3, "three"); - testling = ChatMessageParser(emoticons_, ruleFromKeywords(ruleFromKeyword("one", true, false), ruleFromKeyword("three", false, false))); + testling = ChatMessageParser(emoticons_, ruleListFromKeywords(ruleFromKeyword("one", true, false), ruleFromKeyword("three", false, false))); result = testling.parseMessageBody("zeroOnEtwoThReE"); assertText(result, 0, "zeroOnEtwo"); assertHighlight(result, 1, "ThReE"); - testling = ChatMessageParser(emoticons_, ruleFromKeywords(ruleFromKeyword("one", false, true), ruleFromKeyword("three", false, false))); + testling = ChatMessageParser(emoticons_, ruleListFromKeywords(ruleFromKeyword("one", false, true), ruleFromKeyword("three", false, false))); result = testling.parseMessageBody("zeroonetwothree"); assertText(result, 0, "zeroonetwo"); assertHighlight(result, 1, "three"); - testling = ChatMessageParser(emoticons_, ruleFromKeywords(ruleFromKeyword("one", false, true), ruleFromKeyword("three", false, true))); + testling = ChatMessageParser(emoticons_, ruleListFromKeywords(ruleFromKeyword("one", false, true), ruleFromKeyword("three", false, true))); result = testling.parseMessageBody("zeroonetwothree"); assertText(result, 0, "zeroonetwothree"); } |