From 1f0467bb1b7310de75dacd34b04dc81275d63e13 Mon Sep 17 00:00:00 2001
From: sudden6 <sudden6@gmx.at>
Date: Sun, 5 Mar 2017 21:25:25 +0100
Subject: [PATCH] refactor(core): add new class for easy string conversion

---
 CMakeLists.txt         |  2 +
 qtox.pro               |  2 +
 src/core/toxstring.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++
 src/core/toxstring.h   | 43 +++++++++++++++++++
 4 files changed, 143 insertions(+)
 create mode 100644 src/core/toxstring.cpp
 create mode 100644 src/core/toxstring.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6966629cc..f5db031c5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -229,6 +229,8 @@ set(${PROJECT_NAME}_SOURCES
   src/core/toxid.h
   src/core/toxpk.cpp
   src/core/toxpk.h
+  src/core/toxstring.cpp
+  src/core/toxstring.h
   src/friend.cpp
   src/friend.h
   src/friendlist.cpp
diff --git a/qtox.pro b/qtox.pro
index 379a5473d..1880aa07d 100644
--- a/qtox.pro
+++ b/qtox.pro
@@ -444,6 +444,7 @@ HEADERS  += \
     src/core/toxencrypt.h \
     src/core/toxid.h \
     src/core/toxpk.h \
+    src/core/toxstring.h \
     src/friend.h \
     src/friendlist.h \
     src/group.h \
@@ -564,6 +565,7 @@ SOURCES += \
     src/core/toxencrypt.cpp \
     src/core/toxid.cpp \
     src/core/toxpk.cpp \
+    src/core/toxstring.cpp \
     src/friend.cpp \
     src/friendlist.cpp \
     src/group.cpp \
diff --git a/src/core/toxstring.cpp b/src/core/toxstring.cpp
new file mode 100644
index 000000000..e1249e48e
--- /dev/null
+++ b/src/core/toxstring.cpp
@@ -0,0 +1,96 @@
+/*
+    Copyright © 2017 by The qTox Project Contributors
+
+    This file is part of qTox, a Qt-based graphical interface for Tox.
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    qTox is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with qTox.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "toxstring.h"
+
+#include <QByteArray>
+#include <QString>
+
+#include <cassert>
+#include <climits>
+
+/**
+ * @class ToxString
+ * @brief Helper to convert safely between strings in the c-toxcore representation and QString.
+ */
+
+/**
+ * @brief Creates a ToxString from a QString.
+ * @param string Input text.
+ */
+ToxString::ToxString(const QString& text)
+    : ToxString(text.toUtf8())
+{
+}
+
+/**
+ * @brief Creates a ToxString from bytes in a QByteArray.
+ * @param text Input text.
+ */
+ToxString::ToxString(const QByteArray& text)
+    : string(text)
+{
+}
+
+/**
+ * @brief Creates a ToxString from the representation used by c-toxcore.
+ * @param text Pointer to the beginning of the text.
+ * @param length Number of bytes to read from the beginning.
+ */
+ToxString::ToxString(const uint8_t* text, size_t length)
+{
+    assert(length <= INT_MAX);
+    string = QByteArray(reinterpret_cast<const char*>(text), length);
+}
+
+/**
+ * @brief Returns a pointer to the beginning of the string data.
+ * @return Pointer to the beginning of the string data.
+ */
+const uint8_t* ToxString::data() const
+{
+    return reinterpret_cast<const uint8_t*>(string.constData());
+}
+
+/**
+ * @brief Get the number of bytes in the string.
+ * @return Number of bytes in the string.
+ */
+size_t ToxString::size() const
+{
+    return string.size();
+}
+
+/**
+ * @brief Gets the string as QString.
+ * @return QString representation of the string.
+ */
+QString ToxString::getQString() const
+{
+    return QString::fromUtf8(string);
+}
+
+/**
+ * @brief getBytes Gets the bytes of the string.
+ * @return Bytes of the string as QByteArray.
+ */
+QByteArray ToxString::getBytes() const
+{
+    return QByteArray(string);
+}
diff --git a/src/core/toxstring.h b/src/core/toxstring.h
new file mode 100644
index 000000000..512488a21
--- /dev/null
+++ b/src/core/toxstring.h
@@ -0,0 +1,43 @@
+/*
+    Copyright © 2017 by The qTox Project Contributors
+
+    This file is part of qTox, a Qt-based graphical interface for Tox.
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    qTox is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with qTox.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef TOXSTRING_H
+#define TOXSTRING_H
+
+#include <QByteArray>
+#include <QString>
+
+#include <cstdint>
+
+class ToxString
+{
+public:
+    explicit ToxString(const QString& text);
+    explicit ToxString(const QByteArray& text);
+    ToxString(const uint8_t* text, size_t length);
+
+    const uint8_t* data() const;
+    size_t size() const;
+    QString getQString() const;
+    QByteArray getBytes() const;
+
+private:
+    QByteArray string;
+};
+#endif // TOXSTRING_H