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

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2020 Ahmad Samir <a.samirh78@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #ifndef KIO_NAMEFINDERJOB_H
0009 #define KIO_NAMEFINDERJOB_H
0010 
0011 #include "kiocore_export.h"
0012 #include <KCompositeJob>
0013 
0014 #include <memory>
0015 
0016 namespace KIO
0017 {
0018 class NameFinderJobPrivate;
0019 /**
0020  * @class KIO::NameFinderJob namefinderjob.h <KIO/NameFinderJob>
0021  *
0022  *
0023  * @brief NameFinderJob finds a valid "New Folder" name.
0024  *
0025  * This job is useful when suggesting a new folder/file name, e.g. in KNewFileMenu,
0026  * the text box is pre-filled with a suggested name, typically in the form "New Folder";
0027  * to offer a valid name (i.e. one that doesn't already exist), you can use a NameFinderJob.
0028  *
0029  * Internally it uses a KIO::StatJob to determine if e.g. "New Folder" already exists,
0030  * and in such a case it will use KFileUtils::makeSuggestedName() to make a new name,
0031  * e.g. "New Folder (1)", then if the latter exists, KFileUtils::makeSuggestedName() will
0032  * be called again... etc, until it finds a name that doesn't already exist.
0033  *
0034  * Since NameFinderJob uses a KIO::StatJob, the code is asynchronous, which means it should
0035  * work for both local and remote filesystems without blocking I/O calls (this is important
0036  * when interacting with network mounts (e.g. SMB, NFS), from the upstream QFile perspective
0037  * these network mounts are "local" files even though they could actually reside on a server
0038  * halfway across the world).
0039  *
0040  * Note that KIO::StatJob will resolve URLs such as "desktop:/" to the most local URL, hence
0041  * it's advisable to always use baseUrl() (or finalUrl()) to get the actual URL.
0042  *
0043  * If the job fails for any reason targerUrl() will return an empty URL.
0044  *
0045  * @note You must call start() to start the job.
0046  *
0047  * @code
0048  *    // Create the job
0049  *    auto nameJob = new KIO::NameFinderJob(baseUrl, name, this);
0050  *    // Connect to the result() slot, and after making sure there were no errors call
0051  *    // finalUrl(), baseUrl() or finalName() to get the new url (base
0052  *    // url + suggested name), base url or suggested name, respectively
0053  *    connect(nameJob, &KJob::result, this, []() {
0054  *        if (!nameJob->error()) {
0055  *            const QUrl newBaseUrl = nameJob->baseUrl();
0056  *            const QUrl newName = nameJob->finalName();
0057  *            .....
0058  *            // Create the new dir "newName" in "newBaseUrl"
0059  *        }
0060  *    });
0061  *
0062  *    // Start the job
0063  *    nameJob->start();
0064  * @endcode
0065  *
0066  * @since 5.76
0067  */
0068 
0069 class KIOCORE_EXPORT NameFinderJob : public KCompositeJob
0070 {
0071     Q_OBJECT
0072 
0073 public:
0074     /**
0075      * @brief Creates a NameFinderJob to get a "New Folder" (or "Text File.txt") name that doesn't
0076      * already exist.
0077      *
0078      * @param baseUrl URL of the directory where a new folder/file is going to be created
0079      * @param name the initially proposed name of the new folder/file
0080      */
0081     explicit NameFinderJob(const QUrl &baseUrl, const QString &name, QObject *parent);
0082 
0083     /**
0084      * Destructor
0085      *
0086      * Note that by default jobs auto-delete themselves after emitting result.
0087      */
0088     ~NameFinderJob() override;
0089 
0090     /**
0091      * Starts the job.
0092      */
0093     void start() override;
0094 
0095     /**
0096      * Call this to get the full target URL (basically the baseUrl() + "/" + finalName()).
0097      * Typically you should call this in a slot connected to the result() signal, and after
0098      * making sure no errors occurred (if there were an error this method will return an empty URL).
0099      */
0100     QUrl finalUrl() const;
0101 
0102     /**
0103      * Call this to get the base URL (i.e.\ the URL of the folder where a new folder/file
0104      * is going to be created). Note that this could return a different URL from the one
0105      * the job was initially called on, since the StatJob (which is used internally) will
0106      * resolve the URL to the most local one. See KIO::StatJob::mostLocalUrl() for more details.
0107      *
0108      * Typically you should call this in a slot connected to the result() signal, and after
0109      * making sure no errors occurred.
0110      */
0111     QUrl baseUrl() const;
0112 
0113     /**
0114      * Call this to get the suggested new folder/file name. Typically you should call this in a
0115      * slot connected to the result() signal and after making sure no errors occurred.
0116      */
0117     QString finalName() const;
0118 
0119 private:
0120     friend class NameFinderJobPrivate;
0121     std::unique_ptr<NameFinderJobPrivate> d;
0122 };
0123 
0124 } // namespace KIO
0125 
0126 #endif