altered behavior of the 'move' operation
[case] / src / fileoperator.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 FILEOPERATOR_H
19 #define FILEOPERATOR_H
20
21 #include <QWidget>
22 #include <QProgressBar>
23 #include <QFileInfo>
24 #include <QThread>
25 #include <QMutex>
26 #include <QWaitCondition>
27 #include <QDir>
28 #include <QMap>
29
30
31 class FileManipulatorThread;
32
33
34 class FileOperator : public QWidget {
35     Q_OBJECT
36
37 public:
38     // DONT_ASK_ONCE is a hackish way to avoid asking twice to overwrite the same directory when moving
39     enum Response{NONE, ABORT, RETRY, IGNORE, KEEP, OVERWRITE, DONT_ASK_ONCE};
40
41     FileOperator(QWidget *parent = 0);
42
43     static QString shortenPath(const QString &path);
44
45     void deleteFiles(const QFileInfoList &files);
46     void copyFiles(const QFileInfoList &files, QDir &destination);
47     void moveFiles(const QFileInfoList &files, QDir &destination);
48
49 public slots:
50     void showErrorPrompt(FileManipulatorThread* manipulator,
51         const QString &message,
52         const QString &fileName,
53         const int err);
54     void showOverwritePrompt(FileManipulatorThread* manipulator,
55         const QString &fileName,
56         const bool dirOverDir);
57
58     void remove(FileManipulatorThread* manipulator);
59     void setBarSize(FileManipulatorThread* manipulator, unsigned int size);
60     void updateProgress(FileManipulatorThread* manipulator, int value);
61
62 protected:
63     void caterNewThread(FileManipulatorThread *thread);
64
65     QList<FileManipulatorThread*> manipulatorList;
66 };
67
68
69 class FileManipulatorThread : public QThread {
70     Q_OBJECT
71
72 public:
73     explicit FileManipulatorThread(const QFileInfoList files, QDir dest = QDir());
74     ~FileManipulatorThread();
75     void setResponse(const FileOperator::Response response, const bool appyToAll = false, const int err = 0);
76
77     void setText(int value);
78
79     QProgressBar *progressBar;
80
81     time_t startTime;
82
83 protected:
84     void processFiles(const QFileInfoList &files);
85     virtual void perform(const QFileInfo &file) = 0;
86
87     bool remove(QString &fileName, const bool doUpdates = false);
88     bool remove(const QFileInfoList &files, const bool doUpdates = false);
89     bool remove(const QFileInfo &file, const bool doUpdates = false);
90
91     void copy(const QFileInfo &file);
92
93     unsigned int calculateFileSize(const QFileInfoList &files,
94         const bool count = false,
95         const bool addSize = false);
96
97     QFileInfoList listDirFiles(const QString &dirPath);
98
99     void setBarSize(unsigned int size);
100     void updateProgress(int value);
101     void updateFile(const QString &name);
102
103     const QFileInfoList files;
104     QDir dest;
105
106     FileOperator::Response response;
107     FileOperator::Response overwriteAll;
108     bool abort;
109     bool ignoreAll[256];
110
111     QMap<QString, qint64> fileSizeMap;
112
113     QMutex mutex;
114     QWaitCondition waitCond;
115
116     QString fileName, barText;
117     time_t lastTimeUpdate;
118     char timeBuf[10];
119     unsigned int barSize, barValue, fileSize, fileValue;
120
121 signals:
122     void showErrorPrompt(FileManipulatorThread*, const QString&, const QString&, const int);
123     void showOverwritePrompt(FileManipulatorThread*, const QString&, const bool);
124     void finished(FileManipulatorThread*);
125     void setBarSize(FileManipulatorThread*, unsigned int);
126     void updateProgress(FileManipulatorThread*, int);
127 };
128
129
130 class DeleteThread : public FileManipulatorThread {
131     Q_OBJECT
132
133 public:
134     explicit DeleteThread(const QFileInfoList &files);
135
136 protected:
137     void run();
138     virtual void perform(const QFileInfo &file);
139 };
140
141
142 class CopyThread : public FileManipulatorThread {
143     Q_OBJECT
144
145 public:
146     explicit CopyThread(const QFileInfoList &files, QDir &dest);
147
148 protected:
149     void run();
150     virtual void perform(const QFileInfo &file);
151 };
152
153
154 class MoveThread : public FileManipulatorThread {
155     Q_OBJECT
156
157 public:
158     explicit MoveThread(const QFileInfoList &files, QDir &dest);
159
160 protected:
161     void run();
162     virtual void perform(const QFileInfo &file);
163     void rename(const QFileInfoList &files, const QDir &dest);
164 };
165
166
167 #endif // FILEOPERATOR_H