1 // case - file manager for N900
2 // Copyright (C) 2010 Lukas Hrazky <lukkash@email.cz>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program 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
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef FILEOPERATOR_H
19 #define FILEOPERATOR_H
25 #include <QWaitCondition>
30 #include "progressbar.h"
33 class FileManipulatorThread;
36 class FileOperator : public QWidget {
40 // DONT_ASK_ONCE is a hackish way to avoid asking twice to overwrite the same directory when moving
41 enum Response{NONE, ABORT, RETRY, IGNORE, KEEP, OVERWRITE, SKIP_DIR, ASK, DONT_ASK_ONCE};
43 FileOperator(QWidget *parent = 0);
45 static QString shortenPath(const QString &path);
46 static QString unwindPath(const QString &path);
48 void deleteFiles(const QFileInfoList &files);
49 void copyFiles(const QFileInfoList &files, QDir &destination);
50 void moveFiles(const QFileInfoList &files, QDir &destination);
53 void showErrorPrompt(FileManipulatorThread* manipulator,
54 const QString &message,
55 const QString &fileName,
57 void showOverwritePrompt(FileManipulatorThread* manipulator,
58 const QString &fileName,
59 const bool dirOverDir);
60 void showInputFilenamePrompt(FileManipulatorThread* manipulator,
61 const QFileInfo &fileName,
62 const bool dirOverDir);
64 void remove(FileManipulatorThread* manipulator);
65 void setBarSize(FileManipulatorThread* manipulator, unsigned int size);
66 void updateProgress(FileManipulatorThread* manipulator, int value);
67 void showPaused(FileManipulatorThread* manipulator);
69 void togglePauseOperation(FileManipulatorThread* manipulator);
70 void abortOperation(FileManipulatorThread* manipulator);
73 void caterNewThread(FileManipulatorThread *thread);
75 QList<FileManipulatorThread*> manipulatorList;
79 class FileManipulatorThread : public QThread {
83 explicit FileManipulatorThread(const QFileInfoList files, QDir dest = QDir());
84 ~FileManipulatorThread();
85 void setResponse(const FileOperator::Response response, const bool appyToAll = false, const int err = 0);
87 void setText(int value);
91 ProgressBar *progressBar;
94 // the new name entered from the overwrite dialog
95 QString newNameFromDialog;
96 // flags to abort/pause the operation
100 void processFiles(const QFileInfoList &files);
101 virtual void perform(const QFileInfo &file) = 0;
103 bool remove(QString &fileName, const bool doUpdates = false);
104 bool remove(const QFileInfoList &files, const bool doUpdates = false);
105 bool remove(const QFileInfo &file, const bool doUpdates = false);
107 void copy(const QFileInfo &file);
109 unsigned int calculateFileSize(const QFileInfoList &files,
110 const bool count = false,
111 const bool addSize = false);
113 QFileInfoList listDirFiles(const QString &dirPath);
115 void setBarSize(unsigned int size);
116 void updateProgress(int value);
117 void updateFile(const QString &name);
121 QWaitCondition waitCond;
123 // files to process by the operation
124 const QFileInfoList files;
125 // destination for files - changes as the operation recurses into directories
128 // responses from the dialog prompts (error and overwrite)
129 FileOperator::Response response;
130 FileOperator::Response overwriteAll;
131 // an array indicating whether to always ignore the error of index errno
134 // set of files that won't be deleted by the remove(...) functions
135 // used when move(...) would not overwrite target file to ensure the source file doesn't get deleted
136 QSet<QString> removeExcludeFiles;
138 // A map of file paths to their size. Not the actual size, but what is calculated for the
139 // purpose of the progressbar for the given operation. So either fileSize/BLOCK_SIZE or simply
140 // 1 for a file and file count for dirs (or both for copy&delete)
141 QMap<QString, qint64> fileSizeMap;
143 // the name of the file thats being processed (for progressBar) and the text of the progressBar (the format)
144 QString fileName, barText;
145 // stamp of the last ETA recalculation - done every second
146 time_t lastTimeUpdate;
147 time_t startTime, waitTime;
149 // progress information of the bar and for the current file
150 unsigned int barSize, barValue, fileSize, fileValue;
153 void showErrorPrompt(FileManipulatorThread*, const QString&, const QString&, const int);
154 void showOverwritePrompt(FileManipulatorThread*, const QString&, const bool);
155 void showInputFilenamePrompt(FileManipulatorThread*, const QFileInfo&, const bool);
156 void finished(FileManipulatorThread*);
157 void setBarSize(FileManipulatorThread*, unsigned int);
158 void updateProgress(FileManipulatorThread*, int);
159 void operationPaused(FileManipulatorThread*);
163 class DeleteThread : public FileManipulatorThread {
167 explicit DeleteThread(const QFileInfoList &files);
171 virtual void perform(const QFileInfo &file);
175 class CopyThread : public FileManipulatorThread {
179 explicit CopyThread(const QFileInfoList &files, QDir &dest);
183 virtual void perform(const QFileInfo &file);
187 class MoveThread : public FileManipulatorThread {
191 explicit MoveThread(const QFileInfoList &files, QDir &dest);
195 virtual void perform(const QFileInfo &file);
196 void rename(const QFileInfoList &files, const QDir &dest);
200 #endif // FILEOPERATOR_H