blob: 14df14fc13b45a5986e678e3c21ac43337fd1835 [file] [log] [blame]
that20fb95d2015-09-12 11:27:47 +02001// object.cpp - GUIObject base class
Dees_Troy51a0e822012-09-05 15:24:24 -04002
Dees_Troy51a0e822012-09-05 15:24:24 -04003#include <stdio.h>
4#include <stdlib.h>
Dees_Troy51a0e822012-09-05 15:24:24 -04005
6#include <string>
7
8extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +00009#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040010#include "../variables.h"
11}
12
13#include "rapidxml.hpp"
14#include "objects.hpp"
15#include "../data.hpp"
16
Vojtech Bocekede51c52014-02-07 23:58:09 +010017GUIObject::GUIObject(xml_node<>* node)
Dees_Troy51a0e822012-09-05 15:24:24 -040018{
Vojtech Bocek07220562014-02-08 02:05:33 +010019 mConditionsResult = true;
that20fb95d2015-09-12 11:27:47 +020020 if (node)
21 LoadConditions(node, mConditions);
22}
Vojtech Bocek07220562014-02-08 02:05:33 +010023
that20fb95d2015-09-12 11:27:47 +020024void GUIObject::LoadConditions(xml_node<>* node, std::vector<Condition>& conditions)
25{
Ethan Yonker21ff02a2015-02-18 14:35:00 -060026 xml_node<>* condition = FindNode(node, "conditions");
27 if (condition) condition = FindNode(condition, "condition");
28 else condition = FindNode(node, "condition");
Dees_Troy51a0e822012-09-05 15:24:24 -040029
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020030 while (condition)
31 {
32 Condition cond;
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020034 cond.mCompareOp = "=";
Dees_Troy51a0e822012-09-05 15:24:24 -040035
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020036 xml_attribute<>* attr;
37 attr = condition->first_attribute("var1");
38 if (attr) cond.mVar1 = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040039
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020040 attr = condition->first_attribute("op");
41 if (attr) cond.mCompareOp = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020043 attr = condition->first_attribute("var2");
44 if (attr) cond.mVar2 = attr->value();
Matt Mowerfb1c4ff2014-04-16 13:43:36 -050045
that20fb95d2015-09-12 11:27:47 +020046 conditions.push_back(cond);
Dees_Troy51a0e822012-09-05 15:24:24 -040047
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020048 condition = condition->next_sibling("condition");
49 }
Dees_Troy51a0e822012-09-05 15:24:24 -040050}
51
Vojtech Bocekede51c52014-02-07 23:58:09 +010052GUIObject::~GUIObject()
53{
54}
55
56bool GUIObject::IsConditionVariable(std::string var)
Dees_Troy51a0e822012-09-05 15:24:24 -040057{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020058 std::vector<Condition>::iterator iter;
59 for (iter = mConditions.begin(); iter != mConditions.end(); iter++)
60 {
61 if (iter->mVar1 == var)
62 return true;
63 }
64 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -040065}
66
Vojtech Bocekede51c52014-02-07 23:58:09 +010067bool GUIObject::isConditionTrue()
Dees_Troy51a0e822012-09-05 15:24:24 -040068{
Vojtech Bocek07220562014-02-08 02:05:33 +010069 return mConditionsResult;
Dees_Troy51a0e822012-09-05 15:24:24 -040070}
71
Vojtech Bocekede51c52014-02-07 23:58:09 +010072bool GUIObject::isConditionTrue(Condition* condition)
Dees_Troy51a0e822012-09-05 15:24:24 -040073{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020074 // This is used to hold the proper value of "true" based on the '!' NOT flag
75 bool bTrue = true;
Dees_Troy51a0e822012-09-05 15:24:24 -040076
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 if (condition->mVar1.empty())
78 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -040079
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 if (!condition->mCompareOp.empty() && condition->mCompareOp[0] == '!')
81 bTrue = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040082
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 if (condition->mVar2.empty() && condition->mCompareOp != "modified")
84 {
85 if (!DataManager::GetStrValue(condition->mVar1).empty())
86 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -040087
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 return !bTrue;
89 }
Dees_Troy51a0e822012-09-05 15:24:24 -040090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 string var1, var2;
92 if (DataManager::GetValue(condition->mVar1, var1))
93 var1 = condition->mVar1;
94 if (DataManager::GetValue(condition->mVar2, var2))
95 var2 = condition->mVar2;
Dees_Troy51a0e822012-09-05 15:24:24 -040096
that675ddae2016-02-02 22:12:50 +010097 if (var2.substr(0, 2) == "{@")
98 // translate resource string in value
99 var2 = gui_parse_text(var2);
100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 // This is a special case, we stat the file and that determines our result
102 if (var1 == "fileexists")
103 {
104 struct stat st;
105 if (stat(var2.c_str(), &st) == 0)
106 var2 = var1;
107 else
108 var2 = "FAILED";
109 }
110 if (var1 == "mounted")
111 {
112 if (isMounted(condition->mVar2))
113 var2 = var1;
114 else
115 var2 = "FAILED";
116 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400117
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200118 if (condition->mCompareOp.find('=') != string::npos && var1 == var2)
119 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200121 if (condition->mCompareOp.find('>') != string::npos && (atof(var1.c_str()) > atof(var2.c_str())))
122 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400123
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200124 if (condition->mCompareOp.find('<') != string::npos && (atof(var1.c_str()) < atof(var2.c_str())))
125 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400126
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200127 if (condition->mCompareOp == "modified")
128 {
129 // This is a hack to allow areas to reset the default value
130 if (var1.empty())
131 {
132 condition->mLastVal = var1;
133 return !bTrue;
134 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 if (var1 != condition->mLastVal)
137 return bTrue;
138 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400139
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 return !bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400141}
142
Vojtech Bocekede51c52014-02-07 23:58:09 +0100143bool GUIObject::isConditionValid()
Dees_Troy51a0e822012-09-05 15:24:24 -0400144{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 return !mConditions.empty();
Dees_Troy51a0e822012-09-05 15:24:24 -0400146}
147
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500148int GUIObject::NotifyVarChange(const std::string& varName, const std::string& value __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400149{
that20fb95d2015-09-12 11:27:47 +0200150 mConditionsResult = UpdateConditions(mConditions, varName);
151 return 0;
152}
153
154bool GUIObject::UpdateConditions(std::vector<Condition>& conditions, const std::string& varName)
155{
156 bool result = true;
Vojtech Bocek07220562014-02-08 02:05:33 +0100157
158 const bool varNameEmpty = varName.empty();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200159 std::vector<Condition>::iterator iter;
that20fb95d2015-09-12 11:27:47 +0200160 for (iter = conditions.begin(); iter != conditions.end(); ++iter)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200161 {
Vojtech Bocek07220562014-02-08 02:05:33 +0100162 if(varNameEmpty && iter->mCompareOp == "modified")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 {
164 string val;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500165
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 // If this fails, val will not be set, which is perfect
167 if (DataManager::GetValue(iter->mVar1, val))
168 {
169 DataManager::SetValue(iter->mVar1, "");
170 DataManager::GetValue(iter->mVar1, val);
171 }
172 iter->mLastVal = val;
173 }
Vojtech Bocek07220562014-02-08 02:05:33 +0100174
175 if(varNameEmpty || iter->mVar1 == varName || iter->mVar2 == varName)
176 iter->mLastResult = isConditionTrue(&(*iter));
177
178 if(!iter->mLastResult)
that20fb95d2015-09-12 11:27:47 +0200179 result = false;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200180 }
that20fb95d2015-09-12 11:27:47 +0200181 return result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400182}
183
Vojtech Bocekede51c52014-02-07 23:58:09 +0100184bool GUIObject::isMounted(string vol)
Dees_Troy51a0e822012-09-05 15:24:24 -0400185{
186 FILE *fp;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200187 char tmpOutput[255];
Dees_Troy51a0e822012-09-05 15:24:24 -0400188
Dees_Troy51a0e822012-09-05 15:24:24 -0400189 fp = fopen("/proc/mounts", "rt");
190 while (fgets(tmpOutput,255,fp) != NULL)
191 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200192 char* mnt = tmpOutput;
193 while (*mnt > 32) mnt++;
194 while (*mnt > 0 && *mnt <= 32) mnt++;
195 char* pos = mnt;
196 while (*pos > 32) pos++;
197 *pos = 0;
198 if (vol == mnt)
199 {
200 fclose(fp);
201 return true;
202 }
203 }
204 fclose(fp);
205 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400206}