[win-nix] check file existance moved to thread

This commit is contained in:
Maxim Kadushkin
2016-12-01 13:59:53 +03:00
parent 1a83da1b24
commit 6bb292edaa
5 changed files with 92 additions and 33 deletions

View File

@ -37,7 +37,8 @@ HEADERS += \
$$PWD/src/chelp.h \
$$PWD/src/cmainpanel.h \
$$PWD/src/csplash.h \
$$PWD/src/cmessage.h
$$PWD/src/cmessage.h \
$$PWD/src/cfilechecker.h
# src/ctabbar_p.h \
# src/ctabstyle.h \
# src/ctabstyle_p.h
@ -57,7 +58,8 @@ SOURCES += \
$$PWD/src/cstyletweaks.cpp \
$$PWD/src/chelp.cpp \
$$PWD/src/cmainpanel.cpp \
$$PWD/src/cmessage.cpp
$$PWD/src/cmessage.cpp \
$$PWD/src/cfilechecker.cpp
# src/ctabstyle.cpp
# src/casclabel.cpp

View File

@ -0,0 +1,54 @@
#include "cfilechecker.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QStorageInfo>
#include <QDebug>
#include <QElapsedTimer>
CFileChecker::CFileChecker(const QString& json)
: QThread()
, m_inJson(json)
{
}
void CFileChecker::run()
{
QJsonParseError jerror;
QJsonDocument jdoc = QJsonDocument::fromJson(m_inJson.toUtf8(), &jerror);
if(jerror.error == QJsonParseError::NoError) {
QJsonObject objRoot = jdoc.object();
if ( objRoot.size() ) {
QJsonObject _json_obj;
QString _file_name;
QElapsedTimer timer;
timer.start();
foreach (QString s, objRoot.keys()) {
if ( isInterruptionRequested() ) return;
_file_name = objRoot[s].toString();
// check file is local
QStorageInfo storage(QFileInfo(_file_name).dir());
#ifdef Q_OS_WIN
if (storage.device().startsWith("\\\\?\\")) {
#else
if (storage.device().startsWith("/dev/")) {
#endif
QFileInfo info(_file_name);
if (!info.exists()) _json_obj[s] = "false";
}
}
qDebug() << "check file passed: " << currentThreadId() << ", " << timer.elapsed() << "ms";
if ( _json_obj.size() && !isInterruptionRequested() ) {
emit resultReady(QJsonDocument(_json_obj).toJson(QJsonDocument::Compact));
}
}
}
}

View File

@ -0,0 +1,23 @@
#ifndef CFILECHECKER_H
#define CFILECHECKER_H
#include <QThread>
class CFileChecker : public QThread
{
Q_OBJECT
void run() Q_DECL_OVERRIDE;
QString m_inJson;
public:
explicit CFileChecker(const QString& json);
void abort();
signals:
void resultReady(const QString&);
};
#endif // CFILECHECKER_H

View File

@ -55,6 +55,7 @@
#include "utils.h"
#include "version.h"
#include "cmessage.h"
#include "cfilechecker.h"
#ifdef _WIN32
#include "win/cprintdialog.h"
@ -765,37 +766,16 @@ void CMainPanel::onLocalFilesOpen(void * data)
void CMainPanel::onLocalFilesCheck(QString json)
{
QJsonParseError jerror;
QJsonDocument jdoc = QJsonDocument::fromJson(json.toUtf8(), &jerror);
return;
if(jerror.error == QJsonParseError::NoError) {
QJsonObject objRoot = jdoc.object();
CFileChecker * checker = new CFileChecker(json);
checker->start(QThread::LowPriority);
// checker->requestInterruption();
if ( objRoot.size() ) {
QTimer::singleShot(10, this, [=]{
QJsonObject _json_obj;
QString _file_name;
foreach (QString s, objRoot.keys() ) {
_file_name = objRoot[s].toString();
// check file is local
QStorageInfo storage(QFileInfo(_file_name).absoluteDir());
#ifdef Q_OS_WIN
if (storage.device().startsWith("\\\\?\\")) {
#else
if (storage.device().startsWith("/dev/")) {
#endif
QFileInfo info(_file_name);
if (!info.exists()) _json_obj[s] = "false";
}
}
if ( _json_obj.size() ) {
cmdMainPage("files:checked", Utils::encodeJson(_json_obj));
}
});
}
}
connect(checker, &CFileChecker::resultReady, [=](const QString& json){
cmdMainPage("files:checked", Utils::encodeJson(json));
});
connect(checker, &CFileChecker::finished, checker, &QObject::deleteLater);
}
void CMainPanel::onLocalFileLocation(QString path)

View File

@ -33,8 +33,8 @@
#ifndef VERSION_H
#define VERSION_H
#define VER_FILEVERSION 4,2,3,280
#define VER_FILEVERSION_STR "4.2.3.280\0"
#define VER_FILEVERSION 4,2,3,282
#define VER_FILEVERSION_STR "4.2.3.282\0"
#define VER_PRODUCTVERSION VER_FILEVERSION
#define VER_PRODUCTVERSION_STR "4.2\0"