File: backup.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (125 lines) | stat: -rw-r--r-- 2,993 bytes parent folder | download
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
#include "backup.hpp"
#include "log-helper.hpp"
#include "obs-module-helper.hpp"
#include "path-helpers.hpp"
#include "plugin-state-helpers.hpp"
#include "ui-helpers.hpp"
#include "version.h"

#include <obs-frontend-api.h>
#include <obs-module.h>
#include <util/config-file.h>

#include <QFileDialog>
#include <QMainWindow>
#include <QTextStream>
#include <QTimer>

#include <thread>

namespace advss {

static void showBackupDialogs(const QString &json)
{
	const bool backupWasConfirmed = DisplayMessage(
		obs_module_text("AdvSceneSwitcher.askBackup"), true, false);

	if (!backupWasConfirmed) {
		return;
	}

	QString path = QFileDialog::getSaveFileName(
		nullptr,
		obs_module_text(
			"AdvSceneSwitcher.generalTab.saveOrLoadsettings.exportWindowTitle"),
		GetDefaultSettingsSaveLocation(),
		obs_module_text(
			"AdvSceneSwitcher.generalTab.saveOrLoadsettings.textType"));
	if (path.isEmpty()) {
		return;
	}

	QFile file(path);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
		return;
	}
	auto out = QTextStream(&file);
	out << json;
}

void AskForBackup(obs_data_t *settings)
{
	// This function is called while the plugin settings are being loaded.
	// Blocking at this stage can cause issues such as OBS failing to start
	// or crashing.
	// Therefore, we ask the user whether they want to back up the settings
	// asynchronously.
	//
	// On macOS, an additional QTimer::singleShot wrapper is required for
	// this to work correctly.

	auto json = obs_data_get_json(settings);
	static QString jsonQString = json ? json : "";

	static const auto askForBackupWrapper = [](void *) {
#ifdef __APPLE__
		QTimer::singleShot(0,
				   static_cast<QMainWindow *>(
					   obs_frontend_get_main_window()),
				   []() {
#endif
					   showBackupDialogs(jsonQString);
#ifdef __APPLE__
				   });
#endif
	};

	std::thread t([]() {
		obs_queue_task(OBS_TASK_UI, askForBackupWrapper, nullptr,
			       false);
	});
	t.detach();
}

void BackupSettingsOfCurrentVersion()
{
	auto sceneCollectionName = obs_frontend_get_current_scene_collection();
	const std::string fileName =
		std::string("settings-backup-") +
		(sceneCollectionName ? sceneCollectionName : "-") + "-" +
		g_GIT_TAG + ".json";
	bfree(sceneCollectionName);
	auto settingsFile = obs_module_config_path(fileName.c_str());
	if (!settingsFile) {
		return;
	}

	QString dirPath = QFileInfo(settingsFile).absolutePath();
	QDir dir(dirPath);
	if (!dir.exists()) {
		if (!dir.mkpath(dirPath)) {
			blog(LOG_WARNING,
			     "failed to create directory to save automated backup");
			bfree(settingsFile);
			return;
		}
	}

	QFile file(settingsFile);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
		bfree(settingsFile);
		return;
	}

	OBSDataAutoRelease data = obs_data_create();
	SavePluginSettings(data);

	auto settings = obs_data_get_json(data);
	QString settingsString = QString(settings ? settings : "");

	auto out = QTextStream(&file);
	out << settingsString;
	bfree(settingsFile);
}

} // namespace advss