| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "network-web/adblock/adblockdialog.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | #include "exceptions/applicationexception.h" |
| 7 | #include "gui/guiutilities.h" |
| 8 | #include "gui/messagebox.h" |
| 9 | #include "miscellaneous/application.h" |
| 10 | #include "miscellaneous/iconfactory.h" |
| 11 | #include "miscellaneous/settings.h" |
| 12 | #include "network-web/adblock/adblockmanager.h" |
| 13 | #include "network-web/webfactory.h" |
| 14 | |
| 15 | #include <QMenu> |
| 16 | #include <QMessageBox> |
| 17 | #include <QTimer> |
| 18 | |
| 19 | AdBlockDialog::AdBlockDialog(QWidget* parent) : QDialog(parent), m_manager(qApp->web()->adBlock()), m_loaded(false) { |
| 20 | m_ui.setupUi(this); |
| 21 | m_ui.m_cbEnable->setChecked(m_manager->isEnabled()); |
| 22 | |
| 23 | GuiUtilities::applyDialogProperties(*this, |
| 24 | qApp->icons()->miscIcon(QSL(ADBLOCK_ICON_ACTIVE)), |
| 25 | tr("AdBlock configuration")); |
| 26 | |
| 27 | connect(m_ui.m_btnHelp, &QPushButton::clicked, this, [=]() { |
| 28 | qApp->web()->openUrlInExternalBrowser(QSL(ADBLOCK_HOWTO)); |
| 29 | }); |
| 30 | connect(m_ui.m_cbEnable, &QCheckBox::clicked, this, &AdBlockDialog::enableAdBlock); |
| 31 | connect(m_manager, &AdBlockManager::enabledChanged, this, &AdBlockDialog::onAdBlockEnabledChanged); |
| 32 | connect(m_manager, &AdBlockManager::processTerminated, this, &AdBlockDialog::onAdBlockProcessTerminated); |
| 33 | |
| 34 | m_ui.m_lblTestResult->label()->setWordWrap(true); |
| 35 | m_ui.m_btnHelp->setIcon(qApp->icons()->fromTheme(QSL("help-about"))); |
| 36 | m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Information, |
| 37 | tr("No additional info."), |
| 38 | tr("No additional info.")); |
| 39 | |
| 40 | loadDialog(); |
| 41 | m_ui.m_buttonBox->setFocus(); |
| 42 | } |
| 43 | |
| 44 | void AdBlockDialog::saveOnClose() { |
| 45 | m_manager->setFilterLists(m_ui.m_txtPredefined->toPlainText().split(QSL("\n"))); |
| 46 | m_manager->setCustomFilters(m_ui.m_txtCustom->toPlainText().split(QSL("\n"))); |
| 47 | |
| 48 | try { |
| 49 | auto enabl = m_manager->isEnabled(); |
| 50 | |
| 51 | m_manager->setEnabled(false); |
| 52 | |
| 53 | if (enabl) { |
| 54 | m_manager->setEnabled(enabl); |
| 55 | } |
| 56 | } |
| 57 | catch (const ApplicationException& ex) { |
| 58 | qCriticalNN << LOGSEC_ADBLOCK << "Failed to enable AdBlock, error:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 59 | |
| 60 | MsgBox::show(this, |
| 61 | QMessageBox::Icon::Critical, |
| 62 | tr("Cannot enable AdBlock"), |
| 63 | tr("There is some error in AdBlock component and it cannot be enabled. " |
| 64 | "Check error message below (or application debug log) for more information."), |
| 65 | {}, |
| 66 | ex.message()); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void AdBlockDialog::enableAdBlock(bool enable) { |
| 71 | qApp->settings()->setValue(GROUP(AdBlock), AdBlock::AdBlockEnabled, enable); |
| 72 | |
| 73 | m_manager->setFilterLists(m_ui.m_txtPredefined->toPlainText().split(QSL("\n"))); |
| 74 | m_manager->setCustomFilters(m_ui.m_txtCustom->toPlainText().split(QSL("\n"))); |
| 75 | |
| 76 | try { |
| 77 | m_manager->setEnabled(enable); |
| 78 | } |
| 79 | catch (const ApplicationException& ex) { |
| 80 | qCriticalNN << LOGSEC_ADBLOCK << "Test of configuration failed:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 81 | |
| 82 | m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Error, |
| 83 | tr("There is error, check application log for more details and " |
| 84 | "head to online documentation. Also make sure that Node.js is installed." |
| 85 | "\n\nError: %1") |
| 86 | .arg(ex.message()), |
| 87 | tr("ERROR!")); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void AdBlockDialog::onAdBlockEnabledChanged(bool enabled, const QString& message) { |
| 92 | m_ui.m_cbEnable->setChecked(enabled); |
| 93 | |
| 94 | if (enabled) { |
| 95 | m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Ok, |
| 96 | tr("It seems your AdBlock runs fine, but wait few seconds to be sure."), |
| 97 | tr("OK!")); |
| 98 | } |
| 99 | else if (!message.isEmpty()) { |
| 100 | m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Warning, message, message); |
| 101 | } |
| 102 | else { |
| 103 | m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Information, |
| 104 | tr("No additional info."), |
| 105 | tr("No additional info.")); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void AdBlockDialog::onAdBlockProcessTerminated() { |
| 110 | m_ui.m_cbEnable->setChecked(false); |
| 111 | m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Error, |
| 112 | tr("There is error, check application log for more details and " |
| 113 | "head to online documentation."), |
| 114 | tr("ERROR!")); |
| 115 | } |
| 116 | |
| 117 | void AdBlockDialog::loadDialog() { |
| 118 | m_ui.m_txtCustom->setPlainText(m_manager->customFilters().join(QSL("\n"))); |
| 119 | m_ui.m_txtPredefined->setPlainText(m_manager->filterLists().join(QSL("\n"))); |
| 120 | } |
| 121 | |
| 122 | void AdBlockDialog::hideEvent(QHideEvent* event) { |
| 123 | QDialog::hideEvent(event); |
| 124 | |
| 125 | saveOnClose(); |
| 126 | } |