summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swift/Controllers/HighlightManager.cpp')
-rw-r--r--Swift/Controllers/HighlightManager.cpp92
1 files changed, 42 insertions, 50 deletions
diff --git a/Swift/Controllers/HighlightManager.cpp b/Swift/Controllers/HighlightManager.cpp
index 7ab578e..eac562f 100644
--- a/Swift/Controllers/HighlightManager.cpp
+++ b/Swift/Controllers/HighlightManager.cpp
@@ -4,12 +4,21 @@
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
+/*
+ * Copyright (c) 2014 Kevin Smith and Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
#include <cassert>
#include <boost/algorithm/string.hpp>
#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>
@@ -40,6 +49,7 @@ HighlightManager::HighlightManager(SettingsProvider* settings)
: settings_(settings)
, storingSettings_(false)
{
+ rules_ = boost::make_shared<HighlightRulesList>();
loadSettings();
settings_->onSettingChanged.connect(boost::bind(&HighlightManager::handleSettingChanged, this, _1));
}
@@ -51,40 +61,12 @@ void HighlightManager::handleSettingChanged(const std::string& settingPath)
}
}
-void HighlightManager::loadSettings()
-{
- std::string highlightRules = settings_->getSetting(SettingConstants::HIGHLIGHT_RULES);
- if (highlightRules == "@") {
- rules_ = getDefaultRules();
- } else {
- rules_ = rulesFromString(highlightRules);
- }
-}
-
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::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);
- }
- }
- return rules;
+ std::stringstream stream;
+ boost::archive::text_oarchive archive(stream);
+ archive << rules_->list_;
+ return stream.str();
}
std::vector<HighlightRule> HighlightManager::getDefaultRules()
@@ -97,38 +79,48 @@ std::vector<HighlightRule> HighlightManager::getDefaultRules()
return rules;
}
-void HighlightManager::storeSettings()
-{
- storingSettings_ = true; // don't reload settings while saving
- settings_->storeSetting(SettingConstants::HIGHLIGHT_RULES, rulesToString());
- storingSettings_ = false;
-}
-
HighlightRule HighlightManager::getRule(int index) const
{
- assert(index >= 0 && static_cast<size_t>(index) < rules_.size());
- return rules_[static_cast<size_t>(index)];
+ assert(index >= 0 && static_cast<size_t>(index) < rules_->getSize());
+ return rules_->getRule(static_cast<size_t>(index));
}
void HighlightManager::setRule(int index, const HighlightRule& rule)
{
- assert(index >= 0 && static_cast<size_t>(index) < rules_.size());
- rules_[static_cast<size_t>(index)] = rule;
- storeSettings();
+ assert(index >= 0 && static_cast<size_t>(index) < rules_->getSize());
+ rules_->list_[static_cast<size_t>(index)] = rule;
}
void HighlightManager::insertRule(int index, const HighlightRule& rule)
{
- assert(index >= 0 && boost::numeric_cast<std::vector<std::string>::size_type>(index) <= rules_.size());
- rules_.insert(rules_.begin() + index, rule);
- storeSettings();
+ assert(index >= 0 && boost::numeric_cast<std::vector<std::string>::size_type>(index) <= rules_->getSize());
+ rules_->list_.insert(rules_->list_.begin() + index, rule);
}
void HighlightManager::removeRule(int index)
{
- assert(index >= 0 && boost::numeric_cast<std::vector<std::string>::size_type>(index) < rules_.size());
- rules_.erase(rules_.begin() + index);
- storeSettings();
+ assert(index >= 0 && boost::numeric_cast<std::vector<std::string>::size_type>(index) < rules_->getSize());
+ rules_->list_.erase(rules_->list_.begin() + index);
+}
+
+void HighlightManager::storeSettings()
+{
+ storingSettings_ = true; // don't reload settings while saving
+ settings_->storeSetting(SettingConstants::HIGHLIGHT_RULES, rulesToString());
+ storingSettings_ = false;
+}
+
+void HighlightManager::loadSettings()
+{
+ std::string rulesString = settings_->getSetting(SettingConstants::HIGHLIGHT_RULES);
+ std::stringstream stream;
+ stream << rulesString;
+ try {
+ boost::archive::text_iarchive archive(stream);
+ archive >> rules_->list_;
+ } catch (boost::archive::archive_exception&) {
+ rules_->list_ = getDefaultRules();
+ }
}
Highlighter* HighlightManager::createHighlighter()