From 12dd8b028d390edc28b08cf2ff89c185319131b4 Mon Sep 17 00:00:00 2001
From: Aloshi <allofquist@yahoo.com>
Date: Sun, 13 Oct 2013 16:40:36 -0500
Subject: [PATCH] Disable sleep mode while scraping. Fix to actually update
 metadata. Write changes to gamelist.xml after each game is done.

---
 src/SystemData.cpp               | 10 ++--------
 src/Window.cpp                   | 12 +++++++++++-
 src/Window.h                     |  5 +++++
 src/XMLReader.cpp                |  4 ++--
 src/components/GuiScraperLog.cpp | 32 ++++++++++++++++++++++++++++----
 src/components/GuiScraperLog.h   |  4 ++++
 src/main.cpp                     |  2 +-
 7 files changed, 53 insertions(+), 16 deletions(-)

diff --git a/src/SystemData.cpp b/src/SystemData.cpp
index 087de8bb2..de19c3d0e 100644
--- a/src/SystemData.cpp
+++ b/src/SystemData.cpp
@@ -326,18 +326,12 @@ std::string SystemData::getGamelistPath()
 
 	filePath = getHomePath();
 	filePath += "/.emulationstation/"+ getName() + "/gamelist.xml";
-	if(fs::exists(filePath))
-		return filePath;
-
-	return "";
+	return filePath;
 }
 
 bool SystemData::hasGamelist()
 {
-	if(getGamelistPath().empty())
-		return false;
-	else
-		return true;
+	return (fs::exists(getGamelistPath()));
 }
 
 std::vector<MetaDataDecl> SystemData::getGameMDD()
diff --git a/src/Window.cpp b/src/Window.cpp
index cf1784443..847f55e48 100644
--- a/src/Window.cpp
+++ b/src/Window.cpp
@@ -8,7 +8,7 @@
 #include <iomanip>
 
 Window::Window() : mNormalizeNextUpdate(false), mFrameTimeElapsed(0), mFrameCountElapsed(0), mAverageDeltaTime(10), 
-	mZoomFactor(1.0f), mCenterPoint(0, 0), mMatrix(Eigen::Affine3f::Identity()), mFadePercent(0.0f)
+	mZoomFactor(1.0f), mCenterPoint(0, 0), mMatrix(Eigen::Affine3f::Identity()), mFadePercent(0.0f), mAllowSleep(true)
 {
 	mInputManager = new InputManager(this);
 	setCenterPoint(Eigen::Vector2f(Renderer::getScreenWidth() / 2, Renderer::getScreenHeight() / 2));
@@ -188,3 +188,13 @@ void Window::postProcess()
 		Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), 0x00000000 | ((unsigned char)(mFadePercent * 255)));
 	}
 }
+
+bool Window::getAllowSleep()
+{
+	return mAllowSleep;
+}
+
+void Window::setAllowSleep(bool sleep)
+{
+	mAllowSleep = sleep;
+}
diff --git a/src/Window.h b/src/Window.h
index d568bbda3..dcff3f923 100644
--- a/src/Window.h
+++ b/src/Window.h
@@ -34,6 +34,9 @@ public:
 
 	void setFadePercent(const float& perc);
 
+	bool getAllowSleep();
+	void setAllowSleep(bool sleep);
+	
 private:
 	InputManager* mInputManager;
 	std::vector<GuiComponent*> mGuiStack;
@@ -55,6 +58,8 @@ private:
 
 	void postProcess();
 	float mFadePercent;
+
+	bool mAllowSleep;
 };
 
 #endif
diff --git a/src/XMLReader.cpp b/src/XMLReader.cpp
index 4fa20d655..ab7077ef8 100644
--- a/src/XMLReader.cpp
+++ b/src/XMLReader.cpp
@@ -100,7 +100,7 @@ void parseGamelist(SystemData* system)
 {
 	std::string xmlpath = system->getGamelistPath();
 
-	if(xmlpath.empty())
+	if(!boost::filesystem::exists(xmlpath))
 		return;
 
 	LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\"...";
@@ -198,7 +198,7 @@ void updateGamelist(SystemData* system)
 		return;
 
 	std::string xmlpath = system->getGamelistPath();
-	if(xmlpath.empty())
+	if(!boost::filesystem::exists(xmlpath))
 		return;
 
 	LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\" before writing...";
diff --git a/src/components/GuiScraperLog.cpp b/src/components/GuiScraperLog.cpp
index a768e3bc3..66c5b4dbf 100644
--- a/src/components/GuiScraperLog.cpp
+++ b/src/components/GuiScraperLog.cpp
@@ -3,13 +3,15 @@
 #include "GuiGameScraper.h"
 #include "../Renderer.h"
 #include "../Log.h"
+#include "../XMLReader.h"
 
 GuiScraperLog::GuiScraperLog(Window* window, const std::queue<ScraperSearchParams>& searches, bool manualMode) : GuiComponent(window), 
 	mManualMode(manualMode), 
 	mBox(window, ":/frame.png"),
 	mSearches(searches),
 	mStatus(window), 
-	mTextLines(40)
+	mTextLines(40), 
+	mSuccessCount(0), mSkippedCount(0)
 {
 	addChild(&mBox);
 	addChild(&mStatus);
@@ -25,6 +27,13 @@ GuiScraperLog::GuiScraperLog(Window* window, const std::queue<ScraperSearchParam
 	mBox.setEdgeColor(0x111111FF);
 	mBox.setCenterColor(0xDFDFDFFF);
 	mBox.fitTo(mSize, Eigen::Vector3f::Zero(), Eigen::Vector2f(8, 0));
+
+	mWindow->setAllowSleep(false);
+}
+
+GuiScraperLog::~GuiScraperLog()
+{
+	mWindow->setAllowSleep(true);
 }
 
 void GuiScraperLog::start()
@@ -77,8 +86,16 @@ void GuiScraperLog::resultFetched(ScraperSearchParams params, MetaDataList mdl)
 
 void GuiScraperLog::resultResolved(ScraperSearchParams params, MetaDataList mdl)
 {
+	//apply new metadata
+	*params.game->metadata() = mdl;
+
 	writeLine("   Success!", 0x00FF00FF);
 
+	//write changes to gamelist.xml
+	updateGamelist(params.system);
+
+	mSuccessCount++;
+
 	next();
 }
 
@@ -91,14 +108,21 @@ void GuiScraperLog::resultEmpty(ScraperSearchParams search)
 
 	LOG(LogInfo) << "Scraper skipping [" << search.game->getCleanName() << "]";
 
+	mSkippedCount++;
+
 	next();
 }
 
 void GuiScraperLog::done()
 {
-	writeLine("====================", 0x000000FF);
-	writeLine("DONE!!", 0x00FF00FF);
-	writeLine("====================", 0x000000FF);
+	writeLine("===================================", 0x000000FF);
+	
+	std::stringstream ss;
+	ss << mSuccessCount << " successful, " << mSkippedCount << " skipped";
+	writeLine(ss.str(), 0x000000FF);
+
+	writeLine("DONE!! Press anything to continue.", 0x00FF00FF);
+	writeLine("===================================", 0x000000FF);
 
 	//done with everything!
 }
diff --git a/src/components/GuiScraperLog.h b/src/components/GuiScraperLog.h
index 04a7d14cc..7674d880c 100644
--- a/src/components/GuiScraperLog.h
+++ b/src/components/GuiScraperLog.h
@@ -11,6 +11,7 @@ class GuiScraperLog : public GuiComponent
 {
 public:
 	GuiScraperLog(Window* window, const std::queue<ScraperSearchParams>& params, bool manualMode);
+	~GuiScraperLog();
 
 	void start();
 
@@ -36,4 +37,7 @@ private:
 	
 	TextComponent mStatus;
 	boost::circular_buffer< std::shared_ptr<TextComponent> > mTextLines;
+
+	unsigned int mSuccessCount;
+	unsigned int mSkippedCount;
 };
diff --git a/src/main.cpp b/src/main.cpp
index f8e6c244b..0c905fd38 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -228,7 +228,7 @@ int main(int argc, char* argv[])
 		//sleeping entails setting a flag to start skipping frames
 		//and initially drawing a black semi-transparent rect to dim the screen
 		timeSinceLastEvent += deltaTime;
-		if(timeSinceLastEvent >= (unsigned int)Settings::getInstance()->getInt("DIMTIME") && Settings::getInstance()->getInt("DIMTIME") != 0)
+		if(timeSinceLastEvent >= (unsigned int)Settings::getInstance()->getInt("DIMTIME") && Settings::getInstance()->getInt("DIMTIME") != 0 && window.getAllowSleep())
 		{
 			sleeping = true;
 			timeSinceLastEvent = 0;