Modernize FolderStatusModel::slotSetProgress.
authorCamila Ayres <hello@camilasan.com>
Wed, 7 Feb 2024 21:03:04 +0000 (22:03 +0100)
committerCamila Ayres <hello@camilasan.com>
Mon, 29 Jul 2024 18:06:32 +0000 (20:06 +0200)
- Add const auto.
- Change variable names to be more clear.

Signed-off-by: Camila Ayres <hello@camilasan.com>
src/gui/folderstatusmodel.cpp

index 27f87007dc36fb9804303ee9d9cfd4f7303ae325..b11581b66e32be2f2f20ef80a5a597a7c6bc5212 100644 (file)
@@ -984,63 +984,62 @@ void FolderStatusModel::slotSetProgress(const ProgressInfo &progress)
         return;
     }
 
-    auto *pi = &_folders[folderIndex]._progress;
+    auto *const subFolderProgress = &_folders[folderIndex]._progress;
 
     if (progress.status() == ProgressInfo::Starting) {
         _isSyncRunningForAwhile = false;
     }
 
-    QVector<int> roles;
-    roles << FolderStatusDelegate::SyncProgressItemString
-          << FolderStatusDelegate::WarningCount
-          << Qt::ToolTipRole;
+    const QVector<int> roles{ FolderStatusDelegate::SyncProgressItemString, FolderStatusDelegate::WarningCount,
+                             Qt::ToolTipRole };
 
     if (progress.status() == ProgressInfo::Discovery) {
         if (!progress._currentDiscoveredRemoteFolder.isEmpty()) {
-            pi->_overallSyncString = tr("Checking for changes in remote \"%1\"").arg(progress._currentDiscoveredRemoteFolder);
+            subFolderProgress->_overallSyncString = tr("Checking for changes in remote \"%1\"").arg(progress._currentDiscoveredRemoteFolder);
             emit dataChanged(index(folderIndex), index(folderIndex), roles);
             return;
         } else if (!progress._currentDiscoveredLocalFolder.isEmpty()) {
-            pi->_overallSyncString = tr("Checking for changes in local \"%1\"").arg(progress._currentDiscoveredLocalFolder);
+            subFolderProgress->_overallSyncString = tr("Checking for changes in local \"%1\"").arg(progress._currentDiscoveredLocalFolder);
             emit dataChanged(index(folderIndex), index(folderIndex), roles);
             return;
         }
     }
 
     if (progress.status() == ProgressInfo::Reconcile) {
-        pi->_overallSyncString = tr("Reconciling changes");
+        subFolderProgress->_overallSyncString = tr("Reconciling changes");
         emit dataChanged(index(folderIndex), index(folderIndex), roles);
         return;
     }
 
     // Status is Starting, Propagation or Done
-
     if (!progress._lastCompletedItem.isEmpty()
         && Progress::isWarningKind(progress._lastCompletedItem._status)) {
-        pi->_warningCount++;
+        subFolderProgress->_warningCount++;
     }
 
     // find the single item to display:  This is going to be the bigger item, or the last completed
     // item if no items are in progress.
     auto curItem = progress._lastCompletedItem;
-    qint64 curItemProgress = -1; // -1 means finished
-    qint64 biggerItemSize = 0;
-    quint64 estimatedUpBw = 0;
-    quint64 estimatedDownBw = 0;
+    auto curItemProgress = -1; // -1 means finished
+    auto biggerItemSize = 0;
+    auto estimatedUpBw = 0;
+    auto estimatedDownBw = 0;
     QString allFilenames;
-    for (const auto &citm : progress._currentItems) {
-        if (curItemProgress == -1 || (ProgressInfo::isSizeDependent(citm._item)
-                                         && biggerItemSize < citm._item._size)) {
-            curItemProgress = citm._progress.completed();
-            curItem = citm._item;
-            biggerItemSize = citm._item._size;
+    for (const auto &syncFile : progress._currentItems) {
+        if (curItemProgress == -1 || (ProgressInfo::isSizeDependent(syncFile._item)
+                                         && biggerItemSize < syncFile._item._size)) {
+            curItemProgress = syncFile._progress.completed();
+            curItem = syncFile._item;
+            biggerItemSize = syncFile._item._size;
         }
-        if (citm._item._direction != SyncFileItem::Up) {
-            estimatedDownBw += progress.fileProgress(citm._item).estimatedBandwidth;
+
+        if (syncFile._item._direction != SyncFileItem::Up) {
+            estimatedDownBw += progress.fileProgress(syncFile._item).estimatedBandwidth;
         } else {
-            estimatedUpBw += progress.fileProgress(citm._item).estimatedBandwidth;
+            estimatedUpBw += progress.fileProgress(syncFile._item).estimatedBandwidth;
         }
-        auto fileName = QFileInfo(citm._item._file).fileName();
+
+        auto fileName = QFileInfo(syncFile._item._file).fileName();
         if (allFilenames.length() > 0) {
             //: Build a list of file names
             allFilenames.append(QStringLiteral(", \"%1\"").arg(fileName));
@@ -1058,8 +1057,6 @@ void FolderStatusModel::slotSetProgress(const ProgressInfo &progress)
 
     QString fileProgressString;
     if (ProgressInfo::isSizeDependent(curItem)) {
-        const auto s1 = Utility::octetsToString(curItemProgress);
-        const auto s2 = Utility::octetsToString(curItem._size);
         //quint64 estimatedBw = progress.fileProgress(curItem).estimatedBandwidth;
         if (estimatedUpBw || estimatedDownBw) {
             /*
@@ -1094,38 +1091,40 @@ void FolderStatusModel::slotSetProgress(const ProgressInfo &progress)
             }
         } else {
             //: Example text: "uploading foobar.png (2MB of 2MB)"
-            fileProgressString = tr("%1 %2 (%3 of %4)").arg(kindString, itemFileName, s1, s2);
+            fileProgressString = tr("%1 %2 (%3 of %4)").arg(kindString, itemFileName,
+                                                            Utility::octetsToString(curItemProgress),
+                                                            Utility::octetsToString(curItem._size));
         }
     } else if (!kindString.isEmpty()) {
         //: Example text: "uploading foobar.png"
         fileProgressString = tr("%1 %2").arg(kindString, itemFileName);
     }
-    pi->_progressString = fileProgressString;
+    subFolderProgress->_progressString = fileProgressString;
 
     // overall progress
     const auto completedSize = progress.completedSize();
-    const auto completedFile = progress.completedFiles();
     const auto currentFile = progress.currentFile();
-    const auto totalSize = qMax(completedSize, progress.totalSize());
     const auto totalFileCount = qMax(currentFile, progress.totalFiles());
+    const auto totalSize = qMax(completedSize, progress.totalSize());
+
     QString overallSyncString;
     if (totalSize > 0) {
-        const auto s1 = Utility::octetsToString(completedSize);
-        const auto s2 = Utility::octetsToString(totalSize);
+        const auto completedSizeString = Utility::octetsToString(completedSize);
+        const auto totalSizeString = Utility::octetsToString(totalSize);
 
-        const auto estimatedEta = progress.totalProgress().estimatedEta;
+        if (const auto estimatedEta = progress.totalProgress().estimatedEta;
+            progress.trustEta() && (estimatedEta > 0 || _isSyncRunningForAwhile)) {
 
-        if (progress.trustEta() && (estimatedEta > 0 || _isSyncRunningForAwhile)) {
             _isSyncRunningForAwhile = true;
             //: Example text: "5 minutes left, 12 MB of 345 MB, file 6 of 7"
             if (estimatedEta == 0) {
                 overallSyncString = tr("A few seconds left, %1 of %2, file %3 of %4")
-                                        .arg(s1, s2)
+                                        .arg(completedSizeString, totalSizeString)
                                         .arg(currentFile)
                                         .arg(totalFileCount);
             } else {
                 overallSyncString = tr("%5 left, %1 of %2, file %3 of %4")
-                                        .arg(s1, s2)
+                                        .arg(completedSizeString, totalSizeString)
                                         .arg(currentFile)
                                         .arg(totalFileCount)
                                         .arg(Utility::durationToDescriptiveString1(estimatedEta));
@@ -1134,7 +1133,7 @@ void FolderStatusModel::slotSetProgress(const ProgressInfo &progress)
         } else {
             //: Example text: "12 MB of 345 MB, file 6 of 7"
             overallSyncString = tr("%1 of %2, file %3 of %4")
-                                    .arg(s1, s2)
+                                    .arg(completedSizeString, totalSizeString)
                                     .arg(currentFile)
                                     .arg(totalFileCount);
         }
@@ -1142,15 +1141,14 @@ void FolderStatusModel::slotSetProgress(const ProgressInfo &progress)
         // Don't attempt to estimate the time left if there is no kb to transfer.
         overallSyncString = tr("%1 file %2 of %3").arg(kindString).arg(currentFile).arg(totalFileCount);
     }
-
-    pi->_overallSyncString = overallSyncString;
+    subFolderProgress->_overallSyncString = overallSyncString;
 
     auto overallPercent = 0;
-    if (totalFileCount > 0) {
+    if (const auto completedFile = progress.completedFiles();totalFileCount > 0) {
         // Add one 'byte' for each file so the percentage is moving when deleting or renaming files
         overallPercent = qRound(double(completedSize + completedFile) / double(totalSize + totalFileCount) * 100.0);
     }
-    pi->_overallPercent = qBound(0, overallPercent, 100);
+    subFolderProgress->_overallPercent = qBound(0, overallPercent, 100);
     emit dataChanged(index(folderIndex), index(folderIndex), roles);
 }