Skip to content

Commit

Permalink
add enable/disable local storage config option
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunshine committed Dec 5, 2021
1 parent 7a53319 commit 3ebecf1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
1 change: 1 addition & 0 deletions inc/liquidappconfigwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public slots:

// JavaScript tab
QCheckBox* enableJavaScriptCheckBox;
QCheckBox* enableLocalStorageCheckBox;
QLabel* additionalJsLabel;
QPlainTextEdit* additionalJsTextArea;

Expand Down
2 changes: 2 additions & 0 deletions inc/lqd.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#define LQD_CFG_KEY_NAME_LOCK_WIN_GEOM "LockWindowGeometry" // boolean, defaults to FALSE
#define LQD_CFG_KEY_NAME_MUTE_AUDIO "MuteAudio" // boolean, defaults to FALSE
#define LQD_CFG_KEY_NAME_NOTES "Notes" // text
#define LQD_CFG_KEY_NAME_PRESERVE_COOKIES "PreserveCookies" // boolean, defaults to FALSE
#define LQD_CFG_KEY_NAME_PRESERVE_LOCAL_STORAGE "PreserveLocalStorage" // boolean, defaults to FALSE
#define LQD_CFG_KEY_NAME_PROXY_HOST LQD_CFG_GROUP_NAME_PROXY "/" "Host" // text
#define LQD_CFG_KEY_NAME_PROXY_PORT LQD_CFG_GROUP_NAME_PROXY "/" "Port" // number
#define LQD_CFG_KEY_NAME_PROXY_USE_AUTH LQD_CFG_GROUP_NAME_PROXY "/" "UseAuthentication" // boolean, defaults to FALSE
Expand Down
60 changes: 50 additions & 10 deletions src/liquidappconfigwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,20 +425,55 @@ LiquidAppConfigDialog::LiquidAppConfigDialog(QWidget* parent, QString liquidAppN
jsTabWidget->setLayout(jsTabWidgetLayout);
tabWidget->addTab(jsTabWidget, tr("JavaScript"));

// Enable JavaScript checkbox
// Enable JavaScript & enable local storage checkboxes
{
enableJavaScriptCheckBox = new QCheckBox(tr("Enable JavaScript"), this);
enableJavaScriptCheckBox->setCursor(Qt::PointingHandCursor);
// Enable JavaScript checkbox
{
enableJavaScriptCheckBox = new QCheckBox(tr("Enable JavaScript"), this);
enableJavaScriptCheckBox->setCursor(Qt::PointingHandCursor);

if (isEditingExistingBool) {
bool isChecked = existingLiquidAppConfig->value(LQD_CFG_KEY_NAME_ENABLE_JS).toBool();
enableJavaScriptCheckBox->setChecked(isChecked);
} else {
// Checked by default (when creating new Liquid app)
enableJavaScriptCheckBox->setChecked(true);
if (isEditingExistingBool) {
bool isChecked = existingLiquidAppConfig->value(LQD_CFG_KEY_NAME_ENABLE_JS).toBool();
enableJavaScriptCheckBox->setChecked(isChecked);
} else {
// Checked by default (when creating new Liquid app)
enableJavaScriptCheckBox->setChecked(true);
}

jsTabWidgetLayout->addWidget(enableJavaScriptCheckBox);
}

jsTabWidgetLayout->addWidget(enableJavaScriptCheckBox);
// Enable local storage checkbox
{
QHBoxLayout* enableLocalStorageLayout = new QHBoxLayout();
enableLocalStorageLayout->setContentsMargins(LQD_UI_MARGIN, 0, 0, 0);

enableLocalStorageCheckBox = new QCheckBox(tr("Save and restore local storage data"), this);
enableLocalStorageCheckBox->setCursor(Qt::PointingHandCursor);

if (isEditingExistingBool) {
bool isChecked = existingLiquidAppConfig->value(LQD_CFG_KEY_NAME_PRESERVE_LOCAL_STORAGE).toBool();
enableLocalStorageCheckBox->setChecked(isChecked);
} else {
// Checked by default (when creating new Liquid app)
enableLocalStorageCheckBox->setChecked(true);
}

enableLocalStorageLayout->addWidget(enableLocalStorageCheckBox);

connect(enableJavaScriptCheckBox, &QCheckBox::toggled, [&](const bool isOn){
if (!isOn) {
enableLocalStorageCheckBox->setChecked(false);
}
});
connect(enableLocalStorageCheckBox, &QCheckBox::toggled, [&](const bool isOn){
if (isOn) {
enableJavaScriptCheckBox->setChecked(isOn);
}
});

jsTabWidgetLayout->addLayout(enableLocalStorageLayout);
}
}

// Additonal JavaScript code text area
Expand Down Expand Up @@ -821,6 +856,11 @@ void LiquidAppConfigDialog::save()
tempLiquidAppConfig->setValue(LQD_CFG_KEY_NAME_ENABLE_JS, enableJavaScriptCheckBox->isChecked());
}

// Preserve local storage
{
tempLiquidAppConfig->setValue(LQD_CFG_KEY_NAME_PRESERVE_LOCAL_STORAGE, enableLocalStorageCheckBox->isChecked());
}

// Allow cookies
{
tempLiquidAppConfig->setValue(LQD_CFG_KEY_NAME_ALLOW_COOKIES, allowCookiesCheckBox->isChecked());
Expand Down
2 changes: 1 addition & 1 deletion src/liquidappwebpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void LiquidAppWebPage::setWebSettingsToDefault(QWebEngineSettings* webSettings)
webSettings->setAttribute(QWebEngineSettings::JavascriptCanPaste, false);
#endif
webSettings->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
webSettings->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
webSettings->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
webSettings->setAttribute(QWebEngineSettings::PdfViewerEnabled, false);
#endif
Expand Down
29 changes: 17 additions & 12 deletions src/liquidappwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,26 +538,26 @@ void LiquidAppWindow::loadLiquidAppConfig(void)
}

// Toggle JavaScript on if enabled in application config
if (liquidAppConfig->contains(LQD_CFG_KEY_NAME_ENABLE_JS)) {
settings()->setAttribute(
QWebEngineSettings::JavascriptEnabled,
liquidAppConfig->value(LQD_CFG_KEY_NAME_ENABLE_JS).toBool()
);
if (liquidAppConfig->value(LQD_CFG_KEY_NAME_ENABLE_JS).toBool()) {
settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);

// Restore LocalStorage data if preservation is enabled in application config
if (liquidAppConfig->value(LQD_CFG_KEY_NAME_PRESERVE_LOCAL_STORAGE).toBool()) {
// TODO: restore localStorage items from this Liquid App's config file
}
}

#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
// Hide scrollbars
if (liquidAppConfig->contains(LQD_CFG_KEY_NAME_HIDE_SCROLLBARS)) {
settings()->setAttribute(
QWebEngineSettings::ShowScrollBars,
!liquidAppConfig->value(LQD_CFG_KEY_NAME_HIDE_SCROLLBARS).toBool()
);
if (liquidAppConfig->value(LQD_CFG_KEY_NAME_HIDE_SCROLLBARS).toBool()) {
settings()->setAttribute(QWebEngineSettings::ShowScrollBars, false);
}
#endif

// Mute audio if muted in application config
if (liquidAppConfig->contains(LQD_CFG_KEY_NAME_MUTE_AUDIO)) {
page()->setAudioMuted(liquidAppConfig->value(LQD_CFG_KEY_NAME_MUTE_AUDIO).toBool());
if (liquidAppConfig->value(LQD_CFG_KEY_NAME_MUTE_AUDIO).toBool()) {
page()->setAudioMuted(true);
}

// Restore web view zoom level
Expand Down Expand Up @@ -709,6 +709,11 @@ void LiquidAppWindow::saveLiquidAppConfig(void)
}
}

// Save LocalStorage data if preservation is enabled in application config
if (liquidAppConfig->value(LQD_CFG_KEY_NAME_PRESERVE_LOCAL_STORAGE).toBool()) {
// TODO: restore localStorage items from this Liquid App's config file
}

liquidAppConfig->sync();
}

Expand Down

0 comments on commit 3ebecf1

Please sign in to comment.