Przeglądaj źródła

bug-fix: currentItemChanged() may have NULL pointer

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 lat temu
rodzic
commit
b47c9f3368
3 zmienionych plików z 19 dodań i 1 usunięć
  1. 4 0
      vdirectorytree.cpp
  2. 14 1
      vfilelist.cpp
  3. 1 0
      vfilelist.h

+ 4 - 0
vdirectorytree.cpp

@@ -465,6 +465,10 @@ bool VDirectoryTree::isConflictNameWithChildren(const QTreeWidgetItem *parent, c
 
 void VDirectoryTree::currentDirectoryItemChanged(QTreeWidgetItem *currentItem)
 {
+    if (!currentItem) {
+        emit currentDirectoryChanged(QJsonObject());
+        return;
+    }
     QJsonObject itemJson = currentItem->data(0, Qt::UserRole).toJsonObject();
     Q_ASSERT(!itemJson.isEmpty());
     itemJson["root_path"] = treePath;

+ 14 - 1
vfilelist.cpp

@@ -31,7 +31,11 @@ void VFileList::initActions()
 
 void VFileList::setDirectory(QJsonObject dirJson)
 {
-    Q_ASSERT(!dirJson.isEmpty());
+    if (dirJson.isEmpty()) {
+        clearDirectoryInfo();
+        return;
+    }
+
     directoryName = dirJson["name"].toString();
     rootPath = dirJson["root_path"].toString();
     relativePath = QDir(dirJson["relative_path"].toString()).filePath(directoryName);
@@ -40,6 +44,11 @@ void VFileList::setDirectory(QJsonObject dirJson)
     updateFileList();
 }
 
+void VFileList::clearDirectoryInfo()
+{
+    directoryName = rootPath = relativePath = "";
+    clear();
+}
 
 void VFileList::updateFileList()
 {
@@ -253,6 +262,10 @@ void VFileList::deleteFileAndUpdateList(QListWidgetItem *item)
 
 void VFileList::currentFileItemChanged(QListWidgetItem *currentItem)
 {
+    if (!currentItem) {
+        emit currentFileChanged(QJsonObject());
+        return;
+    }
     QJsonObject itemJson = currentItem->data(Qt::UserRole).toJsonObject();
     Q_ASSERT(!itemJson.isEmpty());
     itemJson["path"] = QDir::cleanPath(QDir(rootPath).filePath(relativePath));

+ 1 - 0
vfilelist.h

@@ -33,6 +33,7 @@ private:
     QListWidgetItem *createFileAndUpdateList(const QString &name,
                                              const QString &description);
     void deleteFileAndUpdateList(QListWidgetItem *item);
+    void clearDirectoryInfo();
 
     QString rootPath;
     QString relativePath;