FolderStatusModel: attempt to detect removed undecided files #3612
authorOlivier Goffart <ogoffart@woboq.com>
Mon, 26 Oct 2015 14:46:11 +0000 (15:46 +0100)
committerOlivier Goffart <ogoffart@woboq.com>
Fri, 30 Oct 2015 11:43:33 +0000 (12:43 +0100)
src/gui/folderstatusmodel.cpp

index 82029eda5c9af9b76295e677d4a33e9b7319fbe7..fd52a38c5e56d00ffc684aa4090a3b87d9aa1da3 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <QFileIconProvider>
 #include <QVarLengthArray>
+#include <set>
 
 Q_DECLARE_METATYPE(QPersistentModelIndex)
 
@@ -546,10 +547,16 @@ void FolderStatusModel::slotUpdateDirectories(const QStringList &list)
         selectiveSyncBlackList = parentInfo->_folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList);
     }
     auto selectiveSyncUndecidedList = parentInfo->_folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncUndecidedList);
-
     QVarLengthArray<int, 10> undecidedIndexes;
-
     QVector<SubFolderInfo> newSubs;
+
+    std::set<QString> selectiveSyncUndecidedSet; // not QSet because it's not sorted
+    foreach (const QString &str, selectiveSyncUndecidedList) {
+        if (str.startsWith(parentInfo->_path) || parentInfo->_path == QLatin1String("/")) {
+            selectiveSyncUndecidedSet.insert(str);
+        }
+    }
+
     newSubs.reserve(list.size() - 1);
     for (int i = 1;  // skip the parent item (first in the list)
             i < list.size(); ++i) {
@@ -586,11 +593,19 @@ void FolderStatusModel::slotUpdateDirectories(const QStringList &list)
             }
         }
 
-        foreach(const QString &str , selectiveSyncUndecidedList) {
-            if (str == relativePath) {
+        auto it = selectiveSyncUndecidedSet.lower_bound(relativePath);
+        if (it != selectiveSyncUndecidedSet.end()) {
+            if (*it == relativePath) {
                 newInfo._isUndecided = true;
-            } else if (str.startsWith(relativePath)) {
+                selectiveSyncUndecidedSet.erase(it);
+            } else if ((*it).startsWith(relativePath)) {
                 undecidedIndexes.append(newInfo._pathIdx.last());
+
+                // Remove all the items from the selectiveSyncUndecidedSet that starts with this path
+                QString relativePathNext = relativePath;
+                relativePathNext[relativePathNext.length()-1].unicode()++;
+                auto it2 = selectiveSyncUndecidedSet.lower_bound(relativePathNext);
+                selectiveSyncUndecidedSet.erase(it, it2);
             }
         }
         newSubs.append(newInfo);
@@ -603,6 +618,16 @@ void FolderStatusModel::slotUpdateDirectories(const QStringList &list)
     for (auto it = undecidedIndexes.begin(); it != undecidedIndexes.end(); ++it) {
         suggestExpand(idx.child(*it, 0));
     }
+
+    /* Try to remove the the undecided lists the items that are not on the server. */
+    auto it = std::remove_if(selectiveSyncUndecidedList.begin(), selectiveSyncUndecidedList.end(),
+            [&](const QString &s) { return selectiveSyncUndecidedSet.count(s); } );
+    if (it != selectiveSyncUndecidedList.end()) {
+        selectiveSyncUndecidedList.erase(it, selectiveSyncUndecidedList.end());
+        parentInfo->_folder->journalDb()->setSelectiveSyncList(
+                            SyncJournalDb::SelectiveSyncUndecidedList, selectiveSyncUndecidedList);
+        emit dirtyChanged();
+    }
 }
 
 void FolderStatusModel::slotLscolFinishedWithError(QNetworkReply* r)