bb9d95a6ce905180f15ab8581fddac84ee06db73
[case] / src / operationthread.h
1 // case - file manager for N900
2 // Copyright (C) 2010 Lukas Hrazky <lukkash@email.cz>
3 // 
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.
8 // 
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.
13 // 
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/>.
16
17
18 #ifndef OPERATIONTHREAD_H
19 #define OPERATIONTHREAD_H
20
21 #include <QThread>
22 #include <QFileInfo>
23 #include <QMap>
24 #include <QSet>
25 #include <QFSFileEngine>
26 #include <QDir>
27 #include <QMutex>
28 #include <QWaitCondition>
29
30
31 class OperationThread : public QThread {
32     Q_OBJECT
33
34 public:
35     // DONT_ASK_ONCE is a hackish way to avoid asking twice to overwrite the same directory when moving
36     enum Response{NONE, ABORT, RETRY, IGNORE, KEEP, OVERWRITE, SKIP_DIR, ASK, DONT_ASK_ONCE};
37
38     explicit OperationThread(const QFileInfoList files, QDir dest = QDir());
39     void setResponse(const Response response, const bool appyToAll = false, const int err = 0);
40
41     void wake();
42
43     // the new name entered from the overwrite dialog
44     QString newNameFromDialog;
45     // flags to abort/pause the operation
46     bool abort, pause;
47
48 protected:
49     void processFiles(const QFileInfoList &files);
50     virtual void perform(const QFileInfo &file) = 0;
51
52     bool remove(QString &fileName, const bool doUpdates = false);
53     bool remove(const QFileInfoList &files, const bool doUpdates = false);
54     bool remove(const QFileInfo &file, const bool doUpdates = false);
55
56     void copy(const QFileInfo &file);
57
58     unsigned int calculateFileSize(const QFileInfoList &files,
59         const bool count = false,
60         const bool addSize = false);
61
62     QFileInfoList listDirFiles(const QString &dirPath);
63
64     void setTotalSize(unsigned int size);
65     void updateProgress(int value);
66     void updateFile(const QString &name);
67
68     void waitOnCond();
69
70     bool checkSequentialFile(const QFSFileEngine &engine);
71
72     QMutex mutex;
73     QWaitCondition waitCond;
74
75     // files to process by the operation
76     const QFileInfoList files;
77     // destination for files - changes as the operation recurses into directories
78     QDir dest;
79
80     // responses from the dialog prompts (error and overwrite)
81     Response response;
82     Response overwriteAll;
83     // an array indicating whether to always ignore the error of index errno
84     bool ignoreAll[256];
85
86     // set of files that won't be deleted by the remove(...) functions
87     // used when move(...) would not overwrite target file to ensure the source file doesn't get deleted
88     QSet<QString> removeExcludeFiles;
89
90     // A map of file paths to their size. Not the actual size, but what is calculated for the
91     // purpose of the progressbar for the given operation. So either fileSize/BLOCK_SIZE or simply
92     // 1 for a file and file count for dirs (or both for copy&delete)
93     QMap<QString, qint64> fileSizeMap;
94
95     // progress information of the bar and for the current file
96     unsigned int totalSize, totalValue, fileValue;
97
98 signals:
99     void showErrorPrompt(OperationThread*, const QString&, const QString&, const int);
100     void showOverwritePrompt(OperationThread*, const QString&, const bool);
101     void showInputFilenamePrompt(OperationThread*, const QFileInfo&, const bool);
102     void finished(OperationThread*);
103
104     void totalSizeChanged(int);
105     void progressUpdate(int);
106     void fileNameUpdated(QString);
107     void operationStarted(time_t);
108     void operationPaused();
109     void operationResumed(time_t);
110     // special signal emitted when operation has to copy files between partitions
111     // to notify the user it is deleting files after succesful copy (which can take some time)
112     void removeAfterCopy();
113 };
114
115
116 class DeleteThread : public OperationThread {
117     Q_OBJECT
118
119 public:
120     explicit DeleteThread(const QFileInfoList &files) : OperationThread(files) {}
121
122 protected:
123     void run();
124     virtual void perform(const QFileInfo &file);
125 };
126
127
128 class CopyThread : public OperationThread {
129     Q_OBJECT
130
131 public:
132     explicit CopyThread(const QFileInfoList &files, QDir &dest) : OperationThread(files, dest) {}
133
134 protected:
135     void run();
136     virtual void perform(const QFileInfo &file);
137 };
138
139
140 class MoveThread : public OperationThread {
141     Q_OBJECT
142
143 public:
144     explicit MoveThread(const QFileInfoList &files, QDir &dest) : OperationThread(files, dest) {}
145
146 protected:
147     void run();
148     virtual void perform(const QFileInfo &file);
149     void rename(const QFileInfoList &files, const QDir &dest);
150 };
151
152 #endif // OPERATIONTHREAD_H