From 349cc54c694529c97b5920b0ab32b39ff6250550 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 15 Mar 2021 11:59:41 +0300 Subject: [PATCH] Don't use deprecated QTextCodec --- src/base/net/smtp.cpp | 20 +++++++++++++------- src/base/net/smtp.h | 2 +- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/base/net/smtp.cpp b/src/base/net/smtp.cpp index 65ff739a4..01b554504 100644 --- a/src/base/net/smtp.cpp +++ b/src/base/net/smtp.cpp @@ -37,7 +37,6 @@ #include #include #include -#include #ifndef QT_NO_OPENSSL #include @@ -89,6 +88,14 @@ namespace return hostname.toLocal8Bit(); } + + bool canEncodeAsLatin1(const QStringView string) + { + return std::none_of(string.cbegin(), string.cend(), [](const QChar &ch) + { + return ch > QChar(0xff); + }); + } } // namespace using namespace Net; @@ -137,11 +144,10 @@ Smtp::~Smtp() void Smtp::sendMail(const QString &from, const QString &to, const QString &subject, const QString &body) { const Preferences *const pref = Preferences::instance(); - QTextCodec *latin1 = QTextCodec::codecForName("latin1"); m_message = "Date: " + getCurrentDateTime().toLatin1() + "\r\n" - + encodeMimeHeader("From", from, latin1) - + encodeMimeHeader("Subject", subject, latin1) - + encodeMimeHeader("To", to, latin1) + + encodeMimeHeader("From", from) + + encodeMimeHeader("Subject", subject) + + encodeMimeHeader("To", to) + "MIME-Version: 1.0\r\n" + "Content-Type: text/plain; charset=UTF-8\r\n" + "Content-Transfer-Encoding: base64\r\n" @@ -312,12 +318,12 @@ void Smtp::readyRead() } } -QByteArray Smtp::encodeMimeHeader(const QString &key, const QString &value, const QTextCodec *latin1, const QByteArray &prefix) +QByteArray Smtp::encodeMimeHeader(const QString &key, const QString &value, const QByteArray &prefix) { QByteArray rv = ""; QByteArray line = key.toLatin1() + ": "; if (!prefix.isEmpty()) line += prefix; - if (!value.contains("=?") && latin1->canEncode(value)) + if (!value.contains("=?") && canEncodeAsLatin1(value)) { bool firstWord = true; for (const QByteArray &word : asConst(value.toLatin1().split(' '))) diff --git a/src/base/net/smtp.h b/src/base/net/smtp.h index 3f63c0652..6b74198cd 100644 --- a/src/base/net/smtp.h +++ b/src/base/net/smtp.h @@ -88,7 +88,7 @@ namespace Net AuthCramMD5 }; - QByteArray encodeMimeHeader(const QString &key, const QString &value, const QTextCodec *latin1, const QByteArray &prefix = {}); + QByteArray encodeMimeHeader(const QString &key, const QString &value, const QByteArray &prefix = {}); void ehlo(); void helo(); void parseEhloResponse(const QByteArray &code, bool continued, const QString &line);