1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 09:32:12 +01:00

Use SiteSettings struct to move the settings data

Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
Juraj Oravec 2022-09-25 20:25:43 +02:00
parent 90181a0606
commit 9628b4b7c5
Signed by: SGOrava
GPG Key ID: 13660A3F1D9F093B
5 changed files with 113 additions and 11 deletions

View File

@ -343,7 +343,7 @@ void ProfileManager::updateDatabase()
"id INTEGER PRIMARY KEY,"
"server TEXT NOT NULL,"
"zoom_level INTEGER DEFAULT 0,"
"zoom_level INTEGER DEFAULT -1,"
"allow_cookies INTEGER DEFAULT 0,"
"wa_autoload_images INTEGER DEFAULT 0,"

View File

@ -59,7 +59,7 @@ CREATE TABLE site_settings (
id INTEGER PRIMARY KEY,
server TEXT NOT NULL,
zoom_level INTEGER DEFAULT 0,
zoom_level INTEGER DEFAULT -1,
allow_cookies INTEGER DEFAULT 0,
wa_autoload_images INTEGER DEFAULT 0,

View File

@ -343,13 +343,13 @@ SiteInfoPermissionItem* SiteInfo::addPermissionOption(SiteSettingsManager::Permi
void SiteInfo::addSiteSettings()
{
auto siteSettings = mApp->siteSettingsManager()->getSiteSettings(m_baseUrl, mApp->isPrivate());
// Attributes
for (const auto &attribute : mApp->siteSettingsManager()->getSupportedAttribute()) {
const auto supportedAttribute = mApp->siteSettingsManager()->getSupportedAttribute();
for (const auto &attribute : supportedAttribute) {
SiteInfoPermissionItem *item = addPermissionOption(siteSettings.attributes[attribute]);
item->setAttribute(attribute);
}
// Permissions
for (const auto &feature : mApp->siteSettingsManager()->getSupportedFeatures()) {
const auto supportedFeatures = mApp->siteSettingsManager()->getSupportedFeatures();
for (const auto &feature : supportedFeatures) {
SiteInfoPermissionItem *item = addPermissionOption(siteSettings.features[feature]);
item->setFeature(feature);
}
@ -357,10 +357,23 @@ void SiteInfo::addSiteSettings()
void SiteInfo::saveSiteSettings()
{
// Save permissions
// This is a nutjob to do it this way, totaly inefficient and stupid, hope it will work at first.
for (int i = 0; i < ui->listPermissions->count(); ++i) {
auto* item = static_cast<SiteInfoPermissionItem*>(ui->listPermissions->itemWidget(ui->listPermissions->item(i)));
mApp->siteSettingsManager()->setOption(item->sqlColumn(), m_baseUrl, item->permission());
SiteSettings siteSettings;
int index = 0;
auto supportedAttribute = mApp->siteSettingsManager()->getSupportedAttribute();
auto supportedFeatures = mApp->siteSettingsManager()->getSupportedFeatures();
for (int i = 0; i < supportedAttribute.size(); ++i, ++index) {
auto* item = static_cast<SiteInfoPermissionItem*>(ui->listPermissions->itemWidget(ui->listPermissions->item(index)));
siteSettings.attributes[supportedAttribute[i]] = item->permission();
}
for (int i = 0; i < supportedFeatures.size(); ++i, ++index) {
auto* item = static_cast<SiteInfoPermissionItem*>(ui->listPermissions->itemWidget(ui->listPermissions->item(index)));
siteSettings.features[supportedFeatures[i]] = item->permission();
}
siteSettings.AllowCookies = SiteSettingsManager::Default;
siteSettings.ZoomLevel = -1;
siteSettings.server = m_baseUrl.host();
mApp->siteSettingsManager()->setSiteSettings(siteSettings);
}

View File

@ -42,6 +42,7 @@ SiteSettingsManager::SiteSettingsManager ( QObject* parent )
supportedAttribute.append(QWebEngineSettings::PlaybackRequiresUserGesture);
supportedAttribute.append(QWebEngineSettings::WebRTCPublicInterfacesOnly);
/* Select SQL for QtWE Attributes */
attributesSql = QSL("SELECT ");
for (int i = 0; i < supportedAttribute.size(); ++i) {
@ -64,6 +65,7 @@ SiteSettingsManager::SiteSettingsManager ( QObject* parent )
supportedFeatures.append(QWebEnginePage::DesktopAudioVideoCapture);
/* Select SQL for SiteSettings */
everythingSql = QSL("SELECT ");
for (int i = 0; i < supportedAttribute.size(); ++i) {
@ -85,6 +87,56 @@ SiteSettingsManager::SiteSettingsManager ( QObject* parent )
everythingSql.append(optionToSqlColumn(poZoomLevel));
everythingSql.append(QSL(" FROM %1 WHERE server=?"));
/* Insert SQL for SiteSettings */
everythingInsertSql = QSL("INSERT INTO %1 (");
for (int i = 0; i < supportedAttribute.size(); ++i) {
everythingInsertSql.append(webAttributeToSqlColumn(supportedAttribute[i]));
everythingInsertSql.append(QSL(", "));
}
for (int i = 0; i < supportedFeatures.size(); ++i) {
everythingInsertSql.append(featureToSqlColumn(supportedFeatures[i]));
everythingInsertSql.append(QSL(", "));
}
everythingInsertSql.append(optionToSqlColumn(poAllowCookies));
everythingInsertSql.append(QSL(", "));
everythingInsertSql.append(optionToSqlColumn(poZoomLevel));
everythingInsertSql.append(QSL(", server"));
everythingInsertSql.append(QSL(") Values ("));
/* Index = sum(server, numberOfAttributes, numberOfFeatures, cookies, zoom) */
int index = 1 + supportedAttribute.size() + supportedFeatures.size() + 2;
for (int i = 0; i < index; ++i) {
if (i > 0) {
everythingInsertSql.append(QSL(", "));
}
everythingInsertSql.append(QSL("?"));
}
everythingInsertSql.append(QSL(")"));
/* Update SQL for SiteSettings */
everythingUpdateSql = QSL("UPDATE %1 SET ");
for (int i = 0; i < supportedAttribute.size(); ++i) {
everythingUpdateSql.append(webAttributeToSqlColumn(supportedAttribute[i]));
everythingUpdateSql.append(QSL("=?, "));
}
for (int i = 0; i < supportedFeatures.size(); ++i) {
everythingUpdateSql.append(featureToSqlColumn(supportedFeatures[i]));
everythingUpdateSql.append(QSL("=?, "));
}
everythingUpdateSql.append(optionToSqlColumn(poAllowCookies));
everythingUpdateSql.append(QSL("=?, "));
everythingUpdateSql.append(optionToSqlColumn(poZoomLevel));
everythingUpdateSql.append(QSL("=? "));
everythingUpdateSql.append(QSL(" WHERE server=?"));
}
SiteSettingsManager::~SiteSettingsManager() noexcept
@ -553,6 +605,40 @@ SiteSettingsManager::SiteSettings SiteSettingsManager::getSiteSettings(QUrl& url
return siteSettings;
}
void SiteSettingsManager::setSiteSettings(SiteSettingsManager::SiteSettings& siteSettings)
{
auto job = new SqlQueryJob(everythingUpdateSql.arg(sqlTable()), this);
for (int i = 0; i < supportedAttribute.size(); ++i) {
job->addBindValue(siteSettings.attributes[supportedAttribute[i]]);
}
for (int i = 0; i < supportedFeatures.size(); ++i) {
job->addBindValue(siteSettings.features[supportedFeatures[i]]);
}
job->addBindValue(siteSettings.AllowCookies);
job->addBindValue(siteSettings.ZoomLevel);
job->addBindValue(siteSettings.server);
connect(job, &SqlQueryJob::finished, this, [=]() {
if (job->numRowsAffected() == 0) {
auto job = new SqlQueryJob(everythingInsertSql.arg(sqlTable()), this);
for (int i = 0; i < supportedAttribute.size(); ++i) {
job->addBindValue(siteSettings.attributes[supportedAttribute[i]]);
}
for (int i = 0; i < supportedFeatures.size(); ++i) {
job->addBindValue(siteSettings.features[supportedFeatures[i]]);
}
job->addBindValue(siteSettings.AllowCookies);
job->addBindValue(siteSettings.ZoomLevel);
job->addBindValue(siteSettings.server);
job->start();
}
});
job->start();
}
QString SiteSettingsManager::sqlTable(bool privateMode)
{
if (privateMode) {

View File

@ -96,6 +96,7 @@ public:
QList<QWebEnginePage::Feature> getSupportedFeatures() const;
SiteSettings getSiteSettings(QUrl &url, bool privateMode);
void setSiteSettings(SiteSettings &siteSettings);
QString sqlTable(bool privateMode);
QString sqlTable();
@ -111,6 +112,8 @@ private:
QString attributesSql;
QString everythingSql;
QString everythingInsertSql;
QString everythingUpdateSql;
};