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