blob: 543452caff770b69abb75597287777e27c5f7e90 [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
Vojtech Bocekede51c52014-02-07 23:58:09 +010030GUIObject::GUIObject(xml_node<>* node)
Dees_Troy51a0e822012-09-05 15:24:24 -040031{
Vojtech Bocek07220562014-02-08 02:05:33 +010032 mConditionsResult = true;
33
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020034 // Break out early, it's too hard to check if valid every step
35 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040036
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020037 // First, get the action
38 xml_node<>* condition = node->first_node("conditions");
39 if (condition) condition = condition->first_node("condition");
40 else condition = node->first_node("condition");
Dees_Troy51a0e822012-09-05 15:24:24 -040041
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020042 if (!condition) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040043
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020044 while (condition)
45 {
46 Condition cond;
Dees_Troy51a0e822012-09-05 15:24:24 -040047
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020048 cond.mCompareOp = "=";
Dees_Troy51a0e822012-09-05 15:24:24 -040049
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020050 xml_attribute<>* attr;
51 attr = condition->first_attribute("var1");
52 if (attr) cond.mVar1 = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040053
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020054 attr = condition->first_attribute("op");
55 if (attr) cond.mCompareOp = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040056
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020057 attr = condition->first_attribute("var2");
58 if (attr) cond.mVar2 = attr->value();
59
60 mConditions.push_back(cond);
Dees_Troy51a0e822012-09-05 15:24:24 -040061
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020062 condition = condition->next_sibling("condition");
63 }
Dees_Troy51a0e822012-09-05 15:24:24 -040064}
65
Vojtech Bocekede51c52014-02-07 23:58:09 +010066GUIObject::~GUIObject()
67{
68}
69
70bool GUIObject::IsConditionVariable(std::string var)
Dees_Troy51a0e822012-09-05 15:24:24 -040071{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 std::vector<Condition>::iterator iter;
73 for (iter = mConditions.begin(); iter != mConditions.end(); iter++)
74 {
75 if (iter->mVar1 == var)
76 return true;
77 }
78 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -040079}
80
Vojtech Bocekede51c52014-02-07 23:58:09 +010081bool GUIObject::isConditionTrue()
Dees_Troy51a0e822012-09-05 15:24:24 -040082{
Vojtech Bocek07220562014-02-08 02:05:33 +010083 return mConditionsResult;
Dees_Troy51a0e822012-09-05 15:24:24 -040084}
85
Vojtech Bocekede51c52014-02-07 23:58:09 +010086bool GUIObject::isConditionTrue(Condition* condition)
Dees_Troy51a0e822012-09-05 15:24:24 -040087{
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
Vojtech Bocekede51c52014-02-07 23:58:09 +0100153bool GUIObject::isConditionValid()
Dees_Troy51a0e822012-09-05 15:24:24 -0400154{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 return !mConditions.empty();
Dees_Troy51a0e822012-09-05 15:24:24 -0400156}
157
Vojtech Bocek07220562014-02-08 02:05:33 +0100158int GUIObject::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400159{
Vojtech Bocek07220562014-02-08 02:05:33 +0100160 mConditionsResult = true;
161
162 const bool varNameEmpty = varName.empty();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 std::vector<Condition>::iterator iter;
Vojtech Bocek07220562014-02-08 02:05:33 +0100164 for (iter = mConditions.begin(); iter != mConditions.end(); ++iter)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200165 {
Vojtech Bocek07220562014-02-08 02:05:33 +0100166 if(varNameEmpty && iter->mCompareOp == "modified")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200167 {
168 string val;
169
170 // If this fails, val will not be set, which is perfect
171 if (DataManager::GetValue(iter->mVar1, val))
172 {
173 DataManager::SetValue(iter->mVar1, "");
174 DataManager::GetValue(iter->mVar1, val);
175 }
176 iter->mLastVal = val;
177 }
Vojtech Bocek07220562014-02-08 02:05:33 +0100178
179 if(varNameEmpty || iter->mVar1 == varName || iter->mVar2 == varName)
180 iter->mLastResult = isConditionTrue(&(*iter));
181
182 if(!iter->mLastResult)
183 mConditionsResult = false;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 }
Vojtech Bocek07220562014-02-08 02:05:33 +0100185 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400186}
187
Vojtech Bocekede51c52014-02-07 23:58:09 +0100188bool GUIObject::isMounted(string vol)
Dees_Troy51a0e822012-09-05 15:24:24 -0400189{
190 FILE *fp;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 char tmpOutput[255];
Dees_Troy51a0e822012-09-05 15:24:24 -0400192
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 fp = fopen("/proc/mounts", "rt");
194 while (fgets(tmpOutput,255,fp) != NULL)
195 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200196 char* mnt = tmpOutput;
197 while (*mnt > 32) mnt++;
198 while (*mnt > 0 && *mnt <= 32) mnt++;
199 char* pos = mnt;
200 while (*pos > 32) pos++;
201 *pos = 0;
202 if (vol == mnt)
203 {
204 fclose(fp);
205 return true;
206 }
207 }
208 fclose(fp);
209 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400210}