1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2003 Thiago Macieira <thiago.macieira@kdemail.net>
4
5 SPDX-License-Identifier: LGPL-2.0-only
6*/
7
8#include "kremoteencoding.h"
9
10#include <QStringConverter>
11#include <QUrl>
12
13class KRemoteEncodingPrivate
14{
15public:
16 QStringDecoder m_decoder;
17 QStringEncoder m_encoder;
18};
19
20KRemoteEncoding::KRemoteEncoding(const char *name)
21 : d(new KRemoteEncodingPrivate)
22{
23 setEncoding(name);
24}
25
26KRemoteEncoding::~KRemoteEncoding() = default;
27
28QString KRemoteEncoding::decode(const QByteArray &name) const
29{
30 QString result = d->m_decoder.decode(ba: name);
31 if (d->m_encoder.encode(str: result) != name)
32 // fallback in case of decoding failure
33 {
34 return QLatin1String(name);
35 }
36
37 return result;
38}
39
40QByteArray KRemoteEncoding::encode(const QString &name) const
41{
42 QByteArray result = d->m_encoder.encode(str: name);
43 if (d->m_decoder.decode(ba: result) != name) {
44 return name.toLatin1();
45 }
46
47 return result;
48}
49
50QByteArray KRemoteEncoding::encode(const QUrl &url) const
51{
52 return encode(name: url.path());
53}
54
55QByteArray KRemoteEncoding::directory(const QUrl &url, bool ignore_trailing_slash) const
56{
57 QUrl dirUrl(url);
58 if (ignore_trailing_slash && dirUrl.path().endsWith(c: QLatin1Char('/'))) {
59 dirUrl = dirUrl.adjusted(options: QUrl::StripTrailingSlash);
60 }
61 const QString dir = dirUrl.adjusted(options: QUrl::RemoveFilename).path();
62 return encode(name: dir);
63}
64
65QByteArray KRemoteEncoding::fileName(const QUrl &url) const
66{
67 return encode(name: url.fileName());
68}
69
70const char *KRemoteEncoding::encoding() const
71{
72 return d->m_decoder.name();
73}
74
75void KRemoteEncoding::setEncoding(const char *name)
76{
77 d->m_decoder = QStringDecoder(name);
78 if (!d->m_decoder.isValid()) {
79 d->m_decoder = QStringDecoder(QStringDecoder::Utf8);
80 }
81 d->m_encoder = QStringEncoder(name);
82 if (!d->m_encoder.isValid()) {
83 d->m_encoder = QStringEncoder(QStringEncoder::Utf8);
84 }
85
86 Q_ASSERT(d->m_decoder.isValid());
87 Q_ASSERT(d->m_encoder.isValid());
88
89 /*qDebug() << "setting encoding" << d->m_decoder.name()
90 << "for name=" << name;*/
91}
92
93void KRemoteEncoding::virtual_hook(int, void *)
94{
95}
96

source code of kio/src/core/kremoteencoding.cpp