From 6e3f7118a9b01fa8404fba436b129504b8764f36 Mon Sep 17 00:00:00 2001
From: Rafael <Syping@users.noreply.github.com>
Date: Thu, 31 Mar 2016 21:33:16 +0200
Subject: [PATCH] Support for Japanese character in savegame

---
 SavegameData.cpp     | 44 +++++++++++++++++++++++++++++++++++++++-----
 SnapmaticPicture.cpp |  4 ++--
 SnapmaticWidget.ui   |  7 +++++++
 3 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/SavegameData.cpp b/SavegameData.cpp
index ef291e1..3fd0620 100755
--- a/SavegameData.cpp
+++ b/SavegameData.cpp
@@ -17,6 +17,8 @@
 *****************************************************************************/
 
 #include "SavegameData.h"
+#include <QTextCodec>
+#include <QDebug>
 #include <QFile>
 
 SavegameData::SavegameData(QString fileName, QObject *parent) : QObject(parent), savegameFileName(fileName)
@@ -62,7 +64,6 @@ bool SavegameData::readingSavegame()
             savegameOk = true;
         }
     }
-
     saveFile->close();
     saveFile->deleteLater();
     delete saveFile;
@@ -71,10 +72,43 @@ bool SavegameData::readingSavegame()
 
 QString SavegameData::getSavegameDataString(QByteArray savegameHeader)
 {
-    QByteArray savegameUsefulBytes = savegameHeader.left(savegameHeaderLength);
-    savegameUsefulBytes.replace(QByteArray::fromHex("00"),"");
-    savegameUsefulBytes.replace(QByteArray::fromHex("01"),"");
-    return QString::fromLatin1(savegameUsefulBytes);
+    QString savegameTitle;
+    QByteArray savegameBytes = savegameHeader.left(savegameHeaderLength);
+    QList<QByteArray> savegameBytesList = savegameBytes.split(char(0x01));
+    savegameBytes = savegameBytesList.at(1);
+
+    int savegameLength = savegameBytes.length();
+    int parsedBytes = 0;
+
+    while (parsedBytes <= savegameLength)
+    {
+        QList<QByteArray> parseByteList;
+        parseByteList.append(savegameBytes.mid(parsedBytes-1, 1));
+        parseByteList.append(savegameBytes.mid(parsedBytes-2, 1));
+        if (parseByteList.at(0).toHex() == "00")
+        {
+            // Latin character
+            savegameTitle.append(QString::fromLatin1(parseByteList.at(1)));
+        }
+        else if (parseByteList.at(0).toHex() == "30")
+        {
+            // Japanese character
+            QByteArray japaneseHex;
+            japaneseHex.append(QByteArray::fromHex("A5"));
+            japaneseHex.append(parseByteList.at(1));
+            savegameTitle.append(QTextCodec::codecForName("EUC-JP")->toUnicode(japaneseHex));
+        }
+        else
+        {
+            // Unsupported
+        }
+        parsedBytes = parsedBytes + 2;
+        parseByteList.clear();
+    }
+
+    savegameBytesList.clear();
+    savegameBytes.clear();
+    return savegameTitle;
 }
 
 bool SavegameData::readingSavegameFromFile(QString fileName)
diff --git a/SnapmaticPicture.cpp b/SnapmaticPicture.cpp
index 93c7e24..c59f05d 100755
--- a/SnapmaticPicture.cpp
+++ b/SnapmaticPicture.cpp
@@ -190,7 +190,7 @@ QString SnapmaticPicture::getSnapmaticJSONString(QByteArray jsonBytes)
     QByteArray jsonUsefulBytes = jsonBytes;
     jsonUsefulBytes.replace((char)0x00, "");
     jsonUsefulBytes.replace((char)0x0c, "");
-    return QString::fromLatin1(jsonUsefulBytes);
+    return QString::fromUtf8(jsonUsefulBytes);
 }
 
 QString SnapmaticPicture::getSnapmaticTIDEString(QByteArray tideBytes)
@@ -198,7 +198,7 @@ QString SnapmaticPicture::getSnapmaticTIDEString(QByteArray tideBytes)
     QByteArray tideUsefulBytes = tideBytes;
     tideUsefulBytes.remove(0, 4);
     QList<QByteArray> tideUsefulBytesList = tideUsefulBytes.split(char(0x00));
-    return QString::fromLatin1(tideUsefulBytesList.at(0));
+    return QString::fromUtf8(tideUsefulBytesList.at(0));
 }
 
 bool SnapmaticPicture::readingPictureFromFile(QString fileName)
diff --git a/SnapmaticWidget.ui b/SnapmaticWidget.ui
index 572bc36..6a9cdec 100755
--- a/SnapmaticWidget.ui
+++ b/SnapmaticWidget.ui
@@ -65,6 +65,13 @@
      </property>
     </widget>
    </item>
+   <item>
+    <widget class="QPushButton" name="cmdCopy">
+     <property name="text">
+      <string>Copy</string>
+     </property>
+    </widget>
+   </item>
    <item>
     <widget class="QPushButton" name="cmdDelete">
      <property name="text">