Debian Codegraph @

scripting/java/Framework/com/sun/star/script/framework/security/SecurityDialog.java

raw
1 /*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19 package com.sun.star.script.framework.security;
20
21 import com.sun.star.awt.ActionEvent;
22 import com.sun.star.awt.ItemEvent;
23 import com.sun.star.awt.XButton;
24 import com.sun.star.awt.XCheckBox;
25 import com.sun.star.awt.XControl;
26 import com.sun.star.awt.XControlContainer;
27 import com.sun.star.awt.XControlModel;
28 import com.sun.star.awt.XDialog;
29 import com.sun.star.awt.XToolkit;
30 import com.sun.star.awt.XWindow;
31
32 import com.sun.star.beans.XPropertySet;
33
34 import com.sun.star.comp.loader.FactoryHelper;
35
36 import com.sun.star.container.XNameContainer;
37
38 import com.sun.star.lang.EventObject;
39 import com.sun.star.lang.IllegalArgumentException;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.lang.XInitialization;
42 import com.sun.star.lang.XMultiComponentFactory;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.lang.XServiceInfo;
45 import com.sun.star.lang.XSingleServiceFactory;
46
47 import com.sun.star.lib.uno.helper.WeakBase;
48
49 import com.sun.star.registry.XRegistryKey;
50
51 import com.sun.star.script.framework.log.LogUtils;
52
53 import com.sun.star.uno.AnyConverter;
54 import com.sun.star.uno.RuntimeException;
55 import com.sun.star.uno.UnoRuntime;
56 import com.sun.star.uno.XComponentContext;
57
58 public class SecurityDialog extends WeakBase implements
59 XComponent, XServiceInfo, XDialog, XInitialization {
60
61 static final String __serviceName =
62 "com.sun.star.script.framework.security.SecurityDialog";
63
64 private static final String _label1Name = "Label1";
65
66 private static final String _label1String =
67 "This document contains macros. Do you want to allow these macros to be run?";
68
69 private static final String _label2Name = "Label2";
70
71 private static final String _label2String =
72 "This document contains macros. According to the security settings, the";
73
74 private static final String _label3Name = "Label3";
75
76 private static final String _label3String =
77 "macros in this document should not be run. Do you want to run them";
78
79 private static final String _label4Name = "Label4";
80 private static final String _label4String = "anyway?";
81
82 private static final String _checkBoxName = "CheckBox";
83
84 private static final String _checkBoxString =
85 "Add this directory to the list of secure paths: ";
86
87 private static final String _label5Name = "Label5";
88
89 private static final String _title = "Run Macro";
90 private static final String _runMacro = "Run";
91 private static final String _runButtonName = "Run";
92 private static final String _doNotRunMacro = "Do Not Run";
93 private static final String _doNotRunButtonName = "DoNotRun";
94
95 private static final int dialogX = 100;
96 private static final int dialogY = 100;
97 private static final int dialogW = 235;
98 private static final int dialogH = 47;
99
100 private int cbIncrW = -20;
101 private int cbIncrH = 19;
102
103 private static final int runButtonW = 40;
104 private static final int runButtonH = 13;
105 private static final int doNotRunButtonW = 40;
106 private static final int doNotRunButtonH = 13;
107
108 // label for warning dialog
109 private static final int label1X = 20;
110 private static final int label1Y = 9;
111 private static final int label1W = 210;
112 private static final int label1H = 10;
113
114 // labels for confirmation dialog
115 private static final int label2X = 22;
116 private static final int label2Y = 7;
117 private static final int label2W = 210;
118 private static final int label2H = 8;
119 private static final int label3X = 22;
120 private static final int label3Y = 15;
121 private static final int label3W = 210;
122 private static final int label3H = 8;
123 private static final int label4X = 22;
124 private static final int label4Y = 23;
125 private static final int label4W = 210;
126 private static final int label4H = 8;
127
128 // checkbox for confirmation dialog
129 private static final int checkBoxX = 22;
130 private static final int checkBoxY = 40;
131 private static final int checkBoxW = 210;
132 private static final int checkBoxH = 9;
133 // extra label if path longer than 21 chars
134 private static final int label5X = 22;
135 private static final int label5Y = 48;
136 private static final int label5W = 210;
137 private static final int label5H = 9;
138
139 private boolean checkBoxDialog;
140 private short _checkBoxState = 0;
141 private boolean extraPathLine = false;
142 private String checkBoxPath = "";
143 private String checkBoxPath2 = "";
144 private static final int lineWrapLength = 21;
145 private static final int lineWrapH = 12;
146 private String _pushed = _doNotRunButtonName;
147
148 private final XComponentContext _xComponentContext;
149 private XDialog _xDialog;
150
151 public SecurityDialog(XComponentContext xComponentContext) {
152 LogUtils.DEBUG("SecurityDialog ctor");
153 _xComponentContext = xComponentContext;
154 }
155
156 public void initialize(Object[] args) throws RuntimeException {
157 LogUtils.DEBUG("SecurityDialog init");
158
159 // figure out if we need a checkbox
160 if (args.length == 1 && AnyConverter.isString(args[0])) {
161 //check args is a path
162 // set checkBoxPath with the arg
163 LogUtils.DEBUG("checkbox");
164
165 try {
166 checkBoxPath = AnyConverter.toString(args[0]);
167 } catch (IllegalArgumentException e) {
168 throw new RuntimeException(e);
169 }
170
171 LogUtils.DEBUG("path: " + checkBoxPath);
172 checkBoxDialog = true;
173
174 if (checkBoxPath.length() > lineWrapLength) {
175 extraPathLine = true;
176 cbIncrH += lineWrapH;
177 checkBoxPath2 = checkBoxPath.substring(lineWrapLength);
178 checkBoxPath = checkBoxPath.substring(0, lineWrapLength);
179 }
180
181 } else {
182 LogUtils.DEBUG("no checkbox: # of args=" + args.length);
183 cbIncrW = 0;
184 cbIncrH = 0;
185 checkBoxDialog = false;
186 }
187
188 // now try and create the dialog
189 try {
190 _xDialog = createDialog();
191 } catch (com.sun.star.uno.Exception e) {
192 throw new RuntimeException(e);
193 } catch (Exception e) {
194 throw new RuntimeException(e);
195 }
196
197 }
198
199 // static component operations
200 public static XSingleServiceFactory __getServiceFactory(String implName,
201 XMultiServiceFactory multiFactory, XRegistryKey regKey) {
202
203 XSingleServiceFactory xSingleServiceFactory = null;
204
205 if (implName.equals(SecurityDialog.class.getName())) {
206
207 xSingleServiceFactory =
208 FactoryHelper.getServiceFactory(SecurityDialog.class,
209 SecurityDialog.__serviceName,
210 multiFactory, regKey);
211
212 }
213
214 return xSingleServiceFactory;
215 }
216
217 // XServiceInfo
218 public String getImplementationName() {
219 return getClass().getName();
220 }
221
222 // XServiceInfo
223 public boolean supportsService(/*IN*/String serviceName) {
224 return serviceName.equals(__serviceName);
225 }
226
227 // XServiceInfo
228 public String[] getSupportedServiceNames() {
229 String[] retValue = new String[1];
230 retValue[0] = __serviceName;
231 return retValue;
232 }
233
234 /** method for creating a dialog at runtime
235 */
236 private XDialog createDialog() throws com.sun.star.uno.Exception {
237
238 // get the service manager from the component context
239 XMultiComponentFactory xMultiComponentFactory =
240 _xComponentContext.getServiceManager();
241
242 // create the dialog model and set the properties
243 Object dialogModel =
244 xMultiComponentFactory.createInstanceWithContext(
245 "com.sun.star.awt.UnoControlDialogModel", _xComponentContext);
246
247 XPropertySet xPSetDialog =
248 UnoRuntime.queryInterface(XPropertySet.class, dialogModel);
249
250 xPSetDialog.setPropertyValue("PositionX", Integer.valueOf(dialogX));
251 xPSetDialog.setPropertyValue("PositionY", Integer.valueOf(dialogY));
252 xPSetDialog.setPropertyValue("Width", Integer.valueOf(dialogW + cbIncrW));
253 xPSetDialog.setPropertyValue("Height", Integer.valueOf(dialogH + cbIncrH));
254 xPSetDialog.setPropertyValue("Title", _title);
255
256 // get the service manager from the dialog model
257 XMultiServiceFactory xMultiServiceFactory =
258 UnoRuntime.queryInterface(XMultiServiceFactory.class, dialogModel);
259
260 // create the Run Macro button model and set the properties
261 Object runButtonModel =
262 xMultiServiceFactory.createInstance(
263 "com.sun.star.awt.UnoControlButtonModel");
264
265 XPropertySet xPSetButton =
266 UnoRuntime.queryInterface(XPropertySet.class, runButtonModel);
267
268 LogUtils.DEBUG("run: x=" + (((dialogW + cbIncrW) / 2) - runButtonW - 1));
269 LogUtils.DEBUG("run: y=" + (dialogH + cbIncrH - runButtonH - 1));
270
271 xPSetButton.setPropertyValue(
272 "PositionX", Integer.valueOf((((dialogW + cbIncrW) / 2) - runButtonW - 1)));
273
274 xPSetButton.setPropertyValue(
275 "PositionY", Integer.valueOf(dialogH + cbIncrH - runButtonH - 1));
276
277 xPSetButton.setPropertyValue("Width", Integer.valueOf(runButtonW));
278 xPSetButton.setPropertyValue("Height", Integer.valueOf(runButtonH));
279 xPSetButton.setPropertyValue("Name", _runButtonName);
280 xPSetButton.setPropertyValue("TabIndex", Short.valueOf((short)1));
281 xPSetButton.setPropertyValue("Label", _runMacro);
282
283 // create the Don't Run Macro button model and set the properties
284 Object doNotRunButtonModel =
285 xMultiServiceFactory.createInstance(
286 "com.sun.star.awt.UnoControlButtonModel");
287
288 xPSetButton =
289 UnoRuntime.queryInterface(XPropertySet.class, doNotRunButtonModel);
290
291 LogUtils.DEBUG("dontrun: x=" + (((dialogW + cbIncrW) / 2) - 1));
292 LogUtils.DEBUG("dontrun: y=" + (dialogH + cbIncrH - doNotRunButtonH - 1));
293
294 xPSetButton.setPropertyValue(
295 "PositionX", Integer.valueOf((((dialogW + cbIncrW) / 2) + 1)));
296
297 xPSetButton.setPropertyValue(
298 "PositionY", Integer.valueOf((dialogH + cbIncrH - doNotRunButtonH - 1)));
299
300 xPSetButton.setPropertyValue("Width", Integer.valueOf(doNotRunButtonW));
301 xPSetButton.setPropertyValue("Height", Integer.valueOf(doNotRunButtonH));
302 xPSetButton.setPropertyValue("Name", _doNotRunButtonName);
303 xPSetButton.setPropertyValue("TabIndex", Short.valueOf((short)0));
304 xPSetButton.setPropertyValue("Label", _doNotRunMacro);
305
306 // insert the control models into the dialog model
307 XNameContainer xNameCont =
308 UnoRuntime.queryInterface(XNameContainer.class, dialogModel);
309
310 xNameCont.insertByName(_runButtonName, runButtonModel);
311 xNameCont.insertByName(_doNotRunButtonName, doNotRunButtonModel);
312
313 if (checkBoxDialog) {
314 LogUtils.DEBUG("creating label & checkbox");
315
316 // create the label model and set the properties
317 Object label2Model =
318 xMultiServiceFactory.createInstance(
319 "com.sun.star.awt.UnoControlFixedTextModel");
320
321 XPropertySet xPSetLabel =
322 UnoRuntime.queryInterface(XPropertySet.class, label2Model);
323
324 xPSetLabel.setPropertyValue("PositionX", Integer.valueOf(label2X));
325 xPSetLabel.setPropertyValue("PositionY", Integer.valueOf(label2Y));
326 xPSetLabel.setPropertyValue("Width", Integer.valueOf(label2W));
327 xPSetLabel.setPropertyValue("Height", Integer.valueOf(label2H));
328 xPSetLabel.setPropertyValue("Name", _label2Name);
329 xPSetLabel.setPropertyValue("TabIndex", Short.valueOf((short)1));
330 xPSetLabel.setPropertyValue("Label", _label2String);
331
332 // create the label model and set the properties
333 Object label3Model =
334 xMultiServiceFactory.createInstance(
335 "com.sun.star.awt.UnoControlFixedTextModel");
336
337 XPropertySet xPSetLabel3 =
338 UnoRuntime.queryInterface(XPropertySet.class, label3Model);
339
340 xPSetLabel3.setPropertyValue("PositionX", Integer.valueOf(label3X));
341 xPSetLabel3.setPropertyValue("PositionY", Integer.valueOf(label3Y));
342 xPSetLabel3.setPropertyValue("Width", Integer.valueOf(label3W));
343 xPSetLabel3.setPropertyValue("Height", Integer.valueOf(label3H));
344 xPSetLabel3.setPropertyValue("Name", _label3Name);
345 xPSetLabel3.setPropertyValue("TabIndex", Short.valueOf((short)1));
346 xPSetLabel3.setPropertyValue("Label", _label3String);
347
348 // create the label model and set the properties
349 Object label4Model =
350 xMultiServiceFactory.createInstance(
351 "com.sun.star.awt.UnoControlFixedTextModel");
352
353 XPropertySet xPSetLabel4 =
354 UnoRuntime.queryInterface(XPropertySet.class, label4Model);
355
356 xPSetLabel4.setPropertyValue("PositionX", Integer.valueOf(label4X));
357 xPSetLabel4.setPropertyValue("PositionY", Integer.valueOf(label4Y));
358 xPSetLabel4.setPropertyValue("Width", Integer.valueOf(label4W));
359 xPSetLabel4.setPropertyValue("Height", Integer.valueOf(label4H));
360 xPSetLabel4.setPropertyValue("Name", _label4Name);
361 xPSetLabel4.setPropertyValue("TabIndex", Short.valueOf((short)1));
362 xPSetLabel4.setPropertyValue("Label", _label4String);
363
364 // create the checkbox model and set the properties
365 Object checkBoxModel =
366 xMultiServiceFactory.createInstance(
367 "com.sun.star.awt.UnoControlCheckBoxModel");
368
369 XPropertySet xPSetCheckBox =
370 UnoRuntime.queryInterface(XPropertySet.class, checkBoxModel);
371
372 xPSetCheckBox.setPropertyValue("PositionX", Integer.valueOf(checkBoxX));
373 xPSetCheckBox.setPropertyValue("PositionY", Integer.valueOf(checkBoxY));
374 xPSetCheckBox.setPropertyValue("Width", Integer.valueOf(checkBoxW));
375 xPSetCheckBox.setPropertyValue("Height", Integer.valueOf(checkBoxH));
376 xPSetCheckBox.setPropertyValue("State", Short.valueOf((short)0));
377 xPSetCheckBox.setPropertyValue("Name", _checkBoxName);
378 xPSetCheckBox.setPropertyValue("TabIndex", Short.valueOf((short)1));
379 xPSetCheckBox.setPropertyValue("Label", _checkBoxString + checkBoxPath);
380
381 // insert the control models into the dialog model
382 xNameCont.insertByName(_label2Name, label2Model);
383 xNameCont.insertByName(_label3Name, label3Model);
384 xNameCont.insertByName(_label4Name, label4Model);
385 xNameCont.insertByName(_checkBoxName, checkBoxModel);
386
387 if (extraPathLine) {
388
389 // create the label model and set the properties
390 Object label5Model =
391 xMultiServiceFactory.createInstance(
392 "com.sun.star.awt.UnoControlFixedTextModel");
393
394 XPropertySet xPSetLabel5 =
395 UnoRuntime.queryInterface(XPropertySet.class, label5Model);
396
397 xPSetLabel5.setPropertyValue("PositionX", Integer.valueOf(label5X));
398 xPSetLabel5.setPropertyValue("PositionY", Integer.valueOf(label5Y));
399 xPSetLabel5.setPropertyValue("Width", Integer.valueOf(label5W));
400 xPSetLabel5.setPropertyValue("Height", Integer.valueOf(label5H));
401 xPSetLabel5.setPropertyValue("Name", _label5Name);
402 xPSetLabel5.setPropertyValue("TabIndex", Short.valueOf((short)1));
403 xPSetLabel5.setPropertyValue("Label", checkBoxPath2);
404 xNameCont.insertByName(_label5Name, label5Model);
405 }
406 } else {
407 // create the label model and set the properties
408 Object labelModel =
409 xMultiServiceFactory.createInstance(
410 "com.sun.star.awt.UnoControlFixedTextModel");
411
412 XPropertySet xPSetLabel =
413 UnoRuntime.queryInterface(XPropertySet.class, labelModel);
414
415 xPSetLabel.setPropertyValue("PositionX", Integer.valueOf(label1X));
416 xPSetLabel.setPropertyValue("PositionY", Integer.valueOf(label1Y));
417 xPSetLabel.setPropertyValue("Width", Integer.valueOf(label1W));
418 xPSetLabel.setPropertyValue("Height", Integer.valueOf(label1H));
419 xPSetLabel.setPropertyValue("Name", _label1Name);
420 xPSetLabel.setPropertyValue("TabIndex", Short.valueOf((short)1));
421 xPSetLabel.setPropertyValue("Label", _label1String);
422
423 // insert the control models into the dialog model
424 xNameCont.insertByName(_label1Name, labelModel);
425 }
426
427 // create the dialog control and set the model
428 Object dialog =
429 xMultiComponentFactory.createInstanceWithContext(
430 "com.sun.star.awt.UnoControlDialog", _xComponentContext);
431
432 XControl xControl =
433 UnoRuntime.queryInterface(XControl.class, dialog);
434
435 XControlModel xControlModel =
436 UnoRuntime.queryInterface(XControlModel.class, dialogModel);
437
438 xControl.setModel(xControlModel);
439
440 // add an action listener to the button control
441 XControlContainer xControlCont =
442 UnoRuntime.queryInterface(XControlContainer.class, dialog);
443
444 // Add to yes button
445 Object objectButton = xControlCont.getControl(_runButtonName);
446
447 XButton xButton =
448 UnoRuntime.queryInterface(XButton.class, objectButton);
449
450 xButton.addActionListener(new ActionListenerImpl(_runButtonName));
451
452 // add to no button
453 objectButton = xControlCont.getControl(_doNotRunButtonName);
454
455 xButton =
456 UnoRuntime.queryInterface(XButton.class, objectButton);
457
458 xButton.addActionListener(new ActionListenerImpl(_doNotRunButtonName));
459
460 if (checkBoxDialog) {
461 // add to checkbox
462 Object objectCheckBox = xControlCont.getControl(_checkBoxName);
463
464 XCheckBox xCheckBox =
465 UnoRuntime.queryInterface(XCheckBox.class, objectCheckBox);
466
467 xCheckBox.addItemListener(new ItemListenerImpl(xControlCont));
468 }
469
470 // create a peer
471 Object toolkit =
472 xMultiComponentFactory.createInstanceWithContext(
473 "com.sun.star.awt.ExtToolkit", _xComponentContext);
474
475 XToolkit xToolkit = UnoRuntime.queryInterface(XToolkit.class, toolkit);
476 XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, xControl);
477 xWindow.setVisible(false);
478 xControl.createPeer(xToolkit, null);
479
480 // return the dialog
481 XDialog xDialog = UnoRuntime.queryInterface(XDialog.class, dialog);
482 return xDialog;
483 }
484
485 public short execute() {
486
487 short result = 0;
488 _pushed = _doNotRunButtonName;
489 LogUtils.DEBUG("*DF* Before execute ");
490 _xDialog.execute();
491 LogUtils.DEBUG("*DF* After execute ");
492
493 if (_pushed.equals(_runButtonName)) {
494 result += 1;
495 }
496
497 if (_checkBoxState == 1) {
498 result += 2;
499 }
500
501 return result;
502 }
503
504 public void endExecute() {
505 _xDialog.endExecute();
506 }
507
508 public String getTitle() {
509 return _xDialog.getTitle();
510 }
511
512 public void setTitle(String Title) {
513 _xDialog.setTitle(Title);
514 }
515
516 public void dispose() {
517
518 if (_xDialog != null) {
519 XComponent xComponent =
520 UnoRuntime.queryInterface(XComponent.class, _xDialog);
521
522 xComponent.dispose();
523 }
524 }
525
526 public void addEventListener(com.sun.star.lang.XEventListener xListener) {
527
528 XComponent xComponent =
529 UnoRuntime.queryInterface(XComponent.class, _xDialog);
530
531 xComponent.addEventListener(xListener);
532 }
533
534 public void removeEventListener(com.sun.star.lang.XEventListener aListener) {
535
536 XComponent xComponent =
537 UnoRuntime.queryInterface(XComponent.class, _xDialog);
538
539 xComponent.removeEventListener(aListener);
540 }
541
542 private class ActionListenerImpl implements com.sun.star.awt.XActionListener {
543
544 private final String _buttonName;
545
546 private ActionListenerImpl(String buttonName) {
547 _buttonName = buttonName;
548 }
549
550 // XEventListener
551 public void disposing(EventObject eventObject) {
552 }
553
554 // XActionListener
555 public void actionPerformed(ActionEvent actionEvent) {
556 _pushed = _buttonName;
557 LogUtils.DEBUG("** Button pushed ->" + _pushed);
558 _xDialog.endExecute();
559 }
560 }
561
562 private class ItemListenerImpl implements com.sun.star.awt.XItemListener {
563
564 private XCheckBox _xCheckBox;
565
566 private ItemListenerImpl(XControlContainer xControlCont) {
567 Object objectCheckBox = xControlCont.getControl(_checkBoxName);
568
569 _xCheckBox =
570 UnoRuntime.queryInterface(XCheckBox.class, objectCheckBox);
571
572 }
573
574 // XEventListener
575 public void disposing(EventObject eventObject) {
576 _xCheckBox = null;
577 }
578
579 // XAdjustmentListener
580 public void itemStateChanged(ItemEvent itemEvent) {
581 _checkBoxState = _xCheckBox.getState();
582 LogUtils.DEBUG("** checkbox state ->" + _checkBoxState);
583 }
584 }
585 }