KIO

copyjob.h
1// -*- c++ -*-
2/*
3 This file is part of the KDE libraries
4 SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
5 SPDX-FileCopyrightText: 2000-2006 David Faure <faure@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#ifndef KIO_COPYJOB_H
11#define KIO_COPYJOB_H
12
13#include <QObject>
14#include <QStringList>
15#include <QUrl>
16
17#include "job_base.h"
18#include "kiocore_export.h"
19
20class QDateTime;
21
22namespace KIO
23{
24
25class CopyJobPrivate;
26/**
27 * @class KIO::CopyJob copyjob.h <KIO/CopyJob>
28 *
29 * CopyJob is used to move, copy or symlink files and directories.
30 * Don't create the job directly, but use KIO::copy(),
31 * KIO::move(), KIO::link() and friends.
32 *
33 * @see KIO::copy()
34 * @see KIO::copyAs()
35 * @see KIO::move()
36 * @see KIO::moveAs()
37 * @see KIO::link()
38 * @see KIO::linkAs()
39 */
40class KIOCORE_EXPORT CopyJob : public Job
41{
42 Q_OBJECT
43
44public:
45 /**
46 * Defines the mode of the operation
47 */
48 enum CopyMode {
49 Copy,
50 Move,
51 Link
52 };
53
54 ~CopyJob() override;
55
56 /**
57 * Returns the mode of the operation (copy, move, or link),
58 * depending on whether KIO::copy(), KIO::move() or KIO::link() was called.
59 */
60 CopyMode operationMode() const;
61
62 /**
63 * Returns the list of source URLs.
64 * @return the list of source URLs.
65 */
66 QList<QUrl> srcUrls() const;
67
68 /**
69 * Returns the destination URL.
70 * @return the destination URL
71 */
72 QUrl destUrl() const;
73
74 /**
75 * By default the permissions of the copied files will be those of the source files.
76 *
77 * But when copying "template" files to "new" files, people prefer the umask
78 * to apply, rather than the template's permissions.
79 * For that case, call setDefaultPermissions(true)
80 */
81 void setDefaultPermissions(bool b);
82
83 /**
84 * Skip copying or moving any file when the destination already exists,
85 * instead of the default behavior (interactive mode: showing a dialog to the user,
86 * non-interactive mode: aborting with an error).
87 * Initially added for a unit test.
88 * \since 4.2
89 */
90 void setAutoSkip(bool autoSkip);
91
92 /**
93 * Rename files automatically when the destination already exists,
94 * instead of the default behavior (interactive mode: showing a dialog to the user,
95 * non-interactive mode: aborting with an error).
96 * Initially added for a unit test.
97 * \since 4.7
98 */
99 void setAutoRename(bool autoRename);
100
101 /**
102 * Reuse any directory that already exists, instead of the default behavior
103 * (interactive mode: showing a dialog to the user,
104 * non-interactive mode: aborting with an error).
105 * \since 4.2
106 */
107 void setWriteIntoExistingDirectories(bool overwriteAllDirs);
108
109 /**
110 * Reimplemented for internal reasons
111 */
112 bool doSuspend() override;
113
114 /**
115 * Reimplemented for internal reasons
116 */
117 bool doResume() override;
118
119Q_SIGNALS:
120 /**
121 * Sends the number of processed files.
122 * @param job the job that emitted this signal
123 * @param files the number of processed files
124 */
125 void processedFiles(KIO::Job *job, unsigned long files);
126 /**
127 * Sends the number of processed directories.
128 * @param job the job that emitted this signal
129 * @param dirs the number of processed dirs
130 */
131 void processedDirs(KIO::Job *job, unsigned long dirs);
132
133 /**
134 * The job is copying a file or directory.
135 *
136 * Note: This signal is used for progress dialogs, it's not emitted for
137 * every file or directory (this would be too slow), but every 200ms.
138 *
139 * @param job the job that emitted this signal
140 * @param src the URL of the file or directory that is currently
141 * being copied
142 * @param dest the destination of the current operation
143 */
144 void copying(KIO::Job *job, const QUrl &src, const QUrl &dest);
145 /**
146 * The job is creating a symbolic link.
147 *
148 * Note: This signal is used for progress dialogs, it's not emitted for
149 * every file or directory (this would be too slow), but every 200ms.
150 *
151 * @param job the job that emitted this signal
152 * @param target the URL of the file or directory that is currently
153 * being linked
154 * @param to the destination of the current operation
155 */
156 void linking(KIO::Job *job, const QString &target, const QUrl &to);
157 /**
158 * The job is moving a file or directory.
159 *
160 * Note: This signal is used for progress dialogs, it's not emitted for
161 * every file or directory (this would be too slow), but every 200ms.
162 *
163 * @param job the job that emitted this signal
164 * @param from the URL of the file or directory that is currently
165 * being moved
166 * @param to the destination of the current operation
167 */
168 void moving(KIO::Job *job, const QUrl &from, const QUrl &to);
169 /**
170 * The job is creating the directory @p dir.
171 *
172 * This signal is emitted for every directory being created.
173 *
174 * @param job the job that emitted this signal
175 * @param dir the directory that is currently being created
176 */
177 void creatingDir(KIO::Job *job, const QUrl &dir);
178 /**
179 * The user chose to rename @p from to @p to.
180 *
181 * @param job the job that emitted this signal
182 * @param from the original name
183 * @param to the new name
184 */
185 void renamed(KIO::Job *job, const QUrl &from, const QUrl &to);
186
187 /**
188 * The job emits this signal when copying or moving a file or directory successfully finished.
189 * This signal is mainly for the Undo feature.
190 * If you simply want to know when a copy job is done, use result().
191 *
192 * @param job the job that emitted this signal
193 * @param from the source URL
194 * @param to the destination URL
195 * @param mtime the modification time of the source file, hopefully set on the destination file
196 * too (when the KIO worker supports it).
197 * @param directory indicates whether a file or directory was successfully copied/moved.
198 * true for a directory, false for file
199 * @param renamed indicates that the destination URL was created using a
200 * rename operation (i.e. fast directory moving). true if is has been renamed
201 */
202 void copyingDone(KIO::Job *job, const QUrl &from, const QUrl &to, const QDateTime &mtime, bool directory, bool renamed);
203 /**
204 * The job is copying or moving a symbolic link, that points to target.
205 * The new link is created in @p to. The existing one is/was in @p from.
206 * This signal is mainly for the Undo feature.
207 * @param job the job that emitted this signal
208 * @param from the source URL
209 * @param target the target
210 * @param to the destination URL
211 */
212 void copyingLinkDone(KIO::Job *job, const QUrl &from, const QString &target, const QUrl &to);
213protected Q_SLOTS:
214 void slotResult(KJob *job) override;
215
216protected:
217 KIOCORE_NO_EXPORT explicit CopyJob(CopyJobPrivate &dd);
218 void emitResult();
219
220private:
221 Q_DECLARE_PRIVATE(CopyJob)
222};
223
224/**
225 * Copy a file or directory @p src into the destination @p dest,
226 * which can be a file (including the final filename) or a directory
227 * (into which @p src will be copied).
228 *
229 * This emulates the cp command completely.
230 *
231 * @param src the file or directory to copy
232 * @param dest the destination
233 * @param flags copy() supports HideProgressInfo and Overwrite.
234 * Note: Overwrite has the meaning of both "write into existing directories" and
235 * "overwrite existing files". However if "dest" exists, then src is copied
236 * into a subdir of dest, just like "cp" does. Use copyAs if you don't want that.
237 *
238 * @return the job handling the operation
239 * @see copyAs()
240 */
241KIOCORE_EXPORT CopyJob *copy(const QUrl &src, const QUrl &dest, JobFlags flags = DefaultFlags);
242
243/**
244 * Copy a file or directory @p src into the destination @p dest,
245 * which is the destination name in any case, even for a directory.
246 *
247 * As opposed to copy(), this doesn't emulate cp, but is the only
248 * way to copy a directory, giving it a new name and getting an error
249 * box if a directory already exists with the same name (or writing the
250 * contents of @p src into @p dest, when using Overwrite).
251 *
252 * @param src the file or directory to copy
253 * @param dest the destination
254 * @param flags copyAs() supports HideProgressInfo and Overwrite.
255 * Note: Overwrite has the meaning of both "write into existing directories" and
256 * "overwrite existing files".
257 *
258 * * @return the job handling the operation
259 */
260KIOCORE_EXPORT CopyJob *copyAs(const QUrl &src, const QUrl &dest, JobFlags flags = DefaultFlags);
261
262/**
263 * Copy a list of file/dirs @p src into a destination directory @p dest.
264 *
265 * @param src the list of files and/or directories
266 * @param dest the destination
267 * @param flags copy() supports HideProgressInfo and Overwrite.
268 * Note: Overwrite has the meaning of both "write into existing directories" and
269 * "overwrite existing files". However if "dest" exists, then src is copied
270 * into a subdir of dest, just like "cp" does.
271 * @return the job handling the operation
272 */
273KIOCORE_EXPORT CopyJob *copy(const QList<QUrl> &src, const QUrl &dest, JobFlags flags = DefaultFlags);
274
275/**
276 * Moves a file or directory @p src to the given destination @p dest.
277 *
278 * @param src the file or directory to copy
279 * @param dest the destination
280 * @param flags move() supports HideProgressInfo and Overwrite.
281 * Note: Overwrite has the meaning of both "write into existing directories" and
282 * "overwrite existing files". However if "dest" exists, then src is copied
283 * into a subdir of dest, just like "cp" does.
284 * @return the job handling the operation
285 * @see copy()
286 * @see moveAs()
287 */
288KIOCORE_EXPORT CopyJob *move(const QUrl &src, const QUrl &dest, JobFlags flags = DefaultFlags);
289/**
290 * Moves a file or directory @p src to the given destination @p dest. Unlike move()
291 * this operation will not move @p src into @p dest when @p dest exists: it will
292 * either fail, or move the contents of @p src into it if Overwrite is set.
293 *
294 * @param src the file or directory to copy
295 * @param dest the destination
296 * @param flags moveAs() supports HideProgressInfo and Overwrite.
297 * Note: Overwrite has the meaning of both "write into existing directories" and
298 * "overwrite existing files".
299 * @return the job handling the operation
300 * @see copyAs()
301 */
302KIOCORE_EXPORT CopyJob *moveAs(const QUrl &src, const QUrl &dest, JobFlags flags = DefaultFlags);
303/**
304 * Moves a list of files or directories @p src to the given destination @p dest.
305 *
306 * @param src the list of files or directories to copy
307 * @param dest the destination
308 * @param flags move() supports HideProgressInfo and Overwrite.
309 * Note: Overwrite has the meaning of both "write into existing directories" and
310 * "overwrite existing files". However if "dest" exists, then src is copied
311 * into a subdir of dest, just like "cp" does.
312 * @return the job handling the operation
313 * @see copy()
314 */
315KIOCORE_EXPORT CopyJob *move(const QList<QUrl> &src, const QUrl &dest, JobFlags flags = DefaultFlags);
316
317/**
318 * Create a link.
319 * If the protocols and hosts are the same, a Unix symlink will be created.
320 * Otherwise, a .desktop file of Type Link and pointing to the src URL will be created.
321 *
322 * @param src The existing file or directory, 'target' of the link.
323 * @param destDir Destination directory where the link will be created.
324 * @param flags link() supports HideProgressInfo only
325 * @return the job handling the operation
326 */
327KIOCORE_EXPORT CopyJob *link(const QUrl &src, const QUrl &destDir, JobFlags flags = DefaultFlags);
328
329/**
330 * Create several links
331 * If the protocols and hosts are the same, a Unix symlink will be created.
332 * Otherwise, a .desktop file of Type Link and pointing to the src URL will be created.
333 *
334 * @param src The existing files or directories, 'targets' of the link.
335 * @param destDir Destination directory where the links will be created.
336 * @param flags link() supports HideProgressInfo only
337 * @return the job handling the operation
338 * @see link()
339 */
340KIOCORE_EXPORT CopyJob *link(const QList<QUrl> &src, const QUrl &destDir, JobFlags flags = DefaultFlags);
341
342/**
343 * Create a link. Unlike link() this operation will fail when @p dest is an existing
344 * directory rather than the final name for the link.
345 * If the protocols and hosts are the same, a Unix symlink will be created.
346 * Otherwise, a .desktop file of Type Link and pointing to the src URL will be created.
347 *
348 * @param src The existing file or directory, 'target' of the link.
349 * @param dest Destination (i.e. the final symlink)
350 * @param flags linkAs() supports HideProgressInfo only
351 * @return the job handling the operation
352 * @see link ()
353 * @see copyAs()
354 */
355KIOCORE_EXPORT CopyJob *linkAs(const QUrl &src, const QUrl &dest, JobFlags flags = DefaultFlags);
356
357/**
358 * Trash a file or directory.
359 * This is currently only supported for local files and directories.
360 * Use QUrl::fromLocalFile to create a URL from a local file path.
361 *
362 * @param src file to delete
363 * @param flags trash() supports HideProgressInfo only
364 * @return the job handling the operation
365 */
366KIOCORE_EXPORT CopyJob *trash(const QUrl &src, JobFlags flags = DefaultFlags);
367
368/**
369 * Trash a list of files or directories.
370 * This is currently only supported for local files and directories.
371 *
372 * @param src the files to delete
373 * @param flags trash() supports HideProgressInfo only
374 * @return the job handling the operation
375 */
376KIOCORE_EXPORT CopyJob *trash(const QList<QUrl> &src, JobFlags flags = DefaultFlags);
377
378}
379
380#endif
CopyJob is used to move, copy or symlink files and directories.
CopyMode
Defines the mode of the operation.
Definition copyjob.h:48
void copyingLinkDone(KIO::Job *job, const QUrl &from, const QString &target, const QUrl &to)
The job is copying or moving a symbolic link, that points to target.
void copying(KIO::Job *job, const QUrl &src, const QUrl &dest)
The job is copying a file or directory.
void creatingDir(KIO::Job *job, const QUrl &dir)
The job is creating the directory dir.
void processedDirs(KIO::Job *job, unsigned long dirs)
Sends the number of processed directories.
void linking(KIO::Job *job, const QString &target, const QUrl &to)
The job is creating a symbolic link.
void moving(KIO::Job *job, const QUrl &from, const QUrl &to)
The job is moving a file or directory.
void processedFiles(KIO::Job *job, unsigned long files)
Sends the number of processed files.
void renamed(KIO::Job *job, const QUrl &from, const QUrl &to)
The user chose to rename from to to.
void copyingDone(KIO::Job *job, const QUrl &from, const QUrl &to, const QDateTime &mtime, bool directory, bool renamed)
The job emits this signal when copying or moving a file or directory successfully finished.
The base class for all jobs.
A namespace for KIO globals.
KIOCORE_EXPORT CopyJob * moveAs(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Moves a file or directory src to the given destination dest.
Definition copyjob.cpp:2669
KIOCORE_EXPORT CopyJob * move(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Moves a file or directory src to the given destination dest.
Definition copyjob.cpp:2657
KIOCORE_EXPORT CopyJob * link(const QUrl &src, const QUrl &destDir, JobFlags flags=DefaultFlags)
Create a link.
Definition copyjob.cpp:2691
KIOCORE_EXPORT CopyJob * copy(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Copy a file or directory src into the destination dest, which can be a file (including the final file...
Definition copyjob.cpp:2635
KIOCORE_EXPORT CopyJob * linkAs(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Create a link.
Definition copyjob.cpp:2703
KIOCORE_EXPORT CopyJob * trash(const QUrl &src, JobFlags flags=DefaultFlags)
Trash a file or directory.
Definition copyjob.cpp:2710
@ DefaultFlags
Show the progress info GUI, no Resume and no Overwrite.
Definition job_base.h:246
KIOCORE_EXPORT CopyJob * copyAs(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Copy a file or directory src into the destination dest, which is the destination name in any case,...
Definition copyjob.cpp:2643
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.