7672de16ec76e7b12c8f7c4401cd340512b1a08e
[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     void deleteFiles(const QFileInfoList &files);
44     void copyFiles(const QFileInfoList &files, QDir &destination);
45     void moveFiles(const QFileInfoList &files, QDir &destination);
46
47 public slots:
48     void showErrorPrompt(FileManipulatorThread* manipulator, const QString &message, const int err);
49     void showOverwritePrompt(FileManipulatorThread* manipulator, const QString &fileName, const bool dirOverDir);
50     void remove(FileManipulatorThread* manipulator);
51     void setBarSize(FileManipulatorThread* manipulator, unsigned int size);
52     void updateProgress(FileManipulatorThread* manipulator, int value);
53
54 protected:
55     void caterNewThread(FileManipulatorThread *thread);
56
57     QList<FileManipulatorThread*> manipulatorList;
58 };
59
60
61 class FileManipulatorThread : public QThread {
62     Q_OBJECT
63
64 public:
65     explicit FileManipulatorThread(const QFileInfoList files, QDir dest = QDir());
66     void setResponse(const FileOperator::Response response, const bool appyToAll = false, const int err = 0);
67
68     QProgressBar *widget;
69
70 protected:
71     void processFiles(const QFileInfoList &files);
72     virtual void perform(const QFileInfo &file) = 0;
73
74     bool remove(QString &filename, const bool ignoreDirNotEmpty = false);
75     bool remove(const QFileInfoList &files, const bool ignoreDirNotEmpty = false);
76     bool remove(const QFileInfo &file, const bool ignoreDirNotEmpty = false);
77
78     void copy(const QFileInfo &file, const bool removeAfterCopy);
79
80     unsigned int countFiles(const QFileInfoList &files);
81     unsigned int calculateFileSize(const QFileInfoList &files);
82
83     QFileInfoList listDirFiles(const QString &dirPath);
84
85     void setBarSize(unsigned int size);
86     void updateProgress(int value);
87     void updateFile(const QString &fileName);
88
89     const QFileInfoList files;
90     QDir dest;
91
92     FileOperator::Response response;
93     FileOperator::Response overwriteAll;
94     bool abort;
95     bool ignoreAll[256];
96
97     QMap<QString, qint64> fileSizeMap;
98
99     QMutex mutex;
100     QWaitCondition waitCond;
101
102     unsigned int barSize, barValue, fileSize, fileValue;
103
104 signals:
105     void showErrorPrompt(FileManipulatorThread*, const QString&, const int);
106     void showOverwritePrompt(FileManipulatorThread*, const QString&, const bool);
107     void finished(FileManipulatorThread*);
108     void setBarSize(FileManipulatorThread*, unsigned int);
109     void updateProgress(FileManipulatorThread*, int);
110     void updateFile(FileManipulatorThread*, const QString&);
111 };
112
113
114 class DeleteThread : public FileManipulatorThread {
115     Q_OBJECT
116
117 public:
118     explicit DeleteThread(const QFileInfoList &files) : FileManipulatorThread(files) {}
119
120 protected:
121     void run();
122     virtual void perform(const QFileInfo &file);
123 };
124
125
126 class CopyThread : public FileManipulatorThread {
127     Q_OBJECT
128
129 public:
130     explicit CopyThread(const QFileInfoList &files, QDir &dest) : FileManipulatorThread(files, dest) {}
131
132 protected:
133     void run();
134     virtual void perform(const QFileInfo &file);
135 };
136
137
138 class MoveThread : public FileManipulatorThread {
139     Q_OBJECT
140
141 public:
142     explicit MoveThread(const QFileInfoList &files, QDir &dest) : FileManipulatorThread(files, dest) {}
143
144 protected:
145     void run();
146     virtual void perform(const QFileInfo &file);
147     void rename(const QFileInfoList &files, const QDir &dest);
148 };
149
150
151 #endif // FILEOPERATOR_H