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