Skip to content

Commit

Permalink
Backup setting on exit to plugin config dir
Browse files Browse the repository at this point in the history
Only one version of the settings file will be kept per scene collection
and plugin version combination
  • Loading branch information
WarmUpTill committed Feb 27, 2024
1 parent f50f31e commit b963241
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ target_sources(
${LIB_NAME}
PRIVATE lib/utils/action-queue.cpp
lib/utils/action-queue.hpp
lib/utils/backup.cpp
lib/utils/backup.hpp
lib/utils/curl-helper.cpp
lib/utils/curl-helper.hpp
lib/utils/duration-control.cpp
Expand Down
33 changes: 3 additions & 30 deletions lib/advanced-scene-switcher.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "advanced-scene-switcher.hpp"
#include "backup.hpp"
#include "curl-helper.hpp"
#include "log-helper.hpp"
#include "macro-helpers.hpp"
Expand All @@ -17,7 +18,6 @@
#include <obs-frontend-api.h>
#include <QAction>
#include <QDirIterator>
#include <QFileDialog>
#include <QMainWindow>
#include <QTextStream>
#include <regex>
Expand Down Expand Up @@ -143,7 +143,6 @@ bool AdvSceneSwitcher::eventFilter(QObject *obj, QEvent *event)
/******************************************************************************
* Saving and loading
******************************************************************************/
static void AskForBackup(const QString &json);

static void SaveSceneSwitcher(obs_data_t *save_data, bool saving, void *)
{
Expand Down Expand Up @@ -192,34 +191,6 @@ static void SaveSceneSwitcher(obs_data_t *save_data, bool saving, void *)
}
}

static void AskForBackup(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;
}

/******************************************************************************
* Main switcher thread
******************************************************************************/
Expand Down Expand Up @@ -635,6 +606,8 @@ static void handleShutdown()
// TODO: Look for a way to possibly resolve this
obs_frontend_save();
}

BackupSettingsOfCurrentVersion();
}

static void handleSceneCollectionChanging()
Expand Down
86 changes: 86 additions & 0 deletions lib/utils/backup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#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 <QFileDialog>
#include <QTextStream>
#include <util/config-file.h>

namespace advss {

void AskForBackup(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 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
9 changes: 9 additions & 0 deletions lib/utils/backup.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include <QString>

namespace advss {

void AskForBackup(const QString &json);
void BackupSettingsOfCurrentVersion();

} // namespace advss
5 changes: 5 additions & 0 deletions lib/utils/plugin-state-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

namespace advss {

void SavePluginSettings(obs_data_t *obj)
{
GetSwitcher()->SaveSettings(obj);
}

void LoadPluginSettings(obs_data_t *obj)
{
GetSwitcher()->LoadSettings(obj);
Expand Down
1 change: 1 addition & 0 deletions lib/utils/plugin-state-helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace advss {

void SavePluginSettings(obs_data_t *);
EXPORT void LoadPluginSettings(obs_data_t *);

EXPORT void AddSaveStep(std::function<void(obs_data_t *)>);
Expand Down

0 comments on commit b963241

Please sign in to comment.