File indexing completed on 2024-04-28 15:26:15

0001 // -*- c++ -*-
0002 /*
0003     This file is part of the KDE libraries
0004     SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
0005     SPDX-FileCopyrightText: 2000-2006 David Faure <faure@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KIO_COPYJOB_H
0011 #define KIO_COPYJOB_H
0012 
0013 #include <QDateTime>
0014 #include <QObject>
0015 #include <QStringList>
0016 #include <QUrl>
0017 
0018 #include "job_base.h"
0019 #include "kiocore_export.h"
0020 #include <kio/global.h> // filesize_t
0021 
0022 class QTimer;
0023 
0024 namespace KIO
0025 {
0026 /// @internal
0027 /// KF6 TODO: move to .cpp and remove aboutToCreate signal
0028 struct CopyInfo {
0029     QUrl uSource;
0030     QUrl uDest;
0031     QString linkDest; // for symlinks only
0032     int permissions;
0033     QDateTime ctime;
0034     QDateTime mtime;
0035     KIO::filesize_t size; // 0 for dirs
0036 };
0037 
0038 class CopyJobPrivate;
0039 /**
0040  * @class KIO::CopyJob copyjob.h <KIO/CopyJob>
0041  *
0042  * CopyJob is used to move, copy or symlink files and directories.
0043  * Don't create the job directly, but use KIO::copy(),
0044  * KIO::move(), KIO::link() and friends.
0045  *
0046  * @see KIO::copy()
0047  * @see KIO::copyAs()
0048  * @see KIO::move()
0049  * @see KIO::moveAs()
0050  * @see KIO::link()
0051  * @see KIO::linkAs()
0052  */
0053 class KIOCORE_EXPORT CopyJob : public Job
0054 {
0055     Q_OBJECT
0056 
0057 public:
0058     /**
0059      * Defines the mode of the operation
0060      */
0061     enum CopyMode { Copy, Move, Link };
0062 
0063     ~CopyJob() override;
0064 
0065     /**
0066      * Returns the mode of the operation (copy, move, or link),
0067      * depending on whether KIO::copy(), KIO::move() or KIO::link() was called.
0068      */
0069     CopyMode operationMode() const;
0070 
0071     /**
0072      * Returns the list of source URLs.
0073      * @return the list of source URLs.
0074      */
0075     QList<QUrl> srcUrls() const;
0076 
0077     /**
0078      * Returns the destination URL.
0079      * @return the destination URL
0080      */
0081     QUrl destUrl() const;
0082 
0083     /**
0084      * By default the permissions of the copied files will be those of the source files.
0085      *
0086      * But when copying "template" files to "new" files, people prefer the umask
0087      * to apply, rather than the template's permissions.
0088      * For that case, call setDefaultPermissions(true)
0089      */
0090     void setDefaultPermissions(bool b);
0091 
0092     /**
0093      * Skip copying or moving any file when the destination already exists,
0094      * instead of the default behavior (interactive mode: showing a dialog to the user,
0095      * non-interactive mode: aborting with an error).
0096      * Initially added for a unit test.
0097      * \since 4.2
0098      */
0099     void setAutoSkip(bool autoSkip);
0100 
0101     /**
0102      * Rename files automatically when the destination already exists,
0103      * instead of the default behavior (interactive mode: showing a dialog to the user,
0104      * non-interactive mode: aborting with an error).
0105      * Initially added for a unit test.
0106      * \since 4.7
0107      */
0108     void setAutoRename(bool autoRename);
0109 
0110     /**
0111      * Reuse any directory that already exists, instead of the default behavior
0112      * (interactive mode: showing a dialog to the user,
0113      * non-interactive mode: aborting with an error).
0114      * \since 4.2
0115      */
0116     void setWriteIntoExistingDirectories(bool overwriteAllDirs);
0117 
0118     /**
0119      * Reimplemented for internal reasons
0120      */
0121     bool doSuspend() override;
0122 
0123     /**
0124      * Reimplemented for internal reasons
0125      */
0126     bool doResume() override;
0127 
0128 Q_SIGNALS:
0129 
0130 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 72)
0131     /**
0132      * Emitted when the total number of files is known.
0133      * @param job the job that emitted this signal
0134      * @param files the total number of files
0135      *
0136      * @deprecated since 5.72, up to Frameworks versions <= 5.79, use the KJob::totalAmount(KJob *, KJob::Unit, qulonglong)
0137      * signal, starting from 5.80 use the KJob::totalAmountChanged(KJob *, KJob::Unit, qulonglong) signal instead.
0138      */
0139     KIOCORE_DEPRECATED_VERSION(5,
0140                                72,
0141                                "Up to Frameworks versions <= 5.79, use the KJob::totalAmount(KJob *, KJob::Unit, qulonglong) signal, starting from 5.80 use "
0142                                "the KJob::totalAmountChanged(KJob *, KJob::Unit, qulonglong) signal instead.")
0143     QT_MOC_COMPAT void totalFiles(KJob *job, unsigned long files);
0144 #endif
0145 
0146 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 72)
0147     /**
0148      * Emitted when the total number of directories is known.
0149      * @param job the job that emitted this signal
0150      * @param dirs the total number of directories
0151      *
0152      * @deprecated since 5.72, up to Frameworks versions <= 5.79, use the KJob::totalAmount(KJob *, KJob::Unit, qulonglong) signal, starting from 5.80 use
0153      * KJob::totalAmountChanged(KJob *, KJob::Unit, qulonglong) signal instead.
0154      */
0155     KIOCORE_DEPRECATED_VERSION(5,
0156                                72,
0157                                "Up to Frameworks versions <= 5.79, use the KJob::totalAmount(KJob *, KJob::Unit, qulonglong) signal, starting from 5.80 use "
0158                                "KJob::totalAmountChanged(KJob *, KJob::Unit, qulonglong) signal instead.")
0159     QT_MOC_COMPAT void totalDirs(KJob *job, unsigned long dirs);
0160 #endif
0161 
0162 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 2)
0163     /**
0164      * Emitted when it is known which files / directories are going
0165      * to be created. Note that this may still change e.g. when
0166      * existing files with the same name are discovered.
0167      * @param job the job that emitted this signal
0168      * @param files a list of items that are about to be created.
0169      * @deprecated since 5.2 -- this signal is unused since kde 3...
0170      */
0171     KIOCORE_DEPRECATED_VERSION(5, 2, "To be removed due to no known users")
0172     QT_MOC_COMPAT void aboutToCreate(KIO::Job *job, const QList<KIO::CopyInfo> &files);
0173 #endif
0174 
0175     /**
0176      * Sends the number of processed files.
0177      * @param job the job that emitted this signal
0178      * @param files the number of processed files
0179      */
0180     void processedFiles(KIO::Job *job, unsigned long files);
0181     /**
0182      * Sends the number of processed directories.
0183      * @param job the job that emitted this signal
0184      * @param dirs the number of processed dirs
0185      */
0186     void processedDirs(KIO::Job *job, unsigned long dirs);
0187 
0188     /**
0189      * The job is copying a file or directory.
0190      *
0191      * Note: This signal is used for progress dialogs, it's not emitted for
0192      * every file or directory (this would be too slow), but every 200ms.
0193      *
0194      * @param job the job that emitted this signal
0195      * @param src the URL of the file or directory that is currently
0196      *             being copied
0197      * @param dest the destination of the current operation
0198      */
0199     void copying(KIO::Job *job, const QUrl &src, const QUrl &dest);
0200     /**
0201      * The job is creating a symbolic link.
0202      *
0203      * Note: This signal is used for progress dialogs, it's not emitted for
0204      * every file or directory (this would be too slow), but every 200ms.
0205      *
0206      * @param job the job that emitted this signal
0207      * @param target the URL of the file or directory that is currently
0208      *             being linked
0209      * @param to the destination of the current operation
0210      */
0211     void linking(KIO::Job *job, const QString &target, const QUrl &to);
0212     /**
0213      * The job is moving a file or directory.
0214      *
0215      * Note: This signal is used for progress dialogs, it's not emitted for
0216      * every file or directory (this would be too slow), but every 200ms.
0217      *
0218      * @param job the job that emitted this signal
0219      * @param from the URL of the file or directory that is currently
0220      *             being moved
0221      * @param to the destination of the current operation
0222      */
0223     void moving(KIO::Job *job, const QUrl &from, const QUrl &to);
0224     /**
0225      * The job is creating the directory @p dir.
0226      *
0227      * This signal is emitted for every directory being created.
0228      *
0229      * @param job the job that emitted this signal
0230      * @param dir the directory that is currently being created
0231      */
0232     void creatingDir(KIO::Job *job, const QUrl &dir);
0233     /**
0234      * The user chose to rename @p from to @p to.
0235      *
0236      * @param job the job that emitted this signal
0237      * @param from the original name
0238      * @param to the new name
0239      */
0240     void renamed(KIO::Job *job, const QUrl &from, const QUrl &to);
0241 
0242     /**
0243      * The job emits this signal when copying or moving a file or directory successfully finished.
0244      * This signal is mainly for the Undo feature.
0245      * If you simply want to know when a copy job is done, use result().
0246      *
0247      * @param job the job that emitted this signal
0248      * @param from the source URL
0249      * @param to the destination URL
0250      * @param mtime the modification time of the source file, hopefully set on the destination file
0251      * too (when the KIO worker supports it).
0252      * @param directory indicates whether a file or directory was successfully copied/moved.
0253      *                  true for a directory, false for file
0254      * @param renamed indicates that the destination URL was created using a
0255      * rename operation (i.e. fast directory moving). true if is has been renamed
0256      */
0257     void copyingDone(KIO::Job *job, const QUrl &from, const QUrl &to, const QDateTime &mtime, bool directory, bool renamed);
0258     /**
0259      * The job is copying or moving a symbolic link, that points to target.
0260      * The new link is created in @p to. The existing one is/was in @p from.
0261      * This signal is mainly for the Undo feature.
0262      * @param job the job that emitted this signal
0263      * @param from the source URL
0264      * @param target the target
0265      * @param to the destination URL
0266      */
0267     void copyingLinkDone(KIO::Job *job, const QUrl &from, const QString &target, const QUrl &to);
0268 protected Q_SLOTS:
0269     void slotResult(KJob *job) override;
0270 
0271 protected:
0272     KIOCORE_NO_EXPORT explicit CopyJob(CopyJobPrivate &dd);
0273     void emitResult();
0274 
0275 private:
0276     Q_DECLARE_PRIVATE(CopyJob)
0277 };
0278 
0279 /**
0280  * Copy a file or directory @p src into the destination @p dest,
0281  * which can be a file (including the final filename) or a directory
0282  * (into which @p src will be copied).
0283  *
0284  * This emulates the cp command completely.
0285  *
0286  * @param src the file or directory to copy
0287  * @param dest the destination
0288  * @param flags copy() supports HideProgressInfo and Overwrite.
0289  * Note: Overwrite has the meaning of both "write into existing directories" and
0290  * "overwrite existing files". However if "dest" exists, then src is copied
0291  * into a subdir of dest, just like "cp" does. Use copyAs if you don't want that.
0292  *
0293  * @return the job handling the operation
0294  * @see copyAs()
0295  */
0296 KIOCORE_EXPORT CopyJob *copy(const QUrl &src, const QUrl &dest, JobFlags flags = DefaultFlags);
0297 
0298 /**
0299  * Copy a file or directory @p src into the destination @p dest,
0300  * which is the destination name in any case, even for a directory.
0301  *
0302  * As opposed to copy(), this doesn't emulate cp, but is the only
0303  * way to copy a directory, giving it a new name and getting an error
0304  * box if a directory already exists with the same name (or writing the
0305  * contents of @p src into @p dest, when using Overwrite).
0306  *
0307  * @param src the file or directory to copy
0308  * @param dest the destination
0309  * @param flags copyAs() supports HideProgressInfo and Overwrite.
0310  * Note: Overwrite has the meaning of both "write into existing directories" and
0311  * "overwrite existing files".
0312  *
0313  * * @return the job handling the operation
0314  */
0315 KIOCORE_EXPORT CopyJob *copyAs(const QUrl &src, const QUrl &dest, JobFlags flags = DefaultFlags);
0316 
0317 /**
0318  * Copy a list of file/dirs @p src into a destination directory @p dest.
0319  *
0320  * @param src the list of files and/or directories
0321  * @param dest the destination
0322  * @param flags copy() supports HideProgressInfo and Overwrite.
0323  * Note: Overwrite has the meaning of both "write into existing directories" and
0324  * "overwrite existing files". However if "dest" exists, then src is copied
0325  * into a subdir of dest, just like "cp" does.
0326  * @return the job handling the operation
0327  */
0328 KIOCORE_EXPORT CopyJob *copy(const QList<QUrl> &src, const QUrl &dest, JobFlags flags = DefaultFlags);
0329 
0330 /**
0331  * Moves a file or directory @p src to the given destination @p dest.
0332  *
0333  * @param src the file or directory to copy
0334  * @param dest the destination
0335  * @param flags move() supports HideProgressInfo and Overwrite.
0336  * Note: Overwrite has the meaning of both "write into existing directories" and
0337  * "overwrite existing files". However if "dest" exists, then src is copied
0338  * into a subdir of dest, just like "cp" does.
0339  * @return the job handling the operation
0340  * @see copy()
0341  * @see moveAs()
0342  */
0343 KIOCORE_EXPORT CopyJob *move(const QUrl &src, const QUrl &dest, JobFlags flags = DefaultFlags);
0344 /**
0345  * Moves a file or directory @p src to the given destination @p dest. Unlike move()
0346  * this operation will not move @p src into @p dest when @p dest exists: it will
0347  * either fail, or move the contents of @p src into it if Overwrite is set.
0348  *
0349  * @param src the file or directory to copy
0350  * @param dest the destination
0351  * @param flags moveAs() supports HideProgressInfo and Overwrite.
0352  * Note: Overwrite has the meaning of both "write into existing directories" and
0353  * "overwrite existing files".
0354  * @return the job handling the operation
0355  * @see copyAs()
0356  */
0357 KIOCORE_EXPORT CopyJob *moveAs(const QUrl &src, const QUrl &dest, JobFlags flags = DefaultFlags);
0358 /**
0359  * Moves a list of files or directories @p src to the given destination @p dest.
0360  *
0361  * @param src the list of files or directories to copy
0362  * @param dest the destination
0363  * @param flags move() supports HideProgressInfo and Overwrite.
0364  * Note: Overwrite has the meaning of both "write into existing directories" and
0365  * "overwrite existing files". However if "dest" exists, then src is copied
0366  * into a subdir of dest, just like "cp" does.
0367  * @return the job handling the operation
0368  * @see copy()
0369  */
0370 KIOCORE_EXPORT CopyJob *move(const QList<QUrl> &src, const QUrl &dest, JobFlags flags = DefaultFlags);
0371 
0372 /**
0373  * Create a link.
0374  * If the protocols and hosts are the same, a Unix symlink will be created.
0375  * Otherwise, a .desktop file of Type Link and pointing to the src URL will be created.
0376  *
0377  * @param src The existing file or directory, 'target' of the link.
0378  * @param destDir Destination directory where the link will be created.
0379  * @param flags link() supports HideProgressInfo only
0380  * @return the job handling the operation
0381  */
0382 KIOCORE_EXPORT CopyJob *link(const QUrl &src, const QUrl &destDir, JobFlags flags = DefaultFlags);
0383 
0384 /**
0385  * Create several links
0386  * If the protocols and hosts are the same, a Unix symlink will be created.
0387  * Otherwise, a .desktop file of Type Link and pointing to the src URL will be created.
0388  *
0389  * @param src The existing files or directories, 'targets' of the link.
0390  * @param destDir Destination directory where the links will be created.
0391  * @param flags link() supports HideProgressInfo only
0392  * @return the job handling the operation
0393  * @see link()
0394  */
0395 KIOCORE_EXPORT CopyJob *link(const QList<QUrl> &src, const QUrl &destDir, JobFlags flags = DefaultFlags);
0396 
0397 /**
0398  * Create a link. Unlike link() this operation will fail when @p dest is an existing
0399  * directory rather than the final name for the link.
0400  * If the protocols and hosts are the same, a Unix symlink will be created.
0401  * Otherwise, a .desktop file of Type Link and pointing to the src URL will be created.
0402  *
0403  * @param src The existing file or directory, 'target' of the link.
0404  * @param dest Destination (i.e. the final symlink)
0405  * @param flags linkAs() supports HideProgressInfo only
0406  * @return the job handling the operation
0407  * @see link ()
0408  * @see copyAs()
0409  */
0410 KIOCORE_EXPORT CopyJob *linkAs(const QUrl &src, const QUrl &dest, JobFlags flags = DefaultFlags);
0411 
0412 /**
0413  * Trash a file or directory.
0414  * This is currently only supported for local files and directories.
0415  * Use QUrl::fromLocalFile to create a URL from a local file path.
0416  *
0417  * @param src file to delete
0418  * @param flags trash() supports HideProgressInfo only
0419  * @return the job handling the operation
0420  */
0421 KIOCORE_EXPORT CopyJob *trash(const QUrl &src, JobFlags flags = DefaultFlags);
0422 
0423 /**
0424  * Trash a list of files or directories.
0425  * This is currently only supported for local files and directories.
0426  *
0427  * @param src the files to delete
0428  * @param flags trash() supports HideProgressInfo only
0429  * @return the job handling the operation
0430  */
0431 KIOCORE_EXPORT CopyJob *trash(const QList<QUrl> &src, JobFlags flags = DefaultFlags);
0432 
0433 }
0434 
0435 #endif