Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // checkbox.cpp - GUICheckbox object |
| 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/reboot.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/ioctl.h> |
| 14 | #include <time.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <string> |
| 19 | |
| 20 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 21 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | #include "../variables.h" |
| 24 | } |
| 25 | |
| 26 | #include "rapidxml.hpp" |
| 27 | #include "objects.hpp" |
| 28 | #include "../data.hpp" |
| 29 | |
| 30 | Conditional::Conditional(xml_node<>* node) |
| 31 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 32 | // Break out early, it's too hard to check if valid every step |
| 33 | if (!node) return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 35 | // First, get the action |
| 36 | xml_node<>* condition = node->first_node("conditions"); |
| 37 | if (condition) condition = condition->first_node("condition"); |
| 38 | else condition = node->first_node("condition"); |
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 | if (!condition) return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 42 | while (condition) |
| 43 | { |
| 44 | Condition cond; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 45 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 46 | cond.mCompareOp = "="; |
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 | xml_attribute<>* attr; |
| 49 | attr = condition->first_attribute("var1"); |
| 50 | if (attr) cond.mVar1 = attr->value(); |
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 | attr = condition->first_attribute("op"); |
| 53 | if (attr) cond.mCompareOp = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 55 | attr = condition->first_attribute("var2"); |
| 56 | if (attr) cond.mVar2 = attr->value(); |
| 57 | |
| 58 | mConditions.push_back(cond); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 59 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 60 | condition = condition->next_sibling("condition"); |
| 61 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool Conditional::IsConditionVariable(std::string var) |
| 65 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 66 | std::vector<Condition>::iterator iter; |
| 67 | for (iter = mConditions.begin(); iter != mConditions.end(); iter++) |
| 68 | { |
| 69 | if (iter->mVar1 == var) |
| 70 | return true; |
| 71 | } |
| 72 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool Conditional::isConditionTrue() |
| 76 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 77 | std::vector<Condition>::iterator iter; |
| 78 | for (iter = mConditions.begin(); iter != mConditions.end(); iter++) |
| 79 | { |
| 80 | if (!isConditionTrue(&(*iter))) |
| 81 | return false; |
| 82 | } |
| 83 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool Conditional::isConditionTrue(Condition* condition) |
| 87 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 88 | // This is used to hold the proper value of "true" based on the '!' NOT flag |
| 89 | bool bTrue = true; |
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 | if (condition->mVar1.empty()) |
| 92 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 93 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 94 | if (!condition->mCompareOp.empty() && condition->mCompareOp[0] == '!') |
| 95 | bTrue = false; |
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 | if (condition->mVar2.empty() && condition->mCompareOp != "modified") |
| 98 | { |
| 99 | if (!DataManager::GetStrValue(condition->mVar1).empty()) |
| 100 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 101 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 102 | return !bTrue; |
| 103 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 104 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 105 | string var1, var2; |
| 106 | if (DataManager::GetValue(condition->mVar1, var1)) |
| 107 | var1 = condition->mVar1; |
| 108 | if (DataManager::GetValue(condition->mVar2, var2)) |
| 109 | var2 = condition->mVar2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 110 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 111 | // This is a special case, we stat the file and that determines our result |
| 112 | if (var1 == "fileexists") |
| 113 | { |
| 114 | struct stat st; |
| 115 | if (stat(var2.c_str(), &st) == 0) |
| 116 | var2 = var1; |
| 117 | else |
| 118 | var2 = "FAILED"; |
| 119 | } |
| 120 | if (var1 == "mounted") |
| 121 | { |
| 122 | if (isMounted(condition->mVar2)) |
| 123 | var2 = var1; |
| 124 | else |
| 125 | var2 = "FAILED"; |
| 126 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 127 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 128 | if (condition->mCompareOp.find('=') != string::npos && var1 == var2) |
| 129 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 130 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 131 | if (condition->mCompareOp.find('>') != string::npos && (atof(var1.c_str()) > atof(var2.c_str()))) |
| 132 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 133 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 134 | if (condition->mCompareOp.find('<') != string::npos && (atof(var1.c_str()) < atof(var2.c_str()))) |
| 135 | return bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 136 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 137 | if (condition->mCompareOp == "modified") |
| 138 | { |
| 139 | // This is a hack to allow areas to reset the default value |
| 140 | if (var1.empty()) |
| 141 | { |
| 142 | condition->mLastVal = var1; |
| 143 | return !bTrue; |
| 144 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 146 | if (var1 != condition->mLastVal) |
| 147 | return bTrue; |
| 148 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 149 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 150 | return !bTrue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool Conditional::isConditionValid() |
| 154 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 155 | return !mConditions.empty(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void Conditional::NotifyPageSet() |
| 159 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 160 | std::vector<Condition>::iterator iter; |
| 161 | for (iter = mConditions.begin(); iter != mConditions.end(); iter++) |
| 162 | { |
| 163 | if (iter->mCompareOp == "modified") |
| 164 | { |
| 165 | string val; |
| 166 | |
| 167 | // If this fails, val will not be set, which is perfect |
| 168 | if (DataManager::GetValue(iter->mVar1, val)) |
| 169 | { |
| 170 | DataManager::SetValue(iter->mVar1, ""); |
| 171 | DataManager::GetValue(iter->mVar1, val); |
| 172 | } |
| 173 | iter->mLastVal = val; |
| 174 | } |
| 175 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | bool Conditional::isMounted(string vol) |
| 179 | { |
| 180 | FILE *fp; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 181 | char tmpOutput[255]; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 182 | |
| 183 | if (strcmp(vol.c_str(), "EXTERNAL") == 0) |
| 184 | DataManager::GetValue(TW_EXTERNAL_MOUNT, vol); |
| 185 | else if (strcmp(vol.c_str(), "INTERNAL") == 0) |
| 186 | DataManager::GetValue(TW_INTERNAL_MOUNT, vol); |
| 187 | fp = fopen("/proc/mounts", "rt"); |
| 188 | while (fgets(tmpOutput,255,fp) != NULL) |
| 189 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 190 | char* mnt = tmpOutput; |
| 191 | while (*mnt > 32) mnt++; |
| 192 | while (*mnt > 0 && *mnt <= 32) mnt++; |
| 193 | char* pos = mnt; |
| 194 | while (*pos > 32) pos++; |
| 195 | *pos = 0; |
| 196 | if (vol == mnt) |
| 197 | { |
| 198 | fclose(fp); |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | fclose(fp); |
| 203 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 204 | } |