Allow listbox to have a list of check boxes

For an example of how to implement a list of check boxes, see:

http://bit.ly/1Giacw2
https://gerrit.omnirom.org/#/c/14284/2/

Change-Id: I0752a084625f4a0ef8cc6d99597f2fb9aa2ab9e0
diff --git a/gui/listbox.cpp b/gui/listbox.cpp
index 4c9a68a..d82736e 100644
--- a/gui/listbox.cpp
+++ b/gui/listbox.cpp
@@ -17,8 +17,6 @@
 */
 
 #include <string.h>
-#include <sys/stat.h>
-#include <dirent.h>
 
 extern "C" {
 #include "../twcommon.h"
@@ -35,6 +33,7 @@
 	xml_node<>* child;
 	mIconSelected = mIconUnselected = NULL;
 	mUpdate = 0;
+	isCheckList = false;
 
 	// Get the icons, if any
 	child = FindNode(node, "icon");
@@ -83,6 +82,19 @@
 			data.action = new GUIAction(action);
 			allowSelection = true;
 		}
+		xml_node<>* variable_name = child->first_node("data");
+		if (variable_name) {
+			attr = variable_name->first_attribute("variable");
+			if (attr) {
+				data.variableName = attr->value();
+				if (DataManager::GetIntValue(data.variableName) == 0)
+					data.selected = 0;
+				else
+					data.selected = 1;
+				allowSelection = true;
+				isCheckList = true;
+			}
+		}
 
 		mList.push_back(data);
 
@@ -116,6 +128,21 @@
 	if(!isConditionTrue())
 		return 0;
 
+	if (isCheckList) {
+		int i, listSize = mList.size();
+
+		for (i = 0; i < listSize; i++) {
+			if (mList.at(i).variableName == varName) {
+				if (value == "0") {
+					mList.at(i).selected = 0;
+				} else {
+					mList.at(i).selected = 1;
+				}
+			}
+		}
+		mUpdate = 1;
+		return 0;
+	}
 	// Check to see if the variable that we are using to store the list selected value has been updated
 	if (varName == mVariable) {
 		int i, listSize = mList.size();
@@ -161,14 +188,26 @@
 
 void GUIListBox::NotifySelect(size_t item_selected)
 {
-	for (size_t i = 0; i < mList.size(); i++) {
-		mList.at(i).selected = 0;
+	if (!isCheckList) {
+		for (size_t i = 0; i < mList.size(); i++) {
+			mList.at(i).selected = 0;
+		}
 	}
 	if (item_selected < mList.size()) {
 		ListData& data = mList.at(item_selected);
-		data.selected = 1;
-		string str = data.variableValue;	// [check] should this set currentValue instead?
-		DataManager::SetValue(mVariable, str);
+		if (isCheckList) {
+			if (data.selected) {
+				data.selected = 0;
+				DataManager::SetValue(data.variableName, "0");
+			} else {
+				data.selected = 1;
+				DataManager::SetValue(data.variableName, "1");
+			}
+		} else {
+			data.selected = 1;
+			string str = data.variableValue;	// [check] should this set currentValue instead?
+			DataManager::SetValue(mVariable, str);
+		}
 		if (data.action)
 			data.action->doActions();
 	}