File indexing completed on 2024-04-21 03:55:09

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2003 Thiago Macieira <thiago.macieira@kdemail.net>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #ifndef KREMOTEENCODING_H
0009 #define KREMOTEENCODING_H
0010 
0011 #include "kiocore_export.h"
0012 #include <QByteArray>
0013 #include <QString>
0014 
0015 #include <memory>
0016 
0017 class QUrl;
0018 class KRemoteEncodingPrivate;
0019 /**
0020  * @class KRemoteEncoding kremoteencoding.h <KRemoteEncoding>
0021  *
0022  * Allows encoding and decoding properly remote filenames into Unicode.
0023  *
0024  * Certain protocols do not specify an appropriate encoding for decoding
0025  * their 8-bit data into proper Unicode forms. Therefore, KIO workers should
0026  * use this class in order to convert those forms into QStrings before
0027  * creating the respective KIO::UDSEntry. The same is true for decoding
0028  * URLs to its components.
0029  *
0030  * Each KIO::WorkerBase has one object of this kind, even if it is not necessary.
0031  * It can be accessed through KIO::WorkerBase::remoteEncoding.
0032  *
0033  * @short A class for handling remote filenames
0034  * @author Thiago Macieira <thiago.macieira@kdemail.net>
0035  */
0036 class KIOCORE_EXPORT KRemoteEncoding
0037 {
0038 public:
0039     /**
0040      * Constructor.
0041      *
0042      * Constructs this object to use the given encoding name.
0043      * If @p name is a null pointer, the standard encoding will be used.
0044      */
0045     explicit KRemoteEncoding(const char *name = nullptr);
0046 
0047     /**
0048      * Destructor
0049      */
0050     virtual ~KRemoteEncoding();
0051 
0052     /**
0053      * Converts the given full pathname or filename to Unicode.
0054      * This function is supposed to work for dirnames, filenames
0055      * or a full pathname.
0056      */
0057     QString decode(const QByteArray &name) const;
0058 
0059     /**
0060      * Converts the given name from Unicode.
0061      * This function is supposed to work for dirnames, filenames
0062      * or a full pathname.
0063      */
0064     QByteArray encode(const QString &name) const;
0065 
0066     /**
0067      * Converts the given URL into its 8-bit components
0068      */
0069     QByteArray encode(const QUrl &url) const;
0070 
0071     /**
0072      * Converts the given URL into 8-bit form and separate the
0073      * dirname from the filename. This is useful for worker functions
0074      * like stat or get.
0075      *
0076      * The dirname is returned with the final slash always stripped
0077      */
0078     QByteArray directory(const QUrl &url, bool ignore_trailing_slash = true) const;
0079 
0080     /**
0081      * Converts the given URL into 8-bit form and retrieve the filename.
0082      */
0083     QByteArray fileName(const QUrl &url) const;
0084 
0085     /**
0086      * Returns the encoding being used.
0087      */
0088     const char *encoding() const;
0089 
0090     /**
0091      * Sets the encoding being used.
0092      * This function does not change the global configuration.
0093      *
0094      * Pass a null pointer in @p name to revert to the standard
0095      * encoding.
0096      */
0097     void setEncoding(const char *name);
0098 
0099 protected:
0100     virtual void virtual_hook(int id, void *data);
0101 
0102 private:
0103     std::unique_ptr<KRemoteEncodingPrivate> const d;
0104 
0105     Q_DISABLE_COPY(KRemoteEncoding)
0106 };
0107 
0108 #endif