• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KIO

  • sources
  • kde-4.14
  • kdelibs
  • kio
  • kio
kremoteencoding.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "kremoteencoding.h"
20 
21 #include <config.h>
22 
23 #include <kdebug.h>
24 #include <kstringhandler.h>
25 
26 
27 class KRemoteEncodingPrivate
28 {
29  public:
30  KRemoteEncodingPrivate()
31  : m_codec(0)
32  {
33  }
34 
35  QTextCodec *m_codec;
36 };
37 
38 KRemoteEncoding::KRemoteEncoding(const char *name)
39  : d(new KRemoteEncodingPrivate)
40 {
41  setEncoding(name);
42 }
43 
44 KRemoteEncoding::~KRemoteEncoding()
45 {
46  delete d;
47 }
48 
49 QString KRemoteEncoding::decode(const QByteArray& name) const
50 {
51 #ifdef CHECK_UTF8
52  if (d->m_codec->mibEnum() == 106 && !KStringHandler::isUtf8(name))
53  return QLatin1String(name);
54 #endif
55 
56  QString result = d->m_codec->toUnicode(name);
57  if (d->m_codec->fromUnicode(result) != name)
58  // fallback in case of decoding failure
59  return QLatin1String(name);
60 
61  return result;
62 }
63 
64 QByteArray KRemoteEncoding::encode(const QString& name) const
65 {
66  QByteArray result = d->m_codec->fromUnicode(name);
67  if (d->m_codec->toUnicode(result) != name)
68  return name.toLatin1();
69 
70  return result;
71 }
72 
73 QByteArray KRemoteEncoding::encode(const KUrl& url) const
74 {
75  return encode(url.path());
76 }
77 
78 QByteArray KRemoteEncoding::directory(const KUrl& url, bool ignore_trailing_slash) const
79 {
80  QString dir = url.directory(ignore_trailing_slash ? KUrl::DirectoryOptions(KUrl::IgnoreTrailingSlash) : KUrl::ObeyTrailingSlash);
81 
82  return encode(dir);
83 }
84 
85 QByteArray KRemoteEncoding::fileName(const KUrl& url) const
86 {
87  return encode(url.fileName());
88 }
89 
90 const char *KRemoteEncoding::encoding() const
91 {
92  return d->m_codec->name();
93 }
94 
95 int KRemoteEncoding::encodingMib() const
96 {
97  return d->m_codec->mibEnum();
98 }
99 
100 void KRemoteEncoding::setEncoding(const char *name)
101 {
102  // don't delete codecs
103 
104  if (name)
105  d->m_codec = QTextCodec::codecForName(name);
106 
107  if (d->m_codec == 0)
108  d->m_codec = QTextCodec::codecForMib( 106 ); // fallback to UTF-8
109 
110  if (d->m_codec == 0)
111  d->m_codec = QTextCodec::codecForMib(4 /* latin-1 */);
112 
113  Q_ASSERT(d->m_codec);
114 
115  kDebug() << "setting encoding" << d->m_codec->name()
116  << "for name=" << name;
117 }
118 
119 void KRemoteEncoding::virtual_hook(int, void*)
120 {
121 }
KUrl::directory
QString directory(const DirectoryOptions &options=IgnoreTrailingSlash) const
kdebug.h
KRemoteEncoding::~KRemoteEncoding
virtual ~KRemoteEncoding()
Destructor.
Definition: kremoteencoding.cpp:44
QByteArray
KRemoteEncoding::decode
QString decode(const QByteArray &name) const
Converts the given full pathname or filename to Unicode.
Definition: kremoteencoding.cpp:49
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KUrl
KRemoteEncoding::fileName
QByteArray fileName(const KUrl &url) const
Converts the given URL into 8-bit form and retrieve the filename.
Definition: kremoteencoding.cpp:85
KRemoteEncoding::setEncoding
void setEncoding(const char *name)
Sets the encoding being used.
Definition: kremoteencoding.cpp:100
KRemoteEncoding::directory
QByteArray directory(const KUrl &url, bool ignore_trailing_slash=true) const
Converts the given URL into 8-bit form and separate the dirname from the filename.
Definition: kremoteencoding.cpp:78
kremoteencoding.h
KStringHandler::isUtf8
bool isUtf8(const char *str)
KRemoteEncoding::encode
QByteArray encode(const QString &name) const
Converts the given name from Unicode.
Definition: kremoteencoding.cpp:64
KRemoteEncoding::KRemoteEncoding
KRemoteEncoding(const char *name=0)
Constructor.
Definition: kremoteencoding.cpp:38
QString
KUrl::path
QString path(AdjustPathOption trailing=LeaveTrailingSlash) const
QTextCodec
kstringhandler.h
QString::toLatin1
QByteArray toLatin1() const
KRemoteEncoding::encodingMib
int encodingMib() const
Returns the MIB for the codec being used.
Definition: kremoteencoding.cpp:95
KRecentDirs::dir
QString dir(const QString &fileClass)
Returns the most recently used directory accociated with this file-class.
Definition: krecentdirs.cpp:68
QLatin1String
KUrl::fileName
QString fileName(const DirectoryOptions &options=IgnoreTrailingSlash) const
QTextCodec::codecForName
QTextCodec * codecForName(const QByteArray &name)
KUrl::ObeyTrailingSlash
KUrl::IgnoreTrailingSlash
QTextCodec::codecForMib
QTextCodec * codecForMib(int mib)
KRemoteEncoding::virtual_hook
virtual void virtual_hook(int id, void *data)
Definition: kremoteencoding.cpp:119
KRemoteEncoding::encoding
const char * encoding() const
Returns the encoding being used.
Definition: kremoteencoding.cpp:90
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal