Skip to content
Snippets Groups Projects
Commit 9494128b authored by Edric Milaret's avatar Edric Milaret Committed by gerrit2
Browse files

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
parent fd77f882
Branches
No related tags found
No related merge requests found
...@@ -55,7 +55,7 @@ public: ...@@ -55,7 +55,7 @@ public:
virtual bool edit ( Media::Recording* item ) override; virtual bool edit ( Media::Recording* item ) override;
virtual bool addNew ( Media::Recording* item ) override; virtual bool addNew ( Media::Recording* item ) override;
virtual bool addExisting( const Media::Recording* item ) override; virtual bool addExisting( const Media::Recording* item ) override;
QByteArray fetch(const QByteArray& sha1); QString fetch(const QByteArray& sha1);
private: private:
virtual QVector<Media::Recording*> items() const override; virtual QVector<Media::Recording*> items() const override;
...@@ -96,6 +96,7 @@ bool LocalTextRecordingEditor::save(const Media::Recording* recording) ...@@ -96,6 +96,7 @@ bool LocalTextRecordingEditor::save(const Media::Recording* recording)
if ( file.open(QIODevice::WriteOnly | QIODevice::Text) ) { if ( file.open(QIODevice::WriteOnly | QIODevice::Text) ) {
QTextStream streamFileOut(&file); QTextStream streamFileOut(&file);
streamFileOut.setCodec("UTF-8");
streamFileOut << i.value(); streamFileOut << i.value();
streamFileOut.flush(); streamFileOut.flush();
file.close(); file.close();
...@@ -132,7 +133,7 @@ bool LocalTextRecordingEditor::addExisting(const Media::Recording* item) ...@@ -132,7 +133,7 @@ bool LocalTextRecordingEditor::addExisting(const Media::Recording* item)
return false; return false;
} }
QByteArray LocalTextRecordingEditor::fetch(const QByteArray& sha1) QString LocalTextRecordingEditor::fetch(const QByteArray& sha1)
{ {
QFile file(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/text/" + sha1 + ".json"); QFile file(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/text/" + sha1 + ".json");
...@@ -141,7 +142,7 @@ QByteArray LocalTextRecordingEditor::fetch(const QByteArray& sha1) ...@@ -141,7 +142,7 @@ QByteArray LocalTextRecordingEditor::fetch(const QByteArray& sha1)
return QByteArray(); return QByteArray();
} }
return file.readAll(); return QString::fromUtf8(file.readAll());
} }
QVector<Media::Recording*> LocalTextRecordingEditor::items() const QVector<Media::Recording*> LocalTextRecordingEditor::items() const
...@@ -230,12 +231,18 @@ bool LocalTextRecordingCollection::fetch(const Element& e) ...@@ -230,12 +231,18 @@ bool LocalTextRecordingCollection::fetch(const Element& e)
Media::TextRecording* LocalTextRecordingCollection::fetchFor(const ContactMethod* cm) Media::TextRecording* LocalTextRecordingCollection::fetchFor(const ContactMethod* cm)
{ {
const QByteArray& sha1 = cm->sha1(); 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()) if (content.isEmpty())
return nullptr; 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); Media::TextRecording* r = Media::TextRecording::fromJson({loadDoc.object()}, cm);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment