2016-10-04 23:55:20 +02:00
|
|
|
#include "./syncthingdir.h"
|
|
|
|
|
|
|
|
#include <c++utilities/conversion/stringconversion.h>
|
|
|
|
|
|
|
|
#include <QStringBuilder>
|
|
|
|
#include <QJsonObject>
|
2017-02-20 18:00:42 +01:00
|
|
|
#include <QCoreApplication>
|
2016-10-04 23:55:20 +02:00
|
|
|
|
|
|
|
using namespace ChronoUtilities;
|
|
|
|
using namespace ConversionUtilities;
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
|
2017-02-20 18:00:42 +01:00
|
|
|
QString statusString(SyncthingDirStatus status)
|
|
|
|
{
|
|
|
|
switch(status) {
|
|
|
|
case SyncthingDirStatus::Unknown:
|
|
|
|
return QCoreApplication::translate("SyncthingDirStatus", "unknown");
|
|
|
|
case SyncthingDirStatus::Idle:
|
|
|
|
return QCoreApplication::translate("SyncthingDirStatus", "idle");
|
|
|
|
case SyncthingDirStatus::Unshared:
|
|
|
|
return QCoreApplication::translate("SyncthingDirStatus", "unshared");
|
|
|
|
case SyncthingDirStatus::Scanning:
|
|
|
|
return QCoreApplication::translate("SyncthingDirStatus", "scanning");
|
|
|
|
case SyncthingDirStatus::Synchronizing:
|
|
|
|
return QCoreApplication::translate("SyncthingDirStatus", "synchronizing");
|
|
|
|
case SyncthingDirStatus::OutOfSync:
|
|
|
|
return QCoreApplication::translate("SyncthingDirStatus", "out of sync");
|
2017-03-02 17:59:55 +01:00
|
|
|
default:
|
|
|
|
return QString();
|
2017-02-20 18:00:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:55:20 +02:00
|
|
|
/*!
|
|
|
|
* \brief Assigns the status from the specified status string.
|
|
|
|
* \returns Returns whether the status has actually changed.
|
|
|
|
*/
|
|
|
|
bool SyncthingDir::assignStatus(const QString &statusStr, ChronoUtilities::DateTime time)
|
|
|
|
{
|
|
|
|
if(lastStatusUpdate > time) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
lastStatusUpdate = time;
|
|
|
|
}
|
|
|
|
SyncthingDirStatus newStatus;
|
|
|
|
if(statusStr == QLatin1String("idle")) {
|
|
|
|
progressPercentage = 0;
|
2016-11-01 16:59:58 +01:00
|
|
|
newStatus = SyncthingDirStatus::Idle;
|
2016-10-04 23:55:20 +02:00
|
|
|
} else if(statusStr == QLatin1String("scanning")) {
|
|
|
|
newStatus = SyncthingDirStatus::Scanning;
|
|
|
|
} else if(statusStr == QLatin1String("syncing")) {
|
2016-10-05 22:33:10 +02:00
|
|
|
// ensure status changed signal is emitted
|
2016-10-04 23:55:20 +02:00
|
|
|
if(!errors.empty()) {
|
2016-10-05 22:33:10 +02:00
|
|
|
status = SyncthingDirStatus::Unknown;
|
2016-10-04 23:55:20 +02:00
|
|
|
}
|
2016-10-05 22:33:10 +02:00
|
|
|
// errors become obsolete; however errors must be kept as previous errors to be able
|
|
|
|
// to identify new errors occuring during this sync attempt as known errors
|
|
|
|
previousErrors.clear();
|
|
|
|
previousErrors.swap(errors);
|
2016-10-04 23:55:20 +02:00
|
|
|
newStatus = SyncthingDirStatus::Synchronizing;
|
|
|
|
} else if(statusStr == QLatin1String("error")) {
|
|
|
|
progressPercentage = 0;
|
|
|
|
newStatus = SyncthingDirStatus::OutOfSync;
|
|
|
|
} else {
|
2016-11-01 16:59:58 +01:00
|
|
|
newStatus = SyncthingDirStatus::Idle;
|
|
|
|
}
|
|
|
|
if(newStatus == SyncthingDirStatus::Idle) {
|
|
|
|
if(!errors.empty()) {
|
|
|
|
newStatus = SyncthingDirStatus::OutOfSync;
|
|
|
|
} else if(devices.size() < 2) {
|
|
|
|
// FIXME: we can assume only own device is assigned, correct?
|
|
|
|
newStatus = SyncthingDirStatus::Unshared;
|
|
|
|
}
|
2016-10-04 23:55:20 +02:00
|
|
|
}
|
|
|
|
if(newStatus != status) {
|
|
|
|
switch(status) {
|
|
|
|
case SyncthingDirStatus::Scanning:
|
|
|
|
lastScanTime = DateTime::now();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
status = newStatus;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SyncthingDir::assignStatus(SyncthingDirStatus newStatus, DateTime time)
|
|
|
|
{
|
|
|
|
if(lastStatusUpdate > time) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
lastStatusUpdate = time;
|
|
|
|
}
|
|
|
|
switch(newStatus) {
|
|
|
|
case SyncthingDirStatus::Unknown:
|
2016-11-01 16:59:58 +01:00
|
|
|
case SyncthingDirStatus::Idle:
|
|
|
|
case SyncthingDirStatus::Unshared:
|
2016-10-04 23:55:20 +02:00
|
|
|
if(!errors.empty()) {
|
|
|
|
newStatus = SyncthingDirStatus::OutOfSync;
|
2016-11-01 16:59:58 +01:00
|
|
|
} else if(devices.size() < 2) {
|
|
|
|
// FIXME: we can assume only own device is assigned, correct?
|
|
|
|
newStatus = SyncthingDirStatus::Unshared;
|
2016-10-04 23:55:20 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if(newStatus != status) {
|
|
|
|
switch(status) {
|
|
|
|
case SyncthingDirStatus::Scanning:
|
|
|
|
lastScanTime = DateTime::now();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
status = newStatus;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-20 21:00:18 +01:00
|
|
|
QString SyncthingDir::statusString() const
|
|
|
|
{
|
|
|
|
if(paused) {
|
|
|
|
return QCoreApplication::translate("SyncthingDir", "paused");
|
|
|
|
} else {
|
|
|
|
return ::Data::statusString(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 23:17:53 +02:00
|
|
|
QStringRef SyncthingDir::pathWithoutTrailingSlash() const
|
|
|
|
{
|
|
|
|
QStringRef dirPath(&path);
|
|
|
|
while(dirPath.endsWith(QChar('/'))) {
|
|
|
|
#if QT_VERSION_MAJOR >= 5 && QT_VERSION_MINOR >= 8
|
|
|
|
dirPath.chop(1);
|
|
|
|
#else
|
|
|
|
dirPath = dirPath.left(dirPath.size() - 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return dirPath;
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:55:20 +02:00
|
|
|
SyncthingItemDownloadProgress::SyncthingItemDownloadProgress(const QString &containingDirPath, const QString &relativeItemPath, const QJsonObject &values) :
|
|
|
|
relativePath(relativeItemPath),
|
|
|
|
fileInfo(containingDirPath % QChar('/') % QString(relativeItemPath).replace(QChar('\\'), QChar('/'))),
|
|
|
|
blocksCurrentlyDownloading(values.value(QStringLiteral("Pulling")).toInt()),
|
|
|
|
blocksAlreadyDownloaded(values.value(QStringLiteral("Pulled")).toInt()),
|
|
|
|
totalNumberOfBlocks(values.value(QStringLiteral("Total")).toInt()),
|
|
|
|
downloadPercentage((blocksAlreadyDownloaded > 0 && totalNumberOfBlocks > 0)
|
|
|
|
? (static_cast<unsigned int>(blocksAlreadyDownloaded) * 100 / static_cast<unsigned int>(totalNumberOfBlocks))
|
|
|
|
: 0),
|
|
|
|
blocksCopiedFromOrigin(values.value(QStringLiteral("CopiedFromOrigin")).toInt()),
|
|
|
|
blocksCopiedFromElsewhere(values.value(QStringLiteral("CopiedFromElsewhere")).toInt()),
|
|
|
|
blocksReused(values.value(QStringLiteral("Reused")).toInt()),
|
|
|
|
bytesAlreadyHandled(values.value(QStringLiteral("BytesDone")).toInt()),
|
|
|
|
totalNumberOfBytes(values.value(QStringLiteral("BytesTotal")).toInt()),
|
|
|
|
label(QStringLiteral("%1 / %2 - %3 %").arg(
|
|
|
|
QString::fromLatin1(dataSizeToString(blocksAlreadyDownloaded > 0 ? static_cast<uint64>(blocksAlreadyDownloaded) * syncthingBlockSize : 0).data()),
|
|
|
|
QString::fromLatin1(dataSizeToString(totalNumberOfBlocks > 0 ? static_cast<uint64>(totalNumberOfBlocks) * syncthingBlockSize : 0).data()),
|
|
|
|
QString::number(downloadPercentage))
|
|
|
|
)
|
|
|
|
{}
|
|
|
|
|
|
|
|
} // namespace Data
|