1 #include "fileworker.h"
5 FileWorker::FileWorker(QObject
*parent
) :
8 m_cancelled(KeepRunning
),
13 FileWorker::~FileWorker()
17 void FileWorker::startDeleteFiles(QStringList filenames
)
20 emit
errorOccurred(tr("File operation already in progress"), "");
24 // basic validity check
25 foreach (QString filename
, filenames
) {
26 if (filename
.isEmpty()) {
27 emit
errorOccurred(tr("Empty filename"), "");
33 m_filenames
= filenames
;
34 m_cancelled
.storeRelease(KeepRunning
);
38 void FileWorker::startCopyFiles(QStringList filenames
, QString destDirectory
)
41 emit
errorOccurred(tr("File operation already in progress"), "");
45 // basic validity check
46 foreach (QString filename
, filenames
) {
47 if (filename
.isEmpty()) {
48 emit
errorOccurred(tr("Empty filename"), "");
54 m_filenames
= filenames
;
55 m_destDirectory
= destDirectory
;
56 m_cancelled
.storeRelease(KeepRunning
);
60 void FileWorker::startMoveFiles(QStringList filenames
, QString destDirectory
)
63 emit
errorOccurred(tr("File operation already in progress"), "");
67 // basic validity check
68 foreach (QString filename
, filenames
) {
69 if (filename
.isEmpty()) {
70 emit
errorOccurred(tr("Empty filename"), "");
76 m_filenames
= filenames
;
77 m_destDirectory
= destDirectory
;
78 m_cancelled
.storeRelease(KeepRunning
);
82 void FileWorker::cancel()
84 m_cancelled
.storeRelease(Cancelled
);
87 void FileWorker::run() Q_DECL_OVERRIDE
101 QString
FileWorker::deleteFile(QString filename
)
103 QFileInfo
info(filename
);
105 return tr("File not found");
108 // this should be custom function to get better error reporting
109 bool ok
= QDir(info
.absoluteFilePath()).removeRecursively();
111 return tr("Folder delete failed");
114 QFile
file(info
.absoluteFilePath());
115 bool ok
= file
.remove();
117 return file
.errorString();
122 void FileWorker::deleteFiles()
125 int fileCount
= m_filenames
.count();
127 foreach (QString filename
, m_filenames
) {
128 m_progress
= 100 * fileIndex
/ fileCount
;
129 emit
progressChanged(m_progress
, filename
);
132 if (m_cancelled
.loadAcquire() == Cancelled
) {
133 emit
errorOccurred(tr("Cancelled"), filename
);
137 // delete file and stop if errors
138 QString errMsg
= deleteFile(filename
);
139 if (!errMsg
.isEmpty()) {
140 emit
errorOccurred(errMsg
, filename
);
143 emit
fileDeleted(filename
);
149 emit
progressChanged(m_progress
, "");
153 void FileWorker::copyOrMoveFiles()
156 int fileCount
= m_filenames
.count();
158 QDir
dest(m_destDirectory
);
159 foreach (QString filename
, m_filenames
) {
160 m_progress
= 100 * fileIndex
/ fileCount
;
161 emit
progressChanged(m_progress
, filename
);
164 if (m_cancelled
.loadAcquire() == Cancelled
) {
165 emit
errorOccurred(tr("Cancelled"), filename
);
169 // check destination does not exists, otherwise copy/move fails
170 QFileInfo
fileInfo(filename
);
171 QString newname
= dest
.absoluteFilePath(fileInfo
.fileName());
173 // move or copy and stop if errors
174 QFile
file(filename
);
175 if (m_mode
== MoveMode
) {
176 if (!file
.rename(newname
)) {
177 emit
errorOccurred(file
.errorString(), filename
);
181 if (fileInfo
.isDir()) {
182 QString errmsg
= copyDirRecursively(filename
, newname
);
183 if (!errmsg
.isEmpty()) {
184 emit
errorOccurred(errmsg
, filename
);
188 QString errmsg
= copyOverwrite(filename
, newname
);
189 if (!errmsg
.isEmpty()) {
190 emit
errorOccurred(errmsg
, filename
);
200 emit
progressChanged(m_progress
, "");
204 QString
FileWorker::copyDirRecursively(QString srcDirectory
, QString destDirectory
)
206 QDir
srcDir(srcDirectory
);
207 if (!srcDir
.exists())
208 return tr("Source folder doesn't exist");
210 QDir
destDir(destDirectory
);
211 if (!destDir
.exists()) {
214 if (!d
.mkdir(destDir
.dirName()))
215 return tr("Can't create target folder %1").arg(destDirectory
);
219 QStringList names
= srcDir
.entryList(QDir::Files
);
220 for (int i
= 0 ; i
< names
.count() ; ++i
) {
222 if (m_cancelled
.loadAcquire() == Cancelled
)
223 return tr("Cancelled");
225 QString filename
= names
.at(i
);
226 emit
progressChanged(m_progress
, filename
);
227 QString spath
= srcDir
.absoluteFilePath(filename
);
228 QString dpath
= destDir
.absoluteFilePath(filename
);
229 QString errmsg
= copyOverwrite(spath
, dpath
);
230 if (!errmsg
.isEmpty())
235 names
= srcDir
.entryList(QDir::NoDotAndDotDot
| QDir::AllDirs
);
236 for (int i
= 0 ; i
< names
.count() ; ++i
) {
238 if (m_cancelled
.loadAcquire() == Cancelled
)
239 return tr("Cancelled");
241 QString filename
= names
.at(i
);
242 emit
progressChanged(m_progress
, filename
);
243 QString spath
= srcDir
.absoluteFilePath(filename
);
244 QString dpath
= destDir
.absoluteFilePath(filename
);
245 QString errmsg
= copyDirRecursively(spath
, dpath
);
246 if (!errmsg
.isEmpty())
253 QString
FileWorker::copyOverwrite(QString src
, QString dest
)
256 if (dfile
.exists()) {
258 return dfile
.errorString();
262 if (!sfile
.copy(dest
))
263 return sfile
.errorString();