KIO

kremoteencoding.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2003 Thiago Macieira <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-only
6 */
7 
8 #include "kremoteencoding.h"
9 
10 #include <KStringHandler>
11 #include <QTextCodec>
12 #include <QUrl>
13 
14 class KRemoteEncodingPrivate
15 {
16 public:
17  KRemoteEncodingPrivate()
18  : m_codec(nullptr)
19  {
20  }
21 
22  QTextCodec *m_codec;
23  QByteArray m_codeName;
24 };
25 
27  : d(new KRemoteEncodingPrivate)
28 {
29  setEncoding(name);
30 }
31 
33 {
34  delete d;
35 }
36 
38 {
39 #ifdef CHECK_UTF8
40  if (d->m_codec->mibEnum() == 106 && !KStringHandler::isUtf8(name)) {
41  return QLatin1String(name);
42  }
43 #endif
44 
45  QString result = d->m_codec->toUnicode(name);
46  if (d->m_codec->fromUnicode(result) != name)
47  // fallback in case of decoding failure
48  {
49  return QLatin1String(name);
50  }
51 
52  return result;
53 }
54 
56 {
57  QByteArray result = d->m_codec->fromUnicode(name);
58  if (d->m_codec->toUnicode(result) != name) {
59  return name.toLatin1();
60  }
61 
62  return result;
63 }
64 
66 {
67  return encode(url.path());
68 }
69 
70 QByteArray KRemoteEncoding::directory(const QUrl &url, bool ignore_trailing_slash) const
71 {
72  QUrl dirUrl(url);
73  if (ignore_trailing_slash && dirUrl.path().endsWith(QLatin1Char('/'))) {
74  dirUrl = dirUrl.adjusted(QUrl::StripTrailingSlash);
75  }
76  const QString dir = dirUrl.adjusted(QUrl::RemoveFilename).path();
77  return encode(dir);
78 }
79 
81 {
82  return encode(url.fileName());
83 }
84 
85 const char *KRemoteEncoding::encoding() const
86 {
87  // KF6 TODO: return QByteArray
88  d->m_codeName = d->m_codec->name();
89  return d->m_codeName.constData();
90 }
91 
93 {
94  return d->m_codec->mibEnum();
95 }
96 
97 void KRemoteEncoding::setEncoding(const char *name)
98 {
99  // don't delete codecs
100 
101  if (name) {
102  d->m_codec = QTextCodec::codecForName(name);
103  }
104 
105  if (d->m_codec == nullptr) {
106  d->m_codec = QTextCodec::codecForMib(106); // fallback to UTF-8
107  }
108 
109  if (d->m_codec == nullptr) {
110  d->m_codec = QTextCodec::codecForMib(4 /* latin-1 */);
111  }
112 
113  Q_ASSERT(d->m_codec);
114 
115  /*qDebug() << "setting encoding" << d->m_codec->name()
116  << "for name=" << name;*/
117 }
118 
119 void KRemoteEncoding::virtual_hook(int, void *)
120 {
121 }
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.
StripTrailingSlash
QByteArray fileName(const QUrl &url) const
Converts the given URL into 8-bit form and retrieve the filename.
virtual ~KRemoteEncoding()
Destructor.
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.
KCOREADDONS_EXPORT bool isUtf8(const char *str)
QByteArray encode(const QString &name) const
Converts the given name from Unicode.
QByteArray encode(const QUrl &url) const
Converts the given URL into its 8-bit components.
QString path(QUrl::ComponentFormattingOptions options) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
KRemoteEncoding(const char *name=nullptr)
Constructor.
QByteArray toLatin1() const const
int encodingMib() const
Returns the MIB for the codec being used.
QUrl adjusted(QUrl::FormattingOptions options) const const
QTextCodec * codecForName(const QByteArray &name)
QTextCodec * codecForMib(int mib)
const char * encoding() const
Returns the encoding being used.
QString fileName(QUrl::ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 23:01:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.