File indexing completed on 2024-05-05 05:00:09

0001 /*
0002     SPDX-FileCopyrightText: 2002 Leo Savernik <l.savernik@aon.at>
0003     Derived from jsopt.cpp, code copied from there is copyrighted to its
0004     respective owners.
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 // Own
0010 #include "jspolicies.h"
0011 
0012 // Qt
0013 #include <QButtonGroup>
0014 #include <QLabel>
0015 #include <QRadioButton>
0016 #include <QGridLayout>
0017 
0018 // KDE
0019 #include <kconfig.h>
0020 #include <kconfiggroup.h>
0021 #include <KLocalizedString>
0022 
0023 // == class JSPolicies ==
0024 
0025 JSPolicies::JSPolicies(KSharedConfig::Ptr config, const QString &group,
0026                        bool global, const QString &domain) :
0027     Policies(config, group, global, domain, QStringLiteral("javascript."), QStringLiteral("EnableJavaScript"))
0028 {
0029 }
0030 
0031 void JSPolicies::load()
0032 {
0033     Policies::load();
0034 
0035     KConfigGroup cg(config, groupname);
0036     QString key;
0037 
0038 //  enableJavaScriptDebugCB->setChecked( m_pConfig->readEntry("EnableJavaScriptDebug", QVariant(false)).toBool());
0039 //  enableDebugOutputCB->setChecked( m_pConfig->readEntry("EnableJSDebugOutput", QVariant(false)).toBool() );
0040     key = prefix + "WindowOpenPolicy";
0041     window_open = cg.readEntry(key, (is_global ? HtmlSettingsInterface::JSWindowOpenSmart : INHERIT_POLICY));
0042 
0043     key = prefix + "WindowResizePolicy";
0044     window_resize = cg.readEntry(key, (is_global ? HtmlSettingsInterface::JSWindowResizeAllow : INHERIT_POLICY));
0045 
0046     key = prefix + "WindowMovePolicy";
0047     window_move = cg.readEntry(key, (is_global ? HtmlSettingsInterface::JSWindowMoveAllow : INHERIT_POLICY));
0048 
0049     key = prefix + "WindowFocusPolicy";
0050     window_focus = cg.readEntry(key, (is_global ? HtmlSettingsInterface::JSWindowFocusAllow : INHERIT_POLICY));
0051 
0052     key = prefix + "WindowStatusPolicy";
0053     window_status = cg.readEntry(key, (is_global ? HtmlSettingsInterface::JSWindowStatusAllow : INHERIT_POLICY));
0054 }
0055 
0056 void JSPolicies::defaults()
0057 {
0058     Policies::defaults();
0059 //  enableJavaScriptGloballyCB->setChecked( true );
0060 //  enableJavaScriptDebugCB->setChecked( false );
0061 //  js_popup->setButton(0);
0062 // enableDebugOutputCB->setChecked( false );
0063     window_open = (is_global ? HtmlSettingsInterface::JSWindowOpenSmart : INHERIT_POLICY);
0064     window_resize = (is_global ? HtmlSettingsInterface::JSWindowResizeAllow : INHERIT_POLICY);
0065     window_move = (is_global ? HtmlSettingsInterface::JSWindowMoveAllow : INHERIT_POLICY);
0066     window_focus = (is_global ? HtmlSettingsInterface::JSWindowFocusAllow : INHERIT_POLICY);
0067     window_status = (is_global ? HtmlSettingsInterface::JSWindowStatusAllow : INHERIT_POLICY);
0068 }
0069 
0070 void JSPolicies::save()
0071 {
0072     Policies::save();
0073 
0074     QString key;
0075     key = prefix + "WindowOpenPolicy";
0076     if (window_open != INHERIT_POLICY) {
0077         config->group(groupname).writeEntry(key, window_open);
0078     } else {
0079         config->group(groupname).deleteEntry(key);
0080     }
0081 
0082     key = prefix + "WindowResizePolicy";
0083     if (window_resize != INHERIT_POLICY) {
0084         config->group(groupname).writeEntry(key, window_resize);
0085     } else {
0086         config->group(groupname).deleteEntry(key);
0087     }
0088 
0089     key = prefix + "WindowMovePolicy";
0090     if (window_move != INHERIT_POLICY) {
0091         config->group(groupname).writeEntry(key, window_move);
0092     } else {
0093         config->group(groupname).deleteEntry(key);
0094     }
0095 
0096     key = prefix + "WindowFocusPolicy";
0097     if (window_focus != INHERIT_POLICY) {
0098         config->group(groupname).writeEntry(key, window_focus);
0099     } else {
0100         config->group(groupname).deleteEntry(key);
0101     }
0102 
0103     key = prefix + "WindowStatusPolicy";
0104     if (window_status != INHERIT_POLICY) {
0105         config->group(groupname).writeEntry(key, window_status);
0106     } else {
0107         config->group(groupname).deleteEntry(key);
0108     }
0109 
0110     // don't do a config->sync() here for sake of efficiency
0111 }
0112 
0113 // == class JSPoliciesFrame ==
0114 
0115 JSPoliciesFrame::JSPoliciesFrame(JSPolicies *policies, const QString &title,
0116                                  QWidget *parent) :
0117     QGroupBox(title, parent),
0118     policies(policies)
0119 {
0120 
0121     bool is_per_domain = !policies->isGlobal();
0122 
0123     QGridLayout *this_layout = new QGridLayout();
0124     setLayout(this_layout);
0125     this_layout->setAlignment(Qt::AlignTop);
0126 
0127     QString wtstr;    // what's this description
0128     int colIdx;       // column index
0129 
0130     // === window.open ================================
0131     colIdx = 0;
0132     QLabel *label = new QLabel(i18n("Open new windows:"), this);
0133     this_layout->addWidget(label, 0, colIdx++);
0134 
0135     js_popup = new QButtonGroup(this);
0136     js_popup->setExclusive(true);
0137 
0138     QRadioButton *policy_btn;
0139     if (is_per_domain) {
0140         policy_btn = new QRadioButton(i18n("Use global"), this);
0141         policy_btn->setToolTip(i18n("Use setting from global policy."));
0142         js_popup->addButton(policy_btn, INHERIT_POLICY);
0143         this_layout->addWidget(policy_btn, 0, colIdx++);
0144         this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0145     }/*end if*/
0146 
0147     policy_btn = new QRadioButton(i18n("Allow"), this);
0148     policy_btn->setToolTip(i18n("Accept all popup window requests."));
0149     js_popup->addButton(policy_btn, HtmlSettingsInterface::JSWindowOpenAllow);
0150     this_layout->addWidget(policy_btn, 0, colIdx++);
0151     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0152 
0153     policy_btn = new QRadioButton(i18n("Ask"), this);
0154     policy_btn->setToolTip(i18n("Prompt every time a popup window is requested."));
0155     js_popup->addButton(policy_btn, HtmlSettingsInterface::JSWindowOpenAsk);
0156     this_layout->addWidget(policy_btn, 0, colIdx++);
0157     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0158 
0159     policy_btn = new QRadioButton(i18n("Deny"), this);
0160     policy_btn->setToolTip(i18n("Reject all popup window requests."));
0161     js_popup->addButton(policy_btn, HtmlSettingsInterface::JSWindowOpenDeny);
0162     this_layout->addWidget(policy_btn, 0, colIdx++);
0163     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0164 
0165     policy_btn = new QRadioButton(i18n("Smart"), this);
0166     policy_btn->setToolTip(i18n("Accept popup window requests only when "
0167                                   "links are activated through an explicit "
0168                                   "mouse click or keyboard operation."));
0169     js_popup->addButton(policy_btn, HtmlSettingsInterface::JSWindowOpenSmart);
0170     this_layout->addWidget(policy_btn, 0, colIdx++);
0171     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0172 
0173     wtstr = i18n("If you disable this, Konqueror will stop "
0174                  "interpreting the <i>window.open()</i> "
0175                  "JavaScript command. This is useful if you "
0176                  "regularly visit sites that make extensive use "
0177                  "of this command to pop up ad banners.<br />"
0178                  "<br /><b>Note:</b> Disabling this option might "
0179                  "also break certain sites that require <i>"
0180                  "window.open()</i> for proper operation. Use "
0181                  "this feature carefully.");
0182     label->setToolTip(wtstr);
0183     connect(js_popup, &QButtonGroup::idClicked, this, &JSPoliciesFrame::setWindowOpenPolicy);
0184 
0185     // === window.resizeBy/resizeTo ================================
0186     colIdx = 0;
0187     label = new QLabel(i18n("Resize window:"), this);
0188     this_layout->addWidget(label, 1, colIdx++);
0189 
0190     js_resize = new QButtonGroup(this);
0191     js_resize->setExclusive(true);
0192 
0193     if (is_per_domain) {
0194         policy_btn = new QRadioButton(i18n("Use global"), this);
0195         policy_btn->setToolTip(i18n("Use setting from global policy."));
0196         js_resize->addButton(policy_btn, INHERIT_POLICY);
0197         this_layout->addWidget(policy_btn, 1, colIdx++);
0198         this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0199     }/*end if*/
0200 
0201     policy_btn = new QRadioButton(i18n("Allow"), this);
0202     policy_btn->setToolTip(i18n("Allow scripts to change the window size."));
0203     js_resize->addButton(policy_btn, HtmlSettingsInterface::JSWindowResizeAllow);
0204     this_layout->addWidget(policy_btn, 1, colIdx++);
0205     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0206 
0207     policy_btn = new QRadioButton(i18n("Ignore"), this);
0208     policy_btn->setToolTip(i18n("Ignore attempts of scripts to change the window size. "
0209                                   "The web page will <i>think</i> it changed the "
0210                                   "size but the actual window is not affected."));
0211     js_resize->addButton(policy_btn, HtmlSettingsInterface::JSWindowResizeIgnore);
0212     this_layout->addWidget(policy_btn, 1, colIdx++);
0213     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0214 
0215     wtstr = i18n("Some websites change the window size on their own by using "
0216                  "<i>window.resizeBy()</i> or <i>window.resizeTo()</i>. "
0217                  "This option specifies the treatment of such "
0218                  "attempts.");
0219     label->setToolTip(wtstr);
0220     connect(js_resize, &QButtonGroup::idClicked, this, &JSPoliciesFrame::setWindowResizePolicy);
0221 
0222     // === window.moveBy/moveTo ================================
0223     colIdx = 0;
0224     label = new QLabel(i18n("Move window:"), this);
0225     this_layout->addWidget(label, 2, colIdx++);
0226 
0227     js_move = new QButtonGroup(this);
0228     js_move->setExclusive(true);
0229 
0230     if (is_per_domain) {
0231         policy_btn = new QRadioButton(i18n("Use global"), this);
0232         policy_btn->setToolTip(i18n("Use setting from global policy."));
0233         js_move->addButton(policy_btn, INHERIT_POLICY);
0234         this_layout->addWidget(policy_btn, 2, colIdx++);
0235         this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0236     }/*end if*/
0237 
0238     policy_btn = new QRadioButton(i18n("Allow"), this);
0239     policy_btn->setToolTip(i18n("Allow scripts to change the window position."));
0240     js_move->addButton(policy_btn, HtmlSettingsInterface::JSWindowMoveAllow);
0241     this_layout->addWidget(policy_btn, 2, colIdx++);
0242     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0243 
0244     policy_btn = new QRadioButton(i18n("Ignore"), this);
0245     policy_btn->setToolTip(i18n("Ignore attempts of scripts to change the window position. "
0246                                   "The web page will <i>think</i> it moved the "
0247                                   "window but the actual position is not affected."));
0248     js_move->addButton(policy_btn, HtmlSettingsInterface::JSWindowMoveIgnore);
0249     this_layout->addWidget(policy_btn, 2, colIdx++);
0250     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0251 
0252     wtstr = i18n("Some websites change the window position on their own by using "
0253                  "<i>window.moveBy()</i> or <i>window.moveTo()</i>. "
0254                  "This option specifies the treatment of such "
0255                  "attempts.");
0256     label->setToolTip(wtstr);
0257     connect(js_move, &QButtonGroup::idClicked, this, &JSPoliciesFrame::setWindowMovePolicy);
0258 
0259     // === window.focus ================================
0260     colIdx = 0;
0261     label = new QLabel(i18n("Focus window:"), this);
0262     this_layout->addWidget(label, 3, colIdx++);
0263 
0264     js_focus = new QButtonGroup(this);
0265     js_focus->setExclusive(true);
0266 
0267     if (is_per_domain) {
0268         policy_btn = new QRadioButton(i18n("Use global"), this);
0269         policy_btn->setToolTip(i18n("Use setting from global policy."));
0270         js_focus->addButton(policy_btn, INHERIT_POLICY);
0271         this_layout->addWidget(policy_btn, 3, colIdx++);
0272         this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0273     }/*end if*/
0274 
0275     policy_btn = new QRadioButton(i18n("Allow"), this);
0276     policy_btn->setToolTip(i18n("Allow scripts to focus the window."));
0277     js_focus->addButton(policy_btn, HtmlSettingsInterface::JSWindowFocusAllow);
0278     this_layout->addWidget(policy_btn, 3, colIdx++);
0279     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0280 
0281     policy_btn = new QRadioButton(i18n("Ignore"), this);
0282     policy_btn->setToolTip(i18n("Ignore attempts of scripts to focus the window. "
0283                                   "The web page will <i>think</i> it brought "
0284                                   "the focus to the window but the actual "
0285                                   "focus will remain unchanged."));
0286     js_focus->addButton(policy_btn, HtmlSettingsInterface::JSWindowFocusIgnore);
0287     this_layout->addWidget(policy_btn, 3, colIdx++);
0288     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0289 
0290     wtstr = i18n("Some websites set the focus to their browser window on their "
0291                  "own by using <i>window.focus()</i>. This usually leads to "
0292                  "the window being moved to the front interrupting whatever "
0293                  "action the user was dedicated to at that time. "
0294                  "This option specifies the treatment of such "
0295                  "attempts.");
0296     label->setToolTip(wtstr);
0297     connect(js_focus, &QButtonGroup::idClicked, this, &JSPoliciesFrame::setWindowFocusPolicy);
0298 
0299     // === window.status ================================
0300     colIdx = 0;
0301     label = new QLabel(i18n("Modify status bar text:"), this);
0302     this_layout->addWidget(label, 4, colIdx++);
0303 
0304     js_statusbar = new QButtonGroup(this);
0305     js_statusbar->setExclusive(true);
0306 
0307     if (is_per_domain) {
0308         policy_btn = new QRadioButton(i18n("Use global"), this);
0309         policy_btn->setToolTip(i18n("Use setting from global policy."));
0310         js_statusbar->addButton(policy_btn, INHERIT_POLICY);
0311         this_layout->addWidget(policy_btn, 4, colIdx++);
0312         this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0313     }/*end if*/
0314 
0315     policy_btn = new QRadioButton(i18n("Allow"), this);
0316     policy_btn->setToolTip(i18n("Allow scripts to change the text of the status bar."));
0317     js_statusbar->addButton(policy_btn, HtmlSettingsInterface::JSWindowStatusAllow);
0318     this_layout->addWidget(policy_btn, 4, colIdx++);
0319     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0320 
0321     policy_btn = new QRadioButton(i18n("Ignore"), this);
0322     policy_btn->setToolTip(i18n("Ignore attempts of scripts to change the status bar text. "
0323                                   "The web page will <i>think</i> it changed "
0324                                   "the text but the actual text will remain "
0325                                   "unchanged."));
0326     js_statusbar->addButton(policy_btn, HtmlSettingsInterface::JSWindowStatusIgnore);
0327     this_layout->addWidget(policy_btn, 4, colIdx++);
0328     this_layout->addItem(new QSpacerItem(10, 0), 0, colIdx++);
0329 
0330     wtstr = i18n("Some websites change the status bar text by setting "
0331                  "<i>window.status</i> or <i>window.defaultStatus</i>, "
0332                  "thus sometimes preventing displaying the real URLs of hyperlinks. "
0333                  "This option specifies the treatment of such "
0334                  "attempts.");
0335     label->setToolTip(wtstr);
0336     connect(js_statusbar, &QButtonGroup::idClicked, this, &JSPoliciesFrame::setWindowStatusPolicy);
0337 }
0338 
0339 void JSPoliciesFrame::refresh()
0340 {
0341     QRadioButton *button;
0342     button = static_cast<QRadioButton *>(js_popup->button(
0343             policies->window_open));
0344     if (button != nullptr) {
0345         button->setChecked(true);
0346     }
0347     button = static_cast<QRadioButton *>(js_resize->button(
0348             policies->window_resize));
0349     if (button != nullptr) {
0350         button->setChecked(true);
0351     }
0352     button = static_cast<QRadioButton *>(js_move->button(
0353             policies->window_move));
0354     if (button != nullptr) {
0355         button->setChecked(true);
0356     }
0357     button = static_cast<QRadioButton *>(js_focus->button(
0358             policies->window_focus));
0359     if (button != nullptr) {
0360         button->setChecked(true);
0361     }
0362     button = static_cast<QRadioButton *>(js_statusbar->button(
0363             policies->window_status));
0364     if (button != nullptr) {
0365         button->setChecked(true);
0366     }
0367 }
0368 
0369 void JSPoliciesFrame::setWindowOpenPolicy(int id)
0370 {
0371     policies->window_open = id;
0372     emit changed();
0373 }
0374 
0375 void JSPoliciesFrame::setWindowResizePolicy(int id)
0376 {
0377     policies->window_resize = id;
0378     emit changed();
0379 }
0380 
0381 void JSPoliciesFrame::setWindowMovePolicy(int id)
0382 {
0383     policies->window_move = id;
0384     emit changed();
0385 }
0386 
0387 void JSPoliciesFrame::setWindowFocusPolicy(int id)
0388 {
0389     policies->window_focus = id;
0390     emit changed();
0391 }
0392 
0393 void JSPoliciesFrame::setWindowStatusPolicy(int id)
0394 {
0395     policies->window_status = id;
0396     emit changed();
0397 }
0398