From 55a51ac4c992deaa1122ec8fccd21f8a336889e0 Mon Sep 17 00:00:00 2001
From: Alex Deryskyba <alex@codesnake.com>
Date: Sun, 19 Oct 2014 16:20:33 +0300
Subject: [PATCH 13/17] Save settings only if they were modified after the last
 save

This prevents from multiple saving the same settings and helps to resolve
the issue on Amlogic G18REF TV-boxes when setiings may be lost after a poweroff.

On G18REF When you press the red button on the remote the system receives a signal that power
is about to off. XBMC always writes guisettings.xml before exit, and the same settings
may be written several times from different places in code. But the power gets turned off
before the system completes all shutdown procedures. There may be the case that guisettings.xml
is written half-way and couldn't be read upon next boot, so the XBMC creates a new one with
default settings.

With this fix the settings will be written at exit only once, minimizing the risk of being lost.

note by seo:
  added utils/md5.h after kodi includes cleanup.
  however, this patch should not be needed at all. TODO remove
---
 xbmc/settings/Settings.cpp | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/xbmc/settings/Settings.cpp b/xbmc/settings/Settings.cpp
index 2e690e3..bd0200d 100644
--- a/xbmc/settings/Settings.cpp
+++ b/xbmc/settings/Settings.cpp
@@ -72,6 +72,7 @@
 #include "threads/SingleLock.h"
 #include "utils/CharsetConverter.h"
 #include "utils/log.h"
+#include "utils/md5.h"
 #include "utils/RssManager.h"
 #include "utils/StringUtils.h"
 #include "utils/SystemInfo.h"
@@ -206,6 +207,17 @@ bool CSettings::Save(const std::string &file)
   if (!m_settingsManager->Save(root))
     return false;
 
+  // Avoid saving if the settings saved earlier are indetical to the current ones
+  if (CFile::Exists(file))
+  {
+    std::string fileMD5 = CUtil::GetFileMD5(file);
+    TiXmlPrinter xmlPrinter;
+    xmlDoc.Accept(&xmlPrinter);
+    std::string settingsMD5 = XBMC::XBMC_MD5::GetMD5(xmlPrinter.CStr());
+    if (fileMD5 == settingsMD5)
+      return true;
+  }
+
   return xmlDoc.SaveFile(file);
 }
 
-- 
2.1.4

