From 9494128b494cc4e90c3d352457dea4ecbf46c236 Mon Sep 17 00:00:00 2001
From: Edric Milaret <edric.ladent-milaret@savoirfairelinux.com>
Date: Thu, 7 Jan 2016 12:39:08 -0500
Subject: [PATCH] textmessage: fix encoding problem

- Read the text message json file in QSring UTF-8 instead
of QByteArray to force the encoding
- Check if there is an error on decoding to
avoid segmentation fault
- Correctly set the the text stream to use UTF-8
when saving text message history file

Change-Id: I70d61d5fbc5cae5de6119898de1954eef7f8ea5a
Tuleap: #223
---
 src/localtextrecordingcollection.cpp | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/src/localtextrecordingcollection.cpp b/src/localtextrecordingcollection.cpp
index 7c55b025..2a9d02d5 100644
--- a/src/localtextrecordingcollection.cpp
+++ b/src/localtextrecordingcollection.cpp
@@ -55,7 +55,7 @@ public:
    virtual bool edit       ( Media::Recording*       item ) override;
    virtual bool addNew     ( Media::Recording*       item ) override;
    virtual bool addExisting( const Media::Recording* item ) override;
-   QByteArray fetch(const QByteArray& sha1);
+   QString fetch(const QByteArray& sha1);
 
 private:
    virtual QVector<Media::Recording*> items() const override;
@@ -96,6 +96,7 @@ bool LocalTextRecordingEditor::save(const Media::Recording* recording)
 
       if ( file.open(QIODevice::WriteOnly | QIODevice::Text) ) {
          QTextStream streamFileOut(&file);
+         streamFileOut.setCodec("UTF-8");
          streamFileOut << i.value();
          streamFileOut.flush();
          file.close();
@@ -132,7 +133,7 @@ bool LocalTextRecordingEditor::addExisting(const Media::Recording* item)
    return false;
 }
 
-QByteArray LocalTextRecordingEditor::fetch(const QByteArray& sha1)
+QString LocalTextRecordingEditor::fetch(const QByteArray& sha1)
 {
    QFile file(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/text/" + sha1 + ".json");
 
@@ -141,7 +142,7 @@ QByteArray LocalTextRecordingEditor::fetch(const QByteArray& sha1)
       return QByteArray();
    }
 
-   return file.readAll();
+   return QString::fromUtf8(file.readAll());
 }
 
 QVector<Media::Recording*> LocalTextRecordingEditor::items() const
@@ -230,12 +231,18 @@ bool LocalTextRecordingCollection::fetch(const Element& e)
 Media::TextRecording* LocalTextRecordingCollection::fetchFor(const ContactMethod* cm)
 {
    const QByteArray& sha1 = cm->sha1();
-   const QByteArray content = static_cast<LocalTextRecordingEditor*>(editor<Media::Recording>())->fetch(sha1);
+   const QString content = static_cast<LocalTextRecordingEditor*>(editor<Media::Recording>())->fetch(sha1);
 
    if (content.isEmpty())
       return nullptr;
 
-   QJsonDocument loadDoc = QJsonDocument::fromJson(content);
+   QJsonParseError err;
+   QJsonDocument loadDoc = QJsonDocument::fromJson(content.toUtf8(), &err);
+
+   if (err.error != QJsonParseError::ParseError::NoError) {
+       qWarning() << "Error Decoding Text Message History Json" << err.errorString();
+       return nullptr;
+   }
 
    Media::TextRecording* r = Media::TextRecording::fromJson({loadDoc.object()}, cm);
 
-- 
GitLab