Commit fc6e5457 authored by Viktor Porvaznik's avatar Viktor Porvaznik Committed by Viktor Porvaznik

Refefactoring in progress

parent 8c3cab96
......@@ -67,6 +67,7 @@ bool FileSync::Client::isDeamonRunning() const
void FileSync::Client::requestSync(const QString &deviceId)
{
qCDebug(LOG_CLIENT) << "Starting sync for: " << deviceId;
QStringList syncList;
syncList.append("ALL");
......
......@@ -30,8 +30,6 @@ ApplicationWindow {
visible: true
width: 800
property QtObject currentDevice: null
menuBar: MenuBar {
Menu {
title: qsTr("File")
......@@ -63,8 +61,11 @@ ApplicationWindow {
id: syncButton
anchors.right: parent.right
text: qsTr("Synchronize ALL")
property QtObject currentDevice: NULL
onClicked: {
client.requestSync(mainWindow.currentDevice.id());
currentDevice = devicesCombo.currentIndex >= 0 ? devicesModel.data(devicesModel.index(devicesCombo.currentIndex, 0), DevicesModel.DeviceRole) : null
console.log("Requesting sync for: " + currentDevice.id())
client.requestSync(currentDevice.id());
}
}
}
......@@ -185,20 +186,18 @@ ApplicationWindow {
}
onRowsInserted: if (devicesCombo.currentIndex < 0) {
devicesCombo.currentIndex = 0
mainWindow.currentDevice = devicesCombo.currentIndex >= 0 ? devicesModel.data(devicesModel.index(devicesCombo.currentIndex, 0), DevicesModel.DeviceRole) : null
}
}
textRole: "display"
property QtObject currentDevice: NULL
onCurrentTextChanged: {
if (count > 0) {
mainWindow.currentDevice = devicesCombo.currentIndex >= 0 ? devicesModel.data(devicesModel.index(devicesCombo.currentIndex, 0), DevicesModel.DeviceRole) : null
if (currentDevice.isReachable()) {
syncTableModel.setDeviceId(mainWindow.currentDevice.id())
syncTableModel.loadModel(mainWindow.currentDevice.id())
console.log("dooo")
currentDevice = devicesCombo.currentIndex >= 0 ? devicesModel.data(devicesModel.index(devicesCombo.currentIndex, 0), DevicesModel.DeviceRole) : null
if (currentDevice.isReachable) {
console.log("Loading model for: " + currentDevice.id())
syncTableModel.setDeviceId(currentDevice.id())
syncTableModel.loadModel(currentDevice.id())
}
} else {
console.log("nothing")
}
}
}
......
......@@ -67,6 +67,7 @@ void FileSync::Executor::run()
}
QJsonArray toSync = Config::instance(m_deviceId).data();
// TODO fileter for only provided m_syncList
QJsonArray *fileSyncList = new QJsonArray();
for (const QJsonValue &value: toSync)
{
......@@ -125,10 +126,11 @@ FileSync::SyncResult FileSync::Executor::determineAndPerformSyncOperation(
{
QString localPath(value.toObject().value("localPath").toString());
QString remotePath(value.toObject().value("remotePath").toString());
QString direction(value.toObject().value("direction").toString());
QString postSyncScript(value.toObject().value("postSyncScript").toString());
SyncOperation *operation = new SyncOperation(determineSyncOperation(
localPath, remotePath));
SyncOperation *operation = new SyncOperation(calculateSyncOperation(
localPath, remotePath, direction));
if (operation != NULL)
{
......@@ -296,8 +298,6 @@ FileSync::Executor::synchronizeEntry(QJsonArray *syncFileList,
FileSync::WRONG_PATHS_ERROR.arg(localPath, remotePath));
// errorOccured = true;
}
LOG_D(LOG_EXECUTOR, "Passx" << remotePath);
}
bool FileSync::Executor::performSyncOperation(
......@@ -396,19 +396,21 @@ bool FileSync::Executor::copyFile(QString sourceFilePath, QString destFilePath)
}
FileSync::SyncOperation
FileSync::Executor::determineSyncOperation(const QString &localPath,
const QString &remotePath)
FileSync::Executor::calculateSyncOperation(const QString &localPath,
const QString &remotePath,
const QString &direction)
{
// TODO improve structure (if / else), possibly introduce uni-directional sync
QString localHash = Util::convertToHash(localPath);
QString remoteHash = Util::convertToHash(remotePath);
QString currentLocalRemotePairHash = localHash + remoteHash;
QString existingLocalRemotePairHash = Index::instance(m_deviceId).search(
currentLocalRemotePairHash);
QString indexKey = localHash + remoteHash;
QString indedValue = Index::instance(m_deviceId).search(
indexKey);
QFileInfo localPathInfo = QFileInfo(localPath);
QFileInfo remotePathInfo = QFileInfo(remotePath);
SyncOperation operation;
operation.setType(SyncOperation::UNKNOWN);
if (!localPathInfo.exists() && !remotePathInfo.exists())
{
// Nothing exists, no operation
......@@ -417,7 +419,7 @@ FileSync::Executor::determineSyncOperation(const QString &localPath,
{
// Remote path exist - operation will be FROM_REMOTE
operation.setType(SyncOperation::FROM_REMOTE);
operation.setHashRecord(currentLocalRemotePairHash,
operation.setHashRecord(indexKey,
Util::convertToHash(
Util::getFileContent(remotePath)));
}
......@@ -425,7 +427,7 @@ FileSync::Executor::determineSyncOperation(const QString &localPath,
{
// Local path exist - operation will be FROM_LOCAL
operation.setType(SyncOperation::FROM_LOCAL);
operation.setHashRecord(currentLocalRemotePairHash,
operation.setHashRecord(indexKey,
Util::convertToHash(
Util::getFileContent(localPath)));
}
......@@ -436,28 +438,19 @@ FileSync::Executor::determineSyncOperation(const QString &localPath,
QString remoteContentHash = Util::convertToHash(
Util::getFileContent(remotePath));
// Both files / directories exist
if (existingLocalRemotePairHash == "")
if (operation.getType() == SyncOperation::UNKNOWN)
{
}
if (indedValue == "")
{
LOG_D(LOG_EXECUTOR, FileSync::SYNC_DETERMINATION_BY_MTIME.arg(localPath,
remotePath));
if (localPathInfo.lastModified().toMSecsSinceEpoch() ==
remotePathInfo.lastModified().toMSecsSinceEpoch())
{
operation.setType(SyncOperation::NONE);
}
else if (localPathInfo.lastModified().toMSecsSinceEpoch() >=
remotePathInfo.lastModified().toMSecsSinceEpoch())
{
operation.setType(SyncOperation::FROM_LOCAL);
operation.setHashRecord(currentLocalRemotePairHash,
localContentHash);
}
else
{
operation.setType(SyncOperation::FROM_REMOTE);
operation.setHashRecord(currentLocalRemotePairHash,
remoteContentHash);
}
operation.setFromTimestamp(
localPathInfo.lastModified().toMSecsSinceEpoch(),
remotePathInfo.lastModified().toMSecsSinceEpoch(),
indexKey, localContentHash, remoteContentHash);
}
else
{
......@@ -465,22 +458,22 @@ FileSync::Executor::determineSyncOperation(const QString &localPath,
{
operation.setType(SyncOperation::NONE);
}
else if ((existingLocalRemotePairHash == remoteContentHash) &&
(existingLocalRemotePairHash != localContentHash))
else if ((indedValue == remoteContentHash) &&
(indedValue != localContentHash))
{
operation.setType(SyncOperation::FROM_LOCAL);
operation.setHashRecord(currentLocalRemotePairHash,
operation.setHashRecord(indexKey,
localContentHash);
}
else if ((existingLocalRemotePairHash != remoteContentHash) &&
(existingLocalRemotePairHash == localContentHash))
else if ((indedValue != remoteContentHash) &&
(indedValue == localContentHash))
{
operation.setType(SyncOperation::FROM_REMOTE);
operation.setHashRecord(currentLocalRemotePairHash,
operation.setHashRecord(indexKey,
remoteContentHash);
}
else if ((existingLocalRemotePairHash != remoteContentHash) &&
(existingLocalRemotePairHash != localContentHash))
else if ((indedValue != remoteContentHash) &&
(indedValue != localContentHash))
{
operation.setType(SyncOperation::CONFLICT);
}
......
......@@ -102,9 +102,10 @@ namespace FileSync
copyFile(QString sourceFilePath, QString destFilePath);
SyncOperation
determineSyncOperation(
calculateSyncOperation(
const QString &localPath,
const QString &remotePath);
const QString &remotePath,
const QString &direction);
Q_SIGNALS:
......
......@@ -42,11 +42,11 @@ QPair<QString, QString> FileSync::SyncOperation::getHashRecord() const
return QPair<QString, QString>(m_indexRecordKey, m_indexRecordValue);
}
void FileSync::SyncOperation::setHashRecord(const QString &newHashRecordKey,
const QString &newHashRecordValue)
void FileSync::SyncOperation::updateRecord(const QString &newIndexRecordKey,
const QString &newIndexRecordValue)
{
m_indexRecordKey = newHashRecordKey;
m_indexRecordValue = newHashRecordValue;
m_indexRecordKey = newIndexRecordKey;
m_indexRecordValue = newIndexRecordValue;
}
FileSync::SyncOperation::SyncOperation(const FileSync::SyncOperation &other)
......
......@@ -36,6 +36,15 @@ namespace FileSync
SyncOperation();
SyncOperation(const SyncOperationType &type, const QString &indexRecordKey,
const QString &indexRecordValue) : m_type(type),
m_indexRecordKey(
indexRecordKey),
m_indexRecordValue(
indexRecordValue)
{
}
SyncOperation(const SyncOperation &other);
SyncOperationType getType() const;
......@@ -44,13 +53,39 @@ namespace FileSync
void setType(SyncOperationType newType);
void setHashRecord(const QString &newHashRecordKey,
const QString &newHashRecordValue);
static SyncOperation calculateFromTimestamp(const qint64 localMTime, const qint64 remoteMTime,
const QString &indexRecordKey,
const QString &localHash, const QString &remoteHash)
{
SyncOperationType type = SyncOperation::UNKNOWN;
QString m_indexRecordKey = ;
if (localMTime ==
remoteMTime)
{
type = SyncOperation::NONE;
}
else if (localMTime >=
remoteMTime)
{
type = SyncOperation::FROM_LOCAL;
updateRecord(indexRecordKey,
localHash);
}
else
{
type = SyncOperation::FROM_REMOTE;
updateRecord(indexRecordKey,
remoteHash);
}
}
private:
SyncOperationType m_type;
QString m_indexRecordKey;
QString m_indexRecordValue;
void updateRecord(const QString &newIndexRecordKey,
const QString &newIndexRecordValue);
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment