浏览代码

UI: Fix imported scene collection names duplicating

If an imported scene collection has a name that already exists, it will
instead be given a name plus an increment (e.g. "name 2", or "name 3",
etc)

Fixes obsproject/obs-studio#4442
jp9000 4 年之前
父节点
当前提交
dde4d57d72
共有 2 个文件被更改,包括 26 次插入1 次删除
  1. 1 1
      UI/window-basic-main-scene-collections.cpp
  2. 25 0
      UI/window-importer.cpp

+ 1 - 1
UI/window-basic-main-scene-collections.cpp

@@ -73,7 +73,7 @@ void EnumSceneCollections(std::function<bool(const char *, const char *)> &&cb)
 	os_globfree(glob);
 	os_globfree(glob);
 }
 }
 
 
-static bool SceneCollectionExists(const char *findName)
+bool SceneCollectionExists(const char *findName)
 {
 {
 	bool found = false;
 	bool found = false;
 	auto func = [&](const char *name, const char *) {
 	auto func = [&](const char *name, const char *) {

+ 25 - 0
UI/window-importer.cpp

@@ -30,6 +30,8 @@
 #include "qt-wrappers.hpp"
 #include "qt-wrappers.hpp"
 #include "importers/importers.hpp"
 #include "importers/importers.hpp"
 
 
+extern bool SceneCollectionExists(const char *findName);
+
 enum ImporterColumn {
 enum ImporterColumn {
 	Selected,
 	Selected,
 	Name,
 	Name,
@@ -532,6 +534,23 @@ void OBSImporter::browseImport()
 	}
 	}
 }
 }
 
 
+bool GetUnusedName(std::string &name)
+{
+	if (!SceneCollectionExists(name.c_str()))
+		return false;
+
+	std::string newName;
+	int inc = 2;
+	do {
+		newName = name;
+		newName += " ";
+		newName += std::to_string(inc++);
+	} while (SceneCollectionExists(newName.c_str()));
+
+	name = newName;
+	return true;
+}
+
 void OBSImporter::importCollections()
 void OBSImporter::importCollections()
 {
 {
 	setEnabled(false);
 	setEnabled(false);
@@ -566,6 +585,12 @@ void OBSImporter::importCollections()
 			std::string name = res["name"].string_value();
 			std::string name = res["name"].string_value();
 			std::string file;
 			std::string file;
 
 
+			if (GetUnusedName(name)) {
+				json11::Json::object newOut = out;
+				newOut["name"] = name;
+				out = newOut;
+			}
+
 			GetUnusedSceneCollectionFile(name, file);
 			GetUnusedSceneCollectionFile(name, file);
 
 
 			std::string save = dst;
 			std::string save = dst;