2
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "TextStream.h"
20
#include <QProcessEnvironment>
26
TextStream::TextStream()
31
TextStream::TextStream(QIODevice* device)
37
TextStream::TextStream(FILE* fileHandle, QIODevice::OpenMode openMode)
38
: QTextStream(fileHandle, openMode)
43
TextStream::TextStream(QString* string, QIODevice::OpenMode openMode)
44
: QTextStream(string, openMode)
49
TextStream::TextStream(QByteArray* array, QIODevice::OpenMode openMode)
50
: QTextStream(array, openMode)
55
TextStream::TextStream(const QByteArray& array, QIODevice::OpenMode openMode)
56
: QTextStream(array, openMode)
61
void TextStream::write(const char* str)
63
// Workaround for an issue with QTextStream. Its operator<<(const char *string) will encode the
64
// string with a non-UTF-8 encoding. We work around this by wrapping the input string into
65
// a QString, thus enforcing UTF-8. More info:
66
// https://code.qt.io/cgit/qt/qtbase.git/commit?id=cec8cdba4d1b856e17c8743ba8803349d42dc701
67
*this << QString(str);
70
void TextStream::detectCodec()
72
QString codecName = "UTF-8";
73
auto env = QProcessEnvironment::systemEnvironment();
78
success = SetConsoleOutputCP(CP_UTF8);
80
if (!success && !env.contains("SHELL")) {
81
// Fall back to cp850 if this is Windows without CP_UTF8 and we
82
// are running in a native shell (i.e., no Msys or Cygwin).
83
codecName = "Windows-850";
86
if (env.contains("LANG") && !env.value("LANG").isEmpty() && env.value("LANG") != "C") {
87
// Only override codec if LANG is set, otherwise Qt will assume
88
// US-ASCII, which is almost always wrong and results in
89
// Unicode passwords being displayed as question marks.
90
codecName = QTextCodec::codecForLocale()->name();
94
codecName = env.value("ENCODING_OVERRIDE", codecName);
95
auto* codec = QTextCodec::codecForName(codecName.toLatin1());