Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / settings / konqhtml / jspolicies.cpp
blob1954ed54b0d0152ae76f1aa0426d021b3e9bb85c
1 /*
2 Copyright (c) 2002 Leo Savernik <l.savernik@aon.at>
3 Derived from jsopt.cpp, code copied from there is copyrighted to its
4 respective owners.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 // Own
23 #include "jspolicies.h"
25 // Qt
26 #include <QButtonGroup>
27 #include <QtGui/QLabel>
28 #include <QtGui/QLayout>
29 #include <QtGui/QRadioButton>
30 #include <QtGui/QGridLayout>
32 // KDE
33 #include <kconfig.h>
34 #include <kconfiggroup.h>
35 #include <kdebug.h>
36 #include <klocale.h>
39 // == class JSPolicies ==
41 JSPolicies::JSPolicies(KSharedConfig::Ptr config,const QString &group,
42 bool global,const QString &domain) :
43 Policies(config,group,global,domain,"javascript.","EnableJavaScript") {
47 JSPolicies::JSPolicies() : Policies(0,QString(),false,
48 QString(),QString(),QString()) {
52 JSPolicies::~JSPolicies() {
55 void JSPolicies::load() {
56 Policies::load();
58 KConfigGroup cg(config, groupname);
59 QString key;
61 // enableJavaScriptDebugCB->setChecked( m_pConfig->readEntry("EnableJavaScriptDebug", QVariant(false)).toBool());
62 // enableDebugOutputCB->setChecked( m_pConfig->readEntry("EnableJSDebugOutput", QVariant(false)).toBool() );
63 key = prefix + "WindowOpenPolicy";
64 window_open = cg.readEntry(key,
65 is_global ? KHTMLSettings::KJSWindowOpenSmart : INHERIT_POLICY);
67 key = prefix + "WindowResizePolicy";
68 window_resize = cg.readEntry(key,
69 is_global ? KHTMLSettings::KJSWindowResizeAllow : INHERIT_POLICY);
71 key = prefix + "WindowMovePolicy";
72 window_move = cg.readEntry(key,
73 is_global ? KHTMLSettings::KJSWindowMoveAllow : INHERIT_POLICY);
75 key = prefix + "WindowFocusPolicy";
76 window_focus = cg.readEntry(key,
77 is_global ? KHTMLSettings::KJSWindowFocusAllow : INHERIT_POLICY);
79 key = prefix + "WindowStatusPolicy";
80 window_status = cg.readEntry(key,
81 is_global ? KHTMLSettings::KJSWindowStatusAllow : INHERIT_POLICY);
84 void JSPolicies::defaults() {
85 Policies::defaults();
86 // enableJavaScriptGloballyCB->setChecked( true );
87 // enableJavaScriptDebugCB->setChecked( false );
88 // js_popup->setButton(0);
89 // enableDebugOutputCB->setChecked( false );
90 window_open = is_global ? KHTMLSettings::KJSWindowOpenSmart : INHERIT_POLICY;
91 window_resize = is_global ? KHTMLSettings::KJSWindowResizeAllow : INHERIT_POLICY;
92 window_move = is_global ? KHTMLSettings::KJSWindowMoveAllow : INHERIT_POLICY;
93 window_focus = is_global ? KHTMLSettings::KJSWindowFocusAllow : INHERIT_POLICY;
94 window_status = is_global ? KHTMLSettings::KJSWindowStatusAllow : INHERIT_POLICY;
97 void JSPolicies::save() {
98 Policies::save();
100 QString key;
101 key = prefix + "WindowOpenPolicy";
102 if (window_open != INHERIT_POLICY)
103 config->group(groupname).writeEntry(key, window_open);
104 else
105 config->group(groupname).deleteEntry(key);
107 key = prefix + "WindowResizePolicy";
108 if (window_resize != INHERIT_POLICY)
109 config->group(groupname).writeEntry(key, window_resize);
110 else
111 config->group(groupname).deleteEntry(key);
113 key = prefix + "WindowMovePolicy";
114 if (window_move != INHERIT_POLICY)
115 config->group(groupname).writeEntry(key, window_move);
116 else
117 config->group(groupname).deleteEntry(key);
119 key = prefix + "WindowFocusPolicy";
120 if (window_focus != INHERIT_POLICY)
121 config->group(groupname).writeEntry(key, window_focus);
122 else
123 config->group(groupname).deleteEntry(key);
125 key = prefix + "WindowStatusPolicy";
126 if (window_status != INHERIT_POLICY)
127 config->group(groupname).writeEntry(key, window_status);
128 else
129 config->group(groupname).deleteEntry(key);
131 // don't do a config->sync() here for sake of efficiency
134 // == class JSPoliciesFrame ==
136 JSPoliciesFrame::JSPoliciesFrame(JSPolicies *policies, const QString &title,
137 QWidget* parent) :
138 QGroupBox(title, parent),
139 policies(policies) {
141 bool is_per_domain = !policies->isGlobal();
143 QGridLayout *this_layout = new QGridLayout();
144 setLayout(this_layout);
145 this_layout->setAlignment(Qt::AlignTop);
146 this_layout->setSpacing(3);
147 this_layout->setMargin(11);
149 QString wtstr; // what's this description
150 int colIdx; // column index
152 // === window.open ================================
153 colIdx = 0;
154 QLabel *label = new QLabel(i18n("Open new windows:"),this);
155 this_layout->addWidget(label,0,colIdx++);
157 js_popup = new QButtonGroup(this);
158 js_popup->setExclusive(true);
160 QRadioButton* policy_btn;
161 if (is_per_domain) {
162 policy_btn = new QRadioButton(i18n("Use global"), this);
163 policy_btn->setWhatsThis(i18n("Use setting from global policy."));
164 js_popup->addButton(policy_btn,INHERIT_POLICY);
165 this_layout->addWidget(policy_btn,0,colIdx++);
166 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
167 }/*end if*/
169 policy_btn = new QRadioButton(i18n("Allow"), this);
170 policy_btn->setWhatsThis(i18n("Accept all popup window requests."));
171 js_popup->addButton(policy_btn,KHTMLSettings::KJSWindowOpenAllow);
172 this_layout->addWidget(policy_btn,0,colIdx++);
173 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
175 policy_btn = new QRadioButton(i18n("Ask"), this);
176 policy_btn->setWhatsThis(i18n("Prompt every time a popup window is requested."));
177 js_popup->addButton(policy_btn,KHTMLSettings::KJSWindowOpenAsk);
178 this_layout->addWidget(policy_btn,0,colIdx++);
179 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
181 policy_btn = new QRadioButton(i18n("Deny"), this);
182 policy_btn->setWhatsThis(i18n("Reject all popup window requests."));
183 js_popup->addButton(policy_btn,KHTMLSettings::KJSWindowOpenDeny);
184 this_layout->addWidget(policy_btn,0,colIdx++);
185 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
187 policy_btn = new QRadioButton(i18n("Smart"), this);
188 policy_btn->setWhatsThis( i18n("Accept popup window requests only when "
189 "links are activated through an explicit "
190 "mouse click or keyboard operation."));
191 js_popup->addButton(policy_btn,KHTMLSettings::KJSWindowOpenSmart);
192 this_layout->addWidget(policy_btn,0,colIdx++);
193 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
195 wtstr = i18n("If you disable this, Konqueror will stop "
196 "interpreting the <i>window.open()</i> "
197 "JavaScript command. This is useful if you "
198 "regularly visit sites that make extensive use "
199 "of this command to pop up ad banners.<br />"
200 "<br /><b>Note:</b> Disabling this option might "
201 "also break certain sites that require <i>"
202 "window.open()</i> for proper operation. Use "
203 "this feature carefully.");
204 label->setWhatsThis( wtstr);
205 connect(js_popup, SIGNAL(buttonClicked(int)), SLOT(setWindowOpenPolicy(int)));
207 // === window.resizeBy/resizeTo ================================
208 colIdx = 0;
209 label = new QLabel(i18n("Resize window:"),this);
210 this_layout->addWidget(label,1,colIdx++);
212 js_resize = new QButtonGroup(this);
213 js_resize->setExclusive(true);
215 if (is_per_domain) {
216 policy_btn = new QRadioButton(i18n("Use global"), this);
217 policy_btn->setWhatsThis(i18n("Use setting from global policy."));
218 js_resize->addButton(policy_btn,INHERIT_POLICY);
219 this_layout->addWidget(policy_btn,1,colIdx++);
220 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
221 }/*end if*/
223 policy_btn = new QRadioButton(i18n("Allow"), this);
224 policy_btn->setWhatsThis(i18n("Allow scripts to change the window size."));
225 js_resize->addButton(policy_btn,KHTMLSettings::KJSWindowResizeAllow);
226 this_layout->addWidget(policy_btn,1,colIdx++);
227 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
229 policy_btn = new QRadioButton(i18n("Ignore"), this);
230 policy_btn->setWhatsThis(i18n("Ignore attempts of scripts to change the window size. "
231 "The web page will <i>think</i> it changed the "
232 "size but the actual window is not affected."));
233 js_resize->addButton(policy_btn,KHTMLSettings::KJSWindowResizeIgnore);
234 this_layout->addWidget(policy_btn,1,colIdx++);
235 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
237 wtstr = i18n("Some websites change the window size on their own by using "
238 "<i>window.resizeBy()</i> or <i>window.resizeTo()</i>. "
239 "This option specifies the treatment of such "
240 "attempts.");
241 label->setWhatsThis( wtstr);
242 connect(js_resize, SIGNAL(buttonClicked(int)), SLOT(setWindowResizePolicy(int)));
244 // === window.moveBy/moveTo ================================
245 colIdx = 0;
246 label = new QLabel(i18n("Move window:"),this);
247 this_layout->addWidget(label,2,colIdx++);
249 js_move = new QButtonGroup(this);
250 js_move->setExclusive(true);
252 if (is_per_domain) {
253 policy_btn = new QRadioButton(i18n("Use global"), this);
254 policy_btn->setWhatsThis(i18n("Use setting from global policy."));
255 js_move->addButton(policy_btn,INHERIT_POLICY);
256 this_layout->addWidget(policy_btn,2,colIdx++);
257 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
258 }/*end if*/
260 policy_btn = new QRadioButton(i18n("Allow"), this);
261 policy_btn->setWhatsThis(i18n("Allow scripts to change the window position."));
262 js_move->addButton(policy_btn,KHTMLSettings::KJSWindowMoveAllow);
263 this_layout->addWidget(policy_btn,2,colIdx++);
264 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
266 policy_btn = new QRadioButton(i18n("Ignore"), this);
267 policy_btn->setWhatsThis(i18n("Ignore attempts of scripts to change the window position. "
268 "The web page will <i>think</i> it moved the "
269 "window but the actual position is not affected."));
270 js_move->addButton(policy_btn,KHTMLSettings::KJSWindowMoveIgnore);
271 this_layout->addWidget(policy_btn,2,colIdx++);
272 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
274 wtstr = i18n("Some websites change the window position on their own by using "
275 "<i>window.moveBy()</i> or <i>window.moveTo()</i>. "
276 "This option specifies the treatment of such "
277 "attempts.");
278 label->setWhatsThis( wtstr);
279 connect(js_move, SIGNAL(buttonClicked(int)), SLOT(setWindowMovePolicy(int)));
281 // === window.focus ================================
282 colIdx = 0;
283 label = new QLabel(i18n("Focus window:"),this);
284 this_layout->addWidget(label,3,colIdx++);
286 js_focus = new QButtonGroup(this);
287 js_focus->setExclusive(true);
289 if (is_per_domain) {
290 policy_btn = new QRadioButton(i18n("Use global"), this);
291 policy_btn->setWhatsThis(i18n("Use setting from global policy."));
292 js_focus->addButton(policy_btn,INHERIT_POLICY);
293 this_layout->addWidget(policy_btn,3,colIdx++);
294 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
295 }/*end if*/
297 policy_btn = new QRadioButton(i18n("Allow"), this);
298 policy_btn->setWhatsThis(i18n("Allow scripts to focus the window.") );
299 js_focus->addButton(policy_btn,KHTMLSettings::KJSWindowFocusAllow);
300 this_layout->addWidget(policy_btn,3,colIdx++);
301 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
303 policy_btn = new QRadioButton(i18n("Ignore"), this);
304 policy_btn->setWhatsThis(i18n("Ignore attempts of scripts to focus the window. "
305 "The web page will <i>think</i> it brought "
306 "the focus to the window but the actual "
307 "focus will remain unchanged.") );
308 js_focus->addButton(policy_btn,KHTMLSettings::KJSWindowFocusIgnore);
309 this_layout->addWidget(policy_btn,3,colIdx++);
310 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
312 wtstr = i18n("Some websites set the focus to their browser window on their "
313 "own by using <i>window.focus()</i>. This usually leads to "
314 "the window being moved to the front interrupting whatever "
315 "action the user was dedicated to at that time. "
316 "This option specifies the treatment of such "
317 "attempts.");
318 label->setWhatsThis( wtstr);
319 connect(js_focus, SIGNAL(buttonClicked(int)), SLOT(setWindowFocusPolicy(int)));
321 // === window.status ================================
322 colIdx = 0;
323 label = new QLabel(i18n("Modify status bar text:"),this);
324 this_layout->addWidget(label,4,colIdx++);
326 js_statusbar = new QButtonGroup(this);
327 js_statusbar->setExclusive(true);
329 if (is_per_domain) {
330 policy_btn = new QRadioButton(i18n("Use global"), this);
331 policy_btn->setWhatsThis(i18n("Use setting from global policy."));
332 js_statusbar->addButton(policy_btn,INHERIT_POLICY);
333 this_layout->addWidget(policy_btn,4,colIdx++);
334 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
335 }/*end if*/
337 policy_btn = new QRadioButton(i18n("Allow"), this);
338 policy_btn->setWhatsThis(i18n("Allow scripts to change the text of the status bar."));
339 js_statusbar->addButton(policy_btn,KHTMLSettings::KJSWindowStatusAllow);
340 this_layout->addWidget(policy_btn,4,colIdx++);
341 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
343 policy_btn = new QRadioButton(i18n("Ignore"), this);
344 policy_btn->setWhatsThis(i18n("Ignore attempts of scripts to change the status bar text. "
345 "The web page will <i>think</i> it changed "
346 "the text but the actual text will remain "
347 "unchanged.") );
348 js_statusbar->addButton(policy_btn,KHTMLSettings::KJSWindowStatusIgnore);
349 this_layout->addWidget(policy_btn,4,colIdx++);
350 this_layout->addItem(new QSpacerItem(10,0),0,colIdx++);
352 wtstr = i18n("Some websites change the status bar text by setting "
353 "<i>window.status</i> or <i>window.defaultStatus</i>, "
354 "thus sometimes preventing displaying the real URLs of hyperlinks. "
355 "This option specifies the treatment of such "
356 "attempts.");
357 label->setWhatsThis( wtstr);
358 connect(js_statusbar, SIGNAL(buttonClicked(int)), SLOT(setWindowStatusPolicy(int)));
361 JSPoliciesFrame::~JSPoliciesFrame() {
364 void JSPoliciesFrame::refresh() {
365 QRadioButton *button;
366 button = static_cast<QRadioButton *>(js_popup->button(
367 policies->window_open));
368 if (button != 0) button->setChecked(true);
369 button = static_cast<QRadioButton *>(js_resize->button(
370 policies->window_resize));
371 if (button != 0) button->setChecked(true);
372 button = static_cast<QRadioButton *>(js_move->button(
373 policies->window_move));
374 if (button != 0) button->setChecked(true);
375 button = static_cast<QRadioButton *>(js_focus->button(
376 policies->window_focus));
377 if (button != 0) button->setChecked(true);
378 button = static_cast<QRadioButton *>(js_statusbar->button(
379 policies->window_status));
380 if (button != 0) button->setChecked(true);
383 void JSPoliciesFrame::setWindowOpenPolicy(int id) {
384 policies->window_open = id;
385 emit changed();
388 void JSPoliciesFrame::setWindowResizePolicy(int id) {
389 policies->window_resize = id;
390 emit changed();
393 void JSPoliciesFrame::setWindowMovePolicy(int id) {
394 policies->window_move = id;
395 emit changed();
398 void JSPoliciesFrame::setWindowFocusPolicy(int id) {
399 policies->window_focus = id;
400 emit changed();
403 void JSPoliciesFrame::setWindowStatusPolicy(int id) {
404 policies->window_status = id;
405 emit changed();
408 #include "jspolicies.moc"