diff options
| -rw-r--r-- | Swift/Controllers/HighlightAction.h | 16 | ||||
| -rw-r--r-- | Swift/Controllers/HighlightManager.cpp | 29 | ||||
| -rw-r--r-- | Swift/Controllers/HighlightRule.cpp | 51 | ||||
| -rw-r--r-- | Swift/Controllers/HighlightRule.h | 21 | 
4 files changed, 49 insertions, 68 deletions
| 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 <string> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> +  namespace Swift {  	class HighlightRule; @@ -34,6 +37,9 @@ namespace Swift {  			bool isEmpty() const { return !highlightText_ && !playSound_; }  		private: +			friend class boost::serialization::access; +			template<class Archive> 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<class Archive> +	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 <boost/regex.hpp>  #include <boost/bind.hpp>  #include <boost/numeric/conversion/cast.hpp> +#include <boost/serialization/vector.hpp> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp>  #include <Swiften/Base/foreach.h>  #include <Swift/Controllers/HighlightManager.h> @@ -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<HighlightRule> HighlightManager::rulesFromString(const std::string& rulesString)  {  	std::vector<HighlightRule> rules; -	std::string s(rulesString); -	typedef boost::split_iterator<std::string::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<std::string>(*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<std::string> 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<std::string> 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 <string>  #include <boost/regex.hpp> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp>  #include <Swift/Controllers/HighlightAction.h> @@ -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<std::string>& getSenders() const { return senders_; }  			void setSenders(const std::vector<std::string>&); @@ -53,6 +52,9 @@ namespace Swift {  			bool isEmpty() const;  		private: +			friend class boost::serialization::access; +			template<class Archive> 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<class Archive> +	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_; +	} +  } | 
 Swift
 Swift