blob: b6010d778de468c4e3dffa1e2f348a3b90d0b262 [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 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
Vojtech Bocekede51c52014-02-07 23:58:09 +010064GUIObject::~GUIObject()
65{
66}
67
68bool GUIObject::IsConditionVariable(std::string var)
Dees_Troy51a0e822012-09-05 15:24:24 -040069{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020070 std::vector<Condition>::iterator iter;
71 for (iter = mConditions.begin(); iter != mConditions.end(); iter++)
72 {
73 if (iter->mVar1 == var)
74 return true;
75 }
76 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -040077}
78
Vojtech Bocekede51c52014-02-07 23:58:09 +010079bool GUIObject::isConditionTrue()
Dees_Troy51a0e822012-09-05 15:24:24 -040080{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020081 std::vector<Condition>::iterator iter;
82 for (iter = mConditions.begin(); iter != mConditions.end(); iter++)
83 {
84 if (!isConditionTrue(&(*iter)))
85 return false;
86 }
87 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -040088}
89
Vojtech Bocekede51c52014-02-07 23:58:09 +010090bool GUIObject::isConditionTrue(Condition* condition)
Dees_Troy51a0e822012-09-05 15:24:24 -040091{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 // This is used to hold the proper value of "true" based on the '!' NOT flag
93 bool bTrue = true;
Dees_Troy51a0e822012-09-05 15:24:24 -040094
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 if (condition->mVar1.empty())
96 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -040097
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 if (!condition->mCompareOp.empty() && condition->mCompareOp[0] == '!')
99 bTrue = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 if (condition->mVar2.empty() && condition->mCompareOp != "modified")
102 {
103 if (!DataManager::GetStrValue(condition->mVar1).empty())
104 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400105
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200106 return !bTrue;
107 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400108
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200109 string var1, var2;
110 if (DataManager::GetValue(condition->mVar1, var1))
111 var1 = condition->mVar1;
112 if (DataManager::GetValue(condition->mVar2, var2))
113 var2 = condition->mVar2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400114
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 // This is a special case, we stat the file and that determines our result
116 if (var1 == "fileexists")
117 {
118 struct stat st;
119 if (stat(var2.c_str(), &st) == 0)
120 var2 = var1;
121 else
122 var2 = "FAILED";
123 }
124 if (var1 == "mounted")
125 {
126 if (isMounted(condition->mVar2))
127 var2 = var1;
128 else
129 var2 = "FAILED";
130 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400131
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (condition->mCompareOp.find('=') != string::npos && var1 == var2)
133 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 if (condition->mCompareOp.find('>') != string::npos && (atof(var1.c_str()) > atof(var2.c_str())))
136 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400137
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 if (condition->mCompareOp.find('<') != string::npos && (atof(var1.c_str()) < atof(var2.c_str())))
139 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400140
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 if (condition->mCompareOp == "modified")
142 {
143 // This is a hack to allow areas to reset the default value
144 if (var1.empty())
145 {
146 condition->mLastVal = var1;
147 return !bTrue;
148 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400149
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 if (var1 != condition->mLastVal)
151 return bTrue;
152 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400153
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 return !bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400155}
156
Vojtech Bocekede51c52014-02-07 23:58:09 +0100157bool GUIObject::isConditionValid()
Dees_Troy51a0e822012-09-05 15:24:24 -0400158{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200159 return !mConditions.empty();
Dees_Troy51a0e822012-09-05 15:24:24 -0400160}
161
Vojtech Bocekede51c52014-02-07 23:58:09 +0100162void GUIObject::NotifyPageSet()
Dees_Troy51a0e822012-09-05 15:24:24 -0400163{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200164 std::vector<Condition>::iterator iter;
165 for (iter = mConditions.begin(); iter != mConditions.end(); iter++)
166 {
167 if (iter->mCompareOp == "modified")
168 {
169 string val;
170
171 // If this fails, val will not be set, which is perfect
172 if (DataManager::GetValue(iter->mVar1, val))
173 {
174 DataManager::SetValue(iter->mVar1, "");
175 DataManager::GetValue(iter->mVar1, val);
176 }
177 iter->mLastVal = val;
178 }
179 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400180}
181
Vojtech Bocekede51c52014-02-07 23:58:09 +0100182bool GUIObject::isMounted(string vol)
Dees_Troy51a0e822012-09-05 15:24:24 -0400183{
184 FILE *fp;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200185 char tmpOutput[255];
Dees_Troy51a0e822012-09-05 15:24:24 -0400186
187 if (strcmp(vol.c_str(), "EXTERNAL") == 0)
188 DataManager::GetValue(TW_EXTERNAL_MOUNT, vol);
189 else if (strcmp(vol.c_str(), "INTERNAL") == 0)
190 DataManager::GetValue(TW_INTERNAL_MOUNT, vol);
191 fp = fopen("/proc/mounts", "rt");
192 while (fgets(tmpOutput,255,fp) != NULL)
193 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200194 char* mnt = tmpOutput;
195 while (*mnt > 32) mnt++;
196 while (*mnt > 0 && *mnt <= 32) mnt++;
197 char* pos = mnt;
198 while (*pos > 32) pos++;
199 *pos = 0;
200 if (vol == mnt)
201 {
202 fclose(fp);
203 return true;
204 }
205 }
206 fclose(fp);
207 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400208}