diff options
-rw-r--r-- | copyworker.cpp | 86 | ||||
-rw-r--r-- | copyworker.h | 38 | ||||
-rw-r--r-- | shemov.pro | 6 |
3 files changed, 128 insertions, 2 deletions
diff --git a/copyworker.cpp b/copyworker.cpp new file mode 100644 index 0000000..dbff451 --- /dev/null +++ b/copyworker.cpp @@ -0,0 +1,86 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version + 2 of the License, or (at your option) any later version. +*/ + +#include <QFile> +#include <QFileInfo> +#include <QMutexLocker> + +#include "copyworker.h" + +CopyWorker::CopyWorker(QObject *parent) : QThread(parent), mMax(0) {} + +void CopyWorker::setDestination(const QString &destDir) { + QFileInfo destFi(destDir); + if(destFi.isDir()){ + mDest = destDir; + }else{ + QString e = QString(tr("%1 is not a directory!")).arg(destFi.absoluteFilePath()); + emit error(e); + } +} + +void CopyWorker::addSource(const QString &path){ + QMutexLocker l(&mSourceMx); + if(!mSources.contains(path)){ + QFileInfo fi(path); + if(fi.exists() && fi.isFile()){ + mSources.append(path); + qint64 size = fi.size(); + size = size / 1024 / 1024; + mMax += size; + } + } +} + +void CopyWorker::clear(){ + if(isRunning()){ + return; + } + QMutexLocker l(&mSourceMx); + mSources.clear(); + mMax = 0; +} + +void CopyWorker::run(){ + foreach(QString f, mSources){ + QFileInfo cur(f); + if(!cur.exists()){ + QString e = QString(tr("%1 has gone away from under us!")).arg(cur.fileName()); + emit error(e); + continue; + } + QString destFile = QString("%1/%2").arg(mDest).arg(cur.fileName()); + QFileInfo destFi(destFile); + if(destFi.exists()){ + QString e = QString(tr("%1 already exists!")).arg(destFi.absoluteFilePath()); + emit error(e); + continue; + } + QFile sourceQF(f); + QFile destQF(destFile); + bool openSource = sourceQF.open(QIODevice::ReadOnly); + bool openDest = destQF.open(QIODevice::WriteOnly); + if(!openSource || !openDest){ + QString e = QString(tr("Failed to open source or destination on %1!")).arg(cur.fileName()); + emit error(e); + continue; + } + char *buf = new char[32768]; + qint64 len = 0; + int total = 0; + while(!sourceQF.atEnd()){ + len = sourceQF.read(buf, 32768); + destQF.write(buf, len); + total +=len; + qint64 cur = total / 1024 / 1024; + emit bytesRead(cur); + } + delete buf; + QString s = QString(tr("Done copying %1")).arg(cur.fileName()); + emit success(s); + } +} diff --git a/copyworker.h b/copyworker.h new file mode 100644 index 0000000..fed3631 --- /dev/null +++ b/copyworker.h @@ -0,0 +1,38 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version + 2 of the License, or (at your option) any later version. +*/ + +#ifndef COPYWORKER_H +#define COPYWORKER_H + +#include <QThread> +#include <QString> +#include <QStringList> +#include <QMutex> + +class CopyWorker : public QThread { + Q_OBJECT + public: + CopyWorker(QObject *parent = 0); + void setDestination(const QString &destDir); + void addSource(const QString &path); + void clear(); + virtual void run(); + + signals: + void success(QString); + void bytesRead(int); + void processint(QString); + void error(QString); + + private: + QStringList mSources; + QString mDest; + QMutex mSourceMx; + int mMax; +}; + +#endif // COPYWORKER_H @@ -44,7 +44,8 @@ SOURCES = main.cpp \ archivebrowser.cpp \ archivebrowsermodel.cpp \ unpacker.cpp \ - searchdialog.cpp + searchdialog.cpp \ + copyworker.cpp HEADERS = \ filesystemdirproxy.h \ filesystemwidget.h \ @@ -84,7 +85,8 @@ HEADERS = \ archivebrowser.h \ archivebrowsermodel.h \ unpacker.h \ - searchdialog.h + searchdialog.h \ + copyworker.h LIBS += -lmagic -lXfixes -lX11 -lMagick++-6.Q16HDRI INCLUDEPATH += /usr/include/ImageMagick-6/ RESOURCES = shemov.qrc |