Synchronization function for #1808 added. Still missing a button
somewhere to call it.
This commit is contained in:
@@ -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
|
||||
|
||||
Executable
+79
@@ -0,0 +1,79 @@
|
||||
#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()
|
||||
{
|
||||
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;
|
||||
}
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#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_ */
|
||||
Executable
+27
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#ifndef HEXSPINBOX_H_
|
||||
#define HEXSPINBOX_H_
|
||||
|
||||
#include <QSpinBox>
|
||||
|
||||
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_ */
|
||||
Reference in New Issue
Block a user