aboutsummaryrefslogtreecommitdiff
path: root/src/filtercontainer.cpp
blob: 87affb65a22392c95cfa79564017b67b4a07761c (plain) (blame)
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
#include "filtercontainer.h"

#include <algorithm>

#include "config.h"
#include "confighandlerexception.h"
#include "configparser.h"
#include "matcher.h"
#include "strprintf.h"
#include "utils.h"

namespace newsboat {

FilterContainer::~FilterContainer() {}

void FilterContainer::handle_action(const std::string& action,
	const std::vector<std::string>& params)
{
	/*
	 * FilterContainer does nothing but to save (filter name, filter
	 * expression) tuples. These tuples are used for enabling the user to
	 * predefine filter expressions and then select them from a list by
	 * their name.
	 */
	if (action == "define-filter") {
		if (params.size() < 2) {
			throw ConfigHandlerException(ActionHandlerStatus::TOO_FEW_PARAMS);
		}

		FilterNameExprPair filter;
		filter.name = params[0];
		filter.expr = params[1];

		Matcher m;
		if (!m.parse(filter.expr)) {
			throw ConfigHandlerException(strprintf::fmt(
					_("couldn't parse filter expression `%s': %s"),
					filter.expr,
					m.get_parse_error()));
		}

		filters.emplace_back(std::move(filter));
	} else {
		throw ConfigHandlerException(ActionHandlerStatus::INVALID_COMMAND);
	}
}

void FilterContainer::dump_config(std::vector<std::string>& config_output) const
{
	for (const auto& filter : filters) {
		config_output.push_back(strprintf::fmt("define-filter %s %s",
				utils::quote(filter.name),
				utils::quote(filter.expr)));
	}
}

nonstd::optional<std::string> FilterContainer::get_filter(const std::string& name)
{
	const auto filter = std::find_if(filters.begin(),
	filters.end(), [&](const FilterNameExprPair& pair) {
		return pair.name == name;
	});

	if (filter != filters.end()) {
		return filter->expr;
	} else {
		return {};
	}
}

} // namespace newsboat