1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
#include "./syncthingdir.h"
#include <c++utilities/conversion/stringconversion.h>
#include <QCoreApplication>
#include <QHash>
#include <QJsonObject>
#include <QStringBuilder>
using namespace CppUtilities;
namespace Data {
QString statusString(SyncthingDirStatus status)
{
switch (status) {
case SyncthingDirStatus::Unknown:
return QCoreApplication::translate("SyncthingDir", "Unknown");
case SyncthingDirStatus::Idle:
return QCoreApplication::translate("SyncthingDir", "Up to Date");
case SyncthingDirStatus::WaitingToScan:
return QCoreApplication::translate("SyncthingDir", "Waiting to Scan");
case SyncthingDirStatus::Scanning:
return QCoreApplication::translate("SyncthingDir", "Scanning");
case SyncthingDirStatus::WaitingToSync:
return QCoreApplication::translate("SyncthingDir", "Waiting to Sync");
case SyncthingDirStatus::PreparingToSync:
return QCoreApplication::translate("SyncthingDir", "Preparing to Sync");
case SyncthingDirStatus::Synchronizing:
return QCoreApplication::translate("SyncthingDir", "Syncing");
case SyncthingDirStatus::Cleaning:
return QCoreApplication::translate("SyncthingDir", "Cleaning Versions");
case SyncthingDirStatus::WaitingToClean:
return QCoreApplication::translate("SyncthingDir", "Waiting to Clean");
case SyncthingDirStatus::OutOfSync:
return QCoreApplication::translate("SyncthingDir", "Out of Sync");
}
return QString();
}
QString dirTypeString(SyncthingDirType dirType)
{
switch (dirType) {
case SyncthingDirType::Unknown:
return QCoreApplication::translate("SyncthingDirType", "Unknown");
case SyncthingDirType::SendReceive:
return QCoreApplication::translate("SyncthingDirType", "Send & Receive");
case SyncthingDirType::SendOnly:
return QCoreApplication::translate("SyncthingDirType", "Send Only");
case SyncthingDirType::ReceiveOnly:
return QCoreApplication::translate("SyncthingDirType", "Receive Only");
case SyncthingDirType::ReceiveEncrypted:
return QCoreApplication::translate("SyncthingDirType", "Receive Encrypted");
}
return QString();
}
bool SyncthingDir::checkWhetherStatusUpdateRelevant(SyncthingEventId eventId, DateTime time)
{
// ignore old updates
if (lastStatusUpdateEvent > eventId) {
return false;
}
lastStatusUpdateEvent = eventId;
lastStatusUpdateTime = time;
return true;
}
bool SyncthingDir::finalizeStatusUpdate(SyncthingDirStatus newStatus, SyncthingEventId eventId, DateTime time)
{
// handle obsoletion of out-of-sync items: no FolderErrors are accepted older than the last "sync" state are accepted
if (newStatus == SyncthingDirStatus::PreparingToSync || newStatus == SyncthingDirStatus::Synchronizing) {
// update time of last "sync" state and obsolete currently assigned errors
lastSyncStartedEvent = eventId;
lastSyncStartedTime = time; // used internally and not displayed, hence keep it GMT
itemErrors.clear();
pullErrorCount = 0;
} else if (lastSyncStartedTime.isNull() && newStatus != SyncthingDirStatus::OutOfSync) {
// prevent adding new errors from "before the first status" if the time of the last "sync" state is unknown
lastSyncStartedEvent = eventId;
lastSyncStartedTime = time;
}
// clear global error if not out-of-sync anymore
if (newStatus != SyncthingDirStatus::OutOfSync) {
globalError.clear();
}
// consider the directory still as out-of-sync if there are still pull errors
// note: Syncthing reports status changes to "idle" despite pull errors. This means we can only rely on reading
// a "FolderSummary" event without pull errors for clearing the out-of-sync status.
if (pullErrorCount && (newStatus == SyncthingDirStatus::Unknown || newStatus == SyncthingDirStatus::Idle)) {
newStatus = SyncthingDirStatus::OutOfSync;
}
if (newStatus == status) {
return false;
}
// update last scan time if the previous status was scanning
if (status == SyncthingDirStatus::Scanning) {
// FIXME: better use \a time and convert it from GMT to local time
lastScanTime = DateTime::now();
}
status = newStatus;
return true;
}
/*!
* \brief Assigns the status from the specified status string.
* \returns Returns whether the status has actually changed.
* \remarks
* The status mapping is defined by `lib/model/folderstate.go`. Additional statuses are made up on UI-level
* in `gui/default/syncthing/core/syncthingController.js` but those are not handled by this function.
*/
bool SyncthingDir::assignStatus(const QString &statusStr, SyncthingEventId eventId, CppUtilities::DateTime time)
{
if (!checkWhetherStatusUpdateRelevant(eventId, time)) {
return false;
}
static const auto statusMapping = QHash<QString, SyncthingDirStatus>{
{ QStringLiteral("idle"), SyncthingDirStatus::Idle },
{ QStringLiteral("scanning"), SyncthingDirStatus::Scanning },
{ QStringLiteral("scan-waiting"), SyncthingDirStatus::WaitingToScan },
{ QStringLiteral("sync-waiting"), SyncthingDirStatus::WaitingToSync },
{ QStringLiteral("sync-preparing"), SyncthingDirStatus::PreparingToSync },
{ QStringLiteral("syncing"), SyncthingDirStatus::Synchronizing },
{ QStringLiteral("cleaning"), SyncthingDirStatus::Cleaning },
{ QStringLiteral("clean-waiting"), SyncthingDirStatus::WaitingToClean },
{ QStringLiteral("error"), SyncthingDirStatus::OutOfSync },
};
const auto i = statusMapping.find(statusStr);
const auto newStatus = i != statusMapping.cend() ? *i : SyncthingDirStatus::Unknown;
switch (newStatus) {
case SyncthingDirStatus::Idle:
case SyncthingDirStatus::OutOfSync:
completionPercentage = 0;
break;
case Data::SyncthingDirStatus::WaitingToSync:
case Data::SyncthingDirStatus::PreparingToSync:
// ensure status changed signal is emitted
if (!itemErrors.empty()) {
status = SyncthingDirStatus::Unknown;
}
break;
default:;
}
rawStatus = statusStr;
return finalizeStatusUpdate(newStatus, eventId, time);
}
bool SyncthingDir::assignDirType(const QString &dirTypeStr)
{
static const auto typeMapping = QHash<QString, SyncthingDirType>{
{ QStringLiteral("sendreceive"), SyncthingDirType::SendReceive },
{ QStringLiteral("readwrite"), SyncthingDirType::SendReceive },
{ QStringLiteral("sendonly"), SyncthingDirType::SendOnly },
{ QStringLiteral("readonly"), SyncthingDirType::SendOnly },
{ QStringLiteral("receiveonly"), SyncthingDirType::ReceiveOnly },
{ QStringLiteral("receiveencrypted"), SyncthingDirType::ReceiveEncrypted },
};
const auto i = typeMapping.find(dirTypeStr);
dirType = i != typeMapping.cend() ? *i : SyncthingDirType::Unknown;
return dirType != SyncthingDirType::Unknown;
}
/*!
* \brief Returns a status string for the directory.
* \remarks
* This function does not only take the SyncthingDirStatus into account but also other properties of \a dir. This function is therefore
* similar to the function `$scope.folderStatus` of Syncthing's official UI (see `gui/default/syncthing/core/syncthingController.js`).
*/
QString SyncthingDir::statusString() const
{
if (paused) {
return QCoreApplication::translate("SyncthingDir", "Paused");
}
if (isUnshared()) {
return QCoreApplication::translate("SyncthingDir", "Unshared");
}
switch (status) {
case SyncthingDirStatus::Unknown:
if (!rawStatus.isEmpty()) {
return rawStatus;
}
break;
case SyncthingDirStatus::Idle:
if (receiveOnlyStats.total > 0) {
switch (dirType) {
case SyncthingDirType::ReceiveOnly:
return QCoreApplication::translate("SyncthingDir", "Local Additions");
case SyncthingDirType::ReceiveEncrypted:
return QCoreApplication::translate("SyncthingDir", "Unexpected Items");
default:;
}
}
break;
case SyncthingDirStatus::Scanning:
if (scanningPercentage > 0) {
if (scanningRate != 0.0) {
return QCoreApplication::translate("SyncthingDir", "Scanning (%1 %, %2)")
.arg(scanningPercentage)
.arg(bitrateToString(scanningRate * 0.008, true).data());
}
return QCoreApplication::translate("SyncthingDir", "Scanning (%1 %)").arg(scanningPercentage);
}
break;
case SyncthingDirStatus::Synchronizing:
if (completionPercentage > 0) {
return QCoreApplication::translate("SyncthingDir", "Syncing (%1 %, %2)")
.arg(completionPercentage)
.arg(dataSizeToString(neededStats.bytes).data());
}
break;
default:;
}
return Data::statusString(status);
}
QtUtilities::StringView SyncthingDir::pathWithoutTrailingSlash() const
{
auto dirPath = QtUtilities::makeStringView(path);
while (dirPath.endsWith(QChar('/'))) {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
dirPath.chop(1);
#else
dirPath = dirPath.left(dirPath.size() - 1);
#endif
}
return dirPath;
}
bool SyncthingDir::areRemotesUpToDate() const
{
for (const auto &completionForDev : completionByDevice) {
if (!completionForDev.second.needed.isNull()) {
return false;
}
}
return true;
}
SyncthingItemDownloadProgress::SyncthingItemDownloadProgress(
const QString &containingDirPath, const QString &relativeItemPath, const QJsonObject &values)
: relativePath(relativeItemPath)
, fileInfo(containingDirPath % QChar('/') % QString(relativeItemPath).replace(QChar('\\'), QChar('/')))
, blocksCurrentlyDownloading(values.value(QLatin1String("Pulling")).toInt())
, blocksAlreadyDownloaded(values.value(QLatin1String("Pulled")).toInt())
, totalNumberOfBlocks(values.value(QLatin1String("Total")).toInt())
, downloadPercentage((blocksAlreadyDownloaded > 0 && totalNumberOfBlocks > 0)
? (static_cast<unsigned int>(blocksAlreadyDownloaded) * 100 / static_cast<unsigned int>(totalNumberOfBlocks))
: 0)
, blocksCopiedFromOrigin(values.value(QLatin1String("CopiedFromOrigin")).toInt())
, blocksCopiedFromElsewhere(values.value(QLatin1String("CopiedFromElsewhere")).toInt())
, blocksReused(values.value(QLatin1String("Reused")).toInt())
, bytesAlreadyHandled(values.value(QLatin1String("BytesDone")).toInt())
, totalNumberOfBytes(values.value(QLatin1String("BytesTotal")).toInt())
, label(QStringLiteral("%1 / %2 - %3 %")
.arg(QString::fromLatin1(
dataSizeToString(blocksAlreadyDownloaded > 0 ? static_cast<std::uint64_t>(blocksAlreadyDownloaded) * syncthingBlockSize : 0)
.data()),
QString::fromLatin1(
dataSizeToString(totalNumberOfBlocks > 0 ? static_cast<std::uint64_t>(totalNumberOfBlocks) * syncthingBlockSize : 0).data()),
QString::number(downloadPercentage)))
{
}
SyncthingStatistics &SyncthingStatistics::operator+=(const SyncthingStatistics &other)
{
bytes += other.bytes;
deletes += other.deletes;
dirs += other.dirs;
files += other.files;
symlinks += other.symlinks;
total += other.total;
return *this;
}
QString Data::SyncthingStatistics::bytesAsString() const
{
return QString::fromStdString(CppUtilities::dataSizeToString(bytes));
}
/*!
* \brief Computes overall statistics for the specified \a directories.
*/
SyncthingOverallDirStatistics::SyncthingOverallDirStatistics(const std::vector<SyncthingDir> &directories)
{
for (const auto &dir : directories) {
local += dir.localStats;
global += dir.globalStats;
needed += dir.neededStats;
}
}
} // namespace Data
|