add more spacing
[personal-kdebase.git] / runtime / kioslave / trash / trashimpl.h
blobbc68723ff5044817b272efef901b6486010d6997
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.
20 #ifndef TRASHIMPL_H
21 #define TRASHIMPL_H
23 #include <kio/jobclasses.h>
24 #include <kconfig.h>
26 #include <QDateTime>
27 #include <QMap>
28 #include <assert.h>
30 /**
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
36 Q_OBJECT
37 public:
38 TrashImpl();
40 /// Check the "home" trash directory
41 /// This MUST be called before doing anything else
42 bool init();
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
72 bool emptyTrash();
74 /// Return true if the trash is empty
75 bool isEmpty() const;
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();
98 /// KIO error code
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;
113 Q_SIGNALS:
114 void leaveModality();
116 private:
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 );
123 void fileAdded();
124 void fileRemoved();
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 );
153 void enterLoop();
155 private Q_SLOTS:
156 void jobFinished(KJob *job);
158 private:
159 /// Last error code stored in class to simplify API.
160 /// Note that this means almost no method can be const.
161 int m_lastErrorCode;
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
172 dev_t m_homeDevice;
173 mutable bool m_trashDirectoriesScanned;
174 int m_mibEnum;
176 KConfig m_config;
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.
185 #endif