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.h, code copied from there is copyrighted to its
0004     respective owners.
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef JSPOLICIES_H
0010 #define JSPOLICIES_H
0011 
0012 #include <QGroupBox>
0013 
0014 #include <htmlextension.h>
0015 #include <htmlsettingsinterface.h>
0016 #include <KSharedConfig>
0017 #include "policies.h"
0018 
0019 class QButtonGroup;
0020 
0021 // special value for inheriting a global policy
0022 #define INHERIT_POLICY      32767
0023 
0024 /**
0025  * @short Contains all the JavaScript policies and methods for their manipulation.
0026  *
0027  * This class provides access to the JavaScript policies.
0028  *
0029  * @author Leo Savernik
0030  */
0031 class JSPolicies : public Policies
0032 {
0033 public:
0034     /**
0035      * constructor
0036      * @param config configuration to initialize this instance from
0037      * @param group config group to use if this instance contains the global
0038      *    policies (global == true)
0039      * @param global true if this instance contains the global policy settings,
0040      *    false if this instance contains policies specific for a domain.
0041      * @param domain name of the domain this instance is used to configure the
0042      *    policies for (case insensitive, ignored if global == true)
0043      */
0044     JSPolicies(KSharedConfig::Ptr config, const QString &group, bool global,
0045                const QString &domain = QString());
0046 
0047     /**
0048      * dummy constructor to make QMap happy.
0049      *
0050      * Never construct an object by using this.
0051      * @internal
0052      */
0053     JSPolicies();
0054 
0055     ~JSPolicies() override = default;
0056 
0057     /**
0058      * Returns whether the WindowOpen policy is inherited.
0059      */
0060     bool isWindowOpenPolicyInherited() const
0061     {
0062         return window_open == INHERIT_POLICY;
0063     }
0064     /**
0065      * Returns the current value of the WindowOpen policy.
0066      *
0067      * This will return an illegal value if isWindowOpenPolicyInherited is
0068      * true.
0069      */
0070     HtmlSettingsInterface::JSWindowOpenPolicy windowOpenPolicy() const
0071     {
0072         return static_cast<HtmlSettingsInterface::JSWindowOpenPolicy>(window_open);
0073     }
0074 
0075     /**
0076      * Returns whether the WindowResize policy is inherited.
0077      */
0078     bool isWindowResizePolicyInherited() const
0079     {
0080         return window_resize == INHERIT_POLICY;
0081     }
0082     /**
0083      * Returns the current value of the WindowResize policy.
0084      *
0085      * This will return an illegal value if isWindowResizePolicyInherited is
0086      * true.
0087      */
0088     HtmlSettingsInterface::JSWindowResizePolicy windowResizePolicy() const
0089     {
0090         return static_cast<HtmlSettingsInterface::JSWindowResizePolicy>(window_resize);
0091     }
0092 
0093     /**
0094      * Returns whether the WindowMove policy is inherited.
0095      */
0096     bool isWindowMovePolicyInherited() const
0097     {
0098         return window_move == INHERIT_POLICY;
0099     }
0100     /**
0101      * Returns the current value of the WindowMove policy.
0102      *
0103      * This will return an illegal value if isWindowMovePolicyInherited is
0104      * true.
0105      */
0106     HtmlSettingsInterface::JSWindowMovePolicy windowMovePolicy() const
0107     {
0108         return static_cast<HtmlSettingsInterface::JSWindowMovePolicy>(window_move);
0109     }
0110 
0111     /**
0112      * Returns whether the WindowFocus policy is inherited.
0113      */
0114     bool isWindowFocusPolicyInherited() const
0115     {
0116         return window_focus == INHERIT_POLICY;
0117     }
0118     /**
0119      * Returns the current value of the WindowFocus policy.
0120      *
0121      * This will return an illegal value if isWindowFocusPolicyInherited is
0122      * true.
0123      */
0124     HtmlSettingsInterface::JSWindowFocusPolicy windowFocusPolicy() const
0125     {
0126         return static_cast<HtmlSettingsInterface::JSWindowFocusPolicy>(window_focus);
0127     }
0128 
0129     /**
0130      * Returns whether the WindowStatus policy is inherited.
0131      */
0132     bool isWindowStatusPolicyInherited() const
0133     {
0134         return window_status == INHERIT_POLICY;
0135     }
0136     /**
0137      * Returns the current value of the WindowStatus policy.
0138      *
0139      * This will return an illegal value if isWindowStatusPolicyInherited is
0140      * true.
0141      */
0142     HtmlSettingsInterface::JSWindowStatusPolicy windowStatusPolicy() const
0143     {
0144         return static_cast<HtmlSettingsInterface::JSWindowStatusPolicy>(window_status);
0145     }
0146 
0147     /**
0148      * (re)loads settings from configuration file given in the constructor.
0149      */
0150     void load() override;
0151     /**
0152      * saves current settings to the configuration file given in the constructor
0153      */
0154     void save() override;
0155     /**
0156      * restores the default settings
0157      */
0158     void defaults() override;
0159 
0160 private:
0161     // one of HtmlSettingsInterface::JSWindowOpenPolicy or INHERIT_POLICY
0162     unsigned int window_open;
0163     // one of HtmlSettingsInterface::JSWindowResizePolicy or INHERIT_POLICY
0164     unsigned int window_resize;
0165     // one of HtmlSettingsInterface::JSWindowMovePolicy or INHERIT_POLICY
0166     unsigned int window_move;
0167     // one of HtmlSettingsInterface::JSWindowFocusPolicy or INHERIT_POLICY
0168     unsigned int window_focus;
0169     // one of HtmlSettingsInterface::JSWindowStatusPolicy or INHERIT_POLICY
0170     unsigned int window_status;
0171 
0172     friend class JSPoliciesFrame; // for changing policies
0173 };
0174 
0175 /**
0176  * @short Provides a framed widget with controls for the JavaScript policy settings.
0177  *
0178  * This widget contains controls for changing all JavaScript policies
0179  * except the JavaScript enabled policy itself. The rationale behind this is
0180  * that the enabled policy be separate from the rest in a prominent
0181  * place.
0182  *
0183  * It is suitable for the global policy settings as well as for the
0184  * domain-specific settings.
0185  *
0186  * The difference between global and domain-specific is the existence of
0187  * a special inheritance option in the latter case. That way domain-specific
0188  * policies can inherit their value from the global policies.
0189  *
0190  * @author Leo Savernik
0191  */
0192 class JSPoliciesFrame : public QGroupBox
0193 {
0194     Q_OBJECT
0195 public:
0196     /**
0197      * constructor
0198      * @param policies associated object containing the policy values. This
0199      *    object will be updated accordingly as the settings are changed.
0200      * @param title title for group box
0201      * @param parent parent widget
0202      */
0203     JSPoliciesFrame(JSPolicies *policies, const QString &title, QWidget *parent = nullptr);
0204 
0205     ~JSPoliciesFrame() override = default;
0206 
0207     /**
0208      * updates the controls to resemble the status of the underlying
0209      * JSPolicies object.
0210      */
0211     void refresh();
0212     /**
0213      * (re)loads settings from configuration file given in the constructor.
0214      */
0215     void load()
0216     {
0217         policies->load();
0218         refresh();
0219     }
0220     /**
0221      * saves current settings to the configuration file given in the constructor
0222      */
0223     void save()
0224     {
0225         policies->save();
0226     }
0227     /**
0228      * restores the default settings
0229      */
0230     void defaults()
0231     {
0232         policies->defaults();
0233         refresh();
0234     }
0235 
0236 Q_SIGNALS:
0237     /**
0238      * emitted every time an option has been changed
0239      */
0240     void changed();
0241 
0242 private Q_SLOTS:
0243     void setWindowOpenPolicy(int id);
0244     void setWindowResizePolicy(int id);
0245     void setWindowMovePolicy(int id);
0246     void setWindowFocusPolicy(int id);
0247     void setWindowStatusPolicy(int id);
0248 
0249 private:
0250 
0251     JSPolicies *policies;
0252     QButtonGroup *js_popup;
0253     QButtonGroup *js_resize;
0254     QButtonGroup *js_move;
0255     QButtonGroup *js_focus;
0256     QButtonGroup *js_statusbar;
0257 };
0258 
0259 #endif      // __JSPOLICIES_H__