KIO

kremoteencoding.cpp
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
21 : d(new KRemoteEncodingPrivate)
22{
23 setEncoding(name);
24}
25
27
29{
30 QString result = d->m_decoder.decode(name);
31 if (d->m_encoder.encode(result) != name)
32 // fallback in case of decoding failure
33 {
34 return QLatin1String(name);
35 }
36
37 return result;
38}
39
41{
42 QByteArray result = d->m_encoder.encode(name);
43 if (d->m_decoder.decode(result) != name) {
44 return name.toLatin1();
45 }
46
47 return result;
48}
49
51{
52 return encode(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(QLatin1Char('/'))) {
59 dirUrl = dirUrl.adjusted(QUrl::StripTrailingSlash);
60 }
61 const QString dir = dirUrl.adjusted(QUrl::RemoveFilename).path();
62 return encode(dir);
63}
64
66{
67 return encode(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()) {
80 }
81 d->m_encoder = QStringEncoder(name);
82 if (!d->m_encoder.isValid()) {
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}
QString decode(const QByteArray &name) const
Converts the given full pathname or filename to Unicode.
void setEncoding(const char *name)
Sets the encoding being used.
virtual ~KRemoteEncoding()
Destructor.
QByteArray encode(const QString &name) const
Converts the given name from Unicode.
const char * encoding() const
Returns the encoding being used.
QByteArray directory(const QUrl &url, bool ignore_trailing_slash=true) const
Converts the given URL into 8-bit form and separate the dirname from the filename.
KRemoteEncoding(const char *name=nullptr)
Constructor.
QByteArray fileName(const QUrl &url) const
Converts the given URL into 8-bit form and retrieve the filename.
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
QByteArray toLatin1() const const
StripTrailingSlash
QUrl adjusted(FormattingOptions options) const const
QString fileName(ComponentFormattingOptions options) const const
QString path(ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.