diff --git a/advanced-scene-switcher.cpp b/advanced-scene-switcher.cpp index 9c367e2b..9b005052 100644 --- a/advanced-scene-switcher.cpp +++ b/advanced-scene-switcher.cpp @@ -249,7 +249,7 @@ SceneSwitcher::SceneSwitcher(QWidget* parent) string sceneName = GetWeakSourceName(s.scene); string transitionName = GetWeakSourceName(s.transition); QString listText = MakeFileSwitchName( - sceneName.c_str(), transitionName.c_str(), s.file.c_str(), s.text.c_str()); + sceneName.c_str(), transitionName.c_str(), s.file.c_str(), s.text.c_str(), s.useRegex, s.useTime); QListWidgetItem* item = new QListWidgetItem(listText, ui->fileScenesList); item->setData(Qt::UserRole, listText); @@ -573,6 +573,8 @@ static void SaveSceneSwitcher(obs_data_t* save_data, bool saving, void*) obs_data_set_string(array_obj, "transition", transitionName); obs_data_set_string(array_obj, "file", s.file.c_str()); obs_data_set_string(array_obj, "text", s.text.c_str()); + obs_data_set_bool(array_obj, "useRegex", s.useRegex); + obs_data_set_bool(array_obj, "useTime", s.useTime); obs_data_array_push_back(fileArray, array_obj); obs_source_release(source); obs_source_release(transition); @@ -876,9 +878,11 @@ static void SaveSceneSwitcher(obs_data_t* save_data, bool saving, void*) const char* transition = obs_data_get_string(array_obj, "transition"); const char* file = obs_data_get_string(array_obj, "file"); const char* text = obs_data_get_string(array_obj, "text"); + bool useRegex = obs_data_get_bool(array_obj, "useRegex"); + bool useTime = obs_data_get_bool(array_obj, "useTime"); switcher->fileSwitches.emplace_back( - GetWeakSourceByName(scene), GetWeakTransitionByName(transition), file, text); + GetWeakSourceByName(scene), GetWeakTransitionByName(transition), file, text, useRegex, useTime); obs_data_release(array_obj); } diff --git a/file-switch.cpp b/file-switch.cpp index 1e0a09ff..0be3dd47 100644 --- a/file-switch.cpp +++ b/file-switch.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include "advanced-scene-switcher.hpp" @@ -125,30 +126,49 @@ void SwitcherData::checkFileContent(bool& match, OBSWeakSource& scene, OBSWeakSo if (!file.open(QIODevice::ReadOnly)) continue; - /*Im using QTextStream here so the conversion between different lineendings is done by QT. - *QT itself uses only the linefeed internally so the input by the user is always using that, - *but the files selected by the user might use different line endings. - *If you are reading this and know of a cleaner way to do this, please let me know :) - */ - QTextStream in(&file); - QTextStream text(&t); - while (!in.atEnd() && !text.atEnd()) + //check file mod date + if (s.useTime) { - QString fileLine = in.readLine(); - QString textLine = text.readLine(); - if (QString::compare(fileLine, textLine, Qt::CaseSensitive) != 0) + QDateTime newLastMod = QFileInfo(file).lastModified(); + if (s.lastMod == newLastMod) + continue; + s.lastMod = newLastMod; + } + + + if (s.useRegex) + { + QTextStream in(&file); + QRegExp rx(t); + equal = rx.exactMatch(in.readAll()); + } + else + { + /*Im using QTextStream here so the conversion between different lineendings is done by QT. + *QT itself uses only the linefeed internally so the input by the user is always using that, + *but the files selected by the user might use different line endings. + *If you are reading this and know of a cleaner way to do this, please let me know :) + */ + QTextStream in(&file); + QTextStream text(&t); + while (!in.atEnd() && !text.atEnd()) { - equal = false; - break; - } - else { - equal = true; + QString fileLine = in.readLine(); + QString textLine = text.readLine(); + if (QString::compare(fileLine, textLine, Qt::CaseSensitive) != 0) + { + equal = false; + break; + } + else { + equal = true; + } } } - file.close(); - if (equal) { + if (equal) + { scene = s.scene; transition = s.transition; match = true; @@ -173,7 +193,8 @@ void SceneSwitcher::on_fileAdd_clicked() QString transitionName = ui->fileTransitions->currentText(); QString fileName = ui->filePathLineEdit->text(); QString text = ui->fileTextEdit->toPlainText(); - //QFile file(QString::fromStdString(fileName)); //don't check if file exists because it might not yet exist + bool useRegex = ui->fileContentRegExCheckBox->isChecked(); + bool useTime = ui->fileContentTimeCheckBox->isChecked(); if (sceneName.isEmpty() || transitionName.isEmpty() || fileName.isEmpty() || text.isEmpty()) return; @@ -181,7 +202,7 @@ void SceneSwitcher::on_fileAdd_clicked() OBSWeakSource source = GetWeakSourceByQString(sceneName); OBSWeakSource transition = GetWeakTransitionByQString(transitionName); - QString switchText = MakeFileSwitchName(sceneName, transitionName, fileName, text); + QString switchText = MakeFileSwitchName(sceneName, transitionName, fileName, text, useRegex, useTime); QVariant v = QVariant::fromValue(switchText); @@ -190,7 +211,7 @@ void SceneSwitcher::on_fileAdd_clicked() lock_guard lock(switcher->m); switcher->fileSwitches.emplace_back( - source, transition, fileName.toUtf8().constData(), text.toUtf8().constData()); + source, transition, fileName.toUtf8().constData(), text.toUtf8().constData(), useRegex, useTime); } @@ -235,5 +256,6 @@ void SceneSwitcher::on_fileScenesList_currentRowChanged(int idx) ui->fileTransitions->setCurrentText(transitionName.c_str()); ui->fileTextEdit->setPlainText(s.text.c_str()); ui->filePathLineEdit->setText(s.file.c_str()); - + ui->fileContentRegExCheckBox->setChecked(s.useRegex); + ui->fileContentTimeCheckBox->setChecked(s.useTime); } diff --git a/forms/advanced-scene-switcher.ui b/forms/advanced-scene-switcher.ui index 787ea13a..fe134d3e 100644 --- a/forms/advanced-scene-switcher.ui +++ b/forms/advanced-scene-switcher.ui @@ -7,7 +7,7 @@ 0 0 981 - 428 + 457 @@ -2109,7 +2109,7 @@ Write To File / Read From File - + @@ -2198,37 +2198,64 @@ - + - - - - - If the file - - - - - - - - - - Browse - - - - - - - contains: - - - - + + + If the file + + - + + + + + + Browse + + + + + + + contains: + + + + + + + + + + + + + + Use regular expression (pattern matching) + + + + + + + Only check contents if modification date changed + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + diff --git a/switcher-data-structs.hpp b/switcher-data-structs.hpp index 143a8761..58e4b39f 100644 --- a/switcher-data-structs.hpp +++ b/switcher-data-structs.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "utility.hpp" #define DEFAULT_INTERVAL 300 @@ -157,13 +158,19 @@ struct FileSwitch OBSWeakSource transition; string file; string text; + bool useRegex = false; + bool useTime = false; + QDateTime lastMod; inline FileSwitch( - OBSWeakSource scene_, OBSWeakSource transition_, const char* file_, const char* text_) + OBSWeakSource scene_, OBSWeakSource transition_, const char* file_, const char* text_, bool useRegex_, bool useTime_) : scene(scene_) , transition(transition_) , file(file_) , text(text_) + , useRegex(useRegex_) + , useTime(useTime_) + , lastMod() { } }; diff --git a/utility.hpp b/utility.hpp index c8bd5124..b946a3d0 100644 --- a/utility.hpp +++ b/utility.hpp @@ -70,15 +70,23 @@ static inline QString MakeRandomSwitchName( } static inline QString MakeFileSwitchName( - const QString& scene, const QString& transition, const QString& fileName, const QString& text) + const QString& scene, const QString& transition, const QString& fileName, const QString& text, bool useRegex, bool useTime) { - - if (text.length() > 30) - return QStringLiteral("Switch to ") + scene + QStringLiteral(" using ") + transition + QStringLiteral(" if ") - + fileName + QStringLiteral(" contains: \n\"") + text.left(27) + QStringLiteral("...\""); + QString switchName = QStringLiteral("Switch to ") + scene + QStringLiteral(" using ") + transition + QStringLiteral(" if ") + + fileName; + if (useTime) + switchName += QStringLiteral(" was modified and "); + switchName += QStringLiteral("contains"); + if (useRegex) + switchName += QStringLiteral(" (RegEx): \n\""); else - return QStringLiteral("Switch to ") + scene + QStringLiteral(" using ") + transition + QStringLiteral(" if ") - + fileName + QStringLiteral(" contains: \n\"") + text + QStringLiteral("\""); + switchName += QStringLiteral(": \n\""); + if (text.length() > 30) + switchName += text.left(27) + QStringLiteral("...\""); + else + switchName += text + QStringLiteral("\""); + + return switchName; } static inline string GetWeakSourceName(obs_weak_source_t* weak_source)