From 6dc4b6d47be3de3cdd4f4d569c5d819c0c38c19b Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Mon, 2 Dec 2024 10:22:11 +0100 Subject: [PATCH] Add option to add site permissions in preferences BUG: 495391 Signed-off-by: Juraj Oravec --- .../preferences/sitesettingsbrowsedialog.cpp | 55 ++++++++++++++- .../preferences/sitesettingsbrowsedialog.h | 6 ++ .../preferences/sitesettingsbrowsedialog.ui | 70 +++++++++++++++---- 3 files changed, 116 insertions(+), 15 deletions(-) diff --git a/src/lib/preferences/sitesettingsbrowsedialog.cpp b/src/lib/preferences/sitesettingsbrowsedialog.cpp index 3e49ee2dd..c3514bd0c 100644 --- a/src/lib/preferences/sitesettingsbrowsedialog.cpp +++ b/src/lib/preferences/sitesettingsbrowsedialog.cpp @@ -8,7 +8,10 @@ #include "mainapplication.h" #include "sqldatabase.h" -const int rolePermission = Qt::UserRole + 10; +#include +#include + +constexpr int rolePermission = Qt::UserRole; SiteSettingsBrowseDialog::SiteSettingsBrowseDialog(QString &name, QString &sqlColumn, QWidget* parent) : QDialog(parent) @@ -20,6 +23,21 @@ SiteSettingsBrowseDialog::SiteSettingsBrowseDialog(QString &name, QString &sqlCo loadItems(); + m_completer = new QCompleter(this); + m_completer->setCompletionMode(QCompleter::PopupCompletion); + m_completer->setFilterMode(Qt::MatchContains); + m_completer->setCaseSensitivity(Qt::CaseInsensitive); + m_completerModel = new QSqlQueryModel(this); + m_completerModel->setQuery(QSL("SELECT server from %1").arg(mApp->siteSettingsManager()->sqlTable()), + SqlDatabase::instance()->database()); + m_completer->setModel(m_completerModel); + m_ui->urlEdit->setCompleter(m_completer); + + m_ui->urlComboBox->addItem(tr("Ask"), SiteSettingsManager::Ask); /* Has to be first to be able to hide it */ + m_ui->urlComboBox->addItem(tr("Allow"), SiteSettingsManager::Allow); + m_ui->urlComboBox->addItem(tr("Deny"), SiteSettingsManager::Deny); + m_ui->urlComboBox->addItem(tr("Default"), SiteSettingsManager::Default); + m_ui->treeWidget->header()->resizeSections(QHeaderView::ResizeToContents); connect(m_ui->askButton, &QPushButton::clicked, this, [=]() { setPermission(SiteSettingsManager::Ask); }); @@ -27,6 +45,7 @@ SiteSettingsBrowseDialog::SiteSettingsBrowseDialog(QString &name, QString &sqlCo connect(m_ui->denyButton, &QPushButton::clicked, this, [=]() { setPermission(SiteSettingsManager::Deny); }); connect(m_ui->defaultButton, &QPushButton::clicked, this, [=]() { setPermission(SiteSettingsManager::Default); }); + connect(m_ui->createButton, &QPushButton::clicked, this, &SiteSettingsBrowseDialog::createPermission); connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &SiteSettingsBrowseDialog::storeChanges); } @@ -71,7 +90,7 @@ void SiteSettingsBrowseDialog::setPermission(const SiteSettingsManager::Permissi } const QString server = item->text(0); - auto itemPermission = static_cast(item->data(0, Qt::UserRole + 10).toInt()); + auto itemPermission = static_cast(item->data(0, rolePermission).toInt()); if (permission == itemPermission) { m_listModifications.remove(server); } @@ -85,6 +104,38 @@ void SiteSettingsBrowseDialog::setPermission(const SiteSettingsManager::Permissi void SiteSettingsBrowseDialog::hideAskButton() { m_ui->askButton->hide(); + m_ui->urlComboBox->removeItem(0); +} + +void SiteSettingsBrowseDialog::createPermission() +{ + const QString server = mApp->siteSettingsManager()->adjustUrl(QUrl(m_ui->urlEdit->text())); + const auto permission = static_cast(m_ui->urlComboBox->currentData(rolePermission).toInt()); + + if (server.isEmpty()) { + /* TODO: Print a status message or something to notify the user that url is not valid */ + qWarning() << "An invalid URL."; + return; + } + + const auto list_items = m_ui->treeWidget->findItems(server, Qt::MatchExactly); + + if (list_items.isEmpty()) { + const QString permissionName = mApp->siteSettingsManager()->getPermissionName(permission); + + auto* item = new QTreeWidgetItem(m_ui->treeWidget); + item->setText(0, server); + item->setText(1, permissionName); + item->setData(0, rolePermission, permission); + + m_ui->treeWidget->addTopLevelItem(item); + m_ui->treeWidget->setCurrentItem(item); + } + else { + m_ui->treeWidget->setCurrentItem(list_items.first()); + } + + setPermission(permission); } SiteSettingsBrowseDialog::~SiteSettingsBrowseDialog() = default; diff --git a/src/lib/preferences/sitesettingsbrowsedialog.h b/src/lib/preferences/sitesettingsbrowsedialog.h index 2325731b2..24add7023 100644 --- a/src/lib/preferences/sitesettingsbrowsedialog.h +++ b/src/lib/preferences/sitesettingsbrowsedialog.h @@ -10,6 +10,9 @@ #include #include +class QCompleter; +class QSqlQueryModel; + namespace Ui { class SiteSettingsBrowseDialog; @@ -28,6 +31,7 @@ public: public Q_SLOTS: void storeChanges(); + void createPermission(); private: void loadItems(); @@ -35,6 +39,8 @@ private: QScopedPointer m_ui; QHash m_listModifications; QString m_sqlColumn; + QCompleter *m_completer; + QSqlQueryModel * m_completerModel; }; #endif // SITESETTINGSBROWSEDIALOG_H diff --git a/src/lib/preferences/sitesettingsbrowsedialog.ui b/src/lib/preferences/sitesettingsbrowsedialog.ui index 3413164eb..b19306d4a 100644 --- a/src/lib/preferences/sitesettingsbrowsedialog.ui +++ b/src/lib/preferences/sitesettingsbrowsedialog.ui @@ -14,7 +14,42 @@ Site Permissions - + + + + + + Url: + + + + + + + https://kde.org + + + + + + + + + + + + + + + + + + + Set + + + + 0 @@ -37,8 +72,24 @@ - + + + + + Qt::Orientation::Vertical + + + QSizePolicy::Policy::Fixed + + + + 20 + 50 + + + + @@ -70,7 +121,7 @@ - Qt::Vertical + Qt::Orientation::Vertical @@ -82,20 +133,13 @@ - + - Qt::Horizontal + Qt::Orientation::Horizontal - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - + QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok