9e2dcfbad5d1b9a289c0d02cf19e6c1e39d19e79
[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 ignoreDirNotEmpty = false);
88     bool remove(const QFileInfoList &files, const bool ignoreDirNotEmpty = false);
89     bool remove(const QFileInfo &file, const bool ignoreDirNotEmpty = false);
90
91     void copy(const QFileInfo &file, const bool removeAfterCopy);
92
93     unsigned int countFiles(const QFileInfoList &files);
94     unsigned int calculateFileSize(const QFileInfoList &files);
95
96     QFileInfoList listDirFiles(const QString &dirPath);
97
98     void setBarSize(unsigned int size);
99     void updateProgress(int value);
100     void updateFile(const QString &name);
101
102     const QFileInfoList files;
103     QDir dest;
104
105     FileOperator::Response response;
106     FileOperator::Response overwriteAll;
107     bool abort;
108     bool ignoreAll[256];
109
110     QMap<QString, qint64> fileSizeMap;
111
112     QMutex mutex;
113     QWaitCondition waitCond;
114
115     QString fileName, barText;
116     time_t lastTimeUpdate;
117     char timeBuf[10];
118     unsigned int barSize, barValue, fileSize, fileValue;
119
120 signals:
121     void showErrorPrompt(FileManipulatorThread*, const QString&, const QString&, const int);
122     void showOverwritePrompt(FileManipulatorThread*, const QString&, const bool);
123     void finished(FileManipulatorThread*);
124     void setBarSize(FileManipulatorThread*, unsigned int);
125     void updateProgress(FileManipulatorThread*, int);
126 };
127
128
129 class DeleteThread : public FileManipulatorThread {
130     Q_OBJECT
131
132 public:
133     explicit DeleteThread(const QFileInfoList &files);
134
135 protected:
136     void run();
137     virtual void perform(const QFileInfo &file);
138 };
139
140
141 class CopyThread : public FileManipulatorThread {
142     Q_OBJECT
143
144 public:
145     explicit CopyThread(const QFileInfoList &files, QDir &dest);
146
147 protected:
148     void run();
149     virtual void perform(const QFileInfo &file);
150 };
151
152
153 class MoveThread : public FileManipulatorThread {
154     Q_OBJECT
155
156 public:
157     explicit MoveThread(const QFileInfoList &files, QDir &dest);
158
159 protected:
160     void run();
161     virtual void perform(const QFileInfo &file);
162     void rename(const QFileInfoList &files, const QDir &dest);
163 };
164
165
166 #endif // FILEOPERATOR_H