blob: cd035d1f51d4fd2803007bcf838ad86c210b4ccc [file] [log] [blame]
Ethan Yonker74db1572015-10-28 12:44:49 -05001/*
2 Copyright 2015 _that/TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef twmsg_h
20#define twmsg_h
21
22#include <cstdlib>
23#include <cstring>
24#include <string>
25#include <sstream>
26#include <vector>
27#include <errno.h>
28
29/*
30Abstract interface for something that can look up strings by name.
31*/
32class StringLookup
33{
34public:
35 virtual std::string operator()(const std::string& name) const = 0;
36 virtual ~StringLookup() {};
37};
38
39
40namespace msg
41{
42 // These get translated to colors in the GUI console
43 enum Kind
44 {
45 kNormal,
46 kHighlight,
47 kWarning,
48 kError
49 };
50
51
52 template<typename T> std::string to_string(const T& v)
53 {
54 std::ostringstream ss;
55 ss << v;
56 return ss.str();
57 }
58}
59
60
61/*
62Generic message formatting class.
63Designed to decouple message generation from actual resource string lookup and variable insertion,
64so that messages can be re-translated at any later time.
65*/
66class Message
67{
68 msg::Kind kind; // severity or similar message kind
69 std::string name; // the resource string name. may be of format "name=default value".
70 std::vector<std::string> variables; // collected insertion variables to replace {1}, {2}, ...
71 const StringLookup& resourceLookup; // object to resolve resource string name into a final format string
72 const StringLookup& varLookup; // object to resolve any non-numeric insertion strings
73
74 std::string GetFormatString(const std::string& name) const;
75
76public:
77 Message(msg::Kind kind, const char* name, const StringLookup& resourceLookup, const StringLookup& varLookup)
78 : kind(kind), name(name), resourceLookup(resourceLookup), varLookup(varLookup) {}
79
80 // Variable insertion.
81 template<typename T>
82 Message& operator()(const T& v) { variables.push_back(msg::to_string(v)); return *this; }
83
84 // conversion to final string
85 operator std::string() const;
86
87 // Get Message Kind
88 msg::Kind GetKind() {return kind;};
89};
90
91
92// Utility functions to create messages with standard resource and data manager lookups.
93// Short names to make usage convenient.
94Message Msg(const char* name);
95Message Msg(msg::Kind kind, const char* name);
96
97#endif