Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / settings / konqhtml / jsopts.cpp
blobd18a5b2473e809ff751bb0df66d3db8543d80458
1 /*
2 * Copyright (c) Martin R. Jones 1996
3 * Copyright (c) Bernd Wuebben 1998
5 * Copyright (c) Torben Weis 1998
6 * KControl port & modifications
8 * Copyright (c) David Faure 1998
9 * End of the KControl port, added 'kfmclient configure' call.
11 * Copyright (C) Kalle Dalheimer 2000
12 * New configuration scheme for JavaScript
14 * Copyright (c) Daniel Molkentin 2000
15 * Major cleanup & Java/JS settings splitted
17 * Copyright (c) Leo Savernik 2002-2003
18 * Big changes to accommodate per-domain settings
22 // Own
23 #include "jsopts.h"
25 // Qt
26 #include <QtGui/QLayout>
27 #include <QtGui/QTreeWidget>
29 // KDE
30 #include <kconfig.h>
31 #include <kdebug.h>
32 #include <kurlrequester.h>
33 #include <klocale.h>
35 // Local
36 #include "htmlopts.h"
37 #include "policydlg.h"
40 #if defined Q_WS_X11 && !defined K_WS_QTONLY
41 #include <X11/Xlib.h>
42 #endif
44 // == class KJavaScriptOptions =====
45 #include <KDebug>
46 KJavaScriptOptions::KJavaScriptOptions( KSharedConfig::Ptr config, const QString &group, const KComponentData &componentData, QWidget *parent) :
47 KCModule(componentData, parent),
48 _removeJavaScriptDomainAdvice(false),
49 m_pConfig( config ), m_groupname( group ),
50 js_global_policies(config,group,true,QString()),
51 _removeECMADomainSettings(false)
53 QVBoxLayout* toplevel = new QVBoxLayout( this );
54 toplevel->setMargin( 10 );
55 toplevel->setSpacing( 5 );
57 enableJavaScriptGloballyCB = new QCheckBox( i18n( "Ena&ble JavaScript globally" ));
58 enableJavaScriptGloballyCB->setWhatsThis( i18n("Enables the execution of scripts written in ECMA-Script "
59 "(also known as JavaScript) that can be contained in HTML pages. "
60 "Note that, as with any browser, enabling scripting languages can be a security problem.") );
61 connect( enableJavaScriptGloballyCB, SIGNAL( clicked() ), SLOT( changed() ) );
62 connect( enableJavaScriptGloballyCB, SIGNAL( clicked() ), this, SLOT( slotChangeJSEnabled() ) );
63 toplevel->addWidget(enableJavaScriptGloballyCB);
65 // the global checkbox
66 QGroupBox* globalGB = new QGroupBox(i18n( "Debugging" ));
67 QHBoxLayout *hbox = new QHBoxLayout(globalGB);
68 toplevel->addWidget( globalGB );
70 jsDebugWindow = new QCheckBox( i18n( "Enable debu&gger" ) );
71 jsDebugWindow->setWhatsThis( i18n( "Enables builtin JavaScript debugger." ) );
72 connect( jsDebugWindow, SIGNAL( clicked() ), SLOT( changed() ) );
73 hbox->addWidget(jsDebugWindow);
75 reportErrorsCB = new QCheckBox( i18n( "Report &errors" ) );
76 reportErrorsCB->setWhatsThis( i18n("Enables the reporting of errors that occur when JavaScript "
77 "code is executed.") );
78 connect( reportErrorsCB, SIGNAL( clicked() ), SLOT( changed() ) );
79 hbox->addWidget(reportErrorsCB);
81 // the domain-specific listview
82 domainSpecific = new JSDomainListView(m_pConfig,m_groupname,this,this);
83 connect(domainSpecific,SIGNAL(changed(bool)),SLOT(changed()));
84 toplevel->addWidget( domainSpecific, 2 );
86 domainSpecific->setWhatsThis( i18n("Here you can set specific JavaScript policies for any particular "
87 "host or domain. To add a new policy, simply click the <i>New...</i> "
88 "button and supply the necessary information requested by the "
89 "dialog box. To change an existing policy, click on the <i>Change...</i> "
90 "button and choose the new policy from the policy dialog box. Clicking "
91 "on the <i>Delete</i> button will remove the selected policy causing the default "
92 "policy setting to be used for that domain. The <i>Import</i> and <i>Export</i> "
93 "button allows you to easily share your policies with other people by allowing "
94 "you to save and retrieve them from a zipped file.") );
96 QString wtstr = i18n("<p>This box contains the domains and hosts you have set "
97 "a specific JavaScript policy for. This policy will be used "
98 "instead of the default policy for enabling or disabling JavaScript on pages sent by these "
99 "domains or hosts.</p><p>Select a policy and use the controls on "
100 "the right to modify it.</p>");
101 domainSpecific->listView()->setWhatsThis( wtstr );
103 domainSpecific->importButton()->setWhatsThis( i18n("Click this button to choose the file that contains "
104 "the JavaScript policies. These policies will be merged "
105 "with the existing ones. Duplicate entries are ignored.") );
106 domainSpecific->exportButton()->setWhatsThis( i18n("Click this button to save the JavaScript policy to a zipped "
107 "file. The file, named <b>javascript_policy.tgz</b>, will be "
108 "saved to a location of your choice." ) );
110 // the frame containing the JavaScript policies settings
111 js_policies_frame = new JSPoliciesFrame(&js_global_policies,
112 i18n("Global JavaScript Policies"),this);
113 toplevel->addWidget(js_policies_frame);
114 connect(js_policies_frame, SIGNAL(changed()), SLOT(changed()));
119 void KJavaScriptOptions::load()
121 // *** load ***
122 KConfigGroup cg(m_pConfig, m_groupname);
123 if( cg.hasKey( "ECMADomains" ) )
124 domainSpecific->initialize(cg.readEntry("ECMADomains", QStringList() ));
125 else if( cg.hasKey( "ECMADomainSettings" ) ) {
126 domainSpecific->updateDomainListLegacy( cg.readEntry( "ECMADomainSettings" , QStringList() ) );
127 _removeECMADomainSettings = true;
128 } else {
129 domainSpecific->updateDomainListLegacy(cg.readEntry("JavaScriptDomainAdvice", QStringList() ) );
130 _removeJavaScriptDomainAdvice = true;
133 // *** apply to GUI ***
134 js_policies_frame->load();
135 enableJavaScriptGloballyCB->setChecked(
136 js_global_policies.isFeatureEnabled());
137 reportErrorsCB->setChecked( cg.readEntry("ReportJavaScriptErrors", false));
138 jsDebugWindow->setChecked( cg.readEntry( "EnableJavaScriptDebug", false) );
139 // js_popup->setButton( m_pConfig->readUnsignedNumEntry("WindowOpenPolicy", 0) );
140 emit changed(false);
143 void KJavaScriptOptions::defaults()
145 js_policies_frame->defaults();
146 enableJavaScriptGloballyCB->setChecked(
147 js_global_policies.isFeatureEnabled());
148 reportErrorsCB->setChecked( false );
149 jsDebugWindow->setChecked( false );
150 emit changed(true);
153 void KJavaScriptOptions::save()
155 KConfigGroup cg(m_pConfig, m_groupname);
156 cg.writeEntry( "ReportJavaScriptErrors", reportErrorsCB->isChecked() );
157 cg.writeEntry( "EnableJavaScriptDebug", jsDebugWindow->isChecked() );
159 domainSpecific->save(m_groupname,"ECMADomains");
160 js_policies_frame->save();
162 if (_removeECMADomainSettings) {
163 cg.deleteEntry("ECMADomainSettings");
164 _removeECMADomainSettings = false;
167 // sync moved to KJSParts::save
168 // cg.sync();
169 emit changed(false);
172 void KJavaScriptOptions::slotChangeJSEnabled() {
173 js_global_policies.setFeatureEnabled(enableJavaScriptGloballyCB->isChecked());
176 // == class JSDomainListView =====
178 JSDomainListView::JSDomainListView(KSharedConfig::Ptr config,const QString &group,
179 KJavaScriptOptions *options, QWidget *parent)
180 : DomainListView(config,i18nc("@title:group", "Do&main-Specific" ), parent),
181 group(group), options(options) {
184 JSDomainListView::~JSDomainListView() {
187 void JSDomainListView::updateDomainListLegacy(const QStringList &domainConfig)
189 domainSpecificLV->clear();
190 JSPolicies pol(config,group,false);
191 pol.defaults();
192 for (QStringList::ConstIterator it = domainConfig.begin();
193 it != domainConfig.end(); ++it) {
194 QString domain;
195 KHTMLSettings::KJavaScriptAdvice javaAdvice;
196 KHTMLSettings::KJavaScriptAdvice javaScriptAdvice;
197 KHTMLSettings::splitDomainAdvice(*it, domain, javaAdvice, javaScriptAdvice);
198 if (javaScriptAdvice != KHTMLSettings::KJavaScriptDunno) {
199 QTreeWidgetItem *index =
200 new QTreeWidgetItem( domainSpecificLV, QStringList() << domain <<
201 i18n(KHTMLSettings::adviceToStr(javaScriptAdvice)) );
203 pol.setDomain(domain);
204 pol.setFeatureEnabled(javaScriptAdvice != KHTMLSettings::KJavaScriptReject);
205 domainPolicies[index] = new JSPolicies(pol);
210 void JSDomainListView::setupPolicyDlg(PushButton trigger,PolicyDialog &pDlg,
211 Policies *pol) {
212 JSPolicies *jspol = static_cast<JSPolicies *>(pol);
213 QString caption;
214 switch (trigger) {
215 case AddButton:
216 caption = i18n( "New JavaScript Policy" );
217 jspol->setFeatureEnabled(!options->enableJavaScriptGloballyCB->isChecked());
218 break;
219 case ChangeButton: caption = i18n( "Change JavaScript Policy" ); break;
220 default: ; // inhibit gcc warning
221 }/*end switch*/
222 pDlg.setCaption(caption);
223 pDlg.setFeatureEnabledLabel(i18n("JavaScript policy:"));
224 pDlg.setFeatureEnabledWhatsThis(i18n("Select a JavaScript policy for "
225 "the above host or domain."));
226 JSPoliciesFrame *panel = new JSPoliciesFrame(jspol,i18n("Domain-Specific "
227 "JavaScript Policies"),pDlg.mainWidget());
228 panel->refresh();
229 pDlg.addPolicyPanel(panel);
230 pDlg.refresh();
233 JSPolicies *JSDomainListView::createPolicies() {
234 return new JSPolicies(config,group,false);
237 JSPolicies *JSDomainListView::copyPolicies(Policies *pol) {
238 return new JSPolicies(*static_cast<JSPolicies *>(pol));
241 #include "jsopts.moc"