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> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 5 | |
| 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 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 97 | // This is a special case, we stat the file and that determines our result |
| 98 | if (var1 == "fileexists") |
| 99 | { |
| 100 | struct stat st; |
| 101 | if (stat(var2.c_str(), &st) == 0) |
| 102 | var2 = var1; |
| 103 | else |
| 104 | var2 = "FAILED"; |
| 105 | } |
| 106 | if (var1 == "mounted") |
| 107 | { |
| 108 | if (isMounted(condition->mVar2)) |
| 109 | var2 = var1; |
| 110 | else |
| 111 | var2 = "FAILED"; |
| 112 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 113 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 114 | if (condition->mCompareOp.find('=') != string::npos && var1 == var2) |
| 115 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 116 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 117 | if (condition->mCompareOp.find('>') != string::npos && (atof(var1.c_str()) > atof(var2.c_str()))) |
| 118 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 120 | if (condition->mCompareOp.find('<') != string::npos && (atof(var1.c_str()) < atof(var2.c_str()))) |
| 121 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 123 | if (condition->mCompareOp == "modified") |
| 124 | { |
| 125 | // This is a hack to allow areas to reset the default value |
| 126 | if (var1.empty()) |
| 127 | { |
| 128 | condition->mLastVal = var1; |
| 129 | return !bTrue; |
| 130 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 131 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 132 | if (var1 != condition->mLastVal) |
| 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 | return !bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 139 | bool GUIObject::isConditionValid() |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 140 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 141 | return !mConditions.empty(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 144 | int GUIObject::NotifyVarChange(const std::string& varName, const std::string& value __unused) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | { |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 146 | mConditionsResult = UpdateConditions(mConditions, varName); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | bool GUIObject::UpdateConditions(std::vector<Condition>& conditions, const std::string& varName) |
| 151 | { |
| 152 | bool result = true; |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 153 | |
| 154 | const bool varNameEmpty = varName.empty(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 155 | std::vector<Condition>::iterator iter; |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 156 | for (iter = conditions.begin(); iter != conditions.end(); ++iter) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 157 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 158 | if(varNameEmpty && iter->mCompareOp == "modified") |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 159 | { |
| 160 | string val; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 161 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 162 | // If this fails, val will not be set, which is perfect |
| 163 | if (DataManager::GetValue(iter->mVar1, val)) |
| 164 | { |
| 165 | DataManager::SetValue(iter->mVar1, ""); |
| 166 | DataManager::GetValue(iter->mVar1, val); |
| 167 | } |
| 168 | iter->mLastVal = val; |
| 169 | } |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 170 | |
| 171 | if(varNameEmpty || iter->mVar1 == varName || iter->mVar2 == varName) |
| 172 | iter->mLastResult = isConditionTrue(&(*iter)); |
| 173 | |
| 174 | if(!iter->mLastResult) |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 175 | result = false; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 176 | } |
that | 20fb95d | 2015-09-12 11:27:47 +0200 | [diff] [blame] | 177 | return result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 178 | } |
| 179 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 180 | bool GUIObject::isMounted(string vol) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 181 | { |
| 182 | FILE *fp; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 183 | char tmpOutput[255]; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 184 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 185 | fp = fopen("/proc/mounts", "rt"); |
| 186 | while (fgets(tmpOutput,255,fp) != NULL) |
| 187 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 188 | char* mnt = tmpOutput; |
| 189 | while (*mnt > 32) mnt++; |
| 190 | while (*mnt > 0 && *mnt <= 32) mnt++; |
| 191 | char* pos = mnt; |
| 192 | while (*pos > 32) pos++; |
| 193 | *pos = 0; |
| 194 | if (vol == mnt) |
| 195 | { |
| 196 | fclose(fp); |
| 197 | return true; |
| 198 | } |
| 199 | } |
| 200 | fclose(fp); |
| 201 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 202 | } |