summaryrefslogtreecommitdiffstats
blob: 17ce776a27d2e3242f257cbbd5a7ba9c67d6cbe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (c) 2012 Maciej Niedzielski
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.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>
#include <Swift/Controllers/Highlighter.h>
#include <Swift/Controllers/Settings/SettingsProvider.h>
#include <Swift/Controllers/SettingConstants.h>

/* How does highlighting work?
 *
 * HighlightManager manages a list of if-then rules used to highlight messages.
 * Rule is represented by HighlightRule. Action ("then" part) is HighlightAction.
 *
 *
 * HighlightManager is also used as a factory for Highlighter objects.
 * Each ChatControllerBase has its own Highlighter.
 * Highligher may be customized by using setNick(), etc.
 *
 * ChatControllerBase passes incoming messages to Highlighter and gets HighlightAction in return
 * (first matching rule is returned).
 * If needed, HighlightAction is then passed back to Highlighter for further handling.
 * This results in HighlightManager emiting onHighlight event,
 * which is handled by SoundController to play sound notification
 */

namespace Swift {

HighlightManager::HighlightManager(SettingsProvider* settings)
	: settings_(settings)
	, storingSettings_(false)
{
	loadSettings();
	settings_->onSettingChanged.connect(boost::bind(&HighlightManager::handleSettingChanged, this, _1));
}

void HighlightManager::handleSettingChanged(const std::string& settingPath)
{
	if (!storingSettings_ && SettingConstants::HIGHLIGHT_RULES.getKey() == settingPath) {
		loadSettings();
	}
}

std::string HighlightManager::rulesToString() const
{
	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;
	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;
}

std::vector<HighlightRule> HighlightManager::getDefaultRules()
{
	std::vector<HighlightRule> rules;
	HighlightRule r;
	r.setMatchChat(true);
	r.getAction().setPlaySound(true);
	rules.push_back(r);
	return rules;
}

HighlightRule HighlightManager::getRule(int index) const
{
	assert(index >= 0 && static_cast<size_t>(index) < rules_.size());
	return rules_[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();
}

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();
}

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();
}

void HighlightManager::storeSettings()
{
	storingSettings_ = true;	// don't reload settings while saving
	settings_->storeSetting(SettingConstants::HIGHLIGHT_RULES, rulesToString());
	storingSettings_ = false;
}

void HighlightManager::loadSettings()
{
	std::string highlightRules = settings_->getSetting(SettingConstants::HIGHLIGHT_RULES);
	rules_ = rulesFromString(highlightRules);
}

Highlighter* HighlightManager::createHighlighter()
{
	return new Highlighter(this);
}

}