keepassxc

Форк
0
/
TextStream.cpp 
99 строк · 2.9 Кб
1
/*
2
 *  Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
3
 *
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.
8
 *
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.
13
 *
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/>.
16
 */
17

18
#include "TextStream.h"
19

20
#include <QProcessEnvironment>
21
#include <QTextCodec>
22
#ifdef Q_OS_WIN
23
#include <windows.h>
24
#endif
25

26
TextStream::TextStream()
27
{
28
    detectCodec();
29
}
30

31
TextStream::TextStream(QIODevice* device)
32
    : QTextStream(device)
33
{
34
    detectCodec();
35
}
36

37
TextStream::TextStream(FILE* fileHandle, QIODevice::OpenMode openMode)
38
    : QTextStream(fileHandle, openMode)
39
{
40
    detectCodec();
41
}
42

43
TextStream::TextStream(QString* string, QIODevice::OpenMode openMode)
44
    : QTextStream(string, openMode)
45
{
46
    detectCodec();
47
}
48

49
TextStream::TextStream(QByteArray* array, QIODevice::OpenMode openMode)
50
    : QTextStream(array, openMode)
51
{
52
    detectCodec();
53
}
54

55
TextStream::TextStream(const QByteArray& array, QIODevice::OpenMode openMode)
56
    : QTextStream(array, openMode)
57
{
58
    detectCodec();
59
}
60

61
void TextStream::write(const char* str)
62
{
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);
68
}
69

70
void TextStream::detectCodec()
71
{
72
    QString codecName = "UTF-8";
73
    auto env = QProcessEnvironment::systemEnvironment();
74

75
#ifdef Q_OS_WIN
76
    bool success = false;
77
#ifdef CP_UTF8
78
    success = SetConsoleOutputCP(CP_UTF8);
79
#endif
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";
84
    }
85
#else
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();
91
    }
92
#endif
93

94
    codecName = env.value("ENCODING_OVERRIDE", codecName);
95
    auto* codec = QTextCodec::codecForName(codecName.toLatin1());
96
    if (codec) {
97
        setCodec(codec);
98
    }
99
}
100

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.