Refactoring
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
set(shared_SRCS
|
||||
foldersync.cpp
|
||||
genericpanel.cpp
|
||||
hexspinbox.cpp
|
||||
verticalscrollarea.cpp
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
#include "foldersync.h"
|
||||
#include <QDirIterator>
|
||||
#include <QDateTime>
|
||||
#include <QMessageBox>
|
||||
#include <QTextStream>
|
||||
#include <QDebug>
|
||||
|
||||
FoldersSyncTask::FoldersSyncTask(const QString &folder1, const QString &folder2)
|
||||
{
|
||||
folders[0] = folder1;
|
||||
folders[1] = folder2;
|
||||
}
|
||||
|
||||
void FoldersSyncTask::run()
|
||||
{
|
||||
if (!QFile::exists(folders[0])) {
|
||||
QMessageBox::warning(NULL, QObject::tr("Synchronization error"), QObject::tr("The directory '%1' doesn't exist!").arg(folders[0]));
|
||||
return;
|
||||
}
|
||||
if (!QFile::exists(folders[1])) {
|
||||
QMessageBox::warning(NULL, QObject::tr("Synchronization error"), QObject::tr("The directory '%1' doesn't exist!").arg(folders[1]));
|
||||
return;
|
||||
}
|
||||
QStringList errors = updateDir(folders[0], folders[1]) + updateDir(folders[1], folders[0]);
|
||||
if (errors.count() > 0) {
|
||||
QMessageBox::warning(NULL, QObject::tr("Synchronization error"), errors.join("\n"));
|
||||
}
|
||||
}
|
||||
|
||||
QStringList FoldersSyncTask::updateDir(const QDir &source, const QDir &destination)
|
||||
{
|
||||
QStringList errors;
|
||||
QDirIterator it(source, QDirIterator::Subdirectories);
|
||||
while (it.hasNext()) {
|
||||
QString path = it.next();
|
||||
// qDebug() << path;
|
||||
QFileInfo sourceInfo(path);
|
||||
QString relativePath = source.relativeFilePath(path);
|
||||
QString destinationPath = destination.absoluteFilePath(relativePath);
|
||||
QFileInfo destinationInfo(destinationPath);
|
||||
if (sourceInfo.isDir()) {
|
||||
if (!destinationInfo.exists()) {
|
||||
if (!destination.mkdir(relativePath)) {
|
||||
errors << QObject::tr("Create '%1' failed").arg(destinationPath);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!destinationInfo.exists()) {
|
||||
qDebug() << "Copy" << path << "to" << destinationPath;
|
||||
if (!QFile::copy(path, destinationPath)) {
|
||||
errors << QObject::tr("Copy '%1' to '%2' failed").arg(path).arg(destinationPath);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (sourceInfo.lastModified() > destinationInfo.lastModified()) {
|
||||
// retrieve source contents
|
||||
QFile sourceFile(path);
|
||||
if (!sourceFile.open(QFile::ReadOnly)) {
|
||||
errors << QObject::tr("Open '%1' failed").arg(path);
|
||||
continue;
|
||||
}
|
||||
QString sourceContents = sourceFile.readAll();
|
||||
sourceFile.close();
|
||||
// try to retrieve destination contents
|
||||
QFile destinationFile(path);
|
||||
if (destinationFile.open(QFile::ReadOnly)) {
|
||||
QString destinationContents = destinationFile.readAll();
|
||||
destinationFile.close();
|
||||
if (sourceContents == destinationContents) {
|
||||
// qDebug() << "Skip" << path;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!destinationFile.open(QFile::WriteOnly)) {
|
||||
errors << QObject::tr("Write '%1' failed").arg(destinationPath);
|
||||
continue;
|
||||
}
|
||||
qDebug() << "Write" << destinationPath;
|
||||
QTextStream destinationStream(&destinationFile);
|
||||
destinationStream << sourceContents;
|
||||
destinationFile.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
#ifndef FOLDERSYNC_H_
|
||||
#define FOLDERSYNC_H_
|
||||
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
class QDir;
|
||||
|
||||
class FoldersSyncTask
|
||||
{
|
||||
public:
|
||||
FoldersSyncTask(const QString &folder1, const QString &folder2);
|
||||
void run();
|
||||
|
||||
protected:
|
||||
QStringList updateDir(const QDir &source, const QDir &destination);
|
||||
QString folders[2];
|
||||
};
|
||||
|
||||
#endif /* FOLDERSYNC_H_ */
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <QGridLayout>
|
||||
#include <QSpacerItem>
|
||||
|
||||
GenericPanel::GenericPanel(QWidget * parent, ModelData * model, GeneralSettings & generalSettings, FirmwareInterface * firmware):
|
||||
GenericPanel::GenericPanel(QWidget * parent, ModelData * model, GeneralSettings & generalSettings, Firmware * firmware):
|
||||
QWidget(parent),
|
||||
model(model),
|
||||
generalSettings(generalSettings),
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
class ModelData;
|
||||
class GeneralSettings;
|
||||
class FirmwareInterface;
|
||||
class Firmware;
|
||||
class QGridLayout;
|
||||
class QString;
|
||||
|
||||
@@ -20,7 +20,7 @@ class GenericPanel : public QWidget
|
||||
friend class AutoLineEdit;
|
||||
|
||||
public:
|
||||
GenericPanel(QWidget *parent, ModelData * model, GeneralSettings & generalSettings, FirmwareInterface * firmware);
|
||||
GenericPanel(QWidget *parent, ModelData * model, GeneralSettings & generalSettings, Firmware * firmware);
|
||||
virtual ~GenericPanel();
|
||||
|
||||
signals:
|
||||
@@ -32,7 +32,7 @@ class GenericPanel : public QWidget
|
||||
protected:
|
||||
ModelData * model;
|
||||
GeneralSettings & generalSettings;
|
||||
FirmwareInterface * firmware;
|
||||
Firmware * firmware;
|
||||
bool lock;
|
||||
void addLabel(QGridLayout * gridLayout, const QString &text, int col, bool mimimize=false);
|
||||
void addEmptyLabel(QGridLayout * gridLayout, int col);
|
||||
|
||||
Reference in New Issue
Block a user