File indexing completed on 2024-05-05 16:08:29

0001 /*
0002     This file is part of the KDE libraries
0003     Copyright (C) 1997 Torben Weis (weis@kde.org)
0004     Copyright (C) 1998 Matthias Ettrich (ettrich@kde.org)
0005     Copyright (C) 1999-2004 David Faure (faure@kde.org)
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef KIO_NETACCESS_h
0024 #define KIO_NETACCESS_h
0025 
0026 #include <QObject>
0027 #include <QString>
0028 #include <kdelibs4support_export.h>
0029 #include <kio/global.h>
0030 #include <kio/udsentry.h>
0031 #include <kio/jobclasses.h> // for KIO::JobFlags
0032 
0033 class QStringList;
0034 class QWidget;
0035 
0036 template<typename T, typename K> class QMap;
0037 
0038 class KJob;
0039 namespace KIO
0040 {
0041 
0042 class Job;
0043 
0044 class NetAccessPrivate;
0045 /**
0046  * Net Transparency.
0047  *
0048  * NetAccess allows you to do simple file operation (load, save,
0049  * copy, delete...) without working with KIO::Job directly.
0050  * Whereas a KIO::Job is asynchronous, meaning that the
0051  * developer has to connect slots for it, KIO::NetAccess provides
0052  * synchronous downloads and uploads, as well as temporary file
0053  * creation and removal. The functions appear to be blocking,
0054  * but the Qt event loop continues running while the operations
0055  * are handled. More precisely, the GUI will still repaint, but no user
0056  * interaction will be possible. If you can, please use async KIO jobs instead!
0057  * See the documentation of KJob::exec() for more about the dangers of NetAccess.
0058  *
0059  * This class isn't meant to be used as a class but only as a simple
0060  * namespace for static functions, though an instance of the class
0061  * is built for internal purposes. TODO KDE5: turn into namespace,
0062  * and make the qobject class private.
0063  *
0064  * Port to kio done by David Faure, faure@kde.org
0065  *
0066  * @short Provides a blocking interface to KIO file operations.
0067  */
0068 class KDELIBS4SUPPORT_DEPRECATED_EXPORT NetAccess : public QObject
0069 {
0070     Q_OBJECT
0071 
0072 public:
0073     enum StatSide {
0074         SourceSide,
0075         DestinationSide
0076     };
0077 
0078     /**
0079      * Downloads a file from an arbitrary URL (@p src) to a
0080      * temporary file on the local filesystem (@p target).
0081      *
0082      * If the argument
0083      * for @p target is an empty string, download will generate a
0084      * unique temporary filename in /tmp. Since @p target is a reference
0085      * to QString you can access this filename easily. Download will
0086      * return true if the download was successful, otherwise false.
0087      *
0088      * Special case:
0089      * If the URL is of kind file:, then no downloading is
0090      * processed but the full filename is returned in @p target.
0091      * That means you @em have to take care about the @p target argument.
0092      * (This is very easy to do, please see the example below.)
0093      *
0094      * Download is synchronous. That means you can use it like this:
0095      * (assuming your application has a loadFile() function)
0096      *
0097      * \code
0098      * QString tmpFile;
0099      * if( KIO::NetAccess::download(url, tmpFile, window)) {
0100      *     loadFile(tmpFile);
0101      *     KIO::NetAccess::removeTempFile(tmpFile);
0102      * } else {
0103      *     KMessageBox::error(this, KIO::NetAccess::lastErrorString());
0104      * }
0105      * \endcode
0106      *
0107      * Of course, your user interface will still process exposure/repaint
0108      * events during the download.
0109      *
0110      * If the download fails, lastError() and lastErrorString() will be set.
0111      *
0112      * If the url is always remote, then you could also just write the more usual way:
0113      * \code
0114      * QTemporaryFile tmpFile;
0115      * if (tmpFile.open()) {
0116      *     KIO::Job* getJob = KIO::file_copy(url, QUrl::fromLocalFile(tmpFile.fileName()), -1, KIO::Overwrite | KIO::HideProgressInfo);
0117      *     getJob->ui()->setWindow(window);
0118      *     if (KIO::NetAccess::synchronousRun(getJob, 0)) {
0119      *         loadFile(tmpFile.fileName());
0120      *     } else {
0121      *         getJob->ui()->showErrorMessage();
0122      *     }
0123      * }
0124      * \endcode
0125      *
0126      * @param src URL Reference to the file to download.
0127      * @param target String containing the final local location of the
0128      *               file.  If you insert an empty string, it will
0129      *               return a location in a temporary spot. <B>Note:</B>
0130      *               you are responsible for the removal of this file when
0131      *               you are finished reading it using removeTempFile.
0132      * @param window main window associated with this job. This is used to
0133      *               automatically cache and discard authentication information
0134      *               as needed. If NULL, authentication information will be
0135      *               cached only for a short duration after which the user will
0136      *               again be prompted for passwords as needed.
0137      * @return true if successful, false for failure.  Use lastErrorString() to
0138      *         get the reason it failed.
0139      *
0140      * @see lastErrorString()
0141      * @deprecated since 5.0, use KIO::storedGet + KJobWidgets::setWindow + job->exec() instead
0142      */
0143     static bool download(const QUrl &src, QString &target, QWidget *window);
0144 
0145     /**
0146      * Removes the specified file if and only if it was created
0147      * by KIO::NetAccess as a temporary file for a former download.
0148      *
0149      * Note: This means that if you created your temporary with KTempFile,
0150      * use KTempFile::unlink() or KTempFile::setAutoDelete() to have
0151      * it removed.
0152      *
0153      * @param name Path to temporary file to remove.  May not be
0154      *             empty.
0155      */
0156     static void removeTempFile(const QString &name);
0157 
0158     /**
0159      * Uploads file @p src to URL @p target.
0160      *
0161      * Both must be specified, unlike download.
0162      * Note that this is assumed to be used for saving a file over
0163      * the network, so overwriting is set to true. This is not the
0164      * case with copy.
0165      *
0166      * @param src URL Referencing the file to upload.
0167      * @param target URL containing the final location of the file.
0168      * @param window main window associated with this job. This is used to
0169      *               automatically cache and discard authentication information
0170      *               as needed. If NULL, authentication information will be cached
0171      *               only for a short duration after which the user will again be
0172      *               prompted for passwords as needed.
0173      *
0174      * @return true if successful, false for failure
0175      * @deprecated since 5.0, use KIO::storedPut or KIO::file_copy + KJobWidgets::setWindow + job->exec() instead
0176      */
0177     static bool upload(const QString &src, const QUrl &target, QWidget *window);
0178 
0179     /**
0180      * Alternative to upload for copying over the network.
0181      * Overwrite is false, so this will fail if @p target exists.
0182      *
0183      * This one takes two URLs and is a direct equivalent of KIO::file_copy.
0184      *
0185      * @param src URL Referencing the file to upload.
0186      * @param target URL containing the final location of the file.
0187      * @param window main window associated with this job. This is used to
0188      *               automatically cache and discard authentication information
0189      *               as needed. If NULL, authentication information will be cached
0190      *               only for a short duration after which the user will again be
0191      *               prompted for passwords as needed.
0192      *
0193      * @return true if successful, false for failure
0194      * @deprecated since 5.0, use KIO::file_copy + job->ui()->setWindow() + job->exec() instead
0195      */
0196 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0197     static KDELIBS4SUPPORT_DEPRECATED bool file_copy(const QUrl &src, const QUrl &target, QWidget *window = nullptr);
0198     static KDELIBS4SUPPORT_DEPRECATED bool copy(const QUrl &src, const QUrl &target,
0199             QWidget *window = nullptr);
0200 #endif
0201 
0202     /**
0203      * Alternative method for copying over the network.
0204      *
0205      * This one takes two URLs and is a direct equivalent
0206      * of KIO::copy!.
0207      * This means that it can copy files and directories alike
0208      * (it should have been named copy()).
0209      *
0210      * This method will bring up a dialog if the destination already exists.
0211      *
0212      * @param src URL Referencing the file to upload.
0213      * @param target URL containing the final location of the
0214      *               file.
0215      * @param window main window associated with this job. This is used to
0216      *               automatically cache and discard authentication information
0217      *               as needed. If NULL, authentication information will be cached
0218      *               only for a short duration after which the user will again be
0219      *               prompted for passwords as needed.
0220      * @return true if successful, false for failure
0221      * @deprecated since 5.0, use KIO::copy + job->ui()->setWindow() + job->exec() instead
0222      */
0223 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0224     static KDELIBS4SUPPORT_DEPRECATED bool dircopy(const QUrl &src, const QUrl &target, QWidget *window);
0225 #endif
0226 
0227     /**
0228      * Overloaded method, which takes a list of source URLs
0229      * @deprecated since 5.0, use KIO::copy + job->ui()->setWindow() + job->exec() instead
0230      */
0231 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0232     static KDELIBS4SUPPORT_DEPRECATED  bool dircopy(const QList<QUrl> &src, const QUrl &target, QWidget *window = nullptr);
0233 #endif
0234     /**
0235      * Full-fledged equivalent of KIO::move.
0236      * Moves or renames one file or directory.
0237      * @deprecated since 5.0, use KIO::move + job->ui()->setWindow() + job->exec() instead
0238      */
0239 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0240     static KDELIBS4SUPPORT_DEPRECATED bool move(const QUrl &src, const QUrl &target, QWidget *window = nullptr);
0241 #endif
0242 
0243     /**
0244      * Full-fledged equivalent of KIO::move.
0245      * Moves or renames a list of files or directories.
0246      * @deprecated since 5.0, use KIO::move + job->ui()->setWindow() + job->exec() instead
0247      */
0248 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0249     static KDELIBS4SUPPORT_DEPRECATED bool move(const QList<QUrl> &src, const QUrl &target, QWidget *window = nullptr);
0250 #endif
0251 
0252     /**
0253      * Tests whether a URL exists.
0254      *
0255      * @param url the URL we are testing
0256      * @param source if true, we want to read from that URL.
0257      *               If false, we want to write to it.
0258      * IMPORTANT: see documentation for KIO::stat for more details about this.
0259      * @param window main window associated with this job. This is used to
0260      *               automatically cache and discard authentication information
0261      *               as needed. If NULL, authentication information will be
0262      *               cached only for a short duration after which the user will
0263      *               again be prompted for passwords as needed.
0264      * @return true if the URL exists and we can do the operation specified by
0265      *              @p source, false otherwise
0266      *
0267      * @deprecated use the StatSide enum instead of the bool source
0268      */
0269 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0270     static KDELIBS4SUPPORT_DEPRECATED bool exists(const QUrl &url, bool source, QWidget *window);
0271 #endif
0272 
0273     /**
0274      * Tests whether a URL exists.
0275      *
0276      * @param url the URL we are testing
0277      * @param statSide determines if we want to read or write.
0278      * IMPORTANT: see documentation for KIO::stat for more details about this.
0279      * @param window main window associated with this job. This is used to
0280      *               automatically cache and discard authentication information
0281      *               as needed. If NULL, authentication information will be
0282      *               cached only for a short duration after which the user will
0283      *               again be prompted for passwords as needed.
0284      * @return true if the URL exists and we can do the operation specified by
0285      *              @p source, false otherwise
0286      * @deprecated since 5.0, use QFile::exists for local files and KIO::stat for transparency
0287      */
0288     static bool exists(const QUrl &url, StatSide statSide, QWidget *window);
0289 
0290     /**
0291      * Tests whether a URL exists and return information on it.
0292      *
0293      * This is a convenience function for KIO::stat
0294      * (it saves creating a slot and testing for the job result).
0295      *
0296      * @param url The URL we are testing.
0297      * @param entry The result of the stat. Iterate over the list
0298      * of atoms to get hold of name, type, size, etc., or use KFileItem.
0299      * @param window main window associated with this job. This is used to
0300      *               automatically cache and discard authentication information
0301      *               as needed. If NULL, authentication information will be
0302      *               cached only for a short duration after which the user will
0303      *               again be prompted for passwords as needed.
0304      * @return true if successful, false for failure
0305      * @deprecated since 5.0, use KIO::stat + KJobWidgets::setWindow + job->exec() instead
0306      */
0307     static bool stat(const QUrl &url, KIO::UDSEntry &entry, QWidget *window);
0308 
0309     /**
0310      * Tries to map a local URL for the given URL.
0311      *
0312      * This is a convenience function for KIO::stat + parsing the
0313      * resulting UDSEntry.
0314      *
0315      * @param url The URL we are testing.
0316      * @param window main window associated with this job. This is used to
0317      *               automatically cache and discard authentication information
0318      *               as needed. If NULL, authentication information will be
0319      *               cached only for a short duration after which the user will
0320      *               again be prompted for passwords as needed.
0321      * @return a local URL corresponding to the same resource than the
0322      *         original URL, or the original URL if no local URL can be mapped
0323      * @deprecated since 5.0, use KIO::mostLocalUrl + KJobWidgets::setWindow + job->exec() instead
0324      */
0325     static QUrl mostLocalUrl(const QUrl &url, QWidget *window);
0326 
0327     /**
0328      * Deletes a file or a directory in a synchronous way.
0329      *
0330      * This is a convenience function for KIO::del
0331      * (it saves creating a slot and testing for the job result).
0332      *
0333      * @param url The file or directory to delete.
0334      * @param window main window associated with this job. This is used to
0335      *               automatically cache and discard authentication information
0336      *               as needed. If NULL, authentication information will be
0337      *               cached only for a short duration after which the user will
0338      *               again be prompted for passwords as needed.
0339      * @return true on success, false on failure.
0340      * @deprecated since 5.0, use KIO::del + job->ui()->setWindow() + job->exec() instead
0341      */
0342 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0343     static KDELIBS4SUPPORT_DEPRECATED bool del(const QUrl &url, QWidget *window);
0344 #endif
0345 
0346     /**
0347      * Creates a directory in a synchronous way.
0348      *
0349      * This is a convenience function for @p KIO::mkdir
0350      * (it saves creating a slot and testing for the job result).
0351      *
0352      * @param url The directory to create.
0353      * @param window main window associated with this job. This is used to
0354      *               automatically cache and discard authentication information
0355      *               as needed. If NULL, authentication information will be
0356      *               cached only for a short duration after which the user will
0357      *               again be prompted for passwords as needed.
0358      * @param permissions directory permissions.
0359      * @return true on success, false on failure.
0360      * @deprecated since 5.0, use KIO::mkdir + job->ui()->setWindow() + job->exec() instead
0361      */
0362 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0363     static KDELIBS4SUPPORT_DEPRECATED bool mkdir(const QUrl &url, QWidget *window, int permissions = -1);
0364 #endif
0365 
0366     /**
0367      * Executes a remote process via the fish ioslave in a synchronous way.
0368      *
0369      * @param url The remote machine where the command should be executed.
0370      *            e.g. fish://someuser\@somehost:sshport/
0371      *            some special cases exist.
0372      *            fish://someuser\@localhost/
0373      *            will use su instead of ssh to connect and execute the command.
0374      *            fish://someuser\@localhost:port/
0375      *            will use ssh to connect and execute the command.
0376      * @param command The command to be executed.
0377      * @param window main window associated with this job. This is used to
0378      *               automatically cache and discard authentication information
0379      *               as needed. If NULL, authentication information will be
0380      *               cached only for a short duration after which the user will
0381      *               again be prompted for passwords as needed.
0382      * @return The resulting output of the @p command that is executed.
0383      */
0384     static QString fish_execute(const QUrl &url, const QString &command, QWidget *window);
0385 
0386     /**
0387      * This function executes a job in a synchronous way.
0388      * If a job fetches some data, pass a QByteArray pointer as data parameter to this function
0389      * and after the function returns it will contain all the data fetched by this job.
0390      *
0391      * @code
0392      * KIO::Job *job = KIO::get( url );
0393      * QMap<QString, QString> metaData;
0394      * metaData.insert( "PropagateHttpHeader", "true" );
0395      * if ( NetAccess::synchronousRun( job, 0, &data, &url, &metaData ) ) {
0396      *   QString responseHeaders = metaData[ "HTTP-Headers" ];
0397      *   qDebug()<<"Response header = "<< responseHeaders;
0398      * }
0399      * @endcode
0400      *
0401      * @param job job which the function will run. Note that after this function
0402      *            finishes running, job is deleted and you can't access it anymore!
0403      * @param window main window associated with this job. This is used to
0404      *               automatically cache and discard authentication information
0405      *               as needed. If NULL, authentication information will be
0406      *               cached only for a short duration after which the user will
0407      *               again be prompted for passwords as needed.
0408      * @param data if passed and relevant to this job then it will contain the data
0409      *               that was fetched by the job
0410      * @param finalURL if passed will contain the final url of this job (it might differ
0411      *                 from the one it was created with if there was a redirection)
0412      * @param metaData you can pass a pointer to the map with meta data you wish to
0413      *                 set on the job. After the job finishes this map will hold all the
0414      *                 meta data from the job.
0415      *
0416      * @return true on success, false on failure.
0417      * @deprecated since 5.0, KJobWidgets::setWindow + job->exec() instead
0418      */
0419     static bool synchronousRun(Job *job, QWidget *window, QByteArray *data = nullptr,
0420                                QUrl *finalURL = nullptr, QMap<QString, QString> *metaData = nullptr);
0421 
0422     /**
0423      * Determines the mimetype of a given URL.
0424      *
0425      * This is a convenience function for KIO::mimetype.  You
0426      * should call this only when really necessary.
0427      * QMimeDatabase::mimeTypeForUrl can determine the mimetype a lot faster, but
0428      * less reliably for remote files. When mimeTypeForUrl() returns
0429      * unknown (application/octet-stream) for a remote file, then this one
0430      * should be used.
0431      *
0432      * @param url The URL whose mimetype we are interested in.
0433      * @param window main window associated with this job. This is used to
0434      *               automatically cache and discard authentication information
0435      *               as needed. If NULL, authentication information will be
0436      *               cached only for a short duration after which the user will
0437      *               again be prompted for passwords as needed.
0438      * @return The mimetype name.
0439      * @deprecated since 5.0, use KIO::mimetype + KJobWidgets::setWindow + job->exec() instead
0440      */
0441     static QString mimetype(const QUrl &url, QWidget *window);
0442 
0443     /**
0444      * Returns the error string for the last job, in case it failed.
0445      * Note that this is already translated.
0446      * @return the last error string, or QString()
0447      */
0448     static QString lastErrorString();
0449 
0450     /**
0451      * Returns the error code for the last job, in case it failed.
0452      * @return the last error code
0453      */
0454     static int lastError();
0455 
0456 Q_SIGNALS:
0457     void leaveModality();
0458 private:
0459     /**
0460      * Private constructor
0461      */
0462     NetAccess();
0463 
0464     /**
0465      * Private destructor
0466      */
0467     ~NetAccess() override;
0468 
0469     /**
0470      * Internal methods
0471      */
0472     bool filecopyInternal(const QUrl &src, const QUrl &target, int permissions,
0473                           KIO::JobFlags flags, QWidget *window, bool move);
0474     bool dircopyInternal(const QList<QUrl> &src, const QUrl &target,
0475                          QWidget *window, bool move);
0476     bool statInternal(const QUrl &url, int details, StatSide side, QWidget *window = nullptr);
0477 
0478     bool delInternal(const QUrl &url, QWidget *window = nullptr);
0479     bool mkdirInternal(const QUrl &url, int permissions, QWidget *window = nullptr);
0480     QString fish_executeInternal(const QUrl &url, const QString &command, QWidget *window = nullptr);
0481     bool synchronousRunInternal(Job *job, QWidget *window, QByteArray *data,
0482                                 QUrl *finalURL, QMap<QString, QString> *metaData);
0483 
0484     QString mimetypeInternal(const QUrl &url, QWidget *window = nullptr);
0485     void enter_loop();
0486 
0487     friend class I_like_this_class;
0488 
0489 private Q_SLOTS:
0490     void slotResult(KJob *job);
0491     void slotMimetype(KIO::Job *job, const QString &type);
0492     void slotData(KIO::Job *, const QByteArray &);
0493     void slotRedirection(KIO::Job *, const QUrl &);
0494 
0495 private:
0496     NetAccessPrivate *const d;
0497 };
0498 
0499 }
0500 
0501 #endif