Merge "Improve ORS scanning of storage locations" into android-4.4
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 e402074..61b0cff 100644
--- a/gui/input.cpp
+++ b/gui/input.cpp
@@ -216,7 +216,6 @@
 	if (mInputText)	 	delete mInputText;
 	if (mBackground)	delete mBackground;
 	if (mCursor)		delete mCursor;
-	if (mFont)			delete mFont;
 	if (mAction)		delete mAction;
 }
 
@@ -592,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/keyboard.cpp b/gui/keyboard.cpp
index f08d714..78e27a1 100644
--- a/gui/keyboard.cpp
+++ b/gui/keyboard.cpp
@@ -304,10 +304,7 @@
 
 GUIKeyboard::~GUIKeyboard()
 {
-	int layoutindex;
 
-	for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++)
-		if (keyboardImg[layoutindex])   delete keyboardImg[layoutindex];
 }
 
 int GUIKeyboard::Render(void)
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 dc9edc7..2953edd 100644
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -193,6 +193,12 @@
 	return;
 }
 
+Page::~Page()
+{
+	for (std::vector<GUIObject*>::iterator itr = mObjects.begin(); itr != mObjects.end(); ++itr)
+		delete *itr;
+}
+
 bool Page::ProcessNode(xml_node<>* page, xml_node<>* templates /* = NULL */, int depth /* = 0 */)
 {
 	if (depth == 10)
@@ -225,86 +231,101 @@
 		if (type == "text")
 		{
 			GUIText* element = new GUIText(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "image")
 		{
 			GUIImage* element = new GUIImage(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 		}
 		else if (type == "fill")
 		{
 			GUIFill* element = new GUIFill(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 		}
 		else if (type == "action")
 		{
 			GUIAction* element = new GUIAction(child);
+			mObjects.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "console")
 		{
 			GUIConsole* element = new GUIConsole(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "button")
 		{
 			GUIButton* element = new GUIButton(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "checkbox")
 		{
 			GUICheckbox* element = new GUICheckbox(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "fileselector")
 		{
 			GUIFileSelector* element = new GUIFileSelector(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "animation")
 		{
 			GUIAnimation* element = new GUIAnimation(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 		}
 		else if (type == "progressbar")
 		{
 			GUIProgressBar* element = new GUIProgressBar(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "slider")
 		{
 			GUISlider* element = new GUISlider(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "slidervalue")
 		{
 			GUISliderValue *element = new GUISliderValue(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "listbox")
 		{
 			GUIListBox* element = new GUIListBox(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "keyboard")
 		{
 			GUIKeyboard* element = new GUIKeyboard(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
 		else if (type == "input")
 		{
 			GUIInput* element = new GUIInput(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 			mInputs.push_back(element);
@@ -312,6 +333,7 @@
 		else if (type == "partitionlist")
 		{
 			GUIPartitionList* element = new GUIPartitionList(child);
+			mObjects.push_back(element);
 			mRenders.push_back(element);
 			mActions.push_back(element);
 		}
@@ -497,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");
@@ -526,6 +543,9 @@
 
 PageSet::~PageSet()
 {
+	for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr)
+		delete *itr;
+
 	delete mResources;
 	free(mXmlFile);
 }
@@ -840,7 +860,10 @@
 
 	tmp = FindPackage(name);
 	if (tmp)
+	{
 		mCurrentSet = tmp;
+		mCurrentSet->NotifyVarChange("", "");
+	}
 	else
 		LOGERR("Unable to find package.\n");
 
@@ -869,6 +892,8 @@
 	}
 	if (mCurrentSet == set)
 		SelectPackage(name);
+	if (mBaseSet == set)
+		mBaseSet = mCurrentSet;
 	delete set;
 	return 0;
 }
diff --git a/gui/pages.hpp b/gui/pages.hpp
index 2a2ef2c..23ceee9 100644
--- a/gui/pages.hpp
+++ b/gui/pages.hpp
@@ -29,14 +29,14 @@
 class ActionObject;
 class InputObject;
 class MouseCursor;
+class GUIObject;
 
 class Page
 {
 public:
-	virtual ~Page() {}
-
-public:
 	Page(xml_node<>* page, xml_node<>* templates = NULL);
+	virtual ~Page();
+
 	std::string GetName(void)   { return mName; }
 
 public:
@@ -51,6 +51,7 @@
 
 protected:
 	std::string mName;
+	std::vector<GUIObject*> mObjects;
 	std::vector<RenderObject*> mRenders;
 	std::vector<ActionObject*> mActions;
 	std::vector<InputObject*> mInputs;
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;
 }