Cache results of GUIObject::isConditionTrue()

Signed-off-by: Vojtech Bocek <vbocek@gmail.com>

Change-Id: Ia50f7c365b2dc0a65ee046bb42972e3594264878
diff --git a/gui/action.cpp b/gui/action.cpp
index 951feb1..66ee5d6 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -144,16 +144,13 @@
 	return 0;
 }
 
-int GUIAction::NotifyVarChange(std::string varName, std::string value)
+int GUIAction::NotifyVarChange(const std::string& varName, const std::string& value)
 {
+	GUIObject::NotifyVarChange(varName, value);
+
 	if (varName.empty() && !isConditionValid() && !mKey && !mActionW)
 		doActions();
-
-	// This handles notifying the condition system of page start
-	if (varName.empty() && isConditionValid())
-		NotifyPageSet();
-
-	if ((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
+	else if((varName.empty() || IsConditionVariable(varName)) && isConditionValid() && isConditionTrue())
 		doActions();
 
 	return 0;
diff --git a/gui/fileselector.cpp b/gui/fileselector.cpp
index 484bcff..e1570ed 100644
--- a/gui/fileselector.cpp
+++ b/gui/fileselector.cpp
@@ -865,8 +865,10 @@
 	return 0;
 }
 
-int GUIFileSelector::NotifyVarChange(std::string varName, std::string value)
+int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value)
 {
+	GUIObject::NotifyVarChange(varName, value);
+
 	if(!isConditionTrue())
 		return 0;
 
diff --git a/gui/input.cpp b/gui/input.cpp
index b006560..61b0cff 100644
--- a/gui/input.cpp
+++ b/gui/input.cpp
@@ -591,8 +591,10 @@
 	return 0;
 }
 
-int GUIInput::NotifyVarChange(std::string varName, std::string value)
+int GUIInput::NotifyVarChange(const std::string& varName, const std::string& value)
 {
+	GUIObject::NotifyVarChange(varName, value);
+
 	if (varName == mVariable && !isLocalChange) {
 		HandleTextLocation(-1003);
 		return 0;
diff --git a/gui/listbox.cpp b/gui/listbox.cpp
index 99e2ded..bbfc934 100644
--- a/gui/listbox.cpp
+++ b/gui/listbox.cpp
@@ -760,8 +760,10 @@
 	return 0;
 }
 
-int GUIListBox::NotifyVarChange(std::string varName, std::string value)
+int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value)
 {
+	GUIObject::NotifyVarChange(varName, value);
+
 	if(!isConditionTrue())
 		return 0;
 
diff --git a/gui/object.cpp b/gui/object.cpp
index b6010d7..41cc822 100644
--- a/gui/object.cpp
+++ b/gui/object.cpp
@@ -29,6 +29,8 @@
 
 GUIObject::GUIObject(xml_node<>* node)
 {
+	mConditionsResult = true;
+
 	// Break out early, it's too hard to check if valid every step
 	if (!node)		return;
 
@@ -78,13 +80,7 @@
 
 bool GUIObject::isConditionTrue()
 {
-	std::vector<Condition>::iterator iter;
-	for (iter = mConditions.begin(); iter != mConditions.end(); iter++)
-	{
-		if (!isConditionTrue(&(*iter)))
-			return false;
-	}
-	return true;
+	return mConditionsResult;
 }
 
 bool GUIObject::isConditionTrue(Condition* condition)
@@ -159,12 +155,15 @@
 	return !mConditions.empty();
 }
 
-void GUIObject::NotifyPageSet()
+int GUIObject::NotifyVarChange(const std::string& varName, const std::string& value)
 {
+	mConditionsResult = true;
+
+	const bool varNameEmpty = varName.empty();
 	std::vector<Condition>::iterator iter;
-	for (iter = mConditions.begin(); iter != mConditions.end(); iter++)
+	for (iter = mConditions.begin(); iter != mConditions.end(); ++iter)
 	{
-		if (iter->mCompareOp == "modified")
+		if(varNameEmpty && iter->mCompareOp == "modified")
 		{
 			string val;
 	
@@ -176,7 +175,14 @@
 			}
 			iter->mLastVal = val;
 		}
+
+		if(varNameEmpty || iter->mVar1 == varName || iter->mVar2 == varName)
+			iter->mLastResult = isConditionTrue(&(*iter));
+
+		if(!iter->mLastResult)
+			mConditionsResult = false;
 	}
+	return 0;
 }
 
 bool GUIObject::isMounted(string vol)
diff --git a/gui/objects.hpp b/gui/objects.hpp
index 42dfb1f..472d23b 100644
--- a/gui/objects.hpp
+++ b/gui/objects.hpp
@@ -114,10 +114,6 @@
 	//  Return 0 if this object handles the request, 1 if not
 	virtual int IsInRegion(int x, int y) { return ((x < mActionX || x > mActionX + mActionW || y < mActionY || y > mActionY + mActionH) ? 0 : 1); }
 
-	// NotifyVarChange - Notify of a variable change
-	//  Returns 0 on success, <0 on error
-	virtual int NotifyVarChange(std::string varName, std::string value) { return 0; }
-
 protected:
 	int mActionX, mActionY, mActionW, mActionH;
 };
@@ -132,16 +128,24 @@
 	bool IsConditionVariable(std::string var);
 	bool isConditionTrue();
 	bool isConditionValid();
-	void NotifyPageSet();
+
+	// NotifyVarChange - Notify of a variable change
+	//  Returns 0 on success, <0 on error
+	virtual int NotifyVarChange(const std::string& varName, const std::string& value);
 
 protected:
 	class Condition
 	{
 	public:
+		Condition() {
+			mLastResult = true;
+		}
+
 		std::string mVar1;
 		std::string mVar2;
 		std::string mCompareOp;
 		std::string mLastVal;
+		bool mLastResult;
 	};
 
 	std::vector<Condition> mConditions;
@@ -149,6 +153,8 @@
 protected:
 	bool isMounted(std::string vol);
 	bool isConditionTrue(Condition* condition);
+
+	bool mConditionsResult;
 };
 
 class InputObject
@@ -189,7 +195,7 @@
 	virtual int GetCurrentBounds(int& w, int& h);
 
 	// Notify of a variable change
-	virtual int NotifyVarChange(std::string varName, std::string value);
+	virtual int NotifyVarChange(const std::string& varName, const std::string& value);
 
 	// Set maximum width in pixels
 	virtual int SetMaxWidth(unsigned width);
@@ -264,7 +270,7 @@
 public:
 	virtual int NotifyTouch(TOUCH_STATE state, int x, int y);
 	virtual int NotifyKey(int key);
-	virtual int NotifyVarChange(std::string varName, std::string value);
+	virtual int NotifyVarChange(const std::string& varName, const std::string& value);
 	virtual int doActions();
 
 protected:
@@ -441,7 +447,7 @@
 	virtual int NotifyTouch(TOUCH_STATE state, int x, int y);
 
 	// NotifyVarChange - Notify of a variable change
-	virtual int NotifyVarChange(std::string varName, std::string value);
+	virtual int NotifyVarChange(const std::string& varName, const std::string& value);
 
 	// SetPos - Update the position of the render object
 	//  Return 0 on success, <0 on error
@@ -545,7 +551,7 @@
 	virtual int NotifyTouch(TOUCH_STATE state, int x, int y);
 
 	// NotifyVarChange - Notify of a variable change
-	virtual int NotifyVarChange(std::string varName, std::string value);
+	virtual int NotifyVarChange(const std::string& varName, const std::string& value);
 
 	// SetPos - Update the position of the render object
 	//  Return 0 on success, <0 on error
@@ -633,7 +639,7 @@
 	virtual int NotifyTouch(TOUCH_STATE state, int x, int y);
 
 	// NotifyVarChange - Notify of a variable change
-	virtual int NotifyVarChange(std::string varName, std::string value);
+	virtual int NotifyVarChange(const std::string& varName, const std::string& value);
 
 	// SetPos - Update the position of the render object
 	//  Return 0 on success, <0 on error
@@ -737,7 +743,7 @@
 
 	// NotifyVarChange - Notify of a variable change
 	//  Returns 0 on success, <0 on error
-	virtual int NotifyVarChange(std::string varName, std::string value);
+	virtual int NotifyVarChange(const std::string& varName, const std::string& value);
 
 protected:
 	Resource* mEmptyBar;
@@ -854,7 +860,7 @@
 	virtual int Update(void);
 
 	// Notify of a variable change
-	virtual int NotifyVarChange(std::string varName, std::string value);
+	virtual int NotifyVarChange(const std::string& varName, const std::string& value);
 
 	// NotifyTouch - Notify of a touch event
 	//  Return 0 on success, >0 to ignore remainder of touch, and <0 on error
@@ -937,7 +943,7 @@
 	virtual int NotifyTouch(TOUCH_STATE state, int x, int y);
 
 	// Notify of a variable change
-	virtual int NotifyVarChange(std::string varName, std::string value);
+	virtual int NotifyVarChange(const std::string& varName, const std::string& value);
 
 	// SetPageFocus - Notify when a page gains or loses focus
 	virtual void SetPageFocus(int inFocus);
diff --git a/gui/pages.cpp b/gui/pages.cpp
index 73aab03..2953edd 100644
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -519,13 +519,8 @@
 
 int Page::NotifyVarChange(std::string varName, std::string value)
 {
-	std::vector<ActionObject*>::iterator iter;
-
-	// Don't try to handle a lack of handlers
-	if (mActions.size() == 0)
-		return 1;
-
-	for (iter = mActions.begin(); iter != mActions.end(); ++iter)
+	std::vector<GUIObject*>::iterator iter;
+	for (iter = mObjects.begin(); iter != mObjects.end(); ++iter)
 	{
 		if ((*iter)->NotifyVarChange(varName, value))
 			LOGERR("An action handler errored on NotifyVarChange.\n");
@@ -865,7 +860,10 @@
 
 	tmp = FindPackage(name);
 	if (tmp)
+	{
 		mCurrentSet = tmp;
+		mCurrentSet->NotifyVarChange("", "");
+	}
 	else
 		LOGERR("Unable to find package.\n");
 
diff --git a/gui/partitionlist.cpp b/gui/partitionlist.cpp
index cb9011f..064b9df 100644
--- a/gui/partitionlist.cpp
+++ b/gui/partitionlist.cpp
@@ -827,8 +827,10 @@
 	return 0;
 }
 
-int GUIPartitionList::NotifyVarChange(std::string varName, std::string value)
+int GUIPartitionList::NotifyVarChange(const std::string& varName, const std::string& value)
 {
+	GUIObject::NotifyVarChange(varName, value);
+
 	if(!isConditionTrue())
 		return 0;
 
diff --git a/gui/progressbar.cpp b/gui/progressbar.cpp
index 9c80eb4..83de4d1 100644
--- a/gui/progressbar.cpp
+++ b/gui/progressbar.cpp
@@ -172,8 +172,10 @@
 	return 2;
 }
 
-int GUIProgressBar::NotifyVarChange(std::string varName, std::string value)
+int GUIProgressBar::NotifyVarChange(const std::string& varName, const std::string& value)
 {
+	GUIObject::NotifyVarChange(varName, value);
+
 	if(!isConditionTrue())
 		return 0;
 
diff --git a/gui/slidervalue.cpp b/gui/slidervalue.cpp
index c83456b..972d1f7 100644
--- a/gui/slidervalue.cpp
+++ b/gui/slidervalue.cpp
@@ -396,8 +396,10 @@
 	return 0;
 }
 
-int GUISliderValue::NotifyVarChange(std::string varName, std::string value)
+int GUISliderValue::NotifyVarChange(const std::string& varName, const std::string& value)
 {
+	GUIObject::NotifyVarChange(varName, value);
+
 	if (mLabel)
 		mLabel->NotifyVarChange(varName, value);
 	if (varName == mVariable) {
diff --git a/gui/text.cpp b/gui/text.cpp
index 715880b..c594f48 100644
--- a/gui/text.cpp
+++ b/gui/text.cpp
@@ -222,8 +222,10 @@
 	}
 }
 
-int GUIText::NotifyVarChange(std::string varName, std::string value)
+int GUIText::NotifyVarChange(const std::string& varName, const std::string& value)
 {
+	GUIObject::NotifyVarChange(varName, value);
+
 	mVarChanged = 1;
 	return 0;
 }