Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 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 | |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 19 | // object.cpp - GUIObject base class |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 20 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 23 | #include <sys/stat.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 24 | #include <string> |
| 25 | |
| 26 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 27 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 28 | #include "../variables.h" |
| 29 | } |
| 30 | |
| 31 | #include "rapidxml.hpp" |
| 32 | #include "objects.hpp" |
| 33 | #include "../data.hpp" |
| 34 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 35 | GUIObject::GUIObject(xml_node<>* node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 37 | mConditionsResult = true; |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 38 | if (node) |
| 39 | LoadConditions(node, mConditions); |
| 40 | } |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 41 | |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 42 | void GUIObject::LoadConditions(xml_node<>* node, std::vector<Condition>& conditions) |
| 43 | { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 44 | xml_node<>* condition = FindNode(node, "conditions"); |
| 45 | if (condition) condition = FindNode(condition, "condition"); |
| 46 | else condition = FindNode(node, "condition"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 48 | while (condition) |
| 49 | { |
| 50 | Condition cond; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 52 | cond.mCompareOp = "="; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 54 | xml_attribute<>* attr; |
| 55 | attr = condition->first_attribute("var1"); |
| 56 | if (attr) cond.mVar1 = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 57 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 58 | attr = condition->first_attribute("op"); |
| 59 | if (attr) cond.mCompareOp = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 60 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 61 | attr = condition->first_attribute("var2"); |
| 62 | if (attr) cond.mVar2 = attr->value(); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 63 | |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 64 | conditions.push_back(cond); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 65 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 66 | condition = condition->next_sibling("condition"); |
| 67 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 70 | GUIObject::~GUIObject() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | bool GUIObject::IsConditionVariable(std::string var) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 76 | std::vector<Condition>::iterator iter; |
| 77 | for (iter = mConditions.begin(); iter != mConditions.end(); iter++) |
| 78 | { |
| 79 | if (iter->mVar1 == var) |
| 80 | return true; |
| 81 | } |
| 82 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 85 | bool GUIObject::isConditionTrue() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 86 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 87 | return mConditionsResult; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 88 | } |
| 89 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 90 | bool GUIObject::isConditionTrue(Condition* condition) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 91 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 92 | // This is used to hold the proper value of "true" based on the '!' NOT flag |
| 93 | bool bTrue = true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 94 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 95 | if (condition->mVar1.empty()) |
| 96 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 97 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 98 | if (!condition->mCompareOp.empty() && condition->mCompareOp[0] == '!') |
| 99 | bTrue = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 100 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 101 | if (condition->mVar2.empty() && condition->mCompareOp != "modified") |
| 102 | { |
| 103 | if (!DataManager::GetStrValue(condition->mVar1).empty()) |
| 104 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 105 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 106 | return !bTrue; |
| 107 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 108 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 109 | string var1, var2; |
| 110 | if (DataManager::GetValue(condition->mVar1, var1)) |
| 111 | var1 = condition->mVar1; |
| 112 | if (DataManager::GetValue(condition->mVar2, var2)) |
| 113 | var2 = condition->mVar2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 114 | |
that | 675ddae | 2016-02-02 22:12:50 +0100 | [diff] [blame] | 115 | if (var2.substr(0, 2) == "{@") |
| 116 | // translate resource string in value |
| 117 | var2 = gui_parse_text(var2); |
| 118 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 119 | // This is a special case, we stat the file and that determines our result |
| 120 | if (var1 == "fileexists") |
| 121 | { |
| 122 | struct stat st; |
| 123 | if (stat(var2.c_str(), &st) == 0) |
| 124 | var2 = var1; |
| 125 | else |
| 126 | var2 = "FAILED"; |
| 127 | } |
| 128 | if (var1 == "mounted") |
| 129 | { |
| 130 | if (isMounted(condition->mVar2)) |
| 131 | var2 = var1; |
| 132 | else |
| 133 | var2 = "FAILED"; |
| 134 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 135 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 136 | if (condition->mCompareOp.find('=') != string::npos && var1 == var2) |
| 137 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 138 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 139 | if (condition->mCompareOp.find('>') != string::npos && (atof(var1.c_str()) > atof(var2.c_str()))) |
| 140 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 141 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 142 | if (condition->mCompareOp.find('<') != string::npos && (atof(var1.c_str()) < atof(var2.c_str()))) |
| 143 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 144 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 145 | if (condition->mCompareOp == "modified") |
| 146 | { |
| 147 | // This is a hack to allow areas to reset the default value |
| 148 | if (var1.empty()) |
| 149 | { |
| 150 | condition->mLastVal = var1; |
| 151 | return !bTrue; |
| 152 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 153 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 154 | if (var1 != condition->mLastVal) |
| 155 | return bTrue; |
| 156 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 157 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 158 | return !bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 161 | bool GUIObject::isConditionValid() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 162 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 163 | return !mConditions.empty(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 166 | int GUIObject::NotifyVarChange(const std::string& varName, const std::string& value __unused) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 167 | { |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 168 | mConditionsResult = UpdateConditions(mConditions, varName); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | bool GUIObject::UpdateConditions(std::vector<Condition>& conditions, const std::string& varName) |
| 173 | { |
| 174 | bool result = true; |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 175 | |
| 176 | const bool varNameEmpty = varName.empty(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 177 | std::vector<Condition>::iterator iter; |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 178 | for (iter = conditions.begin(); iter != conditions.end(); ++iter) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 179 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 180 | if (varNameEmpty && iter->mCompareOp == "modified") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 181 | { |
| 182 | string val; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 183 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 184 | // If this fails, val will not be set, which is perfect |
| 185 | if (DataManager::GetValue(iter->mVar1, val)) |
| 186 | { |
| 187 | DataManager::SetValue(iter->mVar1, ""); |
| 188 | DataManager::GetValue(iter->mVar1, val); |
| 189 | } |
| 190 | iter->mLastVal = val; |
| 191 | } |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 192 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 193 | if (varNameEmpty || iter->mVar1 == varName || iter->mVar2 == varName) |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 194 | iter->mLastResult = isConditionTrue(&(*iter)); |
| 195 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 196 | if (!iter->mLastResult) |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 197 | result = false; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 198 | } |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 199 | return result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 202 | bool GUIObject::isMounted(string vol) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 203 | { |
| 204 | FILE *fp; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 205 | char tmpOutput[255]; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 206 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 207 | fp = fopen("/proc/mounts", "rt"); |
| 208 | while (fgets(tmpOutput,255,fp) != NULL) |
| 209 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 210 | char* mnt = tmpOutput; |
| 211 | while (*mnt > 32) mnt++; |
| 212 | while (*mnt > 0 && *mnt <= 32) mnt++; |
| 213 | char* pos = mnt; |
| 214 | while (*pos > 32) pos++; |
| 215 | *pos = 0; |
| 216 | if (vol == mnt) |
| 217 | { |
| 218 | fclose(fp); |
| 219 | return true; |
| 220 | } |
| 221 | } |
| 222 | fclose(fp); |
| 223 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 224 | } |