From dd2e075c48486824a538229afdfa5871e09c7fda Mon Sep 17 00:00:00 2001 From: Richard Maudsley Date: Mon, 13 Jan 2014 16:19:30 +0000 Subject: Replaced HighlightManager save-data with boost::serialization. Change-Id: Ia4e2baa52ba765f2bfd4a1986d63c0ba2c3acd09 diff --git a/Swift/Controllers/HighlightAction.h b/Swift/Controllers/HighlightAction.h index bfbed74..a8da90a 100644 --- a/Swift/Controllers/HighlightAction.h +++ b/Swift/Controllers/HighlightAction.h @@ -8,6 +8,9 @@ #include +#include +#include + namespace Swift { class HighlightRule; @@ -34,6 +37,9 @@ namespace Swift { bool isEmpty() const { return !highlightText_ && !playSound_; } private: + friend class boost::serialization::access; + template void serialize(Archive & ar, const unsigned int version); + bool highlightText_; std::string textColor_; std::string textBackground_; @@ -42,4 +48,14 @@ namespace Swift { std::string soundFile_; }; + template + void HighlightAction::serialize(Archive& ar, const unsigned int /*version*/) + { + ar & highlightText_; + ar & textColor_; + ar & textBackground_; + ar & playSound_; + ar & soundFile_; + } + } diff --git a/Swift/Controllers/HighlightManager.cpp b/Swift/Controllers/HighlightManager.cpp index 7ab578e..fa98a6e 100644 --- a/Swift/Controllers/HighlightManager.cpp +++ b/Swift/Controllers/HighlightManager.cpp @@ -10,6 +10,9 @@ #include #include #include +#include +#include +#include #include #include @@ -63,25 +66,23 @@ void HighlightManager::loadSettings() std::string HighlightManager::rulesToString() const { - std::string s; - foreach (HighlightRule r, rules_) { - s += r.toString() + '\f'; - } - if (s.size()) { - s.erase(s.end() - 1); - } - return s; + std::stringstream stream; + boost::archive::text_oarchive archive(stream); + archive << rules_; + return stream.str(); } std::vector HighlightManager::rulesFromString(const std::string& rulesString) { std::vector rules; - std::string s(rulesString); - typedef boost::split_iterator split_iterator; - for (split_iterator it = boost::make_split_iterator(s, boost::first_finder("\f")); it != split_iterator(); ++it) { - HighlightRule r = HighlightRule::fromString(boost::copy_range(*it)); - if (!r.isEmpty()) { - rules.push_back(r); + if (rulesString.length()) { + std::stringstream stream; + stream << rulesString; + try { + boost::archive::text_iarchive archive(stream); + archive >> rules; + } catch (boost::archive::archive_exception&) { + /* archive corrupted */ } } return rules; diff --git a/Swift/Controllers/HighlightRule.cpp b/Swift/Controllers/HighlightRule.cpp index 9ca7d86..91bd3ab 100644 --- a/Swift/Controllers/HighlightRule.cpp +++ b/Swift/Controllers/HighlightRule.cpp @@ -56,57 +56,6 @@ bool HighlightRule::boolFromString(const std::string& s) return s == "1"; } -std::string HighlightRule::toString() const -{ - std::vector v; - v.push_back(boost::join(senders_, "\t")); - v.push_back(boost::join(keywords_, "\t")); - v.push_back(boolToString(nickIsKeyword_)); - v.push_back(boolToString(matchChat_)); - v.push_back(boolToString(matchMUC_)); - v.push_back(boolToString(matchCase_)); - v.push_back(boolToString(matchWholeWords_)); - v.push_back(boolToString(action_.highlightText())); - v.push_back(action_.getTextColor()); - v.push_back(action_.getTextBackground()); - v.push_back(boolToString(action_.playSound())); - v.push_back(action_.getSoundFile()); - return boost::join(v, "\n"); -} - -HighlightRule HighlightRule::fromString(const std::string& s) -{ - std::vector v; - boost::split(v, s, boost::is_any_of("\n")); - - HighlightRule r; - size_t i = 0; - try { - boost::split(r.senders_, v.at(i++), boost::is_any_of("\t")); - r.senders_.erase(std::remove_if(r.senders_.begin(), r.senders_.end(), boost::lambda::_1 == ""), r.senders_.end()); - boost::split(r.keywords_, v.at(i++), boost::is_any_of("\t")); - r.keywords_.erase(std::remove_if(r.keywords_.begin(), r.keywords_.end(), boost::lambda::_1 == ""), r.keywords_.end()); - r.nickIsKeyword_ = boolFromString(v.at(i++)); - r.matchChat_ = boolFromString(v.at(i++)); - r.matchMUC_ = boolFromString(v.at(i++)); - r.matchCase_ = boolFromString(v.at(i++)); - r.matchWholeWords_ = boolFromString(v.at(i++)); - r.action_.setHighlightText(boolFromString(v.at(i++))); - r.action_.setTextColor(v.at(i++)); - r.action_.setTextBackground(v.at(i++)); - r.action_.setPlaySound(boolFromString(v.at(i++))); - r.action_.setSoundFile(v.at(i++)); - } - catch (std::out_of_range) { - // this may happen if more properties are added to HighlightRule object, etc... - // in such case, default values (set by default constructor) will be used - } - - r.updateRegex(); - - return r; -} - bool HighlightRule::isMatch(const std::string& body, const std::string& sender, const std::string& nick, MessageType messageType) const { if ((messageType == HighlightRule::ChatMessage && matchChat_) || (messageType == HighlightRule::MUCMessage && matchMUC_)) { diff --git a/Swift/Controllers/HighlightRule.h b/Swift/Controllers/HighlightRule.h index 1abfa5a..523be47 100644 --- a/Swift/Controllers/HighlightRule.h +++ b/Swift/Controllers/HighlightRule.h @@ -10,6 +10,8 @@ #include #include +#include +#include #include @@ -26,9 +28,6 @@ namespace Swift { const HighlightAction& getAction() const { return action_; } HighlightAction& getAction() { return action_; } - static HighlightRule fromString(const std::string&); - std::string toString() const; - const std::vector& getSenders() const { return senders_; } void setSenders(const std::vector&); @@ -53,6 +52,9 @@ namespace Swift { bool isEmpty() const; private: + friend class boost::serialization::access; + template void serialize(Archive & ar, const unsigned int version); + static std::string boolToString(bool); static bool boolFromString(const std::string&); @@ -74,4 +76,17 @@ namespace Swift { HighlightAction action_; }; + template + void HighlightRule::serialize(Archive& ar, const unsigned int /*version*/) + { + ar & senders_; + ar & keywords_; + ar & nickIsKeyword_; + ar & matchChat_; + ar & matchMUC_; + ar & matchCase_; + ar & matchWholeWords_; + ar & action_; + } + } -- cgit v0.10.2-6-g49f6