From 32e60ba65f654cabaa95ed02dedde9ce12abc915 Mon Sep 17 00:00:00 2001 From: Michel Date: Mon, 29 Jan 2018 11:30:47 +0100 Subject: [PATCH] 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 --- UI/window-basic-main-scene-collections.cpp | 56 ++++++++++++++++------ 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/UI/window-basic-main-scene-collections.cpp b/UI/window-basic-main-scene-collections.cpp index 067c7b7a4..ff92c063f 100644 --- a/UI/window-basic-main-scene-collections.cpp +++ b/UI/window-basic-main-scene-collections.cpp @@ -372,7 +372,7 @@ void OBSBasic::on_actionImportSceneCollection_triggered() { char path[512]; - QString home = QDir::homePath(); + QString qhome = QDir::homePath(); int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/"); if (ret <= 0) { @@ -380,25 +380,53 @@ void OBSBasic::on_actionImportSceneCollection_triggered() return; } - QString file = QFileDialog::getOpenFileName( + QString qfilePath = QFileDialog::getOpenFileName( this, QTStr("Basic.MainMenu.SceneCollection.Import"), - home, + qhome, "JSON Files (*.json)"); - QFileInfo finfo(file); - QString filename = finfo.fileName(); - QFileInfo destinfo(path + filename); + QFileInfo finfo(qfilePath); + QString qfilename = finfo.fileName(); + QString qpath = QT_UTF8(path); + QFileInfo destinfo(QT_UTF8(path) + qfilename); - 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")); + 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(); } }