OBSImporter.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /******************************************************************************
  2. Copyright (C) 2019-2020 by Dillon Pentz <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "OBSImporter.hpp"
  15. #include "ImporterEntryPathItemDelegate.hpp"
  16. #include "ImporterModel.hpp"
  17. #include <importers/importers.hpp>
  18. #include <models/SceneCollection.hpp>
  19. #include <widgets/OBSBasic.hpp>
  20. #include <qt-wrappers.hpp>
  21. #include <QDirIterator>
  22. #include <QDropEvent>
  23. #include <QMessageBox>
  24. #include <QMimeData>
  25. #include <QPushButton>
  26. #include "moc_OBSImporter.cpp"
  27. OBSImporter::OBSImporter(QWidget *parent) : QDialog(parent), optionsModel(new ImporterModel), ui(new Ui::OBSImporter)
  28. {
  29. setAcceptDrops(true);
  30. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  31. ui->setupUi(this);
  32. ui->tableView->setModel(optionsModel);
  33. ui->tableView->setItemDelegateForColumn(ImporterColumn::Path, new ImporterEntryPathItemDelegate());
  34. ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents);
  35. ui->tableView->horizontalHeader()->setSectionResizeMode(ImporterColumn::Name, QHeaderView::ResizeMode::Stretch);
  36. ui->tableView->horizontalHeader()->setSectionResizeMode(ImporterColumn::Path, QHeaderView::ResizeMode::Stretch);
  37. connect(optionsModel, &ImporterModel::dataChanged, this, &OBSImporter::dataChanged);
  38. ui->tableView->setEditTriggers(QAbstractItemView::EditTrigger::CurrentChanged);
  39. ui->buttonBox->button(QDialogButtonBox::Ok)->setText(QTStr("Import"));
  40. connect(ui->buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, this,
  41. &OBSImporter::importCollections);
  42. connect(ui->importerSelectFiles, &QPushButton::clicked, this, &OBSImporter::browseImport);
  43. connect(ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &OBSImporter::close);
  44. ImportersInit();
  45. bool autoSearchPrompt = config_get_bool(App()->GetUserConfig(), "General", "AutoSearchPrompt");
  46. if (!autoSearchPrompt) {
  47. QMessageBox::StandardButton button = OBSMessageBox::question(
  48. parent, QTStr("Importer.AutomaticCollectionPrompt"), QTStr("Importer.AutomaticCollectionText"));
  49. if (button == QMessageBox::Yes) {
  50. config_set_bool(App()->GetUserConfig(), "General", "AutomaticCollectionSearch", true);
  51. } else {
  52. config_set_bool(App()->GetUserConfig(), "General", "AutomaticCollectionSearch", false);
  53. }
  54. config_set_bool(App()->GetUserConfig(), "General", "AutoSearchPrompt", true);
  55. }
  56. bool autoSearch = config_get_bool(App()->GetUserConfig(), "General", "AutomaticCollectionSearch");
  57. OBSImporterFiles f;
  58. if (autoSearch)
  59. f = ImportersFindFiles();
  60. for (size_t i = 0; i < f.size(); i++) {
  61. QString path = f[i].c_str();
  62. path.replace("\\", "/");
  63. addImportOption(path, true);
  64. }
  65. f.clear();
  66. ui->tableView->resizeColumnsToContents();
  67. QModelIndex index = optionsModel->createIndex(optionsModel->rowCount() - 1, 2);
  68. QMetaObject::invokeMethod(ui->tableView, "setCurrentIndex", Qt::QueuedConnection,
  69. Q_ARG(const QModelIndex &, index));
  70. }
  71. void OBSImporter::addImportOption(QString path, bool automatic)
  72. {
  73. QStringList list;
  74. list.append(path);
  75. QModelIndex insertIndex = optionsModel->index(optionsModel->rowCount() - 1, ImporterColumn::Path);
  76. optionsModel->setData(insertIndex, list, automatic ? ImporterEntryRole::AutoPath : ImporterEntryRole::NewPath);
  77. }
  78. void OBSImporter::dropEvent(QDropEvent *ev)
  79. {
  80. for (QUrl url : ev->mimeData()->urls()) {
  81. QFileInfo fileInfo(url.toLocalFile());
  82. if (fileInfo.isDir()) {
  83. QDirIterator dirIter(fileInfo.absoluteFilePath(), QDir::Files);
  84. while (dirIter.hasNext()) {
  85. addImportOption(dirIter.next(), false);
  86. }
  87. } else {
  88. addImportOption(fileInfo.canonicalFilePath(), false);
  89. }
  90. }
  91. }
  92. void OBSImporter::dragEnterEvent(QDragEnterEvent *ev)
  93. {
  94. if (ev->mimeData()->hasUrls())
  95. ev->accept();
  96. }
  97. void OBSImporter::browseImport()
  98. {
  99. QString Pattern = "(*.json *.bpres *.xml *.xconfig)";
  100. QStringList paths = OpenFiles(this, QTStr("Importer.SelectCollection"), "",
  101. QTStr("Importer.Collection") + QString(" ") + Pattern);
  102. if (!paths.empty()) {
  103. for (int i = 0; i < paths.count(); i++) {
  104. addImportOption(paths[i], false);
  105. }
  106. }
  107. }
  108. bool GetUnusedName(std::string &name)
  109. {
  110. OBSBasic *basic = OBSBasic::Get();
  111. if (!basic->GetSceneCollectionByName(name)) {
  112. return false;
  113. }
  114. std::string newName;
  115. int inc = 2;
  116. do {
  117. newName = name;
  118. newName += " ";
  119. newName += std::to_string(inc++);
  120. } while (basic->GetSceneCollectionByName(newName));
  121. name = newName;
  122. return true;
  123. }
  124. constexpr std::string_view OBSSceneCollectionPath = "obs-studio/basic/scenes/";
  125. void OBSImporter::importCollections()
  126. {
  127. setEnabled(false);
  128. const std::filesystem::path sceneCollectionLocation =
  129. App()->userScenesLocation / std::filesystem::u8path(OBSSceneCollectionPath);
  130. for (int i = 0; i < optionsModel->rowCount() - 1; i++) {
  131. int selected = optionsModel->index(i, ImporterColumn::Selected).data(Qt::CheckStateRole).value<int>();
  132. if (selected == Qt::Unchecked)
  133. continue;
  134. std::string pathStr = optionsModel->index(i, ImporterColumn::Path)
  135. .data(Qt::DisplayRole)
  136. .value<QString>()
  137. .toStdString();
  138. std::string nameStr = optionsModel->index(i, ImporterColumn::Name)
  139. .data(Qt::DisplayRole)
  140. .value<QString>()
  141. .toStdString();
  142. json11::Json res;
  143. ImportSC(pathStr, nameStr, res);
  144. if (res != json11::Json()) {
  145. json11::Json::object out = res.object_items();
  146. std::string name = res["name"].string_value();
  147. std::string file;
  148. if (GetUnusedName(name)) {
  149. json11::Json::object newOut = out;
  150. newOut["name"] = name;
  151. out = newOut;
  152. }
  153. std::string fileName;
  154. if (!GetFileSafeName(name.c_str(), fileName)) {
  155. blog(LOG_WARNING, "Failed to create safe file name for '%s'", fileName.c_str());
  156. }
  157. std::string collectionFile;
  158. collectionFile.reserve(sceneCollectionLocation.u8string().size() + fileName.size());
  159. collectionFile.append(sceneCollectionLocation.u8string()).append(fileName);
  160. if (!GetClosestUnusedFileName(collectionFile, "json")) {
  161. blog(LOG_WARNING, "Failed to get closest file name for %s", fileName.c_str());
  162. }
  163. std::string out_str = json11::Json(out).dump();
  164. bool success = os_quick_write_utf8_file(collectionFile.c_str(), out_str.c_str(), out_str.size(),
  165. false);
  166. blog(LOG_INFO, "Import Scene Collection: %s (%s) - %s", name.c_str(), fileName.c_str(),
  167. success ? "SUCCESS" : "FAILURE");
  168. }
  169. }
  170. close();
  171. }
  172. void OBSImporter::dataChanged()
  173. {
  174. ui->tableView->resizeColumnToContents(ImporterColumn::Name);
  175. bool enableImportButton = false;
  176. for (int i = 0; i < optionsModel->rowCount() - 1; i++) {
  177. int selected = optionsModel->index(i, ImporterColumn::Selected).data(Qt::CheckStateRole).value<int>();
  178. if (selected == Qt::Checked) {
  179. enableImportButton = true;
  180. break;
  181. }
  182. }
  183. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enableImportButton);
  184. }