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