forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window-basic-main-scene-collections.cpp
500 lines (396 loc) · 12.5 KB
/
window-basic-main-scene-collections.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/******************************************************************************
Copyright (C) 2015 by Hugh Bailey <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <obs.hpp>
#include <util/util.hpp>
#include <QMessageBox>
#include <QVariant>
#include <QFileDialog>
#include <QStandardPaths>
#include "item-widget-helpers.hpp"
#include "window-basic-main.hpp"
#include "window-namedialog.hpp"
#include "qt-wrappers.hpp"
using namespace std;
void EnumSceneCollections(std::function<bool (const char *, const char *)> &&cb)
{
char path[512];
os_glob_t *glob;
int ret = GetConfigPath(path, sizeof(path),
"obs-studio/basic/scenes/*.json");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get config path for scene "
"collections");
return;
}
if (os_glob(path, 0, &glob) != 0) {
blog(LOG_WARNING, "Failed to glob scene collections");
return;
}
for (size_t i = 0; i < glob->gl_pathc; i++) {
const char *filePath = glob->gl_pathv[i].path;
if (glob->gl_pathv[i].directory)
continue;
obs_data_t *data = obs_data_create_from_json_file_safe(filePath,
"bak");
std::string name = obs_data_get_string(data, "name");
/* if no name found, use the file name as the name
* (this only happens when switching to the new version) */
if (name.empty()) {
name = strrchr(filePath, '/') + 1;
name.resize(name.size() - 5);
}
obs_data_release(data);
if (!cb(name.c_str(), filePath))
break;
}
os_globfree(glob);
}
static bool SceneCollectionExists(const char *findName)
{
bool found = false;
auto func = [&](const char *name, const char*)
{
if (strcmp(name, findName) == 0) {
found = true;
return false;
}
return true;
};
EnumSceneCollections(func);
return found;
}
static bool GetSceneCollectionName(QWidget *parent, std::string &name,
std::string &file, const char *oldName = nullptr)
{
bool rename = oldName != nullptr;
const char *title;
const char *text;
char path[512];
size_t len;
int ret;
if (rename) {
title = Str("Basic.Main.RenameSceneCollection.Title");
text = Str("Basic.Main.AddSceneCollection.Text");
} else {
title = Str("Basic.Main.AddSceneCollection.Title");
text = Str("Basic.Main.AddSceneCollection.Text");
}
for (;;) {
bool success = NameDialog::AskForName(parent, title, text,
name, QT_UTF8(oldName));
if (!success) {
return false;
}
if (name.empty()) {
OBSMessageBox::information(parent,
QTStr("NoNameEntered.Title"),
QTStr("NoNameEntered.Text"));
continue;
}
if (SceneCollectionExists(name.c_str())) {
OBSMessageBox::information(parent,
QTStr("NameExists.Title"),
QTStr("NameExists.Text"));
continue;
}
break;
}
if (!GetFileSafeName(name.c_str(), file)) {
blog(LOG_WARNING, "Failed to create safe file name for '%s'",
name.c_str());
return false;
}
ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes/");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get scene collection config path");
return false;
}
len = file.size();
file.insert(0, path);
if (!GetClosestUnusedFileName(file, "json")) {
blog(LOG_WARNING, "Failed to get closest file name for %s",
file.c_str());
return false;
}
file.erase(file.size() - 5, 5);
file.erase(0, file.size() - len);
return true;
}
void OBSBasic::AddSceneCollection(bool create_new)
{
std::string name;
std::string file;
if (!GetSceneCollectionName(this, name, file))
return;
SaveProjectNow();
config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
name.c_str());
config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
file.c_str());
if (create_new) {
CreateDefaultScene(false);
}
SaveProjectNow();
RefreshSceneCollections();
blog(LOG_INFO, "Added scene collection '%s' (%s, %s.json)",
name.c_str(), create_new ? "clean" : "duplicate",
file.c_str());
blog(LOG_INFO, "------------------------------------------------");
UpdateTitleBar();
if (api) {
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
}
}
void OBSBasic::RefreshSceneCollections()
{
QList<QAction*> menuActions = ui->sceneCollectionMenu->actions();
int count = 0;
for (int i = 0; i < menuActions.count(); i++) {
QVariant v = menuActions[i]->property("file_name");
if (v.typeName() != nullptr)
delete menuActions[i];
}
const char *cur_name = config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollection");
auto addCollection = [&](const char *name, const char *path)
{
std::string file = strrchr(path, '/') + 1;
file.erase(file.size() - 5, 5);
QAction *action = new QAction(QT_UTF8(name), this);
action->setProperty("file_name", QT_UTF8(path));
connect(action, &QAction::triggered,
this, &OBSBasic::ChangeSceneCollection);
action->setCheckable(true);
action->setChecked(strcmp(name, cur_name) == 0);
ui->sceneCollectionMenu->addAction(action);
count++;
return true;
};
EnumSceneCollections(addCollection);
/* force saving of first scene collection on first run, otherwise
* no scene collections will show up */
if (!count) {
long prevDisableVal = disableSaving;
disableSaving = 0;
SaveProjectNow();
disableSaving = prevDisableVal;
EnumSceneCollections(addCollection);
}
ui->actionRemoveSceneCollection->setEnabled(count > 1);
OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
main->ui->actionPasteFilters->setEnabled(false);
main->ui->actionPasteRef->setEnabled(false);
main->ui->actionPasteDup->setEnabled(false);
}
void OBSBasic::on_actionNewSceneCollection_triggered()
{
AddSceneCollection(true);
}
void OBSBasic::on_actionDupSceneCollection_triggered()
{
AddSceneCollection(false);
}
void OBSBasic::on_actionRenameSceneCollection_triggered()
{
std::string name;
std::string file;
std::string oldFile = config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollectionFile");
const char *oldName = config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollection");
bool success = GetSceneCollectionName(this, name, file, oldName);
if (!success)
return;
config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
name.c_str());
config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
file.c_str());
SaveProjectNow();
char path[512];
int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get scene collection config path");
return;
}
oldFile.insert(0, path);
oldFile += ".json";
os_unlink(oldFile.c_str());
oldFile += ".bak";
os_unlink(oldFile.c_str());
blog(LOG_INFO, "------------------------------------------------");
blog(LOG_INFO, "Renamed scene collection to '%s' (%s.json)",
name.c_str(), file.c_str());
blog(LOG_INFO, "------------------------------------------------");
UpdateTitleBar();
RefreshSceneCollections();
if (api) {
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
}
}
void OBSBasic::on_actionRemoveSceneCollection_triggered()
{
std::string newName;
std::string newPath;
std::string oldFile = config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollectionFile");
std::string oldName = config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollection");
auto cb = [&](const char *name, const char *filePath)
{
if (strcmp(oldName.c_str(), name) != 0) {
newName = name;
newPath = filePath;
return false;
}
return true;
};
EnumSceneCollections(cb);
/* this should never be true due to menu item being grayed out */
if (newPath.empty())
return;
QString text = QTStr("ConfirmRemove.Text");
text.replace("$1", QT_UTF8(oldName.c_str()));
QMessageBox::StandardButton button = OBSMessageBox::question(this,
QTStr("ConfirmRemove.Title"), text);
if (button == QMessageBox::No)
return;
char path[512];
int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get scene collection config path");
return;
}
oldFile.insert(0, path);
oldFile += ".json";
os_unlink(oldFile.c_str());
oldFile += ".bak";
os_unlink(oldFile.c_str());
Load(newPath.c_str());
RefreshSceneCollections();
const char *newFile = config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollectionFile");
blog(LOG_INFO, "Removed scene collection '%s' (%s.json), "
"switched to '%s' (%s.json)",
oldName.c_str(), oldFile.c_str(),
newName.c_str(), newFile);
blog(LOG_INFO, "------------------------------------------------");
UpdateTitleBar();
if (api) {
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
}
}
void OBSBasic::on_actionImportSceneCollection_triggered()
{
char path[512];
QString qhome = QDir::homePath();
int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get scene collection config path");
return;
}
QString qfilePath = QFileDialog::getOpenFileName(
this,
QTStr("Basic.MainMenu.SceneCollection.Import"),
qhome,
"JSON Files (*.json)");
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, "Failed to create "
"safe file name for '%s'",
name.c_str());
return;
}
string filePath = path + file;
if (!GetClosestUnusedFileName(filePath, "json")) {
blog(LOG_WARNING, "Failed to get "
"closest file name for %s",
file.c_str());
return;
}
obs_data_save_json_safe(scenedata, filePath.c_str(),
"tmp", "bak");
RefreshSceneCollections();
}
}
void OBSBasic::on_actionExportSceneCollection_triggered()
{
char path[512];
QString home = QDir::homePath();
QString currentFile = QT_UTF8(config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollectionFile"));
int ret = GetConfigPath(path, 512, "obs-studio/basic/scenes/");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get scene collection config path");
return;
}
QString exportFile = QFileDialog::getSaveFileName(
this,
QTStr("Basic.MainMenu.SceneCollection.Export"),
home + "/" + currentFile,
"JSON Files (*.json)");
string file = QT_TO_UTF8(exportFile);
if (!exportFile.isEmpty() && !exportFile.isNull()) {
if (QFile::exists(exportFile))
QFile::remove(exportFile);
QFile::copy(path + currentFile + ".json", exportFile);
}
}
void OBSBasic::ChangeSceneCollection()
{
QAction *action = reinterpret_cast<QAction*>(sender());
std::string fileName;
if (!action)
return;
fileName = QT_TO_UTF8(action->property("file_name").value<QString>());
if (fileName.empty())
return;
const char *oldName = config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollection");
if (action->text().compare(QT_UTF8(oldName)) == 0) {
action->setChecked(true);
return;
}
SaveProjectNow();
Load(fileName.c_str());
RefreshSceneCollections();
const char *newName = config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollection");
const char *newFile = config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollectionFile");
blog(LOG_INFO, "Switched to scene collection '%s' (%s.json)",
newName, newFile);
blog(LOG_INFO, "------------------------------------------------");
UpdateTitleBar();
if (api)
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
}