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
|
#include "macro-action-run.hpp"
#include "layout-helpers.hpp"
#include <QProcess>
#include <QDesktopServices>
namespace advss {
const std::string MacroActionRun::id = "run";
bool MacroActionRun::_registered = MacroActionFactory::Register(
MacroActionRun::id, {MacroActionRun::Create, MacroActionRunEdit::Create,
"AdvSceneSwitcher.action.run"});
bool MacroActionRun::PerformAction()
{
if (_wait) {
_procConfig.StartProcessAndWait(_timeout.Milliseconds());
SetTempVarValues();
return true;
}
bool procStarted = _procConfig.StartProcessDetached();
// Fall back to using default application to open given file
if (!procStarted && _procConfig.Args().empty()) {
vblog(LOG_INFO, "run \"%s\" using QDesktopServices",
_procConfig.Path().c_str());
QDesktopServices::openUrl(QUrl::fromLocalFile(
QString::fromStdString(_procConfig.Path())));
}
return true;
}
void MacroActionRun::LogAction() const
{
ablog(LOG_INFO, "run \"%s\"", _procConfig.UnresolvedPath().c_str());
}
void MacroActionRun::SetupTempVars()
{
MacroAction::SetupTempVars();
if (!_wait) {
AddTempvar(
"process.none",
obs_module_text(
"AdvSceneSwitcher.tempVar.run.process.none"),
obs_module_text(
"AdvSceneSwitcher.tempVar.run.process.none.description"));
return;
}
AddTempvar(
"process.id",
obs_module_text("AdvSceneSwitcher.tempVar.run.process.id"),
obs_module_text(
"AdvSceneSwitcher.tempVar.run.process.id.description"));
AddTempvar(
"process.exitCode",
obs_module_text(
"AdvSceneSwitcher.tempVar.run.process.exitCode"),
obs_module_text(
"AdvSceneSwitcher.tempVar.run.process.exitCode.description"));
AddTempvar(
"process.stream.output",
obs_module_text(
"AdvSceneSwitcher.tempVar.run.process.stream.output"),
obs_module_text(
"AdvSceneSwitcher.tempVar.run.process.stream.output.description"));
AddTempvar(
"process.stream.error",
obs_module_text(
"AdvSceneSwitcher.tempVar.run.process.stream.error"),
obs_module_text(
"AdvSceneSwitcher.tempVar.run.process.stream.error.description"));
}
void MacroActionRun::SetTempVarValues()
{
SetTempVarValue("process.id", _procConfig.GetProcessId());
SetTempVarValue("process.exitCode", _procConfig.GetProcessExitCode());
SetTempVarValue("process.stream.output",
_procConfig.GetProcessOutputStream());
SetTempVarValue("process.stream.error",
_procConfig.GetProcessErrorStream());
}
bool MacroActionRun::Save(obs_data_t *obj) const
{
MacroAction::Save(obj);
_procConfig.Save(obj);
_timeout.Save(obj);
obs_data_set_bool(obj, "wait", _wait);
obs_data_set_int(obj, "version", 1);
return true;
}
bool MacroActionRun::Load(obs_data_t *obj)
{
MacroAction::Load(obj);
_procConfig.Load(obj);
// TODO: Remove this fallback in a future version
if (!obs_data_has_user_value(obj, "version")) {
return true;
}
_timeout.Load(obj);
_wait = obs_data_get_bool(obj, "wait");
return true;
}
std::string MacroActionRun::GetShortDesc() const
{
return _procConfig.UnresolvedPath();
}
std::shared_ptr<MacroAction> MacroActionRun::Create(Macro *m)
{
return std::make_shared<MacroActionRun>(m);
}
std::shared_ptr<MacroAction> MacroActionRun::Copy() const
{
return std::make_shared<MacroActionRun>(*this);
}
void MacroActionRun::ResolveVariablesToFixedValues()
{
_procConfig.ResolveVariables();
_timeout.ResolveVariables();
}
void MacroActionRun::SetWaitEnabled(bool value)
{
_wait = value;
SetupTempVars();
}
MacroActionRunEdit::MacroActionRunEdit(
QWidget *parent, std::shared_ptr<MacroActionRun> entryData)
: QWidget(parent),
_procConfig(new ProcessConfigEdit(this)),
_waitLayout(new QHBoxLayout()),
_wait(new QCheckBox()),
_timeout(new DurationSelection(this, true, 0.1)),
_waitHelp(new HelpIcon(obs_module_text(
"AdvSceneSwitcher.action.run.wait.help.tooltip")))
{
_waitHelp->hide();
QWidget::connect(_procConfig,
SIGNAL(ConfigChanged(const ProcessConfig &)), this,
SLOT(ProcessConfigChanged(const ProcessConfig &)));
QWidget::connect(_procConfig, SIGNAL(AdvancedSettingsEnabled()), this,
SLOT(ProcessConfigAdvancedSettingsShown()));
QWidget::connect(_wait, SIGNAL(stateChanged(int)), this,
SLOT(WaitChanged(int)));
QWidget::connect(_timeout, SIGNAL(DurationChanged(const Duration &)),
this, SLOT(TimeoutChanged(const Duration &)));
PlaceWidgets(obs_module_text("AdvSceneSwitcher.action.run.wait.entry"),
_waitLayout,
{{"{{wait}}", _wait},
{"{{timeout}}", _timeout},
{"{{waitHelp}}", _waitHelp}});
SetLayoutVisible(_waitLayout, false);
auto layout = new QVBoxLayout;
layout->addWidget(_procConfig);
layout->addLayout(_waitLayout);
setLayout(layout);
_entryData = entryData;
UpdateEntryData();
_loading = false;
}
void MacroActionRunEdit::UpdateEntryData()
{
if (!_entryData) {
return;
}
_procConfig->SetProcessConfig(_entryData->_procConfig);
_wait->setChecked(_entryData->IsWaitEnabled());
_timeout->SetDuration(_entryData->_timeout);
}
void MacroActionRunEdit::ProcessConfigAdvancedSettingsShown()
{
SetLayoutVisible(_waitLayout, true);
}
void MacroActionRunEdit::WaitChanged(int value)
{
GUARD_LOADING_AND_LOCK();
_entryData->SetWaitEnabled(value);
}
void MacroActionRunEdit::TimeoutChanged(const Duration &timeout)
{
GUARD_LOADING_AND_LOCK();
_entryData->_timeout = timeout;
}
void MacroActionRunEdit::ProcessConfigChanged(const ProcessConfig &conf)
{
GUARD_LOADING_AND_LOCK();
_entryData->_procConfig = conf;
adjustSize();
updateGeometry();
emit HeaderInfoChanged(
QString::fromStdString(_entryData->GetShortDesc()));
}
} // namespace advss
|