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