blob: 206f2c7261d8c02da08351f65b470cfbef537ecf [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
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097 // This is a special case, we stat the file and that determines our result
98 if (var1 == "fileexists")
99 {
100 struct stat st;
101 if (stat(var2.c_str(), &st) == 0)
102 var2 = var1;
103 else
104 var2 = "FAILED";
105 }
106 if (var1 == "mounted")
107 {
108 if (isMounted(condition->mVar2))
109 var2 = var1;
110 else
111 var2 = "FAILED";
112 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400113
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114 if (condition->mCompareOp.find('=') != string::npos && var1 == var2)
115 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 if (condition->mCompareOp.find('>') != string::npos && (atof(var1.c_str()) > atof(var2.c_str())))
118 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 if (condition->mCompareOp.find('<') != string::npos && (atof(var1.c_str()) < atof(var2.c_str())))
121 return bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400122
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200123 if (condition->mCompareOp == "modified")
124 {
125 // This is a hack to allow areas to reset the default value
126 if (var1.empty())
127 {
128 condition->mLastVal = var1;
129 return !bTrue;
130 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400131
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (var1 != condition->mLastVal)
133 return bTrue;
134 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 return !bTrue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400137}
138
Vojtech Bocekede51c52014-02-07 23:58:09 +0100139bool GUIObject::isConditionValid()
Dees_Troy51a0e822012-09-05 15:24:24 -0400140{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 return !mConditions.empty();
Dees_Troy51a0e822012-09-05 15:24:24 -0400142}
143
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500144int GUIObject::NotifyVarChange(const std::string& varName, const std::string& value __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400145{
that20fb95d2015-09-12 11:27:47 +0200146 mConditionsResult = UpdateConditions(mConditions, varName);
147 return 0;
148}
149
150bool GUIObject::UpdateConditions(std::vector<Condition>& conditions, const std::string& varName)
151{
152 bool result = true;
Vojtech Bocek07220562014-02-08 02:05:33 +0100153
154 const bool varNameEmpty = varName.empty();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 std::vector<Condition>::iterator iter;
that20fb95d2015-09-12 11:27:47 +0200156 for (iter = conditions.begin(); iter != conditions.end(); ++iter)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 {
Vojtech Bocek07220562014-02-08 02:05:33 +0100158 if(varNameEmpty && iter->mCompareOp == "modified")
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200159 {
160 string val;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500161
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200162 // If this fails, val will not be set, which is perfect
163 if (DataManager::GetValue(iter->mVar1, val))
164 {
165 DataManager::SetValue(iter->mVar1, "");
166 DataManager::GetValue(iter->mVar1, val);
167 }
168 iter->mLastVal = val;
169 }
Vojtech Bocek07220562014-02-08 02:05:33 +0100170
171 if(varNameEmpty || iter->mVar1 == varName || iter->mVar2 == varName)
172 iter->mLastResult = isConditionTrue(&(*iter));
173
174 if(!iter->mLastResult)
that20fb95d2015-09-12 11:27:47 +0200175 result = false;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200176 }
that20fb95d2015-09-12 11:27:47 +0200177 return result;
Dees_Troy51a0e822012-09-05 15:24:24 -0400178}
179
Vojtech Bocekede51c52014-02-07 23:58:09 +0100180bool GUIObject::isMounted(string vol)
Dees_Troy51a0e822012-09-05 15:24:24 -0400181{
182 FILE *fp;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200183 char tmpOutput[255];
Dees_Troy51a0e822012-09-05 15:24:24 -0400184
Dees_Troy51a0e822012-09-05 15:24:24 -0400185 fp = fopen("/proc/mounts", "rt");
186 while (fgets(tmpOutput,255,fp) != NULL)
187 {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200188 char* mnt = tmpOutput;
189 while (*mnt > 32) mnt++;
190 while (*mnt > 0 && *mnt <= 32) mnt++;
191 char* pos = mnt;
192 while (*pos > 32) pos++;
193 *pos = 0;
194 if (vol == mnt)
195 {
196 fclose(fp);
197 return true;
198 }
199 }
200 fclose(fp);
201 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400202}