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