File indexing completed on 2024-04-14 03:53:29

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2000 Simon Hausmann <hausmann@kde.org>
0004     SPDX-FileCopyrightText: 2006, 2008 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KIO_FILEUNDOMANAGER_H
0010 #define KIO_FILEUNDOMANAGER_H
0011 
0012 #include <QObject>
0013 #include <QUrl>
0014 
0015 #include "kiowidgets_export.h"
0016 
0017 #include <memory>
0018 
0019 class QDateTime;
0020 
0021 namespace KIO
0022 {
0023 class Job;
0024 class CopyJob;
0025 class FileUndoManagerPrivate;
0026 class FileUndoManagerSingleton;
0027 class CommandRecorder;
0028 class UndoCommand;
0029 class UndoJob;
0030 
0031 /**
0032  * @class KIO::FileUndoManager fileundomanager.h <KIO/FileUndoManager>
0033  *
0034  * FileUndoManager: makes it possible to undo kio jobs.
0035  * This class is a singleton, use self() to access its only instance.
0036  */
0037 class KIOWIDGETS_EXPORT FileUndoManager : public QObject
0038 {
0039     Q_OBJECT
0040 public:
0041     /**
0042      * @return the FileUndoManager instance
0043      */
0044     static FileUndoManager *self();
0045 
0046     /**
0047      * Interface for the gui handling of FileUndoManager.
0048      * This includes three events currently:
0049      * - error when undoing a job
0050      * - (until KF 5.78) confirm deletion before undoing a copy job
0051      * - confirm deletion when the copied file has been modified afterwards
0052      *
0053      * By default UiInterface shows message boxes in all three cases;
0054      * applications can reimplement this interface to provide different user interfaces.
0055      */
0056     class KIOWIDGETS_EXPORT UiInterface
0057     {
0058     public:
0059         UiInterface();
0060         virtual ~UiInterface();
0061 
0062         /**
0063          * Sets whether to show progress info when running the KIO jobs for undoing.
0064          */
0065         void setShowProgressInfo(bool b);
0066         /**
0067          * @returns whether progress info dialogs are shown while undoing.
0068          */
0069         bool showProgressInfo() const;
0070 
0071         /**
0072          * Sets the parent widget to use for message boxes.
0073          */
0074         void setParentWidget(QWidget *parentWidget);
0075 
0076         /**
0077          * @return the parent widget passed to the last call to undo(parentWidget), or @c nullptr.
0078          */
0079         QWidget *parentWidget() const;
0080 
0081         /**
0082          * Called when an undo job errors; default implementation displays a message box.
0083          */
0084         virtual void jobError(KIO::Job *job);
0085 
0086         /**
0087          * Called when dest was modified since it was copied from src.
0088          * Note that this is called after confirmDeletion.
0089          * Return true if we should proceed with deleting dest.
0090          */
0091         virtual bool copiedFileWasModified(const QUrl &src, const QUrl &dest, const QDateTime &srcTime, const QDateTime &destTime);
0092 
0093         // TODO KF6 replace hook with virtual AskUserActionInterface* askUserActionInterface(); // (does not take ownership)
0094         enum { HookGetAskUserActionInterface = 1 };
0095         /**
0096          * \internal, for future extensions
0097          */
0098         virtual void virtual_hook(int id, void *data);
0099 
0100     private:
0101         class UiInterfacePrivate;
0102         UiInterfacePrivate *d;
0103     };
0104 
0105     /**
0106      * Set a new UiInterface implementation.
0107      * This deletes the previous one.
0108      * @param ui the UiInterface instance, which becomes owned by the undo manager.
0109      */
0110     void setUiInterface(UiInterface *ui);
0111 
0112     /**
0113      * @return the UiInterface instance passed to setUiInterface.
0114      * This is useful for calling setParentWidget on it. Never delete it!
0115      */
0116     UiInterface *uiInterface() const;
0117 
0118     /**
0119      * The type of job.
0120      */
0121     enum CommandType {
0122         Copy,
0123         Move,
0124         Rename,
0125         Link,
0126         Mkdir,
0127         Trash,
0128         Put, ///< Represents the creation of a file from data in memory. Used when pasting data from clipboard or drag-n-drop. @since 4.7
0129         Mkpath, ///< Represents a KIO::mkpath() job. @since 5.4
0130         BatchRename ///< Represents a KIO::batchRename() job. Used when renaming multiple files. @since 5.42
0131     };
0132 
0133     /**
0134      * Record this job while it's happening and add a command for it so that the user can undo it.
0135      * The signal jobRecordingStarted() is emitted.
0136      * @param op the type of job - which is also the type of command that will be created for it
0137      * @param src list of source urls. This is empty for Mkdir, Mkpath, Put operations.
0138      * @param dst destination url
0139      * @param job the job to record
0140      */
0141     void recordJob(CommandType op, const QList<QUrl> &src, const QUrl &dst, KIO::Job *job);
0142 
0143     /**
0144      * Record this CopyJob while it's happening and add a command for it so that the user can undo it.
0145      * The signal jobRecordingStarted() is emitted.
0146      */
0147     void recordCopyJob(KIO::CopyJob *copyJob);
0148 
0149     /**
0150      * @return true if undo is possible. Usually used for enabling/disabling the undo action.
0151      *
0152      * @since 5.79
0153      */
0154     bool isUndoAvailable() const;
0155 
0156     /**
0157      * @return the current text for the undo action.
0158      */
0159     QString undoText() const;
0160 
0161     /**
0162      * These two functions are useful when wrapping FileUndoManager and adding custom commands.
0163      * Each command has a unique ID. You can get a new serial number for a custom command
0164      * with newCommandSerialNumber(), and then when you want to undo, check if the command
0165      * FileUndoManager would undo is newer or older than your custom command.
0166      */
0167     quint64 newCommandSerialNumber();
0168     quint64 currentCommandSerialNumber() const;
0169 
0170 public Q_SLOTS:
0171     /**
0172      * Undoes the last command
0173      * Remember to call uiInterface()->setParentWidget(parentWidget) first,
0174      * if you have multiple mainwindows.
0175      *
0176      * This operation is asynchronous.
0177      * undoJobFinished will be emitted once the undo is complete.
0178      */
0179     void undo(); // TODO pass QWindow*, for askUserInterface->askUserDelete and error handling etc.
0180 
0181 Q_SIGNALS:
0182     /// Emitted when the value of isUndoAvailable() changes
0183     void undoAvailable(bool avail);
0184 
0185     /// Emitted when the value of undoText() changes
0186     void undoTextChanged(const QString &text);
0187 
0188     /// Emitted when an undo job finishes. Used for unit testing.
0189     void undoJobFinished();
0190 
0191     /**
0192      * Emitted when a job recording has been started by FileUndoManager::recordJob()
0193      * or FileUndoManager::recordCopyJob(). After the job recording has been finished,
0194      * the signal jobRecordingFinished() will be emitted.
0195      */
0196     void jobRecordingStarted(CommandType op);
0197 
0198     /**
0199      * Emitted when a job that has been recorded by FileUndoManager::recordJob()
0200      * or FileUndoManager::recordCopyJob has been finished. The command
0201      * is now available for an undo-operation.
0202      */
0203     void jobRecordingFinished(FileUndoManager::CommandType op);
0204 
0205 private:
0206     KIOWIDGETS_NO_EXPORT FileUndoManager();
0207     KIOWIDGETS_NO_EXPORT ~FileUndoManager() override;
0208     friend class FileUndoManagerSingleton;
0209 
0210     friend class UndoJob;
0211     friend class CommandRecorder;
0212 
0213     friend class FileUndoManagerPrivate;
0214     std::unique_ptr<FileUndoManagerPrivate> d;
0215 };
0216 
0217 } // namespace
0218 
0219 #endif