1 | /* This file is part of the KDE project |
2 | Copyright 2000-2007 David Faure <faure@kde.org> |
3 | Copyright 2003 Waldo Bastian <bastian@kde.org> |
4 | Copyright 2001-2002 Alexander Neundorf <neundorf@kde.org> |
5 | Copyright 2002 Michael Brade <brade@kde.org> |
6 | |
7 | This library is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU Library General Public |
9 | License as published by the Free Software Foundation; either |
10 | version 2 of the License, or (at your option) version 3. |
11 | |
12 | This library is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | Library General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Library General Public License |
18 | along with this library; see the file COPYING.LIB. If not, write to |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #ifndef __konq_operations_h__ |
24 | #define __konq_operations_h__ |
25 | |
26 | #include <kurl.h> |
27 | #include <libkonq_export.h> |
28 | |
29 | #include <QtCore/QObject> |
30 | #include <QDropEvent> |
31 | |
32 | class KJob; |
33 | namespace KIO { class Job; class SimpleJob; struct CopyInfo; } |
34 | class QWidget; |
35 | class KFileItem; |
36 | class KFileItemListProperties; |
37 | |
38 | /** |
39 | * Implements file operations (move,del,trash,paste,copy,move,link...) |
40 | * for file managers |
41 | */ |
42 | class LIBKONQ_EXPORT KonqOperations : public QObject |
43 | { |
44 | Q_OBJECT |
45 | protected: |
46 | KonqOperations( QWidget * parent ); |
47 | virtual ~KonqOperations(); |
48 | |
49 | public: |
50 | /** |
51 | * Pop up properties dialog for mimetype @p mimeType. |
52 | * @param parent parent widget (for dialogs) |
53 | */ |
54 | static void editMimeType( const QString & mimeType, QWidget* parent ); |
55 | |
56 | enum Operation { TRASH, DEL, COPY, MOVE, LINK, EMPTYTRASH, STAT, MKDIR, RESTORE, UNKNOWN, PUT, RENAME }; |
57 | /** |
58 | * Delete the @p selectedUrls if possible. |
59 | * |
60 | * @param parent parent widget (for error dialog box if any) |
61 | * @param method should be TRASH or DEL |
62 | * @param selectedUrls the URLs to be deleted |
63 | */ |
64 | static void del( QWidget * parent, Operation method, const KUrl::List & selectedUrls ); |
65 | |
66 | /** |
67 | * Copy the @p selectedUrls to the destination @p destUrl. |
68 | * |
69 | * @param parent parent widget (for error dialog box if any) |
70 | * @param method should be COPY, MOVE or LINK |
71 | * @param selectedUrls the URLs to copy |
72 | * @param destUrl destination of the copy |
73 | * |
74 | * @todo document restrictions on the kind of destination |
75 | */ |
76 | static void copy( QWidget * parent, Operation method, const KUrl::List & selectedUrls, const KUrl& destUrl ); |
77 | /** |
78 | * Drop |
79 | * @param destItem destination KFileItem for the drop (background or item) |
80 | * @param destUrl destination URL for the drop. |
81 | * @param ev the drop event |
82 | * @param parent parent widget (for error dialog box if any) |
83 | * |
84 | * If destItem is 0L, doDrop will stat the URL to determine it. |
85 | */ |
86 | static void doDrop( const KFileItem & destItem, const KUrl & destUrl, QDropEvent * ev, QWidget * parent ); |
87 | |
88 | /** |
89 | * Drop |
90 | * @param destItem destination KFileItem for the drop (background or item) |
91 | * @param destUrl destination URL for the drop. |
92 | * @param ev the drop event |
93 | * @param parent parent widget (for error dialog box if any) |
94 | * @param userActions additional actions to include in the drop menu |
95 | * |
96 | * This is an overloaded member function that lets you add your own actions |
97 | * to the drop menu shown by KonqOperations. |
98 | * |
99 | * The drop menu will be shown when the application re-enters the event loop. |
100 | * |
101 | * If destItem is 0L, doDrop will stat the URL to determine it. |
102 | * |
103 | * Note that the returned KonqOperations object will be deleted automatically |
104 | * when the drop is completed. |
105 | * |
106 | * It is still valid when a slot connected to a triggered() signal in one |
107 | * of the user actions is invoked, but should not be assumed to be valid |
108 | * after the slot returns. |
109 | * |
110 | * @return The KonqOperations object |
111 | * @since 4.3 |
112 | */ |
113 | static KonqOperations *doDrop( const KFileItem & destItem, const KUrl & destUrl, QDropEvent * ev, QWidget * parent, |
114 | const QList<QAction*> &userActions ); |
115 | |
116 | /** |
117 | * Paste the clipboard contents |
118 | */ |
119 | static void doPaste( QWidget * parent, const KUrl & destUrl, const QPoint &pos = QPoint() ); |
120 | |
121 | /** |
122 | * Paste the clipboard contents |
123 | * |
124 | * @return The KonqOperations object |
125 | * @since 4.10 |
126 | * |
127 | * @todo TODO KDE 5,0 - Merge doPaste and doPasteV2 |
128 | */ |
129 | static KonqOperations *doPasteV2(QWidget * parent, const KUrl & destUrl, const QPoint &pos = QPoint()); |
130 | |
131 | /** |
132 | * Returns the state of the paste action: |
133 | * first is whether the action should be enabled |
134 | * second is the text for the action |
135 | * @since 4.3 |
136 | */ |
137 | static QPair<bool, QString> pasteInfo(const KUrl& targetUrl); |
138 | |
139 | /** |
140 | * Empty the trash |
141 | */ |
142 | static void emptyTrash( QWidget* parent ); |
143 | /** |
144 | * Restore trashed items |
145 | */ |
146 | static void restoreTrashedItems( const KUrl::List& urls, QWidget* parent ); |
147 | |
148 | /** |
149 | * Create a directory. Same as KIO::mkdir but records job into KonqFileUndoManager for undo/redo purposes. |
150 | */ |
151 | static KIO::SimpleJob* mkdir( QWidget *parent, const KUrl & url ); |
152 | |
153 | /** |
154 | * Ask for the name of a new directory and create it. |
155 | * Calls KonqOperations::mkdir. |
156 | * |
157 | * @param parent the parent widget |
158 | * @param baseUrl the directory to create the new directory in |
159 | * @return the job used to create the directory or 0 if the creation was cancelled by the user |
160 | */ |
161 | static KIO::SimpleJob* newDir( QWidget * parent, const KUrl & baseUrl ); |
162 | enum NewDirFlag { ViewShowsHiddenFile = 1 }; |
163 | Q_DECLARE_FLAGS(NewDirFlags, NewDirFlag) |
164 | /** |
165 | * Ask for the name of a new directory and create it. |
166 | * Calls KonqOperations::mkdir. |
167 | * |
168 | * @param parent the parent widget |
169 | * @param baseUrl the directory to create the new directory in |
170 | * @param flags see NewDirFlags |
171 | * @return the job used to create the directory or 0 if the creation was cancelled by the user |
172 | */ |
173 | static KIO::SimpleJob* newDir( QWidget * parent, const KUrl & baseUrl, NewDirFlags flags ); |
174 | |
175 | /** |
176 | * Get info about a given URL, and when that's done (it's asynchronous!), |
177 | * call a given slot with a const KFileItem& as argument. |
178 | * The KFileItem will be deleted by statUrl after calling the slot. Make a copy |
179 | * if you need one ! |
180 | */ |
181 | static void statUrl( const KUrl & url, const QObject *receiver, const char *member, QWidget* parent ); |
182 | |
183 | /** |
184 | * Do a renaming. |
185 | * @param parent the parent widget, passed to KonqOperations ctor |
186 | * @param oldurl the current url of the file to be renamed |
187 | * @param name the new name for the file. Shouldn't include '/'. |
188 | */ |
189 | static void rename( QWidget * parent, const KUrl & oldurl, const QString & name ); |
190 | |
191 | /** |
192 | * Do a renaming. |
193 | * @param parent the parent widget, passed to KonqOperations ctor |
194 | * @param oldurl the current url of the file to be renamed |
195 | * @param name the new name for the file. Shouldn't include '/'. |
196 | * |
197 | * @return The KonqOperations object |
198 | * @since 4.11 |
199 | * |
200 | * @todo TODO KDE 5.0 - Merge rename and renameV2 |
201 | */ |
202 | static KonqOperations *renameV2( QWidget * parent, const KUrl & oldurl, const QString & name ); |
203 | |
204 | /** |
205 | * Do a renaming. |
206 | * @param parent the parent widget, passed to KonqOperations ctor |
207 | * @param oldurl the current url of the file to be renamed |
208 | * @param newurl the new url for the file |
209 | * Use this version if the other one wouldn't work :) (e.g. because name could |
210 | * be a relative path, including a '/'). |
211 | */ |
212 | static void rename( QWidget * parent, const KUrl & oldurl, const KUrl & newurl ); |
213 | |
214 | /** |
215 | * Do a renaming. |
216 | * @param parent the parent widget, passed to KonqOperations ctor |
217 | * @param oldurl the current url of the file to be renamed |
218 | * @param newurl the new url for the file |
219 | * Use this version if the other one wouldn't work :) (e.g. because name could |
220 | * be a relative path, including a '/'). |
221 | * |
222 | * @return The KonqOperations object |
223 | * @since 4.11 |
224 | * |
225 | * @todo TODO KDE 5.0 - Merge rename and renameV2 |
226 | */ |
227 | static KonqOperations *renameV2( QWidget * parent, const KUrl & oldurl, const KUrl & newurl ); |
228 | |
229 | enum ConfirmationType { DEFAULT_CONFIRMATION, SKIP_CONFIRMATION, FORCE_CONFIRMATION }; |
230 | /** |
231 | * Ask for confirmation before deleting/trashing @p selectedUrls. |
232 | * @param selectedUrls the urls about to be deleted |
233 | * @param method the type of deletion (DEL for real deletion, anything else for trash) |
234 | * @param confirmation default (based on config file), skip (no confirmation) or force (always confirm) |
235 | * @param widget parent widget for message boxes |
236 | * @return true if confirmed |
237 | */ |
238 | static bool askDeleteConfirmation( const KUrl::List & selectedUrls, int method, ConfirmationType confirmation, QWidget* widget ); |
239 | |
240 | /** |
241 | * Returns the list of dropped URL's. |
242 | * |
243 | * You can call this method on the object returned by KonqOperations::doDrop(), |
244 | * to obtain the list of URL's this object handles. |
245 | * |
246 | * @since 4.3 |
247 | */ |
248 | KUrl::List droppedUrls() const; |
249 | |
250 | /** |
251 | * Returns the position where the drop occurred. |
252 | * @since 4.3 |
253 | */ |
254 | QPoint dropPosition() const; |
255 | |
256 | Q_SIGNALS: |
257 | void statFinished( const KFileItem & item ); |
258 | void aboutToCreate(const QPoint &pos, const QList<KIO::CopyInfo> &files); |
259 | void aboutToCreate(const KUrl::List &urls); |
260 | void renamingFailed(const KUrl &oldUrl, const KUrl &newUrl); |
261 | |
262 | private: |
263 | QWidget* parentWidget() const; |
264 | void _del( Operation method, const KUrl::List & selectedUrls, ConfirmationType confirmation ); |
265 | void _restoreTrashedItems( const KUrl::List& urls ); |
266 | void _statUrl( const KUrl & url, const QObject *receiver, const char *member ); |
267 | void _addPluginActions(QList<QAction*>& pluginActions, const KUrl& destination, const KFileItemListProperties& info); |
268 | |
269 | // internal, for COPY/MOVE/LINK/MKDIR |
270 | void setOperation( KIO::Job * job, Operation method, const KUrl & dest ); |
271 | |
272 | struct DropInfo |
273 | { |
274 | DropInfo( Qt::KeyboardModifiers k, const KUrl::List & u, const QMap<QString,QString> &m, |
275 | const QPoint& pos, Qt::DropAction a, const QList<QAction *> &actions) : |
276 | keyboardModifiers(k), urls(u), metaData(m), mousePos(pos), action(a), userActions(actions) |
277 | {} |
278 | Qt::KeyboardModifiers keyboardModifiers; |
279 | KUrl::List urls; |
280 | QMap<QString,QString> metaData; |
281 | QPoint mousePos; |
282 | Qt::DropAction action; |
283 | QList<QAction*> userActions; |
284 | }; |
285 | // internal, for doDrop |
286 | void setDropInfo( DropInfo * info ) { m_info = info; } |
287 | |
288 | struct KIOPasteInfo // KDE4: remove and use DropInfo instead or a QPoint member |
289 | { |
290 | // Used to position the files at the position where RMB/Paste was used [mostly on the desktop] |
291 | QPoint mousePos; |
292 | }; |
293 | void setPasteInfo( KIOPasteInfo * info ) { m_pasteInfo = info; } |
294 | |
295 | protected Q_SLOTS: |
296 | |
297 | void slotAboutToCreate(KIO::Job *job, const QList<KIO::CopyInfo> &files); |
298 | void slotResult( KJob * job ); |
299 | void slotStatResult( KJob * job ); |
300 | void asyncDrop( const KFileItem & item ); |
301 | void doDropFileCopy(); |
302 | void slotCopyingDone(KIO::Job *job, const KUrl &from, const KUrl &to); |
303 | void slotCopyingLinkDone(KIO::Job *job, const KUrl &from, const QString &target, const KUrl &to); |
304 | |
305 | private: |
306 | Operation m_method; |
307 | //KUrl::List m_srcUrls; |
308 | KUrl::List m_createdUrls; |
309 | KUrl m_destUrl; |
310 | // for doDrop |
311 | DropInfo * m_info; |
312 | KIOPasteInfo * m_pasteInfo; |
313 | }; |
314 | |
315 | #include <kio/job.h> |
316 | |
317 | /// Restore multiple trashed files |
318 | class KonqMultiRestoreJob : public KIO::Job |
319 | { |
320 | Q_OBJECT |
321 | |
322 | public: |
323 | KonqMultiRestoreJob( const KUrl::List& urls ); |
324 | |
325 | protected Q_SLOTS: |
326 | virtual void slotStart(); |
327 | virtual void slotResult( KJob *job ); |
328 | |
329 | private: |
330 | const KUrl::List m_urls; |
331 | KUrl::List::const_iterator m_urlsIterator; |
332 | int m_progress; |
333 | }; |
334 | |
335 | #endif |
336 | |