KIO

fileundomanager.h
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2000 Simon Hausmann <hausmann@kde.org>
4 SPDX-FileCopyrightText: 2006, 2008 David Faure <faure@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#ifndef KIO_FILEUNDOMANAGER_H
10#define KIO_FILEUNDOMANAGER_H
11
12#include <QObject>
13#include <QUrl>
14
15#include "kiowidgets_export.h"
16
17#include <memory>
18
19class QDateTime;
20
21namespace KIO
22{
23class Job;
24class CopyJob;
25class FileUndoManagerPrivate;
26class FileUndoManagerSingleton;
27class CommandRecorder;
28class UndoCommand;
29class UndoJob;
30
31/**
32 * @class KIO::FileUndoManager fileundomanager.h <KIO/FileUndoManager>
33 *
34 * FileUndoManager: makes it possible to undo kio jobs.
35 * This class is a singleton, use self() to access its only instance.
36 */
37class KIOWIDGETS_EXPORT FileUndoManager : public QObject
38{
39 Q_OBJECT
40public:
41 /**
42 * @return the FileUndoManager instance
43 */
44 static FileUndoManager *self();
45
46 /**
47 * Interface for the gui handling of FileUndoManager.
48 * This includes three events currently:
49 * - error when undoing a job
50 * - (until KF 5.78) confirm deletion before undoing a copy job
51 * - confirm deletion when the copied file has been modified afterwards
52 *
53 * By default UiInterface shows message boxes in all three cases;
54 * applications can reimplement this interface to provide different user interfaces.
55 */
56 class KIOWIDGETS_EXPORT UiInterface
57 {
58 public:
60 virtual ~UiInterface();
61
62 /**
63 * Sets whether to show progress info when running the KIO jobs for undoing.
64 */
65 void setShowProgressInfo(bool b);
66 /**
67 * @returns whether progress info dialogs are shown while undoing.
68 */
69 bool showProgressInfo() const;
70
71 /**
72 * Sets the parent widget to use for message boxes.
73 */
74 void setParentWidget(QWidget *parentWidget);
75
76 /**
77 * @return the parent widget passed to the last call to undo(parentWidget), or @c nullptr.
78 */
79 QWidget *parentWidget() const;
80
81 /**
82 * Called when an undo job errors; default implementation displays a message box.
83 */
84 virtual void jobError(KIO::Job *job);
85
86 /**
87 * Called when dest was modified since it was copied from src.
88 * Note that this is called after confirmDeletion.
89 * Return true if we should proceed with deleting dest.
90 */
91 virtual bool copiedFileWasModified(const QUrl &src, const QUrl &dest, const QDateTime &srcTime, const QDateTime &destTime);
92
93 // TODO KF6 replace hook with virtual AskUserActionInterface* askUserActionInterface(); // (does not take ownership)
94 enum {
95 HookGetAskUserActionInterface = 1
96 };
97 /**
98 * \internal, for future extensions
99 */
100 virtual void virtual_hook(int id, void *data);
101
102 private:
103 class UiInterfacePrivate;
104 UiInterfacePrivate *d;
105 };
106
107 /**
108 * Set a new UiInterface implementation.
109 * This deletes the previous one.
110 * @param ui the UiInterface instance, which becomes owned by the undo manager.
111 */
112 void setUiInterface(UiInterface *ui);
113
114 /**
115 * @return the UiInterface instance passed to setUiInterface.
116 * This is useful for calling setParentWidget on it. Never delete it!
117 */
118 UiInterface *uiInterface() const;
119
120 /**
121 * The type of job.
122 */
124 Copy,
125 Move,
126 Rename,
127 Link,
128 Mkdir,
129 Trash,
130 Put, ///< Represents the creation of a file from data in memory. Used when pasting data from clipboard or drag-n-drop. @since 4.7
131 Mkpath, ///< Represents a KIO::mkpath() job. @since 5.4
132 BatchRename ///< Represents a KIO::batchRename() job. Used when renaming multiple files. @since 5.42
133 };
134
135 /**
136 * Record this job while it's happening and add a command for it so that the user can undo it.
137 * The signal jobRecordingStarted() is emitted.
138 * @param op the type of job - which is also the type of command that will be created for it
139 * @param src list of source urls. This is empty for Mkdir, Mkpath, Put operations.
140 * @param dst destination url
141 * @param job the job to record
142 */
143 void recordJob(CommandType op, const QList<QUrl> &src, const QUrl &dst, KIO::Job *job);
144
145 /**
146 * Record this CopyJob while it's happening and add a command for it so that the user can undo it.
147 * The signal jobRecordingStarted() is emitted.
148 */
149 void recordCopyJob(KIO::CopyJob *copyJob);
150
151 /**
152 * @return true if undo is possible. Usually used for enabling/disabling the undo action.
153 *
154 * @since 5.79
155 */
156 bool isUndoAvailable() const;
157
158 /**
159 * @return the current text for the undo action.
160 */
161 QString undoText() const;
162
163 /**
164 * These two functions are useful when wrapping FileUndoManager and adding custom commands.
165 * Each command has a unique ID. You can get a new serial number for a custom command
166 * with newCommandSerialNumber(), and then when you want to undo, check if the command
167 * FileUndoManager would undo is newer or older than your custom command.
168 */
169 quint64 newCommandSerialNumber();
170 quint64 currentCommandSerialNumber() const;
171
172public Q_SLOTS:
173 /**
174 * Undoes the last command
175 * Remember to call uiInterface()->setParentWidget(parentWidget) first,
176 * if you have multiple mainwindows.
177 *
178 * This operation is asynchronous.
179 * undoJobFinished will be emitted once the undo is complete.
180 */
181 void undo(); // TODO pass QWindow*, for askUserInterface->askUserDelete and error handling etc.
182
183Q_SIGNALS:
184 /// Emitted when the value of isUndoAvailable() changes
185 void undoAvailable(bool avail);
186
187 /// Emitted when the value of undoText() changes
188 void undoTextChanged(const QString &text);
189
190 /// Emitted when an undo job finishes. Used for unit testing.
192
193 /**
194 * Emitted when a job recording has been started by FileUndoManager::recordJob()
195 * or FileUndoManager::recordCopyJob(). After the job recording has been finished,
196 * the signal jobRecordingFinished() will be emitted.
197 */
199
200 /**
201 * Emitted when a job that has been recorded by FileUndoManager::recordJob()
202 * or FileUndoManager::recordCopyJob has been finished. The command
203 * is now available for an undo-operation.
204 */
206
207private:
208 KIOWIDGETS_NO_EXPORT FileUndoManager();
209 KIOWIDGETS_NO_EXPORT ~FileUndoManager() override;
210 friend class FileUndoManagerSingleton;
211
212 friend class UndoJob;
213 friend class CommandRecorder;
214
215 friend class FileUndoManagerPrivate;
216 std::unique_ptr<FileUndoManagerPrivate> d;
217};
218
219} // namespace
220
221#endif
CopyJob is used to move, copy or symlink files and directories.
Interface for the gui handling of FileUndoManager.
FileUndoManager: makes it possible to undo kio jobs.
void undoJobFinished()
Emitted when an undo job finishes. Used for unit testing.
void jobRecordingFinished(FileUndoManager::CommandType op)
Emitted when a job that has been recorded by FileUndoManager::recordJob() or FileUndoManager::recordC...
void undoAvailable(bool avail)
Emitted when the value of isUndoAvailable() changes.
void jobRecordingStarted(CommandType op)
Emitted when a job recording has been started by FileUndoManager::recordJob() or FileUndoManager::rec...
void undoTextChanged(const QString &text)
Emitted when the value of undoText() changes.
CommandType
The type of job.
@ Put
Represents the creation of a file from data in memory. Used when pasting data from clipboard or drag-...
@ Mkpath
Represents a KIO::mkpath() job.
The base class for all jobs.
A namespace for KIO globals.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:09:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.