File indexing completed on 2024-09-29 06:32:09

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