From 1c6f93872384e35bd769918482259e6125863374 Mon Sep 17 00:00:00 2001 From: bsongis Date: Wed, 5 Nov 2014 07:29:26 +0100 Subject: [PATCH] Synchronization function for #1808 added. Still missing a button somewhere to call it. --- companion/src/CMakeLists.txt | 3 ++ companion/src/foldersync.cpp | 79 ++++++++++++++++++++++++++++++++++++ companion/src/foldersync.h | 20 +++++++++ companion/src/hexspinbox.cpp | 27 ++++++++++++ companion/src/hexspinbox.h | 24 +++++++++++ 5 files changed, 153 insertions(+) create mode 100755 companion/src/foldersync.cpp create mode 100755 companion/src/foldersync.h create mode 100755 companion/src/hexspinbox.cpp create mode 100755 companion/src/hexspinbox.h diff --git a/companion/src/CMakeLists.txt b/companion/src/CMakeLists.txt index 1aa1b077d..b3c4398f2 100644 --- a/companion/src/CMakeLists.txt +++ b/companion/src/CMakeLists.txt @@ -167,6 +167,8 @@ set(companion_SRCS qcustomplot.cpp wizarddata.cpp wizarddialog.cpp + foldersync.cpp + hexspinbox.cpp ) set(companion_MOC_HDRS @@ -192,6 +194,7 @@ set(companion_MOC_HDRS qautocombobox.h helpers.h wizarddialog.h + hexspinbox.h ) set(companion_UIS diff --git a/companion/src/foldersync.cpp b/companion/src/foldersync.cpp new file mode 100755 index 000000000..694dc5487 --- /dev/null +++ b/companion/src/foldersync.cpp @@ -0,0 +1,79 @@ +#include "foldersync.h" +#include +#include +#include +#include +#include + +FoldersSyncTask::FoldersSyncTask(const QString &folder1, const QString &folder2) +{ + folders[0] = folder1; + folders[1] = folder2; +} + +void FoldersSyncTask::run() +{ + QStringList errors = updateDir(folders[0], folders[1]) + updateDir(folders[1], folders[0]); + if (errors.count() > 0) { + QMessageBox::warning(NULL, QObject::tr("Copy errors"), 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(); + 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; +} diff --git a/companion/src/foldersync.h b/companion/src/foldersync.h new file mode 100755 index 000000000..fe11bfaed --- /dev/null +++ b/companion/src/foldersync.h @@ -0,0 +1,20 @@ +#ifndef FOLDERSYNC_H_ +#define FOLDERSYNC_H_ + +#include +#include + +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_ */ diff --git a/companion/src/hexspinbox.cpp b/companion/src/hexspinbox.cpp new file mode 100755 index 000000000..4e76918f7 --- /dev/null +++ b/companion/src/hexspinbox.cpp @@ -0,0 +1,27 @@ +#include "hexspinbox.h" + +HexSpinBox::HexSpinBox(QWidget *parent): + QSpinBox(parent) +{ + validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this); +} + +QValidator::State HexSpinBox::validate(QString &text, int &pos) const +{ + return validator->validate(text, pos); +} + +int HexSpinBox::valueFromText(const QString &text) const +{ + return text.toInt(0, 16); +} + +QString HexSpinBox::textFromValue(int value) const +{ + QString text; + text.sprintf("%04X", value); + return text; +} + + + diff --git a/companion/src/hexspinbox.h b/companion/src/hexspinbox.h new file mode 100755 index 000000000..4cbc6d43a --- /dev/null +++ b/companion/src/hexspinbox.h @@ -0,0 +1,24 @@ +#ifndef HEXSPINBOX_H_ +#define HEXSPINBOX_H_ + +#include + +class QRegExpValidator; + +class HexSpinBox : public QSpinBox +{ + Q_OBJECT + + public: + HexSpinBox(QWidget *parent = 0); + + protected: + QValidator::State validate(QString &text, int &pos) const; + int valueFromText(const QString &text) const; + QString textFromValue(int value) const; + + private: + QRegExpValidator *validator; +}; + +#endif /* HEXSPINBOX_H_ */