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