diff --git a/pri/install.pri b/pri/install.pri index ed4c9d604..b1a3f84e3 100644 --- a/pri/install.pri +++ b/pri/install.pri @@ -175,7 +175,7 @@ win32 { lib.path = $$PREFIX lib.CONFIG = no_check_exist - clng.files = ../../resources/scripts/clang-format/clang-format.exe + clng.files = ../../resources/scripts/clang-format clng.path = $$PREFIX INSTALLS += target lib clng diff --git a/src/librssguard/gui/dialogs/formmessagefiltersmanager.cpp b/src/librssguard/gui/dialogs/formmessagefiltersmanager.cpp index a8baaaa0b..14d45191b 100644 --- a/src/librssguard/gui/dialogs/formmessagefiltersmanager.cpp +++ b/src/librssguard/gui/dialogs/formmessagefiltersmanager.cpp @@ -1,5 +1,6 @@ #include #include +#include // For license of this file, see /LICENSE.md. @@ -8,6 +9,7 @@ #include "core/messagefilter.h" #include "exceptions/filteringexception.h" #include "gui/guiutilities.h" +#include "gui/messagebox.h" #include "miscellaneous/application.h" #include "miscellaneous/feedreader.h" #include "miscellaneous/iconfactory.h" @@ -21,8 +23,10 @@ FormMessageFiltersManager::FormMessageFiltersManager(FeedReader* reader, const Q GuiUtilities::applyDialogProperties(*this, qApp->icons()->fromTheme(QSL("view-list-details"))); + m_ui.m_treeFeeds->setModel(m_feedsModel); m_ui.m_btnAddNew->setIcon(qApp->icons()->fromTheme(QSL("list-add"))); m_ui.m_btnRemoveSelected->setIcon(qApp->icons()->fromTheme(QSL("list-remove"))); + m_ui.m_btnBeautify->setIcon(qApp->icons()->fromTheme(QSL("format-justify-fill"))); m_ui.m_btnTest->setIcon(qApp->icons()->fromTheme(QSL("media-playback-start"))); m_ui.m_btnDetailedHelp->setIcon(qApp->icons()->fromTheme(QSL("help-contents"))); m_ui.m_txtScript->setFont(QFontDatabase::systemFont(QFontDatabase::SystemFont::FixedFont)); @@ -37,9 +41,16 @@ FormMessageFiltersManager::FormMessageFiltersManager(FeedReader* reader, const Q connect(m_ui.m_txtTitle, &QLineEdit::textChanged, this, &FormMessageFiltersManager::saveSelectedFilter); connect(m_ui.m_txtScript, &QPlainTextEdit::textChanged, this, &FormMessageFiltersManager::saveSelectedFilter); connect(m_ui.m_btnTest, &QPushButton::clicked, this, &FormMessageFiltersManager::testFilter); + connect(m_ui.m_btnBeautify, &QPushButton::clicked, this, &FormMessageFiltersManager::beautifyScript); + connect(m_ui.m_cmbAccounts, static_cast(&QComboBox::currentIndexChanged), this, [this]() { + // Load feeds/categories of the account and check marks. + loadSelectedAccount(); + loadFilterFeedAssignments(); + }); initializeTestingMessage(); loadFilter(); + loadAccounts(); } FormMessageFiltersManager::~FormMessageFiltersManager() { @@ -56,7 +67,9 @@ MessageFilter* FormMessageFiltersManager::selectedFilter() const { } ServiceRoot* FormMessageFiltersManager::selectedAccount() const { - return nullptr; + auto dat = m_ui.m_cmbAccounts->currentData(Qt::ItemDataRole::UserRole); + + return dat.isNull() ? nullptr : dat.value(); } void FormMessageFiltersManager::addNewFilter() { @@ -93,16 +106,9 @@ void FormMessageFiltersManager::loadFilter() { auto* acc = selectedAccount(); showFilter(filter); - updateFeedAssignments(filter, acc); } void FormMessageFiltersManager::testFilter() { - // TODO: Add button to beautify JavaScript code, call clang-format and distribute - // it under windows. On other platforms, just try to call and raise messagebox - // error with "install clang-format" if not found. - // then call like this with qt process api. - // echo "script-code" | ./clang-format.exe --assume-filename="script.js" --style="Chromium" - // Perform per-message filtering. QJSEngine filter_engine; QSqlDatabase database = qApp->database()->connection(metaObject()->className()); @@ -127,9 +133,9 @@ void FormMessageFiltersManager::testFilter() { m_ui.m_txtErrors->setTextColor(decision == FilteringAction::Accept ? Qt::GlobalColor::darkGreen : Qt::GlobalColor::red); - QString answer = tr("Message will be %1.\n").arg(decision == FilteringAction::Accept - ? tr("accepted") - : tr("rejected")); + QString answer = tr("Message will be %1.\n\n").arg(decision == FilteringAction::Accept + ? tr("ACCEPTED") + : tr("REJECTED")); answer += tr("Output (modified) message is:\n" " Title = '%1'\n" @@ -154,6 +160,13 @@ void FormMessageFiltersManager::testFilter() { m_ui.m_tcMessage->setCurrentIndex(1); } +void FormMessageFiltersManager::loadSelectedAccount() { + m_feedsModel->setRootItem(selectedAccount(), false, true); + m_ui.m_treeFeeds->expandAll(); +} + +void FormMessageFiltersManager::loadFilterFeedAssignments() {} + void FormMessageFiltersManager::showFilter(MessageFilter* filter) { m_loadingFilter = true; @@ -173,7 +186,61 @@ void FormMessageFiltersManager::showFilter(MessageFilter* filter) { m_loadingFilter = false; } -void FormMessageFiltersManager::updateFeedAssignments(MessageFilter* filter, ServiceRoot* account) {} +void FormMessageFiltersManager::loadAccounts() { + for (auto* acc : m_accounts) { + m_ui.m_cmbAccounts->addItem(acc->icon(), + acc->title(), + QVariant::fromValue(acc)); + } +} + +void FormMessageFiltersManager::beautifyScript() { + QProcess proc_clang_format(this); + + proc_clang_format.setInputChannelMode(QProcess::InputChannelMode::ManagedInputChannel); + proc_clang_format.setArguments({"--assume-filename=script.js", "--style=Chromium"}); + +#if defined (Q_OS_WIN) + proc_clang_format.setProgram(qApp->applicationDirPath() + QDir::separator() + + QSL("clang-format") + QDir::separator() + + QSL("clang-format.exe")); +#else + proc_clang_format.setProgram(QSL("clang-format")); +#endif + + if (!proc_clang_format.open()) { + MessageBox::show(this, QMessageBox::Icon::Critical, + tr("Cannot find 'clang-format'"), + tr("Script was not beautified, because 'clang-format' tool was not found.")); + return; + } + + proc_clang_format.write(m_ui.m_txtScript->toPlainText().toUtf8()); + proc_clang_format.closeWriteChannel(); + + if (proc_clang_format.waitForFinished(3000)) { + if (proc_clang_format.exitCode() == 0) { + auto script = proc_clang_format.readAllStandardOutput(); + + m_ui.m_txtScript->setPlainText(script); + } + else { + auto err = proc_clang_format.readAllStandardError(); + + MessageBox::show(this, QMessageBox::Icon::Critical, + tr("Error"), + tr("Script was not beautified, because 'clang-format' tool thrown error."), + QString(), + err); + } + } + else { + proc_clang_format.kill(); + MessageBox::show(this, QMessageBox::Icon::Critical, + tr("Beautifier was running for too long time"), + tr("Script was not beautified, is 'clang-format' installed?")); + } +} void FormMessageFiltersManager::initializeTestingMessage() { m_ui.m_cbSampleImportant->setChecked(true); diff --git a/src/librssguard/gui/dialogs/formmessagefiltersmanager.h b/src/librssguard/gui/dialogs/formmessagefiltersmanager.h index d80cd810a..1f1e16d58 100644 --- a/src/librssguard/gui/dialogs/formmessagefiltersmanager.h +++ b/src/librssguard/gui/dialogs/formmessagefiltersmanager.h @@ -29,13 +29,18 @@ class FormMessageFiltersManager : public QDialog { void loadFilter(); void testFilter(); + // Load feeds/categories tree. + void loadSelectedAccount(); + + // Load checkmarks according to already active assignments. + void loadFilterFeedAssignments(); + // Display filter title/contents. void showFilter(MessageFilter* filter); - // Load feeds/categories of the account, place checkmarks where filter is used. - void updateFeedAssignments(MessageFilter* filter, ServiceRoot* account); - private: + void loadAccounts(); + void beautifyScript(); void initializeTestingMessage(); Message testingMessage() const; diff --git a/src/librssguard/gui/dialogs/formmessagefiltersmanager.ui b/src/librssguard/gui/dialogs/formmessagefiltersmanager.ui index ef4036be7..3d9607165 100644 --- a/src/librssguard/gui/dialogs/formmessagefiltersmanager.ui +++ b/src/librssguard/gui/dialogs/formmessagefiltersmanager.ui @@ -44,7 +44,11 @@ - + + + true + + @@ -56,12 +60,12 @@ Account - comboBox + m_cmbAccounts - + @@ -73,6 +77,12 @@ 150 + + true + + + false + @@ -339,7 +349,14 @@ - &Test the script + &Test! + + + + + + + &Beautify! @@ -387,13 +404,14 @@ m_listFilters m_btnAddNew m_btnRemoveSelected - comboBox + m_cmbAccounts m_treeFeeds pushButton pushButton_2 m_txtTitle m_txtScript m_btnTest + m_btnBeautify m_btnDetailedHelp m_tcMessage m_cbSampleRead diff --git a/src/librssguard/services/abstract/accountcheckmodel.cpp b/src/librssguard/services/abstract/accountcheckmodel.cpp index f35523c74..928368976 100644 --- a/src/librssguard/services/abstract/accountcheckmodel.cpp +++ b/src/librssguard/services/abstract/accountcheckmodel.cpp @@ -24,9 +24,20 @@ RootItem* AccountCheckModel::rootItem() const { return m_rootItem; } -void AccountCheckModel::setRootItem(RootItem* root_item) { - delete m_rootItem; +void AccountCheckModel::setRootItem(RootItem* root_item, bool delete_previous_root, bool with_layout_change) { + if (with_layout_change) { + emit layoutAboutToBeChanged(); + } + + if (delete_previous_root && m_rootItem != nullptr) { + m_rootItem->deleteLater(); + } + m_rootItem = root_item; + + if (with_layout_change) { + emit layoutChanged(); + } } void AccountCheckModel::checkAllItems() { @@ -163,6 +174,9 @@ QVariant AccountCheckModel::data(const QModelIndex& index, int role) const { return ic.isNull() ? QVariant() : ic; } + else if (role == Qt::EditRole) { + return QVariant::fromValue(item); + } else if (role == Qt::DisplayRole) { switch (item->kind()) { case RootItemKind::Category: diff --git a/src/librssguard/services/abstract/accountcheckmodel.h b/src/librssguard/services/abstract/accountcheckmodel.h index f96152c49..c0444461e 100644 --- a/src/librssguard/services/abstract/accountcheckmodel.h +++ b/src/librssguard/services/abstract/accountcheckmodel.h @@ -37,7 +37,7 @@ class AccountCheckModel : public QAbstractItemModel { // Root item manipulators. RootItem* rootItem() const; - void setRootItem(RootItem* root_item); + void setRootItem(RootItem* root_item, bool delete_previous_root = true, bool with_layout_change = false); public slots: void checkAllItems();