LCOV - code coverage report
Current view: top level - libreoffice/cui/source/dialogs - scriptdlg.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 48 733 6.5 %
Date: 2012-12-17 Functions: 10 55 18.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <memory>
      21             : 
      22             : #include <sfx2/objsh.hxx>
      23             : #include <vcl/svapp.hxx>
      24             : #include <vcl/msgbox.hxx>
      25             : #include <osl/mutex.hxx>
      26             : 
      27             : #include <cuires.hrc>
      28             : #include "scriptdlg.hrc"
      29             : #include "scriptdlg.hxx"
      30             : #include <dialmgr.hxx>
      31             : #include "selector.hxx"
      32             : 
      33             : #include <com/sun/star/uno/XComponentContext.hpp>
      34             : #include <com/sun/star/frame/XDesktop.hpp>
      35             : #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
      36             : #include <com/sun/star/script/provider/XScriptProvider.hpp>
      37             : #include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
      38             : #include <com/sun/star/script/browse/XBrowseNodeFactory.hpp>
      39             : #include <com/sun/star/script/browse/BrowseNodeFactoryViewTypes.hpp>
      40             : #include <com/sun/star/script/provider/ScriptErrorRaisedException.hpp>
      41             : #include <com/sun/star/script/provider/ScriptExceptionRaisedException.hpp>
      42             : #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
      43             : #include <com/sun/star/frame/ModuleManager.hpp>
      44             : #include <com/sun/star/script/XInvocation.hpp>
      45             : #include <com/sun/star/document/XEmbeddedScripts.hpp>
      46             : 
      47             : #include <cppuhelper/implbase1.hxx>
      48             : #include <comphelper/documentinfo.hxx>
      49             : #include <comphelper/uno3.hxx>
      50             : #include <comphelper/processfactory.hxx>
      51             : #include <comphelper/broadcasthelper.hxx>
      52             : #include <comphelper/propertycontainer.hxx>
      53             : #include <comphelper/proparrhlp.hxx>
      54             : 
      55             : #include <basic/sbx.hxx>
      56             : #include <svtools/imagemgr.hxx>
      57             : #include "svtools/treelistentry.hxx"
      58             : #include <tools/urlobj.hxx>
      59             : #include <vector>
      60             : #include <algorithm>
      61             : 
      62             : using namespace ::com::sun::star;
      63             : using namespace ::com::sun::star::uno;
      64             : using namespace ::com::sun::star::script;
      65             : using namespace ::com::sun::star::frame;
      66             : using namespace ::com::sun::star::document;
      67             : 
      68           0 : void ShowErrorDialog( const Any& aException )
      69             : {
      70           0 :     SvxScriptErrorDialog* pDlg = new SvxScriptErrorDialog( NULL, aException );
      71           0 :     pDlg->Execute();
      72           0 :     delete pDlg;
      73           0 : }
      74             : 
      75           0 : SFTreeListBox::SFTreeListBox(Window* pParent)
      76             :     : SvTreeListBox(pParent)
      77           0 :     , m_hdImage(CUI_RES(RID_CUIIMG_HARDDISK))
      78           0 :     , m_libImage(CUI_RES(RID_CUIIMG_LIB))
      79           0 :     , m_macImage(CUI_RES(RID_CUIIMG_MACRO))
      80           0 :     , m_docImage(CUI_RES(RID_CUIIMG_DOC))
      81           0 :     , m_sMyMacros(CUI_RESSTR(RID_SVXSTR_MYMACROS))
      82           0 :     , m_sProdMacros(CUI_RESSTR(RID_SVXSTR_PRODMACROS))
      83             : {
      84           0 :     SetSelectionMode( SINGLE_SELECTION );
      85             : 
      86           0 :     SetStyle( GetStyle() | WB_CLIPCHILDREN | WB_HSCROLL |
      87             :                    WB_HASBUTTONS | WB_HASBUTTONSATROOT | WB_HIDESELECTION |
      88           0 :                    WB_HASLINES | WB_HASLINESATROOT | WB_TABSTOP );
      89           0 :     SetNodeDefaultImages();
      90             : 
      91           0 :     nMode = 0xFF;    // everything
      92           0 : }
      93             : 
      94           0 : extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeSFTreeListBox(Window *pParent, VclBuilder::stringmap &)
      95             : {
      96           0 :     return new SFTreeListBox(pParent);
      97             : }
      98             : 
      99           0 : SFTreeListBox::~SFTreeListBox()
     100             : {
     101           0 :     deleteAllTree();
     102           0 : }
     103             : 
     104           0 : void SFTreeListBox::delUserData( SvTreeListEntry* pEntry )
     105             : {
     106           0 :     if ( pEntry )
     107             :     {
     108           0 :         SFEntry* pUserData = (SFEntry*)pEntry->GetUserData();
     109           0 :         if ( pUserData )
     110             :         {
     111           0 :             delete pUserData;
     112             :             // TBD seem to get a Select event on node that is remove ( below )
     113             :             // so need to be able to detect that this node is not to be
     114             :             // processed in order to do this, setting userData to NULL ( must
     115             :             // be a better way to do this )
     116           0 :             pUserData = 0;
     117           0 :             pEntry->SetUserData( pUserData );
     118             :         }
     119             :     }
     120           0 : }
     121             : 
     122           0 : void SFTreeListBox::deleteTree( SvTreeListEntry* pEntry )
     123             : {
     124             : 
     125           0 :     delUserData( pEntry );
     126           0 :     pEntry = FirstChild( pEntry );
     127           0 :     while ( pEntry )
     128             :     {
     129           0 :         SvTreeListEntry* pNextEntry = NextSibling( pEntry );
     130           0 :         deleteTree( pEntry );
     131           0 :         GetModel()->Remove( pEntry );
     132           0 :         pEntry = pNextEntry;
     133             :     }
     134           0 : }
     135             : 
     136           0 : void SFTreeListBox::deleteAllTree()
     137             : {
     138           0 :     SvTreeListEntry* pEntry =  GetEntry( 0 );
     139             : 
     140             :     // TBD - below is a candidate for a destroyAllTrees method
     141           0 :     if ( pEntry )
     142             :     {
     143           0 :         while ( pEntry )
     144             :         {
     145           0 :             SvTreeListEntry* pNextEntry = NextSibling( pEntry ) ;
     146           0 :             deleteTree( pEntry );
     147           0 :             GetModel()->Remove( pEntry );
     148           0 :             pEntry = pNextEntry;
     149             :         }
     150             :     }
     151           0 : }
     152             : 
     153           0 : void SFTreeListBox::Init( const ::rtl::OUString& language  )
     154             : {
     155           0 :     SetUpdateMode( sal_False );
     156             : 
     157           0 :     deleteAllTree();
     158             : 
     159           0 :     Reference< browse::XBrowseNode > rootNode;
     160             :     Reference< XComponentContext > xCtx(
     161           0 :         comphelper::getProcessComponentContext() );
     162             : 
     163           0 :     Sequence< Reference< browse::XBrowseNode > > children;
     164             : 
     165           0 :     ::rtl::OUString userStr( RTL_CONSTASCII_USTRINGPARAM("user") );
     166           0 :     ::rtl::OUString shareStr( RTL_CONSTASCII_USTRINGPARAM("share") );
     167             : 
     168           0 :     ::rtl::OUString singleton( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.script.browse.theBrowseNodeFactory" ) );
     169             : 
     170             :     try
     171             :     {
     172             :         Reference< browse::XBrowseNodeFactory > xFac(
     173           0 :             xCtx->getValueByName( singleton ), UNO_QUERY_THROW );
     174             : 
     175           0 :         rootNode.set( xFac->createView(
     176           0 :             browse::BrowseNodeFactoryViewTypes::MACROORGANIZER ) );
     177             : 
     178           0 :         if (  rootNode.is() && rootNode->hasChildNodes() == sal_True )
     179             :         {
     180           0 :             children = rootNode->getChildNodes();
     181           0 :         }
     182             :     }
     183           0 :     catch( Exception& e )
     184             :     {
     185             :         OSL_TRACE("Exception getting root browse node from factory: %s",
     186             :             ::rtl::OUStringToOString(
     187             :                 e.Message , RTL_TEXTENCODING_ASCII_US ).pData->buffer );
     188             :         // TODO exception handling
     189             :     }
     190             : 
     191           0 :     Reference<XModel> xDocumentModel;
     192           0 :     for ( sal_Int32 n = 0; n < children.getLength(); n++ )
     193             :     {
     194           0 :         bool app = false;
     195           0 :         ::rtl::OUString uiName = children[ n ]->getName();
     196           0 :         ::rtl::OUString factoryURL;
     197           0 :         if ( uiName.equals( userStr ) || uiName.equals( shareStr ) )
     198             :         {
     199           0 :             app = true;
     200           0 :             if ( uiName.equals( userStr ) )
     201             :             {
     202           0 :                 uiName = m_sMyMacros;
     203             :             }
     204             :             else
     205             :             {
     206           0 :                 uiName = m_sProdMacros;
     207             :             }
     208             :         }
     209             :         else
     210             :         {
     211           0 :             xDocumentModel.set(getDocumentModel(xCtx, uiName ), UNO_QUERY);
     212             : 
     213           0 :             if ( xDocumentModel.is() )
     214             :             {
     215           0 :                 Reference< frame::XModuleManager2 > xModuleManager( frame::ModuleManager::create(xCtx) );
     216             : 
     217             :                 // get the long name of the document:
     218           0 :                 Sequence<beans::PropertyValue> moduleDescr;
     219             :                 try{
     220           0 :                     ::rtl::OUString appModule = xModuleManager->identify( xDocumentModel );
     221           0 :                     xModuleManager->getByName(appModule) >>= moduleDescr;
     222           0 :                 } catch(const uno::Exception&)
     223             :                     {}
     224             : 
     225             :                 beans::PropertyValue const * pmoduleDescr =
     226           0 :                     moduleDescr.getConstArray();
     227           0 :                 for ( sal_Int32 pos = moduleDescr.getLength(); pos--; )
     228             :                 {
     229           0 :                     if ( pmoduleDescr[ pos ].Name == "ooSetupFactoryEmptyDocumentURL" )
     230             :                     {
     231           0 :                         pmoduleDescr[ pos ].Value >>= factoryURL;
     232           0 :                         break;
     233             :                     }
     234           0 :                 }
     235             :             }
     236             :         }
     237             : 
     238           0 :         ::rtl::OUString lang( language );
     239             :         Reference< browse::XBrowseNode > langEntries =
     240           0 :             getLangNodeFromRootNode( children[ n ], lang );
     241             : 
     242             :         SAL_WNODEPRECATED_DECLARATIONS_PUSH
     243             :         insertEntry( uiName, app ? RID_CUIIMG_HARDDISK : RID_CUIIMG_DOC,
     244           0 :             0, true, std::auto_ptr< SFEntry >(new SFEntry( OBJTYPE_SFROOT, langEntries, xDocumentModel )), factoryURL );
     245             :         SAL_WNODEPRECATED_DECLARATIONS_POP
     246           0 :     }
     247             : 
     248           0 :     SetUpdateMode( sal_True );
     249           0 : }
     250             : 
     251             : Reference< XInterface  >
     252           0 : SFTreeListBox::getDocumentModel( Reference< XComponentContext >& xCtx, ::rtl::OUString& docName )
     253             : {
     254           0 :     Reference< XInterface > xModel;
     255             :     Reference< lang::XMultiComponentFactory > mcf =
     256           0 :             xCtx->getServiceManager();
     257             :     Reference< frame::XDesktop > desktop (
     258           0 :         mcf->createInstanceWithContext(
     259           0 :             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop") ),                 xCtx ),
     260           0 :             UNO_QUERY );
     261             : 
     262             :     Reference< container::XEnumerationAccess > componentsAccess =
     263           0 :         desktop->getComponents();
     264             :     Reference< container::XEnumeration > components =
     265           0 :         componentsAccess->createEnumeration();
     266           0 :     while (components->hasMoreElements())
     267             :     {
     268             :         Reference< frame::XModel > model(
     269           0 :             components->nextElement(), UNO_QUERY );
     270           0 :         if ( model.is() )
     271             :         {
     272           0 :             ::rtl::OUString sTdocUrl = ::comphelper::DocumentInfo::getDocumentTitle( model );
     273           0 :             if( sTdocUrl.equals( docName ) )
     274             :             {
     275           0 :                 xModel = model;
     276             :                 break;
     277           0 :             }
     278             :         }
     279           0 :     }
     280           0 :     return xModel;
     281             : }
     282             : 
     283             : Reference< browse::XBrowseNode >
     284           0 : SFTreeListBox::getLangNodeFromRootNode( Reference< browse::XBrowseNode >& rootNode, ::rtl::OUString& language )
     285             : {
     286           0 :     Reference< browse::XBrowseNode > langNode;
     287             : 
     288             :     try
     289             :     {
     290           0 :         Sequence < Reference< browse::XBrowseNode > > children = rootNode->getChildNodes();
     291           0 :         for ( sal_Int32 n = 0; n < children.getLength(); n++ )
     292             :         {
     293           0 :             if ( children[ n ]->getName().equals( language ) )
     294             :             {
     295           0 :                 langNode = children[ n ];
     296           0 :                 break;
     297             :             }
     298           0 :         }
     299             :     }
     300           0 :     catch ( Exception& )
     301             :     {
     302             :         // if getChildNodes() throws an exception we just return
     303             :         // the empty Reference
     304             :     }
     305           0 :     return langNode;
     306             : }
     307             : 
     308           0 : void SFTreeListBox:: RequestSubEntries( SvTreeListEntry* pRootEntry, Reference< ::com::sun::star::script::browse::XBrowseNode >& node,
     309             :                                        Reference< XModel >& model )
     310             : {
     311           0 :     if (! node.is() )
     312             :     {
     313           0 :         return;
     314             :     }
     315             : 
     316           0 :     Sequence< Reference< browse::XBrowseNode > > children;
     317             :     try
     318             :     {
     319           0 :         children = node->getChildNodes();
     320             :     }
     321           0 :     catch ( Exception& )
     322             :     {
     323             :         // if we catch an exception in getChildNodes then no entries are added
     324             :     }
     325             : 
     326           0 :     for ( sal_Int32 n = 0; n < children.getLength(); n++ )
     327             :     {
     328           0 :         ::rtl::OUString name( children[ n ]->getName() );
     329           0 :         if (  children[ n ]->getType() !=  browse::BrowseNodeTypes::SCRIPT)
     330             :         {
     331             :             SAL_WNODEPRECATED_DECLARATIONS_PUSH
     332           0 :             insertEntry( name, RID_CUIIMG_LIB, pRootEntry, true, std::auto_ptr< SFEntry >(new SFEntry( OBJTYPE_SCRIPTCONTAINER, children[ n ],model )));
     333             :             SAL_WNODEPRECATED_DECLARATIONS_POP
     334             :         }
     335             :         else
     336             :         {
     337           0 :             if ( children[ n ]->getType() == browse::BrowseNodeTypes::SCRIPT )
     338             :             {
     339             :                 SAL_WNODEPRECATED_DECLARATIONS_PUSH
     340           0 :                 insertEntry( name, RID_CUIIMG_MACRO, pRootEntry, false, std::auto_ptr< SFEntry >(new SFEntry( OBJTYPE_METHOD, children[ n ],model )));
     341             :                 SAL_WNODEPRECATED_DECLARATIONS_POP
     342             : 
     343             :             }
     344             :         }
     345           0 :     }
     346             : }
     347             : 
     348           0 : long SFTreeListBox::ExpandingHdl()
     349             : {
     350           0 :     return sal_True;
     351             : }
     352             : 
     353           0 : void SFTreeListBox::ExpandAllTrees()
     354             : {
     355           0 : }
     356             : 
     357             : SAL_WNODEPRECATED_DECLARATIONS_PUSH
     358           0 : SvTreeListEntry * SFTreeListBox::insertEntry(
     359             :     String const & rText, sal_uInt16 nBitmap, SvTreeListEntry * pParent,
     360             :     bool bChildrenOnDemand, std::auto_ptr< SFEntry > aUserData, ::rtl::OUString factoryURL )
     361             : {
     362             :     SvTreeListEntry * p;
     363           0 :     if( nBitmap == RID_CUIIMG_DOC && !factoryURL.isEmpty() )
     364             :     {
     365           0 :         Image aImage = SvFileInformationManager::GetFileImage( INetURLObject(factoryURL), false );
     366             :         p = InsertEntry(
     367             :             rText, aImage, aImage, pParent, bChildrenOnDemand, LIST_APPEND,
     368           0 :             aUserData.release()); // XXX possible leak
     369             :     }
     370             :     else
     371             :     {
     372           0 :         p = insertEntry( rText, nBitmap, pParent, bChildrenOnDemand, aUserData );
     373             :     }
     374           0 :     return p;
     375             : }
     376             : SAL_WNODEPRECATED_DECLARATIONS_POP
     377             : 
     378             : SAL_WNODEPRECATED_DECLARATIONS_PUSH
     379           0 : SvTreeListEntry * SFTreeListBox::insertEntry(
     380             :     String const & rText, sal_uInt16 nBitmap, SvTreeListEntry * pParent,
     381             :     bool bChildrenOnDemand, std::auto_ptr< SFEntry > aUserData )
     382             : {
     383           0 :     Image aImage;
     384           0 :     if( nBitmap == RID_CUIIMG_HARDDISK )
     385             :     {
     386           0 :         aImage = m_hdImage;
     387             :     }
     388           0 :     else if( nBitmap == RID_CUIIMG_LIB )
     389             :     {
     390           0 :         aImage = m_libImage;
     391             :     }
     392           0 :     else if( nBitmap == RID_CUIIMG_MACRO )
     393             :     {
     394           0 :         aImage = m_macImage;
     395             :     }
     396           0 :     else if( nBitmap == RID_CUIIMG_DOC )
     397             :     {
     398           0 :         aImage = m_docImage;
     399             :     }
     400             :     SvTreeListEntry * p = InsertEntry(
     401             :         rText, aImage, aImage, pParent, bChildrenOnDemand, LIST_APPEND,
     402           0 :         aUserData.release()); // XXX possible leak
     403           0 :    return p;
     404             : }
     405             : SAL_WNODEPRECATED_DECLARATIONS_POP
     406             : 
     407           0 : void SFTreeListBox::RequestingChildren( SvTreeListEntry* pEntry )
     408             : {
     409           0 :     SFEntry* userData = 0;
     410           0 :     if ( !pEntry )
     411             :     {
     412           0 :         return;
     413             :     }
     414           0 :     userData = (SFEntry*)pEntry->GetUserData();
     415             : 
     416           0 :     Reference< browse::XBrowseNode > node;
     417           0 :     Reference< XModel > model;
     418           0 :     if ( userData && !userData->isLoaded() )
     419             :     {
     420           0 :         node = userData->GetNode();
     421           0 :         model = userData->GetModel();
     422           0 :         RequestSubEntries( pEntry, node, model );
     423           0 :         userData->setLoaded();
     424           0 :     }
     425             : }
     426             : 
     427           0 : void SFTreeListBox::ExpandedHdl()
     428             : {
     429           0 : }
     430             : 
     431             : // ----------------------------------------------------------------------------
     432             : // CuiInputDialog ------------------------------------------------------------
     433             : // ----------------------------------------------------------------------------
     434           0 : CuiInputDialog::CuiInputDialog(Window * pParent, sal_uInt16 nMode )
     435           0 :     : ModalDialog( pParent, CUI_RES( RID_DLG_NEWLIB ) ),
     436           0 :         aText( this, CUI_RES( FT_NEWLIB ) ),
     437           0 :         aEdit( this, CUI_RES( ED_LIBNAME ) ),
     438           0 :         aOKButton( this, CUI_RES( PB_OK ) ),
     439           0 :         aCancelButton( this, CUI_RES( PB_CANCEL ) )
     440             : {
     441           0 :     aEdit.GrabFocus();
     442           0 :     if ( nMode == INPUTMODE_NEWLIB )
     443             :     {
     444           0 :         SetText( String( CUI_RES( STR_NEWLIB ) ) );
     445             :     }
     446           0 :     else if ( nMode == INPUTMODE_NEWMACRO )
     447             :     {
     448           0 :         SetText( String( CUI_RES( STR_NEWMACRO ) ) );
     449           0 :         aText.SetText( String( CUI_RES( STR_FT_NEWMACRO ) ) );
     450             :     }
     451           0 :     else if ( nMode == INPUTMODE_RENAME )
     452             :     {
     453           0 :         SetText( String( CUI_RES( STR_RENAME ) ) );
     454           0 :         aText.SetText( String( CUI_RES( STR_FT_RENAME ) ) );
     455             :     }
     456           0 :     FreeResource();
     457             : 
     458             :     // some resizing so that the text fits
     459           0 :     Point point, newPoint;
     460           0 :     Size siz, newSiz;
     461             :     long gap;
     462             : 
     463             :     sal_uInt16 style = TEXT_DRAW_MULTILINE | TEXT_DRAW_TOP |
     464           0 :                    TEXT_DRAW_LEFT | TEXT_DRAW_WORDBREAK;
     465             : 
     466             :     // get dimensions of dialog instructions control
     467           0 :     point = aText.GetPosPixel();
     468           0 :     siz = aText.GetSizePixel();
     469             : 
     470             :     // get dimensions occupied by text in the control
     471             :     Rectangle rect =
     472           0 :         GetTextRect( Rectangle( point, siz ), aText.GetText(), style );
     473           0 :     newSiz = rect.GetSize();
     474             : 
     475             :     // the gap is the difference between the text width and its control width
     476           0 :     gap = siz.Height() - newSiz.Height();
     477             : 
     478             :     //resize the text field
     479           0 :     newSiz = Size( siz.Width(), siz.Height() - gap );
     480           0 :     aText.SetSizePixel( newSiz );
     481             : 
     482             :     //move the OK & cancel buttons
     483           0 :     point = aEdit.GetPosPixel();
     484           0 :     newPoint = Point( point.X(), point.Y() - gap );
     485           0 :     aEdit.SetPosPixel( newPoint );
     486             : 
     487           0 : }
     488             : 
     489           0 : CuiInputDialog::~CuiInputDialog()
     490             : {
     491           0 : }
     492             : // ----------------------------------------------------------------------------
     493             : // ScriptOrgDialog ------------------------------------------------------------
     494             : // ----------------------------------------------------------------------------
     495           0 : SvxScriptOrgDialog::SvxScriptOrgDialog( Window* pParent, ::rtl::OUString language )
     496             :     : SfxModalDialog(pParent, "ScriptOrganizerDialog", "cui/ui/scriptorganizer.ui")
     497             :     , m_sLanguage(language)
     498           0 :     , m_delErrStr(CUI_RESSTR(RID_SVXSTR_DELFAILED))
     499           0 :     , m_delErrTitleStr(CUI_RESSTR(RID_SVXSTR_DELFAILED_TITLE))
     500           0 :     , m_delQueryStr(CUI_RES(RID_SVXSTR_DELQUERY))
     501           0 :     , m_delQueryTitleStr(CUI_RESSTR(RID_SVXSTR_DELQUERY_TITLE))
     502           0 :     , m_createErrStr(CUI_RESSTR(RID_SVXSTR_CREATEFAILED))
     503           0 :     , m_createDupStr(CUI_RESSTR(RID_SVXSTR_CREATEFAILEDDUP))
     504           0 :     , m_createErrTitleStr(CUI_RESSTR(RID_SVXSTR_CREATEFAILED_TITLE))
     505           0 :     , m_renameErrStr(CUI_RESSTR(RID_SVXSTR_RENAMEFAILED))
     506           0 :     , m_renameErrTitleStr(CUI_RESSTR(RID_SVXSTR_RENAMEFAILED_TITLE))
     507             : {
     508           0 :     get(m_pScriptsBox, "scripts");
     509           0 :     get(m_pRunButton, "run");
     510           0 :     get(m_pCloseButton, "close");
     511           0 :     get(m_pCreateButton, "create");
     512           0 :     get(m_pEditButton, "edit");
     513           0 :     get(m_pRenameButton, "rename");
     514           0 :     get(m_pDelButton, "delete");
     515             :     // must be a neater way to deal with the strings than as above
     516             :     // append the language to the dialog title
     517           0 :     String winTitle( GetText() );
     518           0 :     winTitle.SearchAndReplace( rtl::OUString( "%MACROLANG" ), m_sLanguage );
     519           0 :     SetText( winTitle );
     520             : 
     521           0 :     m_pScriptsBox->SetSelectHdl( LINK( this, SvxScriptOrgDialog, ScriptSelectHdl ) );
     522           0 :     m_pRunButton->SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
     523           0 :     m_pCloseButton->SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
     524           0 :     m_pRenameButton->SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
     525           0 :     m_pEditButton->SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
     526           0 :     m_pDelButton->SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
     527           0 :     m_pCreateButton->SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
     528             : 
     529           0 :     m_pRunButton->Disable();
     530           0 :     m_pRenameButton->Disable();
     531           0 :     m_pEditButton->Disable();
     532           0 :     m_pDelButton->Disable();
     533           0 :     m_pCreateButton->Disable();
     534             : 
     535           0 :     m_pScriptsBox->Init( m_sLanguage );
     536           0 :     RestorePreviousSelection();
     537           0 : }
     538             : 
     539           0 : SvxScriptOrgDialog::~SvxScriptOrgDialog()
     540             : {
     541             :     // clear the SelectHdl so that it isn't called during the dtor
     542           0 :     m_pScriptsBox->SetSelectHdl( Link() );
     543           0 : };
     544             : 
     545           0 : short SvxScriptOrgDialog::Execute()
     546             : {
     547             : 
     548           0 :     SfxObjectShell *pDoc = SfxObjectShell::GetFirst();
     549             : 
     550             :     // force load of MSPs for all documents
     551           0 :     while ( pDoc )
     552             :     {
     553             :         Reference< provider::XScriptProviderSupplier > xSPS =
     554             :             Reference< provider::XScriptProviderSupplier >
     555           0 :                                         ( pDoc->GetModel(), UNO_QUERY );
     556           0 :         if ( xSPS.is() )
     557             :         {
     558             :             Reference< provider::XScriptProvider > ScriptProvider =
     559           0 :             xSPS->getScriptProvider();
     560             :         }
     561             : 
     562           0 :         pDoc = SfxObjectShell::GetNext(*pDoc);
     563           0 :     }
     564           0 :     m_pScriptsBox->ExpandAllTrees();
     565             : 
     566           0 :     Window* pPrevDlgParent = Application::GetDefDialogParent();
     567           0 :     Application::SetDefDialogParent( this );
     568           0 :     short nRet = ModalDialog::Execute();
     569           0 :     Application::SetDefDialogParent( pPrevDlgParent );
     570           0 :     return nRet;
     571             : }
     572             : 
     573           0 : void SvxScriptOrgDialog::CheckButtons( Reference< browse::XBrowseNode >& node )
     574             : {
     575           0 :     if ( node.is() )
     576             :     {
     577           0 :         if ( node->getType() == browse::BrowseNodeTypes::SCRIPT)
     578             :         {
     579           0 :             m_pRunButton->Enable();
     580             :         }
     581             :         else
     582             :         {
     583           0 :             m_pRunButton->Disable();
     584             :         }
     585           0 :         Reference< beans::XPropertySet > xProps( node, UNO_QUERY );
     586             : 
     587           0 :         if ( !xProps.is() )
     588             :         {
     589           0 :             m_pEditButton->Disable();
     590           0 :             m_pDelButton->Disable();
     591           0 :             m_pCreateButton->Disable();
     592           0 :             m_pRunButton->Disable();
     593           0 :             return;
     594             :         }
     595             : 
     596           0 :         ::rtl::OUString sName("Editable")  ;
     597             : 
     598           0 :         if ( getBoolProperty( xProps, sName ) )
     599             :         {
     600           0 :             m_pEditButton->Enable();
     601             :         }
     602             :         else
     603             :         {
     604           0 :             m_pEditButton->Disable();
     605             :         }
     606             : 
     607           0 :         sName = rtl::OUString("Deletable")  ;
     608             : 
     609           0 :         if ( getBoolProperty( xProps, sName ) )
     610             :         {
     611           0 :             m_pDelButton->Enable();
     612             :         }
     613             :         else
     614             :         {
     615           0 :             m_pDelButton->Disable();
     616             :         }
     617             : 
     618           0 :         sName = rtl::OUString("Creatable")  ;
     619             : 
     620           0 :         if ( getBoolProperty( xProps, sName ) )
     621             :         {
     622           0 :             m_pCreateButton->Enable();
     623             :         }
     624             :         else
     625             :         {
     626           0 :             m_pCreateButton->Disable();
     627             :         }
     628             : 
     629           0 :         sName = rtl::OUString("Renamable")  ;
     630             : 
     631           0 :         if ( getBoolProperty( xProps, sName ) )
     632             :         {
     633           0 :             m_pRenameButton->Enable();
     634             :         }
     635             :         else
     636             :         {
     637           0 :             m_pRenameButton->Disable();
     638           0 :         }
     639             :     }
     640             :     else
     641             :     {
     642             :         // no node info available, disable all configurable actions
     643           0 :         m_pDelButton->Disable();
     644           0 :         m_pCreateButton->Disable();
     645           0 :         m_pEditButton->Disable();
     646           0 :         m_pRunButton->Disable();
     647           0 :         m_pRenameButton->Disable();
     648             :     }
     649             : }
     650             : 
     651           0 : IMPL_LINK( SvxScriptOrgDialog, ScriptSelectHdl, SvTreeListBox *, pBox )
     652             : {
     653           0 :     if ( !pBox->IsSelected( pBox->GetHdlEntry() ) )
     654             :     {
     655           0 :         return 0;
     656             :     }
     657             : 
     658           0 :     SvTreeListEntry* pEntry = pBox->GetHdlEntry();
     659             : 
     660           0 :     SFEntry* userData = 0;
     661           0 :     if ( !pEntry )
     662             :     {
     663           0 :         return 0;
     664             :     }
     665           0 :     userData = (SFEntry*)pEntry->GetUserData();
     666             : 
     667           0 :     Reference< browse::XBrowseNode > node;
     668           0 :     if ( userData )
     669             :     {
     670           0 :               node = userData->GetNode();
     671           0 :         CheckButtons( node );
     672             :     }
     673             : 
     674           0 :     return 0;
     675             : }
     676             : 
     677           0 : IMPL_LINK( SvxScriptOrgDialog, ButtonHdl, Button *, pButton )
     678             : {
     679           0 :     if ( pButton == m_pCloseButton )
     680             :     {
     681           0 :         StoreCurrentSelection();
     682           0 :         EndDialog( 0 );
     683             :     }
     684           0 :     if ( pButton == m_pEditButton ||
     685             :             pButton == m_pCreateButton ||
     686             :             pButton == m_pDelButton ||
     687             :             pButton == m_pRunButton ||
     688             :             pButton == m_pRenameButton )
     689             : 
     690             :     {
     691           0 :         if ( m_pScriptsBox->IsSelected( m_pScriptsBox->GetHdlEntry() ) )
     692             :         {
     693           0 :             SvTreeListEntry* pEntry = m_pScriptsBox->GetHdlEntry();
     694           0 :             SFEntry* userData = 0;
     695           0 :             if ( !pEntry )
     696             :             {
     697           0 :                 return 0;
     698             :             }
     699           0 :             userData = (SFEntry*)pEntry->GetUserData();
     700           0 :             if ( userData )
     701             :             {
     702           0 :                 Reference< browse::XBrowseNode > node;
     703           0 :                 Reference< XModel > xModel;
     704             : 
     705           0 :                 node = userData->GetNode();
     706           0 :                 xModel = userData->GetModel();
     707             : 
     708           0 :                 if ( !node.is() )
     709             :                 {
     710           0 :                     return 0;
     711             :                 }
     712             : 
     713           0 :                 if ( pButton == m_pRunButton )
     714             :                 {
     715           0 :                     ::rtl::OUString tmpString;
     716           0 :                     Reference< beans::XPropertySet > xProp( node, UNO_QUERY );
     717           0 :                     Reference< provider::XScriptProvider > mspNode;
     718           0 :                     if( !xProp.is() )
     719             :                     {
     720           0 :                         return 0;
     721             :                     }
     722             : 
     723           0 :                     if ( xModel.is() )
     724             :                     {
     725           0 :                         Reference< XEmbeddedScripts >  xEmbeddedScripts( xModel, UNO_QUERY);
     726           0 :                         if( !xEmbeddedScripts.is() )
     727             :                         {
     728           0 :                             return 0;
     729             :                         }
     730             : 
     731           0 :                         if (!xEmbeddedScripts->getAllowMacroExecution())
     732             :                         {
     733             :                             // Please FIXME: Show a message box if AllowMacroExecution is false
     734           0 :                             return 0;
     735           0 :                         }
     736             :                     }
     737             : 
     738             : 
     739           0 :                     SvTreeListEntry* pParent = m_pScriptsBox->GetParent( pEntry );
     740           0 :                     while ( pParent && !mspNode.is() )
     741             :                     {
     742           0 :                         SFEntry* mspUserData = (SFEntry*)pParent->GetUserData();
     743           0 :                         mspNode.set( mspUserData->GetNode() , UNO_QUERY );
     744           0 :                         pParent = m_pScriptsBox->GetParent( pParent );
     745             :                     }
     746           0 :                     xProp->getPropertyValue( rtl::OUString("URI" ) ) >>= tmpString;
     747           0 :                     const String scriptURL( tmpString );
     748             : 
     749           0 :                     if ( mspNode.is() )
     750             :                     {
     751             :                         try
     752             :                         {
     753             :                             Reference< provider::XScript > xScript(
     754           0 :                             mspNode->getScript( scriptURL ), UNO_QUERY_THROW );
     755             : 
     756           0 :                             const Sequence< Any > args(0);
     757           0 :                             Any aRet;
     758           0 :                             Sequence< sal_Int16 > outIndex;
     759           0 :                             Sequence< Any > outArgs( 0 );
     760           0 :                             aRet = xScript->invoke( args, outIndex, outArgs );
     761             :                         }
     762           0 :                         catch ( reflection::InvocationTargetException& ite )
     763             :                         {
     764           0 :                             ::com::sun::star::uno::Any a = makeAny(ite);
     765           0 :                             ShowErrorDialog(a);
     766             :                         }
     767           0 :                         catch ( provider::ScriptFrameworkErrorException& ite )
     768             :                         {
     769           0 :                             ::com::sun::star::uno::Any a = makeAny(ite);
     770           0 :                             ShowErrorDialog(a);
     771             :                         }
     772           0 :                         catch ( RuntimeException& re )
     773             :                         {
     774           0 :                             ::com::sun::star::uno::Any a = makeAny(re);
     775           0 :                             ShowErrorDialog(a);
     776             :                         }
     777           0 :                         catch ( Exception& e )
     778             :                         {
     779           0 :                             ::com::sun::star::uno::Any a = makeAny(e);
     780           0 :                             ShowErrorDialog(a);
     781             :                         }
     782             :                     }
     783           0 :                     StoreCurrentSelection();
     784           0 :                     EndDialog( 0 );
     785             :                 }
     786           0 :                 else if ( pButton == m_pEditButton )
     787             :                 {
     788           0 :                     Reference< script::XInvocation > xInv( node, UNO_QUERY );
     789           0 :                     if ( xInv.is() )
     790             :                     {
     791           0 :                         StoreCurrentSelection();
     792           0 :                         EndDialog( 0 );
     793           0 :                         Sequence< Any > args(0);
     794           0 :                         Sequence< Any > outArgs( 0 );
     795           0 :                         Sequence< sal_Int16 > outIndex;
     796             :                         try
     797             :                         {
     798             :                             // ISSUE need code to run script here
     799           0 :                             xInv->invoke( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Editable" ) ), args, outIndex, outArgs );
     800             :                         }
     801           0 :                         catch( Exception& e )
     802             :                         {
     803             :                             OSL_TRACE("Caught exception trying to invoke %s", ::rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
     804             : 
     805           0 :                         }
     806           0 :                     }
     807             :                 }
     808           0 :                 else if ( pButton == m_pCreateButton )
     809             :                 {
     810           0 :                     createEntry( pEntry );
     811             :                 }
     812           0 :                 else if ( pButton == m_pDelButton )
     813             :                 {
     814           0 :                     deleteEntry( pEntry );
     815             :                 }
     816           0 :                 else if ( pButton == m_pRenameButton )
     817             :                 {
     818           0 :                     renameEntry( pEntry );
     819           0 :                 }
     820             :             }
     821             :         }
     822             :     }
     823           0 :     return 0;
     824             : }
     825             : 
     826           0 : Reference< browse::XBrowseNode > SvxScriptOrgDialog::getBrowseNode( SvTreeListEntry* pEntry )
     827             : {
     828           0 :     Reference< browse::XBrowseNode > node;
     829           0 :     if ( pEntry )
     830             :     {
     831           0 :         SFEntry* userData = (SFEntry*)pEntry->GetUserData();
     832           0 :         if ( userData )
     833             :         {
     834           0 :             node = userData->GetNode();
     835             :         }
     836             :     }
     837             : 
     838           0 :     return node;
     839             : }
     840             : 
     841           0 : Reference< XModel > SvxScriptOrgDialog::getModel( SvTreeListEntry* pEntry )
     842             : {
     843           0 :     Reference< XModel > model;
     844           0 :     if ( pEntry )
     845             :     {
     846           0 :         SFEntry* userData = (SFEntry*)pEntry->GetUserData();
     847           0 :         if ( userData )
     848             :         {
     849           0 :             model = userData->GetModel();
     850             :         }
     851             :     }
     852             : 
     853           0 :     return model;
     854             : }
     855             : 
     856           0 : void SvxScriptOrgDialog::createEntry( SvTreeListEntry* pEntry )
     857             : {
     858             : 
     859           0 :     Reference< browse::XBrowseNode >  aChildNode;
     860           0 :     Reference< browse::XBrowseNode > node = getBrowseNode( pEntry );
     861           0 :     Reference< script::XInvocation > xInv( node, UNO_QUERY );
     862             : 
     863           0 :     if ( xInv.is() )
     864             :     {
     865           0 :         ::rtl::OUString aNewName;
     866           0 :         ::rtl::OUString aNewStdName;
     867           0 :         sal_uInt16 nMode = INPUTMODE_NEWLIB;
     868           0 :         if( m_pScriptsBox->GetModel()->GetDepth( pEntry ) == 0 )
     869             :         {
     870           0 :             aNewStdName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Library") ) ;
     871             :         }
     872             :         else
     873             :         {
     874           0 :             aNewStdName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Macro") ) ;
     875           0 :             nMode = INPUTMODE_NEWMACRO;
     876             :         }
     877             :         //do we need L10N for this? ie somethng like:
     878             :         //String aNewStdName( ResId( STR_STDMODULENAME ) );
     879           0 :         sal_Bool bValid = sal_False;
     880           0 :         sal_uInt16 i = 1;
     881             : 
     882           0 :         Sequence< Reference< browse::XBrowseNode > > childNodes;
     883             :         // no children => ok to create Parcel1 or Script1 without checking
     884             :         try
     885             :         {
     886           0 :             if( node->hasChildNodes() == sal_False )
     887             :             {
     888           0 :                 aNewName = aNewStdName;
     889           0 :                 aNewName += String::CreateFromInt32( i );
     890           0 :                 bValid = sal_True;
     891             :             }
     892             :             else
     893             :             {
     894           0 :                 childNodes = node->getChildNodes();
     895             :             }
     896             :         }
     897           0 :         catch ( Exception& )
     898             :         {
     899             :             // ignore, will continue on with empty sequence
     900             :         }
     901             : 
     902           0 :         ::rtl::OUString extn;
     903           0 :         while ( !bValid )
     904             :         {
     905           0 :             aNewName = aNewStdName;
     906           0 :             aNewName += String::CreateFromInt32( i );
     907           0 :             sal_Bool bFound = sal_False;
     908           0 :             if(childNodes.getLength() > 0 )
     909             :             {
     910           0 :                 ::rtl::OUString nodeName = childNodes[0]->getName();
     911           0 :                 sal_Int32 extnPos = nodeName.lastIndexOf( '.' );
     912           0 :                 if(extnPos>0)
     913           0 :                     extn = nodeName.copy(extnPos);
     914             :             }
     915           0 :             for( sal_Int32 index = 0; index < childNodes.getLength(); index++ )
     916             :             {
     917           0 :                 if ( (aNewName+extn) == childNodes[index]->getName() )
     918             :                 {
     919           0 :                     bFound = sal_True;
     920           0 :                     break;
     921             :                 }
     922             :             }
     923           0 :             if( bFound )
     924             :             {
     925           0 :                 i++;
     926             :             }
     927             :             else
     928             :             {
     929           0 :                 bValid = sal_True;
     930             :             }
     931             :         }
     932             : 
     933             :         SAL_WNODEPRECATED_DECLARATIONS_PUSH
     934           0 :         std::auto_ptr< CuiInputDialog > xNewDlg( new CuiInputDialog( static_cast<Window*>(this), nMode ) );
     935             :         SAL_WNODEPRECATED_DECLARATIONS_POP
     936           0 :         xNewDlg->SetObjectName( aNewName );
     937             : 
     938           0 :         do
     939             :         {
     940           0 :             if ( xNewDlg->Execute() && xNewDlg->GetObjectName().Len() )
     941             :             {
     942           0 :                 ::rtl::OUString aUserSuppliedName = xNewDlg->GetObjectName();
     943           0 :                 bValid = sal_True;
     944           0 :                 for( sal_Int32 index = 0; index < childNodes.getLength(); index++ )
     945             :                 {
     946           0 :                     if ( (aUserSuppliedName+extn) == childNodes[index]->getName() )
     947             :                     {
     948           0 :                         bValid = sal_False;
     949           0 :                         String aError( m_createErrStr );
     950           0 :                         aError.Append( m_createDupStr );
     951           0 :                         ErrorBox aErrorBox( static_cast<Window*>(this), WB_OK | RET_OK, aError );
     952           0 :                         aErrorBox.SetText( m_createErrTitleStr );
     953           0 :                         aErrorBox.Execute();
     954           0 :                         xNewDlg->SetObjectName( aNewName );
     955           0 :                         break;
     956             :                     }
     957             :                 }
     958           0 :                 if( bValid )
     959           0 :                     aNewName = aUserSuppliedName;
     960             :             }
     961             :             else
     962             :             {
     963             :                 // user hit cancel or hit OK with nothing in the editbox
     964             : 
     965           0 :                 return;
     966             :             }
     967             :         }
     968           0 :         while ( !bValid );
     969             : 
     970             :         // open up parent node (which ensures it's loaded)
     971           0 :         m_pScriptsBox->RequestingChildren( pEntry );
     972             : 
     973           0 :         Sequence< Any > args( 1 );
     974           0 :         args[ 0 ] <<= aNewName;
     975           0 :         Sequence< Any > outArgs( 0 );
     976           0 :         Sequence< sal_Int16 > outIndex;
     977             :         try
     978             :         {
     979           0 :             Any aResult;
     980           0 :             aResult = xInv->invoke( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Creatable") ), args, outIndex, outArgs );
     981           0 :             Reference< browse::XBrowseNode > newNode( aResult, UNO_QUERY );
     982           0 :             aChildNode = newNode;
     983             : 
     984             :         }
     985           0 :         catch( Exception& e )
     986             :         {
     987             :             OSL_TRACE("Caught exception trying to Create %s",
     988             :                 ::rtl::OUStringToOString(
     989             :                     e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
     990           0 :         }
     991             :     }
     992           0 :     if ( aChildNode.is() )
     993             :     {
     994           0 :         String aChildName = aChildNode->getName();
     995           0 :         SvTreeListEntry* pNewEntry = NULL;
     996             : 
     997             : 
     998           0 :         ::rtl::OUString name( aChildName );
     999           0 :         Reference<XModel> xDocumentModel = getModel( pEntry );
    1000             : 
    1001             :         // ISSUE do we need to remove all entries for parent
    1002             :         // to achieve sort? Just need to determine position
    1003             :         // SvTreeListBox::InsertEntry can take position arg
    1004             :         // -- Basic doesn't do this on create.
    1005             :         // Suppose we could avoid this too. -> created nodes are
    1006             :         // not in alphabetical order
    1007           0 :         if ( aChildNode->getType() == browse::BrowseNodeTypes::SCRIPT )
    1008             :         {
    1009             :             SAL_WNODEPRECATED_DECLARATIONS_PUSH
    1010             :             pNewEntry = m_pScriptsBox->insertEntry( aChildName,
    1011           0 :                     RID_CUIIMG_MACRO, pEntry, false, std::auto_ptr< SFEntry >(new SFEntry( OBJTYPE_METHOD, aChildNode,xDocumentModel ) ) );
    1012             :             SAL_WNODEPRECATED_DECLARATIONS_POP
    1013             : 
    1014             :         }
    1015             :         else
    1016             :         {
    1017             :             SAL_WNODEPRECATED_DECLARATIONS_PUSH
    1018             :             pNewEntry = m_pScriptsBox->insertEntry( aChildName,
    1019           0 :                 RID_CUIIMG_LIB, pEntry, false, std::auto_ptr< SFEntry >(new SFEntry( OBJTYPE_SCRIPTCONTAINER, aChildNode,xDocumentModel ) ) );
    1020             :             SAL_WNODEPRECATED_DECLARATIONS_POP
    1021             : 
    1022             :             // If the Parent is not loaded then set to
    1023             :             // loaded, this will prevent RequestingChildren ( called
    1024             :             // from vcl via RequestingChildren ) from
    1025             :             // creating new ( duplicate ) children
    1026           0 :             SFEntry* userData = (SFEntry*)pEntry->GetUserData();
    1027           0 :             if ( userData &&  !userData->isLoaded() )
    1028             :             {
    1029           0 :                 userData->setLoaded();
    1030             :             }
    1031             :         }
    1032           0 :         m_pScriptsBox->SetCurEntry( pNewEntry );
    1033           0 :         m_pScriptsBox->Select( m_pScriptsBox->GetCurEntry() );
    1034             : 
    1035             :     }
    1036             :     else
    1037             :     {
    1038             :         //ISSUE L10N & message from exception?
    1039           0 :         String aError( m_createErrStr );
    1040           0 :         ErrorBox aErrorBox( static_cast<Window*>(this), WB_OK | RET_OK, aError );
    1041           0 :         aErrorBox.SetText( m_createErrTitleStr );
    1042           0 :         aErrorBox.Execute();
    1043           0 :     }
    1044             : }
    1045             : 
    1046           0 : void SvxScriptOrgDialog::renameEntry( SvTreeListEntry* pEntry )
    1047             : {
    1048             : 
    1049           0 :     Reference< browse::XBrowseNode >  aChildNode;
    1050           0 :     Reference< browse::XBrowseNode > node = getBrowseNode( pEntry );
    1051           0 :     Reference< script::XInvocation > xInv( node, UNO_QUERY );
    1052             : 
    1053           0 :     if ( xInv.is() )
    1054             :     {
    1055           0 :         ::rtl::OUString aNewName = node->getName();
    1056           0 :         sal_Int32 extnPos = aNewName.lastIndexOf( '.' );
    1057           0 :         ::rtl::OUString extn;
    1058           0 :         if(extnPos>0)
    1059             :         {
    1060           0 :             extn = aNewName.copy(extnPos);
    1061           0 :             aNewName = aNewName.copy(0,extnPos);
    1062             :         }
    1063           0 :         sal_uInt16 nMode = INPUTMODE_RENAME;
    1064             : 
    1065             :         SAL_WNODEPRECATED_DECLARATIONS_PUSH
    1066           0 :         std::auto_ptr< CuiInputDialog > xNewDlg( new CuiInputDialog( static_cast<Window*>(this), nMode ) );
    1067             :         SAL_WNODEPRECATED_DECLARATIONS_POP
    1068           0 :         xNewDlg->SetObjectName( aNewName );
    1069             : 
    1070             :         sal_Bool bValid;
    1071           0 :         do
    1072             :         {
    1073           0 :             if ( xNewDlg->Execute() && xNewDlg->GetObjectName().Len() )
    1074             :             {
    1075           0 :                 ::rtl::OUString aUserSuppliedName = xNewDlg->GetObjectName();
    1076           0 :                 bValid = sal_True;
    1077           0 :                 if( bValid )
    1078           0 :                     aNewName = aUserSuppliedName;
    1079             :             }
    1080             :             else
    1081             :             {
    1082             :                 // user hit cancel or hit OK with nothing in the editbox
    1083           0 :                 return;
    1084             :             }
    1085             :         }
    1086           0 :         while ( !bValid );
    1087             : 
    1088           0 :         Sequence< Any > args( 1 );
    1089           0 :         args[ 0 ] <<= aNewName;
    1090           0 :         Sequence< Any > outArgs( 0 );
    1091           0 :         Sequence< sal_Int16 > outIndex;
    1092             :         try
    1093             :         {
    1094           0 :             Any aResult;
    1095           0 :             aResult = xInv->invoke( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Renamable") ), args, outIndex, outArgs );
    1096           0 :             Reference< browse::XBrowseNode > newNode( aResult, UNO_QUERY );
    1097           0 :             aChildNode = newNode;
    1098             : 
    1099             :         }
    1100           0 :         catch( Exception& e )
    1101             :         {
    1102             :             OSL_TRACE("Caught exception trying to Rename %s",
    1103             :                 ::rtl::OUStringToOString(
    1104             :                     e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
    1105           0 :         }
    1106             :     }
    1107           0 :     if ( aChildNode.is() )
    1108             :     {
    1109           0 :         m_pScriptsBox->SetEntryText( pEntry, aChildNode->getName() );
    1110           0 :         m_pScriptsBox->SetCurEntry( pEntry );
    1111           0 :         m_pScriptsBox->Select( m_pScriptsBox->GetCurEntry() );
    1112             : 
    1113             :     }
    1114             :     else
    1115             :     {
    1116             :         //ISSUE L10N & message from exception?
    1117           0 :         String aError( m_renameErrStr );
    1118           0 :         ErrorBox aErrorBox( static_cast<Window*>(this), WB_OK | RET_OK, aError );
    1119           0 :         aErrorBox.SetText( m_renameErrTitleStr );
    1120           0 :         aErrorBox.Execute();
    1121           0 :     }
    1122             : }
    1123           0 : void SvxScriptOrgDialog::deleteEntry( SvTreeListEntry* pEntry )
    1124             : {
    1125           0 :     sal_Bool result = sal_False;
    1126           0 :     Reference< browse::XBrowseNode > node = getBrowseNode( pEntry );
    1127             :     // ISSUE L10N string & can we centre list?
    1128           0 :     String aQuery( m_delQueryStr );
    1129           0 :     aQuery.Append( getListOfChildren( node, 0 ) );
    1130           0 :     QueryBox aQueryBox( static_cast<Window*>(this), WB_YES_NO | WB_DEF_YES, aQuery );
    1131           0 :     aQueryBox.SetText( m_delQueryTitleStr );
    1132           0 :     if ( aQueryBox.Execute() == RET_NO )
    1133             :     {
    1134           0 :         return;
    1135             :     }
    1136             : 
    1137           0 :     Reference< script::XInvocation > xInv( node, UNO_QUERY );
    1138           0 :     if ( xInv.is() )
    1139             :     {
    1140           0 :         Sequence< Any > args( 0 );
    1141           0 :         Sequence< Any > outArgs( 0 );
    1142           0 :         Sequence< sal_Int16 > outIndex;
    1143             :         try
    1144             :         {
    1145           0 :             Any aResult;
    1146           0 :             aResult = xInv->invoke( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Deletable") ), args, outIndex, outArgs );
    1147           0 :             aResult >>= result; // or do we just assume true if no exception ?
    1148             :         }
    1149           0 :         catch( Exception& e )
    1150             :         {
    1151             :             OSL_TRACE("Caught exception trying to delete %s",
    1152             :                 ::rtl::OUStringToOString(
    1153             :                     e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
    1154           0 :         }
    1155             :     }
    1156             : 
    1157           0 :     if ( result == sal_True )
    1158             :     {
    1159           0 :         m_pScriptsBox->deleteTree( pEntry );
    1160           0 :         m_pScriptsBox->GetModel()->Remove( pEntry );
    1161             :     }
    1162             :     else
    1163             :     {
    1164             :         //ISSUE L10N & message from exception?
    1165           0 :         ErrorBox aErrorBox( static_cast<Window*>(this), WB_OK | RET_OK, m_delErrStr );
    1166           0 :         aErrorBox.SetText( m_delErrTitleStr );
    1167           0 :         aErrorBox.Execute();
    1168           0 :     }
    1169             : 
    1170             : }
    1171             : 
    1172           0 : sal_Bool SvxScriptOrgDialog::getBoolProperty( Reference< beans::XPropertySet >& xProps,
    1173             :                 ::rtl::OUString& propName )
    1174             : {
    1175           0 :     sal_Bool result = false;
    1176             :     try
    1177             :     {
    1178           0 :         sal_Bool bTemp = sal_False;
    1179           0 :         xProps->getPropertyValue( propName ) >>= bTemp;
    1180           0 :         result = ( bTemp == sal_True );
    1181             :     }
    1182           0 :     catch ( Exception& )
    1183             :     {
    1184           0 :         return result;
    1185             :     }
    1186           0 :     return result;
    1187             : }
    1188             : 
    1189           0 : String SvxScriptOrgDialog::getListOfChildren( Reference< browse::XBrowseNode > node, int depth )
    1190             : {
    1191           0 :     String result;
    1192           0 :     result.Append( rtl::OUString( "\n" ) );
    1193           0 :     for( int i=0;i<=depth;i++ )
    1194             :     {
    1195           0 :         result.Append( rtl::OUString( "\t" ) );
    1196             :     }
    1197           0 :     result.Append( String( node->getName() ) );
    1198             : 
    1199             :     try
    1200             :     {
    1201           0 :         if ( node->hasChildNodes() == sal_True )
    1202             :         {
    1203             :             Sequence< Reference< browse::XBrowseNode > > children
    1204           0 :                 = node->getChildNodes();
    1205           0 :             for ( sal_Int32 n = 0; n < children.getLength(); n++ )
    1206             :             {
    1207           0 :                 result.Append( getListOfChildren( children[ n ] , depth+1 ) );
    1208           0 :             }
    1209             :         }
    1210             :     }
    1211           0 :     catch ( Exception& )
    1212             :     {
    1213             :         // ignore, will return an empty string
    1214             :     }
    1215             : 
    1216           0 :     return result;
    1217             : }
    1218             : 
    1219           2 : Selection_hash SvxScriptOrgDialog::m_lastSelection;
    1220             : 
    1221           0 : void SvxScriptOrgDialog::StoreCurrentSelection()
    1222             : {
    1223           0 :     String aDescription;
    1224           0 :     if ( m_pScriptsBox->IsSelected( m_pScriptsBox->GetHdlEntry() ) )
    1225             :     {
    1226           0 :         SvTreeListEntry* pEntry = m_pScriptsBox->GetHdlEntry();
    1227           0 :         while( pEntry )
    1228             :         {
    1229           0 :             aDescription.Insert( m_pScriptsBox->GetEntryText( pEntry ), 0 );
    1230           0 :             pEntry = m_pScriptsBox->GetParent( pEntry );
    1231           0 :             if ( pEntry )
    1232           0 :                 aDescription.Insert( ';', 0 );
    1233             :         }
    1234           0 :         ::rtl::OUString sDesc( aDescription );
    1235           0 :         m_lastSelection[ m_sLanguage ] = sDesc;
    1236           0 :     }
    1237           0 : }
    1238             : 
    1239           0 : void SvxScriptOrgDialog::RestorePreviousSelection()
    1240             : {
    1241           0 :     String aStoredEntry = String( m_lastSelection[ m_sLanguage ] );
    1242           0 :     if( aStoredEntry.Len() <= 0 )
    1243           0 :         return;
    1244           0 :     SvTreeListEntry* pEntry = 0;
    1245           0 :     sal_uInt16 nIndex = 0;
    1246           0 :     while ( nIndex != STRING_NOTFOUND )
    1247             :     {
    1248           0 :         String aTmp( aStoredEntry.GetToken( 0, ';', nIndex ) );
    1249           0 :         SvTreeListEntry* pTmpEntry = m_pScriptsBox->FirstChild( pEntry );
    1250           0 :         while ( pTmpEntry )
    1251             :         {
    1252           0 :             if ( m_pScriptsBox->GetEntryText( pTmpEntry ) == aTmp )
    1253             :             {
    1254           0 :                 pEntry = pTmpEntry;
    1255           0 :                 break;
    1256             :             }
    1257           0 :             pTmpEntry = m_pScriptsBox->NextSibling( pTmpEntry );
    1258             :         }
    1259           0 :         if ( !pTmpEntry )
    1260             :             break;
    1261           0 :         m_pScriptsBox->RequestingChildren( pEntry );
    1262           0 :     }
    1263           0 :     m_pScriptsBox->SetCurEntry( pEntry );
    1264             : }
    1265             : 
    1266           6 : ::rtl::OUString ReplaceString(
    1267             :     const ::rtl::OUString& source,
    1268             :     const ::rtl::OUString& token,
    1269             :     const ::rtl::OUString& value )
    1270             : {
    1271           6 :     sal_Int32 pos = source.indexOf( token );
    1272             : 
    1273           6 :     if ( pos != -1 && !value.isEmpty() )
    1274             :     {
    1275           4 :         return source.replaceAt( pos, token.getLength(), value );
    1276             :     }
    1277             :     else
    1278             :     {
    1279           2 :         return source;
    1280             :     }
    1281             : }
    1282             : 
    1283           2 : ::rtl::OUString FormatErrorString(
    1284             :     const ::rtl::OUString& unformatted,
    1285             :     const ::rtl::OUString& language,
    1286             :     const ::rtl::OUString& script,
    1287             :     const ::rtl::OUString& line,
    1288             :     const ::rtl::OUString& type,
    1289             :     const ::rtl::OUString& message )
    1290             : {
    1291           2 :     ::rtl::OUString result = unformatted.copy( 0 );
    1292             : 
    1293             :     result = ReplaceString(
    1294           2 :         result, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%LANGUAGENAME") ), language );
    1295             :     result = ReplaceString(
    1296           2 :         result, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%SCRIPTNAME") ), script );
    1297             :     result = ReplaceString(
    1298           2 :         result, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%LINENUMBER") ), line );
    1299             : 
    1300           2 :     if ( !type.isEmpty() )
    1301             :     {
    1302           0 :         result += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n\n") );
    1303           0 :         result += ::rtl::OUString(String(CUI_RES(RID_SVXSTR_ERROR_TYPE_LABEL)));
    1304           0 :         result += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ") );
    1305           0 :         result += type;
    1306             :     }
    1307             : 
    1308           2 :     if ( !message.isEmpty() )
    1309             :     {
    1310           2 :         result += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n\n") );
    1311           2 :         result += ::rtl::OUString(String(CUI_RES(RID_SVXSTR_ERROR_MESSAGE_LABEL)));
    1312           2 :         result += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ") );
    1313           2 :         result += message;
    1314             :     }
    1315             : 
    1316           2 :     return result;
    1317             : }
    1318             : 
    1319           0 : ::rtl::OUString GetErrorMessage(
    1320             :     const provider::ScriptErrorRaisedException& eScriptError )
    1321             : {
    1322           0 :     ::rtl::OUString unformatted = String( CUI_RES( RID_SVXSTR_ERROR_AT_LINE ) );
    1323             : 
    1324           0 :     ::rtl::OUString unknown( RTL_CONSTASCII_USTRINGPARAM("UNKNOWN") );
    1325           0 :     ::rtl::OUString language = unknown;
    1326           0 :     ::rtl::OUString script = unknown;
    1327           0 :     ::rtl::OUString line = unknown;
    1328           0 :     ::rtl::OUString type = ::rtl::OUString();
    1329           0 :     ::rtl::OUString message = eScriptError.Message;
    1330             : 
    1331           0 :         if ( !eScriptError.language.isEmpty() )
    1332             :         {
    1333           0 :             language = eScriptError.language;
    1334             :         }
    1335             : 
    1336           0 :         if ( !eScriptError.scriptName.isEmpty() )
    1337             :         {
    1338           0 :             script = eScriptError.scriptName;
    1339             :         }
    1340             : 
    1341           0 :         if ( !eScriptError.Message.isEmpty() )
    1342             :         {
    1343           0 :             message = eScriptError.Message;
    1344             :         }
    1345           0 :         if ( eScriptError.lineNum != -1 )
    1346             :         {
    1347           0 :             line = ::rtl::OUString::valueOf( eScriptError.lineNum );
    1348             :             unformatted = String(
    1349           0 :                 CUI_RES( RID_SVXSTR_ERROR_AT_LINE ) );
    1350             :         }
    1351             :         else
    1352             :         {
    1353             :             unformatted = String(
    1354           0 :                 CUI_RES( RID_SVXSTR_ERROR_RUNNING ) );
    1355             :         }
    1356             : 
    1357             :     return FormatErrorString(
    1358           0 :         unformatted, language, script, line, type, message );
    1359             : }
    1360             : 
    1361           0 : ::rtl::OUString GetErrorMessage(
    1362             :     const provider::ScriptExceptionRaisedException& eScriptException )
    1363             : {
    1364             :     ::rtl::OUString unformatted =
    1365           0 :           String( CUI_RES( RID_SVXSTR_EXCEPTION_AT_LINE ) );
    1366             : 
    1367           0 :     ::rtl::OUString unknown( RTL_CONSTASCII_USTRINGPARAM("UNKNOWN") );
    1368           0 :     ::rtl::OUString language = unknown;
    1369           0 :     ::rtl::OUString script = unknown;
    1370           0 :     ::rtl::OUString line = unknown;
    1371           0 :     ::rtl::OUString type = unknown;
    1372           0 :     ::rtl::OUString message = eScriptException.Message;
    1373             : 
    1374           0 :     if ( !eScriptException.language.isEmpty() )
    1375             :     {
    1376           0 :         language = eScriptException.language;
    1377             :     }
    1378           0 :     if ( !eScriptException.scriptName.isEmpty() )
    1379             :     {
    1380           0 :         script = eScriptException.scriptName;
    1381             :     }
    1382             : 
    1383           0 :     if ( !eScriptException.Message.isEmpty() )
    1384             :     {
    1385           0 :         message = eScriptException.Message;
    1386             :     }
    1387             : 
    1388           0 :     if ( eScriptException.lineNum != -1 )
    1389             :     {
    1390           0 :         line = ::rtl::OUString::valueOf( eScriptException.lineNum );
    1391             :         unformatted = String(
    1392           0 :             CUI_RES( RID_SVXSTR_EXCEPTION_AT_LINE ) );
    1393             :     }
    1394             :     else
    1395             :     {
    1396             :         unformatted = String(
    1397           0 :             CUI_RES( RID_SVXSTR_EXCEPTION_RUNNING ) );
    1398             :     }
    1399             : 
    1400           0 :     if ( !eScriptException.exceptionType.isEmpty() )
    1401             :     {
    1402           0 :         type = eScriptException.exceptionType;
    1403             :     }
    1404             : 
    1405             :     return FormatErrorString(
    1406           0 :         unformatted, language, script, line, type, message );
    1407             : 
    1408             : }
    1409           2 : ::rtl::OUString GetErrorMessage(
    1410             :     const provider::ScriptFrameworkErrorException& sError )
    1411             : {
    1412             :     ::rtl::OUString unformatted = String(
    1413           2 :         CUI_RES( RID_SVXSTR_FRAMEWORK_ERROR_RUNNING ) );
    1414             : 
    1415           2 :     ::rtl::OUString language( RTL_CONSTASCII_USTRINGPARAM("UNKNOWN") );
    1416             : 
    1417           2 :     ::rtl::OUString script( RTL_CONSTASCII_USTRINGPARAM("UNKNOWN") );
    1418             : 
    1419           2 :     ::rtl::OUString message;
    1420             : 
    1421           2 :     if ( !sError.scriptName.isEmpty() )
    1422             :     {
    1423           2 :         script = sError.scriptName;
    1424             :     }
    1425           2 :     if ( !sError.language.isEmpty() )
    1426             :     {
    1427           2 :         language = sError.language;
    1428             :     }
    1429           2 :     if ( sError.errorType == provider::ScriptFrameworkErrorType::NOTSUPPORTED )
    1430             :     {
    1431             :         message = String(
    1432           0 :             CUI_RES(  RID_SVXSTR_ERROR_LANG_NOT_SUPPORTED ) );
    1433             :         message =  ReplaceString(
    1434           0 :             message, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%LANGUAGENAME") ), language );
    1435             : 
    1436             :     }
    1437             :     else
    1438             :     {
    1439           2 :         message = sError.Message;
    1440             :     }
    1441             :     return FormatErrorString(
    1442           2 :         unformatted, language, script, ::rtl::OUString(), ::rtl::OUString(), message );
    1443             : }
    1444             : 
    1445           0 : ::rtl::OUString GetErrorMessage( const RuntimeException& re )
    1446             : {
    1447           0 :     Type t = ::getCppuType( &re );
    1448           0 :     ::rtl::OUString message = t.getTypeName();
    1449           0 :     message += re.Message;
    1450             : 
    1451           0 :     return message;
    1452             : }
    1453             : 
    1454           0 : ::rtl::OUString GetErrorMessage( const Exception& e )
    1455             : {
    1456           0 :     Type t = ::getCppuType( &e );
    1457           0 :     ::rtl::OUString message = t.getTypeName();
    1458           0 :     message += e.Message;
    1459             : 
    1460           0 :     return message;
    1461             : }
    1462             : 
    1463           2 : ::rtl::OUString GetErrorMessage( const com::sun::star::uno::Any& aException )
    1464             : {
    1465           4 :     if ( aException.getValueType() ==
    1466           4 :          ::getCppuType( (const reflection::InvocationTargetException* ) NULL ) )
    1467             :     {
    1468           0 :         reflection::InvocationTargetException ite;
    1469           0 :         aException >>= ite;
    1470           0 :         if ( ite.TargetException.getValueType() == ::getCppuType( ( const provider::ScriptErrorRaisedException* ) NULL ) )
    1471             :         {
    1472             :             // Error raised by script
    1473           0 :             provider::ScriptErrorRaisedException scriptError;
    1474           0 :             ite.TargetException >>= scriptError;
    1475           0 :             return GetErrorMessage( scriptError );
    1476             :         }
    1477           0 :         else if ( ite.TargetException.getValueType() == ::getCppuType( ( const provider::ScriptExceptionRaisedException* ) NULL ) )
    1478             :         {
    1479             :             // Exception raised by script
    1480           0 :             provider::ScriptExceptionRaisedException scriptException;
    1481           0 :             ite.TargetException >>= scriptException;
    1482           0 :             return GetErrorMessage( scriptException );
    1483             :         }
    1484             :         else
    1485             :         {
    1486             :             // Unknown error, shouldn't happen
    1487             :             // OSL_ASSERT(...)
    1488           0 :         }
    1489             : 
    1490             :     }
    1491           2 :     else if ( aException.getValueType() == ::getCppuType( ( const provider::ScriptFrameworkErrorException* ) NULL ) )
    1492             :     {
    1493             :         // A Script Framework error has occurred
    1494           2 :         provider::ScriptFrameworkErrorException sfe;
    1495           2 :         aException >>= sfe;
    1496           2 :         return GetErrorMessage( sfe );
    1497             : 
    1498             :     }
    1499             :     // unknown exception
    1500           0 :     Exception e;
    1501           0 :     RuntimeException rte;
    1502           0 :     if ( aException >>= rte )
    1503             :     {
    1504           0 :         return GetErrorMessage( rte );
    1505             :     }
    1506             : 
    1507           0 :     aException >>= e;
    1508           0 :     return GetErrorMessage( e );
    1509             : 
    1510             : }
    1511             : 
    1512           2 : SvxScriptErrorDialog::SvxScriptErrorDialog(
    1513             :     Window* , ::com::sun::star::uno::Any aException )
    1514           2 :     : m_sMessage()
    1515             : {
    1516           2 :     SolarMutexGuard aGuard;
    1517           2 :     m_sMessage = GetErrorMessage( aException );
    1518           2 : }
    1519             : 
    1520           4 : SvxScriptErrorDialog::~SvxScriptErrorDialog()
    1521             : {
    1522           4 : }
    1523             : 
    1524           2 : short SvxScriptErrorDialog::Execute()
    1525             : {
    1526             :     // Show Error dialog asynchronously
    1527             :     //
    1528             :     // Pass a copy of the message to the ShowDialog method as the
    1529             :     // SvxScriptErrorDialog may be deleted before ShowDialog is called
    1530             :     Application::PostUserEvent(
    1531             :         LINK( this, SvxScriptErrorDialog, ShowDialog ),
    1532           2 :         new rtl::OUString( m_sMessage ) );
    1533             : 
    1534           2 :     return 0;
    1535             : }
    1536             : 
    1537           0 : IMPL_LINK( SvxScriptErrorDialog, ShowDialog, ::rtl::OUString*, pMessage )
    1538             : {
    1539           0 :     ::rtl::OUString message;
    1540             : 
    1541           0 :     if ( pMessage && !pMessage->isEmpty() )
    1542             :     {
    1543           0 :         message = *pMessage;
    1544             :     }
    1545             :     else
    1546             :     {
    1547           0 :         message = String( CUI_RES( RID_SVXSTR_ERROR_TITLE ) );
    1548             :     }
    1549             : 
    1550           0 :     MessBox* pBox = new WarningBox( NULL, WB_OK, message );
    1551           0 :     pBox->SetText( CUI_RES( RID_SVXSTR_ERROR_TITLE ) );
    1552           0 :     pBox->Execute();
    1553             : 
    1554           0 :     delete pBox;
    1555           0 :     delete pMessage;
    1556             : 
    1557           0 :     return 0;
    1558           6 : }
    1559             : 
    1560             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10