blob: 5ea8d176c24d9cfcae21dbd69c2cc0884f838018 [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{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020032 // Break out early, it's too hard to check if valid every step
33 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040034
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020035 // 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_Troy51a0e822012-09-05 15:24:24 -040039
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020040 if (!condition) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040041
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020042 while (condition)
43 {
44 Condition cond;
Dees_Troy51a0e822012-09-05 15:24:24 -040045
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020046 cond.mCompareOp = "=";
Dees_Troy51a0e822012-09-05 15:24:24 -040047
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020048 xml_attribute<>* attr;
49 attr = condition->first_attribute("var1");
50 if (attr) cond.mVar1 = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040051
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 attr = condition->first_attribute("op");
53 if (attr) cond.mCompareOp = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040054
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020055 attr = condition->first_attribute("var2");
56 if (attr) cond.mVar2 = attr->value();
57
58 mConditions.push_back(cond);
Dees_Troy51a0e822012-09-05 15:24:24 -040059
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060 condition = condition->next_sibling("condition");
61 }
Dees_Troy51a0e822012-09-05 15:24:24 -040062}
63
64bool Conditional::IsConditionVariable(std::string var)
65{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 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_Troy51a0e822012-09-05 15:24:24 -040073}
74
75bool Conditional::isConditionTrue()
76{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 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_Troy51a0e822012-09-05 15:24:24 -040084}
85
86bool Conditional::isConditionTrue(Condition* condition)
87{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 // This is used to hold the proper value of "true" based on the '!' NOT flag
89 bool bTrue = true;
Dees_Troy51a0e822012-09-05 15:24:24 -040090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 if (condition->mVar1.empty())
92 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -040093
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 if (!condition->mCompareOp.empty() && condition->mCompareOp[0] == '!')
95 bTrue = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040096
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097 if (condition->mVar2.empty() && condition->mCompareOp != "modified")
98 {
99 if (!DataManager::GetStrValue(condition->mVar1).empty())
100 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400101
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200102 return !bTrue;
103 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400104
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105 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_Troy51a0e822012-09-05 15:24:24 -0400110
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200111 // 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_Troy51a0e822012-09-05 15:24:24 -0400127
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200128 if (condition->mCompareOp.find('=') != string::npos && var1 == var2)
129 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400130
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200131 if (condition->mCompareOp.find('>') != string::npos && (atof(var1.c_str()) > atof(var2.c_str())))
132 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200134 if (condition->mCompareOp.find('<') != string::npos && (atof(var1.c_str()) < atof(var2.c_str())))
135 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137 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_Troy51a0e822012-09-05 15:24:24 -0400145
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 if (var1 != condition->mLastVal)
147 return bTrue;
148 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400149
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 return !bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400151}
152
153bool Conditional::isConditionValid()
154{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 return !mConditions.empty();
Dees_Troy51a0e822012-09-05 15:24:24 -0400156}
157
158void Conditional::NotifyPageSet()
159{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 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_Troy51a0e822012-09-05 15:24:24 -0400176}
177
178bool Conditional::isMounted(string vol)
179{
180 FILE *fp;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200181 char tmpOutput[255];
Dees_Troy51a0e822012-09-05 15:24:24 -0400182
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 Bocekfafb0c52013-07-25 22:53:02 +0200190 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_Troy51a0e822012-09-05 15:24:24 -0400204}