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
22 #include <QProgressBar>
26 #include <QWaitCondition>
32 class FileManipulatorThread;
35 class FileOperator : public QWidget {
39 // DONT_ASK_ONCE is a hackish way to avoid asking twice to overwrite the same directory when moving
40 enum Response{NONE, ABORT, RETRY, IGNORE, KEEP, OVERWRITE, SKIP_DIR, ASK, DONT_ASK_ONCE};
42 FileOperator(QWidget *parent = 0);
44 static QString shortenPath(const QString &path);
46 void deleteFiles(const QFileInfoList &files);
47 void copyFiles(const QFileInfoList &files, QDir &destination);
48 void moveFiles(const QFileInfoList &files, QDir &destination);
51 void showErrorPrompt(FileManipulatorThread* manipulator,
52 const QString &message,
53 const QString &fileName,
55 void showOverwritePrompt(FileManipulatorThread* manipulator,
56 const QString &fileName,
57 const bool dirOverDir);
58 void showInputFilenamePrompt(FileManipulatorThread* manipulator,
59 const QFileInfo &fileName,
60 const bool dirOverDir);
62 void remove(FileManipulatorThread* manipulator);
63 void setBarSize(FileManipulatorThread* manipulator, unsigned int size);
64 void updateProgress(FileManipulatorThread* manipulator, int value);
67 void caterNewThread(FileManipulatorThread *thread);
69 QList<FileManipulatorThread*> manipulatorList;
73 class FileManipulatorThread : public QThread {
77 explicit FileManipulatorThread(const QFileInfoList files, QDir dest = QDir());
78 ~FileManipulatorThread();
79 void setResponse(const FileOperator::Response response, const bool appyToAll = false, const int err = 0);
81 void setText(int value);
83 QProgressBar *progressBar;
87 QWaitCondition waitCond;
88 // the new name entered from the overwrite dialog
89 QString newNameFromDialog;
92 void processFiles(const QFileInfoList &files);
93 virtual void perform(const QFileInfo &file) = 0;
95 bool remove(QString &fileName, const bool doUpdates = false);
96 bool remove(const QFileInfoList &files, const bool doUpdates = false);
97 bool remove(const QFileInfo &file, const bool doUpdates = false);
99 void copy(const QFileInfo &file);
101 unsigned int calculateFileSize(const QFileInfoList &files,
102 const bool count = false,
103 const bool addSize = false);
105 QFileInfoList listDirFiles(const QString &dirPath);
107 void setBarSize(unsigned int size);
108 void updateProgress(int value);
109 void updateFile(const QString &name);
111 // files to process by the operation
112 const QFileInfoList files;
113 // destination for files - changes as the operation recurses into directories
116 // responses from the dialog prompts (error and overwrite)
117 FileOperator::Response response;
118 FileOperator::Response overwriteAll;
119 // a flag to abort the operation
121 // an array indicating whether to always ignore the error of index errno
124 // set of files that won't be deleted by the remove(...) functions
125 // used when move(...) would not overwrite target file to ensure the source file doesn't get deleted
126 QSet<QString> removeExcludeFiles;
128 // A map of file paths to their size. Not the actual size, but what is calculated for the
129 // purpose of the progressbar for the given operation. So either fileSize/BLOCK_SIZE or simply
130 // 1 for a file and file count for dirs (or both for copy&delete)
131 QMap<QString, qint64> fileSizeMap;
133 // the name of the file thats being processed (for progressBar) and the text of the progressBar (the format)
134 QString fileName, barText;
135 // stamp of the last ETA recalculation - done every second
136 time_t lastTimeUpdate;
138 // progress information of the bar and for the current file
139 unsigned int barSize, barValue, fileSize, fileValue;
142 void showErrorPrompt(FileManipulatorThread*, const QString&, const QString&, const int);
143 void showOverwritePrompt(FileManipulatorThread*, const QString&, const bool);
144 void showInputFilenamePrompt(FileManipulatorThread*, const QFileInfo&, const bool);
145 void finished(FileManipulatorThread*);
146 void setBarSize(FileManipulatorThread*, unsigned int);
147 void updateProgress(FileManipulatorThread*, int);
151 class DeleteThread : public FileManipulatorThread {
155 explicit DeleteThread(const QFileInfoList &files);
159 virtual void perform(const QFileInfo &file);
163 class CopyThread : public FileManipulatorThread {
167 explicit CopyThread(const QFileInfoList &files, QDir &dest);
171 virtual void perform(const QFileInfo &file);
175 class MoveThread : public FileManipulatorThread {
179 explicit MoveThread(const QFileInfoList &files, QDir &dest);
183 virtual void perform(const QFileInfo &file);
184 void rename(const QFileInfoList &files, const QDir &dest);
188 #endif // FILEOPERATOR_H