summaryrefslogtreecommitdiffstats
path: root/gui/twmsg.cpp
blob: ebd6fc313185eda913cdec44a1fc2185bd7207b8 (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
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
137
138
139
/*
	Copyright 2015 _that/TeamWin
	This file is part of TWRP/TeamWin Recovery Project.

	TWRP is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	TWRP is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "../data.hpp"
#include "pages.hpp"
#include "resources.hpp"

#include "twmsg.h"
#include <cctype>

std::string Message::GetFormatString(const std::string& name) const
{
	return resourceLookup(name);
}

// Look up in local replacement vars first, if not found then use outer lookup object
class LocalLookup : public StringLookup
{
	const std::vector<std::string>& vars;
	const StringLookup& next;
public:
	LocalLookup(const std::vector<std::string>& vars, const StringLookup& next) : vars(vars), next(next) {}
	virtual std::string operator()(const std::string& name) const
	{
		if (!name.empty() && isdigit(name[0])) { // {1}..{9}
			int i = atoi(name.c_str());
			if (i > 0 && i <= (int)vars.size())
				return vars[i - 1];
		}
		return next(name);
	}
};

// conversion to final string
Message::operator std::string() const
{
	// do resource lookup
	std::string str = GetFormatString(name);

	LocalLookup lookup(variables, varLookup);

	// now insert stuff into curly braces

	size_t pos = 0;
	while ((pos = str.find('{', pos)) < std::string::npos) {
		size_t end = str.find('}', pos);
		if (end == std::string::npos)
			break;

		std::string varname = str.substr(pos + 1, end - pos - 1);
		std::string vartext = lookup(varname);

		str.replace(pos, end - pos + 1, vartext);
	}
	// TODO: complain about too many or too few numbered replacement variables
	return str;
}

/*
Resource manager lookup
*/
class ResourceLookup : public StringLookup
{
public:
	virtual std::string operator()(const std::string& name) const
	{
		std::string resname;
		std::string default_value;

		size_t pos = name.find('=');
		if (pos == std::string::npos) {
			resname = name;
		} else {
			resname = name.substr(0, pos);
			default_value = name.substr(pos + 1);
		}
#ifndef BUILD_TWRPTAR_MAIN
		const ResourceManager* res = PageManager::GetResources();
		if (res) {
			if (default_value.empty())
				return res->FindString(resname);
			else
				return res->FindString(resname, default_value);
		}
#endif
		if (!default_value.empty()) {
			return default_value;
		}
		return name;
	}
};
ResourceLookup resourceLookup;


/*
DataManager lookup
*/
class DataLookup : public StringLookup
{
public:
	virtual std::string operator()(const std::string& name) const
	{
#ifndef BUILD_TWRPTAR_MAIN
		std::string value;
		if (DataManager::GetValue(name, value) == 0)
			return value;
		else
#endif
			return "";
	}
};
DataLookup dataLookup;


// Utility functions to create messages. Short names to make usage convenient.
Message Msg(const char* name)
{
	return Message(msg::kNormal, name, resourceLookup, dataLookup);
}

Message Msg(msg::Kind kind, const char* name)
{
	return Message(kind, name, resourceLookup, dataLookup);
}