summaryrefslogtreecommitdiff
path: root/src/configcontainer.cpp
blob: 3f344bf9180250f4d12be4bbca3ec9a36d27a718 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <configcontainer.h>
#include <configparser.h>
#include <sstream>
#include <iostream>

namespace newsbeuter
{

configcontainer::configcontainer()
{
	// configure default values
	config_data["show-read-feeds"] = "yes";
	config_data["browser"] = "lynx";
}

configcontainer::~configcontainer()
{
}

void configcontainer::register_commands(configparser& cfgparser)
{
	cfgparser.register_handler("show-read-feeds", this);
	cfgparser.register_handler("browser", this);
	cfgparser.register_handler("max-items", this);
}

action_handler_status configcontainer::handle_action(const std::string& action, const std::vector<std::string>& params) {
	if (action == "show-read-feeds") {
		if (params.size() < 1) {
			return AHS_TOO_FEW_PARAMS;
		}
		if (!is_bool(params[0])) {
			return AHS_INVALID_PARAMS;
		}
		config_data[action] = params[0];
		// std::cerr << "setting " << action << " to `" << params[0] << "'" << std::endl;
		return AHS_OK; 
	} else if (action == "browser") {
		if (params.size() < 1) {
			return AHS_TOO_FEW_PARAMS;
		}
		config_data[action] = params[0];
		return AHS_OK;	
	} else if (action == "max-items") {
		if (params.size() < 1) {
			return AHS_TOO_FEW_PARAMS;
		}
		config_data[action] = params[0];
		return AHS_OK;	
	}
	return AHS_INVALID_COMMAND;	
}

bool configcontainer::is_bool(const std::string& s) {
	char * bool_values[] = { "yes", "no", "true", "false", 0 };
	for (int i=0; bool_values[i] ; ++i) {
		if (s == bool_values[i])
			return true;	
	}
	return false;
}

std::string configcontainer::get_configvalue(const std::string& key) {
	return config_data[key];	
}

int configcontainer::get_configvalue_as_int(const std::string& key) {
	std::istringstream is(config_data[key]);
	int i;
	is >> i;
	return i;	
}

bool configcontainer::get_configvalue_as_bool(const std::string& key) {
	if (config_data[key] == "true" || config_data[key] == "yes")
		return true;
	return false;	
}

void configcontainer::set_configvalue(const std::string& key, const std::string& value) {
	config_data[key] = value;
}

}