1 /* This file is part of the KDE project
2 Copyright (C) 2004 David Faure <faure@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
23 #include <kio/jobclasses.h>
31 * Implementation of all low-level operations done by kio_trash
32 * The structure of the trash directory follows the freedesktop.org standard <TODO URL>
34 class TrashImpl
: public QObject
40 /// Check the "home" trash directory
41 /// This MUST be called before doing anything else
44 /// Create info for a file to be trashed
45 /// Returns trashId and fileId
46 /// The caller is then responsible for actually trashing the file
47 bool createInfo( const QString
& origPath
, int& trashId
, QString
& fileId
);
49 /// Delete info file for a file to be trashed
50 /// Usually used for undoing what createInfo did if trashing failed
51 bool deleteInfo( int trashId
, const QString
& fileId
);
53 /// Moving a file or directory into the trash. The ids come from createInfo.
54 bool moveToTrash( const QString
& origPath
, int trashId
, const QString
& fileId
);
56 /// Moving a file or directory out of the trash. The ids come from createInfo.
57 bool moveFromTrash( const QString
& origPath
, int trashId
, const QString
& fileId
, const QString
& relativePath
);
59 /// Copying a file or directory into the trash. The ids come from createInfo.
60 bool copyToTrash( const QString
& origPath
, int trashId
, const QString
& fileId
);
62 /// Copying a file or directory out of the trash. The ids come from createInfo.
63 bool copyFromTrash( const QString
& origPath
, int trashId
, const QString
& fileId
, const QString
& relativePath
);
65 /// Create a top-level trashed directory
66 //bool mkdir( int trashId, const QString& fileId, int permissions );
68 /// Get rid of a trashed file
69 bool del( int trashId
, const QString
& fileId
);
71 /// Empty trash, i.e. delete all trashed files
74 /// Return true if the trash is empty
77 struct TrashedFileInfo
{
78 int trashId
; // for the url
79 QString fileId
; // for the url
80 QString physicalPath
; // for stat'ing etc.
81 QString origPath
; // from info file
82 QDateTime deletionDate
; // from info file
84 /// List trashed files
85 typedef QList
<TrashedFileInfo
> TrashedFileInfoList
;
86 TrashedFileInfoList
list();
88 /// Return the info for a given trashed file
89 bool infoForFile( int trashId
, const QString
& fileId
, TrashedFileInfo
& info
);
91 /// Return the physicalPath for a given trashed file - helper method which
92 /// encapsulates the call to infoForFile. Don't use if you need more info from TrashedFileInfo.
93 QString
physicalPath( int trashId
, const QString
& fileId
, const QString
& relativePath
);
95 /// Move data from the old trash system to the new one
96 void migrateOldTrash();
99 int lastErrorCode() const { return m_lastErrorCode
; }
100 QString
lastErrorMessage() const { return m_lastErrorMessage
; }
102 QStringList
listDir( const QString
& physicalPath
);
104 static KUrl
makeURL( int trashId
, const QString
& fileId
, const QString
& relativePath
);
105 static bool parseURL( const KUrl
& url
, int& trashId
, QString
& fileId
, QString
& relativePath
);
107 typedef QMap
<int, QString
> TrashDirMap
;
108 /// @internal This method is for TestTrash only. Home trash is included (id 0).
109 TrashDirMap
trashDirectories() const;
110 /// @internal This method is for TestTrash only. No entry with id 0.
111 TrashDirMap
topDirectories() const;
114 void leaveModality();
117 /// Helper method. Moves a file or directory using the appropriate method.
118 bool move( const QString
& src
, const QString
& dest
);
119 bool copy( const QString
& src
, const QString
& dest
);
120 /// Helper method. Tries to call ::rename(src,dest) and does error handling.
121 bool directRename( const QString
& src
, const QString
& dest
);
126 bool adaptTrashSize( const QString
& origPath
, int trashId
);
128 // Warning, returns error code, not a bool
129 int testDir( const QString
& name
) const;
130 void error( int e
, const QString
& s
);
132 bool readInfoFile( const QString
& infoPath
, TrashedFileInfo
& info
, int trashId
);
134 QString
infoPath( int trashId
, const QString
& fileId
) const;
135 QString
filesPath( int trashId
, const QString
& fileId
) const;
137 /// Find the trash dir to use for a given file to delete, based on original path
138 int findTrashDirectory( const QString
& origPath
);
140 QString
trashDirectoryPath( int trashId
) const;
141 QString
topDirectoryPath( int trashId
) const;
143 bool synchronousDel( const QString
& path
, bool setLastErrorCode
, bool isDir
);
145 void scanTrashDirectories() const;
147 int idForTrashDirectory( const QString
& trashDir
) const;
148 bool initTrashDirectory( const QByteArray
& trashDir_c
) const;
149 bool checkTrashSubdirs( const QByteArray
& trashDir_c
) const;
150 QString
trashForMountPoint( const QString
& topdir
, bool createIfNeeded
) const;
151 static QString
makeRelativePath( const QString
& topdir
, const QString
& path
);
156 void jobFinished(KJob
*job
);
159 /// Last error code stored in class to simplify API.
160 /// Note that this means almost no method can be const.
162 QString m_lastErrorMessage
;
164 enum { InitToBeDone
, InitOK
, InitError
} m_initStatus
;
166 // A "trash directory" is a physical directory on disk,
167 // e.g. $HOME/.local/share/Trash/$uid or /mnt/foo/.Trash/$uid
168 // It has an id (number) and a path.
169 // The home trash has id 0.
170 mutable TrashDirMap m_trashDirectories
; // id -> path of trash directory
171 mutable TrashDirMap m_topDirectories
; // id -> $topdir of partition
173 mutable bool m_trashDirectoriesScanned
;
178 // We don't cache any data related to the trashed files.
179 // Another kioslave could change that behind our feet.
180 // If we want to start caching data - and avoiding some race conditions -,
181 // we should turn this class into a kded module and use DCOP to talk to it
182 // from the kioslave.