Przeglądaj źródła

UI: Import scene collection with correct filename

OBS assumes the name of the imported json file is also the name
of the scene collection, if the filename is different this can result
in duplicate scene collections after importing.

Closes jp9000/obs-studio#1177
Michel 7 lat temu
rodzic
commit
32e60ba65f
1 zmienionych plików z 43 dodań i 15 usunięć
  1. 43 15
      UI/window-basic-main-scene-collections.cpp

+ 43 - 15
UI/window-basic-main-scene-collections.cpp

@@ -372,7 +372,7 @@ void OBSBasic::on_actionImportSceneCollection_triggered()
 {
 {
 	char path[512];
 	char path[512];
 
 
-	QString home = QDir::homePath();
+	QString qhome = QDir::homePath();
 
 
 	int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
 	int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
 	if (ret <= 0) {
 	if (ret <= 0) {
@@ -380,25 +380,53 @@ void OBSBasic::on_actionImportSceneCollection_triggered()
 		return;
 		return;
 	}
 	}
 
 
-	QString file = QFileDialog::getOpenFileName(
+	QString qfilePath = QFileDialog::getOpenFileName(
 			this,
 			this,
 			QTStr("Basic.MainMenu.SceneCollection.Import"),
 			QTStr("Basic.MainMenu.SceneCollection.Import"),
-			home,
+			qhome,
 			"JSON Files (*.json)");
 			"JSON Files (*.json)");
 
 
-	QFileInfo finfo(file);
-	QString filename = finfo.fileName();
-	QFileInfo destinfo(path + filename);
-
-	if (!file.isEmpty() && !file.isNull()) {
-		 if (!destinfo.exists()) {
-			QFile::copy(file, path + filename);
-			RefreshSceneCollections();
-		} else {
-			OBSMessageBox::information(this,
-				QTStr("Basic.MainMenu.SceneCollection.Import"),
-				QTStr("Basic.MainMenu.SceneCollection.Exists"));
+	QFileInfo finfo(qfilePath);
+	QString qfilename = finfo.fileName();
+	QString qpath = QT_UTF8(path);
+	QFileInfo destinfo(QT_UTF8(path) + qfilename);
+
+	if (!qfilePath.isEmpty() && !qfilePath.isNull()) {
+		string absPath = QT_TO_UTF8(finfo.absoluteFilePath());
+		OBSData scenedata =
+			obs_data_create_from_json_file(absPath.c_str());
+		obs_data_release(scenedata);
+
+		string origName = obs_data_get_string(scenedata, "name");
+		string name = origName;
+		string file;
+		int inc = 1;
+
+		while (SceneCollectionExists(name.c_str())) {
+			name = origName + " (" + to_string(++inc) + ")";
 		}
 		}
+
+		obs_data_set_string(scenedata, "name", name.c_str());
+
+		if (!GetFileSafeName(name.c_str(), file)) {
+			blog(LOG_WARNING, __FUNCTION__ ": Failed to create "
+					"safe file name for '%s'",
+					name.c_str());
+			return;
+		}
+
+		string filePath = path + file;
+
+		if (!GetClosestUnusedFileName(filePath, "json")) {
+			blog(LOG_WARNING, __FUNCTION__ ": Failed to get "
+					"closest file name for %s",
+					file.c_str());
+			return;
+		}
+
+		obs_data_save_json_safe(scenedata, filePath.c_str(),
+				"tmp", "bak");
+		RefreshSceneCollections();
 	}
 	}
 }
 }