KIO
kremoteencoding.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "kremoteencoding.h" 00020 00021 #include <config.h> 00022 00023 #include <kdebug.h> 00024 #include <kstringhandler.h> 00025 00026 00027 class KRemoteEncodingPrivate 00028 { 00029 public: 00030 KRemoteEncodingPrivate() 00031 : m_codec(0) 00032 { 00033 } 00034 00035 QTextCodec *m_codec; 00036 }; 00037 00038 KRemoteEncoding::KRemoteEncoding(const char *name) 00039 : d(new KRemoteEncodingPrivate) 00040 { 00041 setEncoding(name); 00042 } 00043 00044 KRemoteEncoding::~KRemoteEncoding() 00045 { 00046 delete d; 00047 } 00048 00049 QString KRemoteEncoding::decode(const QByteArray& name) const 00050 { 00051 #ifdef CHECK_UTF8 00052 if (d->m_codec->mibEnum() == 106 && !KStringHandler::isUtf8(name)) 00053 return QLatin1String(name); 00054 #endif 00055 00056 QString result = d->m_codec->toUnicode(name); 00057 if (d->m_codec->fromUnicode(result) != name) 00058 // fallback in case of decoding failure 00059 return QLatin1String(name); 00060 00061 return result; 00062 } 00063 00064 QByteArray KRemoteEncoding::encode(const QString& name) const 00065 { 00066 QByteArray result = d->m_codec->fromUnicode(name); 00067 if (d->m_codec->toUnicode(result) != name) 00068 return name.toLatin1(); 00069 00070 return result; 00071 } 00072 00073 QByteArray KRemoteEncoding::encode(const KUrl& url) const 00074 { 00075 return encode(url.path()); 00076 } 00077 00078 QByteArray KRemoteEncoding::directory(const KUrl& url, bool ignore_trailing_slash) const 00079 { 00080 QString dir = url.directory(ignore_trailing_slash ? KUrl::DirectoryOptions(KUrl::IgnoreTrailingSlash) : KUrl::ObeyTrailingSlash); 00081 00082 return encode(dir); 00083 } 00084 00085 QByteArray KRemoteEncoding::fileName(const KUrl& url) const 00086 { 00087 return encode(url.fileName()); 00088 } 00089 00090 const char *KRemoteEncoding::encoding() const 00091 { 00092 return d->m_codec->name(); 00093 } 00094 00095 int KRemoteEncoding::encodingMib() const 00096 { 00097 return d->m_codec->mibEnum(); 00098 } 00099 00100 void KRemoteEncoding::setEncoding(const char *name) 00101 { 00102 // don't delete codecs 00103 00104 if (name) 00105 d->m_codec = QTextCodec::codecForName(name); 00106 00107 if (d->m_codec == 0) 00108 d->m_codec = QTextCodec::codecForMib( 106 ); // fallback to UTF-8 00109 00110 if (d->m_codec == 0) 00111 d->m_codec = QTextCodec::codecForMib(4 /* latin-1 */); 00112 00113 Q_ASSERT(d->m_codec); 00114 00115 kDebug() << "setting encoding" << d->m_codec->name() 00116 << "for name=" << name; 00117 } 00118 00119 void KRemoteEncoding::virtual_hook(int, void*) 00120 { 00121 }