update dev300-m58
[ooovba.git] / svx / source / cui / scriptdlg.cxx
blob09b00a1cb8d5560b550a9bcc076d56c675771408
1 /**********************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: scriptdlg.cxx,v $
10 * $Revision: 1.26 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include <memory>
36 #include <sfx2/objsh.hxx>
37 #include <vcl/svapp.hxx>
38 #include <vcl/msgbox.hxx>
39 #include <vos/mutex.hxx>
41 #include <svx/dialogs.hrc>
42 #include "scriptdlg.hrc"
43 #include "fmresids.hrc"
44 #include "scriptdlg.hxx"
45 #include <svx/dialmgr.hxx>
46 #include "selector.hxx"
48 #include <com/sun/star/uno/XComponentContext.hpp>
49 #include <com/sun/star/frame/XDesktop.hpp>
50 #include <com/sun/star/script/XInvocation.hpp>
51 #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
52 #include <com/sun/star/script/provider/XScriptProvider.hpp>
53 #include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
54 #include <com/sun/star/script/browse/XBrowseNodeFactory.hpp>
55 #include <com/sun/star/script/browse/BrowseNodeFactoryViewTypes.hpp>
56 #include <com/sun/star/script/provider/ScriptErrorRaisedException.hpp>
57 #include <com/sun/star/script/provider/ScriptExceptionRaisedException.hpp>
58 #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
59 #include <com/sun/star/frame/XModuleManager.hpp>
60 #include <com/sun/star/script/XInvocation.hpp>
61 #include <com/sun/star/document/XEmbeddedScripts.hpp>
63 #include <cppuhelper/implbase1.hxx>
64 #include <comphelper/documentinfo.hxx>
65 #include <comphelper/uno3.hxx>
66 #include <comphelper/processfactory.hxx>
67 #include <comphelper/broadcasthelper.hxx>
68 #include <comphelper/propertycontainer.hxx>
69 #include <comphelper/proparrhlp.hxx>
71 #include <basic/sbx.hxx>
72 #include <svtools/imagemgr.hxx>
73 #include <tools/urlobj.hxx>
74 #include <vector>
75 #include <algorithm>
77 using namespace ::com::sun::star;
78 using namespace ::com::sun::star::uno;
79 using namespace ::com::sun::star::script;
80 using namespace ::com::sun::star::frame;
81 using namespace ::com::sun::star::document;
83 void ShowErrorDialog( const Any& aException )
85 SvxScriptErrorDialog* pDlg = new SvxScriptErrorDialog( NULL, aException );
86 pDlg->Execute();
87 delete pDlg;
90 SFTreeListBox::SFTreeListBox( Window* pParent, const ResId& rResId ) :
91 SvTreeListBox( pParent, ResId( rResId.GetId(),*rResId.GetResMgr() ) ),
92 m_hdImage(ResId(IMG_HARDDISK,*rResId.GetResMgr())),
93 m_hdImage_hc(ResId(IMG_HARDDISK_HC,*rResId.GetResMgr())),
94 m_libImage(ResId(IMG_LIB,*rResId.GetResMgr())),
95 m_libImage_hc(ResId(IMG_LIB_HC,*rResId.GetResMgr())),
96 m_macImage(ResId(IMG_MACRO,*rResId.GetResMgr())),
97 m_macImage_hc(ResId(IMG_MACRO_HC,*rResId.GetResMgr())),
98 m_docImage(ResId(IMG_DOCUMENT,*rResId.GetResMgr())),
99 m_docImage_hc(ResId(IMG_DOCUMENT_HC,*rResId.GetResMgr())),
100 m_sMyMacros(String(ResId(STR_MYMACROS,*rResId.GetResMgr()))),
101 m_sProdMacros(String(ResId(STR_PRODMACROS,*rResId.GetResMgr())))
103 FreeResource();
104 SetSelectionMode( SINGLE_SELECTION );
106 SetWindowBits( GetStyle() | WB_CLIPCHILDREN | WB_HSCROLL |
107 WB_HASBUTTONS | WB_HASBUTTONSATROOT | WB_HIDESELECTION |
108 WB_HASLINES | WB_HASLINESATROOT );
109 SetNodeDefaultImages();
111 nMode = 0xFF; // Alles
114 SFTreeListBox::~SFTreeListBox()
116 deleteAllTree();
119 void SFTreeListBox::delUserData( SvLBoxEntry* pEntry )
121 if ( pEntry )
124 String text = GetEntryText( pEntry );
125 SFEntry* pUserData = (SFEntry*)pEntry->GetUserData();
126 if ( pUserData )
128 delete pUserData;
129 // TBD seem to get a Select event on node that is remove ( below )
130 // so need to be able to detect that this node is not to be
131 // processed in order to do this, setting userData to NULL ( must
132 // be a better way to do this )
133 pUserData = 0;
134 pEntry->SetUserData( pUserData );
139 void SFTreeListBox::deleteTree( SvLBoxEntry* pEntry )
142 delUserData( pEntry );
143 pEntry = FirstChild( pEntry );
144 while ( pEntry )
146 SvLBoxEntry* pNextEntry = NextSibling( pEntry );
147 deleteTree( pEntry );
148 GetModel()->Remove( pEntry );
149 pEntry = pNextEntry;
153 void SFTreeListBox::deleteAllTree()
155 SvLBoxEntry* pEntry = GetEntry( 0 );
157 // TBD - below is a candidate for a destroyAllTrees method
158 if ( pEntry )
160 while ( pEntry )
162 String text = GetEntryText( pEntry );
163 SvLBoxEntry* pNextEntry = NextSibling( pEntry ) ;
164 deleteTree( pEntry );
165 GetModel()->Remove( pEntry );
166 pEntry = pNextEntry;
171 void SFTreeListBox::Init( const ::rtl::OUString& language )
173 SetUpdateMode( FALSE );
175 deleteAllTree();
177 Reference< browse::XBrowseNode > rootNode;
178 Reference< XComponentContext > xCtx;
180 Sequence< Reference< browse::XBrowseNode > > children;
182 ::rtl::OUString userStr = ::rtl::OUString::createFromAscii("user");
183 ::rtl::OUString shareStr = ::rtl::OUString::createFromAscii("share");
185 ::rtl::OUString singleton = ::rtl::OUString::createFromAscii(
186 "/singletons/com.sun.star.script.browse.theBrowseNodeFactory" );
190 Reference < beans::XPropertySet > xProps(
191 ::comphelper::getProcessServiceFactory(), UNO_QUERY_THROW );
193 xCtx.set( xProps->getPropertyValue( rtl::OUString(
194 RTL_CONSTASCII_USTRINGPARAM("DefaultContext" ))), UNO_QUERY_THROW );
196 Reference< browse::XBrowseNodeFactory > xFac(
197 xCtx->getValueByName( singleton ), UNO_QUERY_THROW );
199 rootNode.set( xFac->createView(
200 browse::BrowseNodeFactoryViewTypes::MACROORGANIZER ) );
202 if ( rootNode.is() && rootNode->hasChildNodes() == sal_True )
204 children = rootNode->getChildNodes();
207 catch( Exception& e )
209 OSL_TRACE("Exception getting root browse node from factory: %s",
210 ::rtl::OUStringToOString(
211 e.Message , RTL_TEXTENCODING_ASCII_US ).pData->buffer );
212 // TODO exception handling
215 Reference<XModel> xDocumentModel;
216 for ( sal_Int32 n = 0; n < children.getLength(); n++ )
218 bool app = false;
219 ::rtl::OUString uiName = children[ n ]->getName();
220 ::rtl::OUString factoryURL;
221 if ( uiName.equals( userStr ) || uiName.equals( shareStr ) )
223 app = true;
224 if ( uiName.equals( userStr ) )
226 uiName = m_sMyMacros;
228 else
230 uiName = m_sProdMacros;
233 else
235 xDocumentModel.set(getDocumentModel(xCtx, uiName ), UNO_QUERY);
237 if ( xDocumentModel.is() )
239 Reference< ::com::sun::star::frame::XModuleManager >
240 xModuleManager( xCtx->getServiceManager()->createInstanceWithContext(
241 ::rtl::OUString::createFromAscii("com.sun.star.frame.ModuleManager"), xCtx ),
242 UNO_QUERY_THROW );
244 Reference<container::XNameAccess> xModuleConfig(
245 xModuleManager, UNO_QUERY_THROW );
246 // get the long name of the document:
247 Sequence<beans::PropertyValue> moduleDescr;
248 try{
249 ::rtl::OUString appModule = xModuleManager->identify( xDocumentModel );
250 xModuleConfig->getByName(appModule) >>= moduleDescr;
251 } catch(const uno::Exception&)
254 beans::PropertyValue const * pmoduleDescr =
255 moduleDescr.getConstArray();
256 for ( sal_Int32 pos = moduleDescr.getLength(); pos--; )
258 if (pmoduleDescr[ pos ].Name.equalsAsciiL(
259 RTL_CONSTASCII_STRINGPARAM(
260 "ooSetupFactoryEmptyDocumentURL") ))
262 pmoduleDescr[ pos ].Value >>= factoryURL;
263 break;
269 ::rtl::OUString lang( language );
270 Reference< browse::XBrowseNode > langEntries =
271 getLangNodeFromRootNode( children[ n ], lang );
273 /*SvLBoxEntry* pBasicManagerRootEntry =*/
274 insertEntry( uiName, app ? IMG_HARDDISK : IMG_DOCUMENT,
275 0, true, std::auto_ptr< SFEntry >(new SFEntry( OBJTYPE_SFROOT, langEntries, xDocumentModel )), factoryURL );
278 SetUpdateMode( TRUE );
281 Reference< XInterface >
282 SFTreeListBox::getDocumentModel( Reference< XComponentContext >& xCtx, ::rtl::OUString& docName )
284 Reference< XInterface > xModel;
285 Reference< lang::XMultiComponentFactory > mcf =
286 xCtx->getServiceManager();
287 Reference< frame::XDesktop > desktop (
288 mcf->createInstanceWithContext(
289 ::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop"), xCtx ),
290 UNO_QUERY );
292 Reference< container::XEnumerationAccess > componentsAccess =
293 desktop->getComponents();
294 Reference< container::XEnumeration > components =
295 componentsAccess->createEnumeration();
296 while (components->hasMoreElements())
298 Reference< frame::XModel > model(
299 components->nextElement(), UNO_QUERY );
300 if ( model.is() )
302 ::rtl::OUString sTdocUrl = ::comphelper::DocumentInfo::getDocumentTitle( model );
303 if( sTdocUrl.equals( docName ) )
305 xModel = model;
306 break;
310 return xModel;
313 Reference< browse::XBrowseNode >
314 SFTreeListBox::getLangNodeFromRootNode( Reference< browse::XBrowseNode >& rootNode, ::rtl::OUString& language )
316 Reference< browse::XBrowseNode > langNode;
320 Sequence < Reference< browse::XBrowseNode > > children = rootNode->getChildNodes();
321 for ( sal_Int32 n = 0; n < children.getLength(); n++ )
323 if ( children[ n ]->getName().equals( language ) )
325 langNode = children[ n ];
326 break;
330 catch ( Exception& )
332 // if getChildNodes() throws an exception we just return
333 // the empty Reference
335 return langNode;
338 void SFTreeListBox:: RequestSubEntries( SvLBoxEntry* pRootEntry, Reference< ::com::sun::star::script::browse::XBrowseNode >& node,
339 Reference< XModel >& model )
341 if (! node.is() )
343 return;
346 Sequence< Reference< browse::XBrowseNode > > children;
349 children = node->getChildNodes();
351 catch ( Exception& )
353 // if we catch an exception in getChildNodes then no entries are added
356 for ( sal_Int32 n = 0; n < children.getLength(); n++ )
358 ::rtl::OUString name( children[ n ]->getName() );
359 if ( children[ n ]->getType() != browse::BrowseNodeTypes::SCRIPT)
361 insertEntry( name, IMG_LIB, pRootEntry, true, std::auto_ptr< SFEntry >(new SFEntry( OBJTYPE_SCRIPTCONTAINER, children[ n ],model )));
363 else
365 if ( children[ n ]->getType() == browse::BrowseNodeTypes::SCRIPT )
367 insertEntry( name, IMG_MACRO, pRootEntry, false, std::auto_ptr< SFEntry >(new SFEntry( OBJTYPE_METHOD, children[ n ],model )));
374 void SFTreeListBox::UpdateEntries()
378 SvLBoxEntry* SFTreeListBox::FindEntry( SvLBoxEntry* , const String& , BYTE )
380 return 0;
383 long SFTreeListBox::ExpandingHdl()
385 return TRUE;
388 void SFTreeListBox::ExpandAllTrees()
392 SvLBoxEntry * SFTreeListBox::insertEntry(
393 String const & rText, USHORT nBitmap, SvLBoxEntry * pParent,
394 bool bChildrenOnDemand, std::auto_ptr< SFEntry > aUserData, ::rtl::OUString factoryURL )
396 SvLBoxEntry * p;
397 if( nBitmap == IMG_DOCUMENT && factoryURL.getLength() > 0 )
399 Image aImage = SvFileInformationManager::GetFileImage(
400 INetURLObject(factoryURL), false,
401 BMP_COLOR_NORMAL );
402 Image aHCImage = SvFileInformationManager::GetFileImage(
403 INetURLObject(factoryURL), false,
404 BMP_COLOR_HIGHCONTRAST );
405 p = InsertEntry(
406 rText, aImage, aImage, pParent, bChildrenOnDemand, LIST_APPEND,
407 aUserData.release()); // XXX possible leak
408 SetExpandedEntryBmp(p, aHCImage, BMP_COLOR_HIGHCONTRAST);
409 SetCollapsedEntryBmp(p, aHCImage, BMP_COLOR_HIGHCONTRAST);
411 else
413 p = insertEntry( rText, nBitmap, pParent, bChildrenOnDemand, aUserData );
415 return p;
418 SvLBoxEntry * SFTreeListBox::insertEntry(
419 String const & rText, USHORT nBitmap, SvLBoxEntry * pParent,
420 bool bChildrenOnDemand, std::auto_ptr< SFEntry > aUserData )
422 Image aHCImage, aImage;
423 if( nBitmap == IMG_HARDDISK )
425 aImage = m_hdImage;
426 aHCImage = m_hdImage_hc;
428 else if( nBitmap == IMG_LIB )
430 aImage = m_libImage;
431 aHCImage = m_libImage_hc;
433 else if( nBitmap == IMG_MACRO )
435 aImage = m_macImage;
436 aHCImage = m_macImage_hc;
438 else if( nBitmap == IMG_DOCUMENT )
440 aImage = m_docImage;
441 aHCImage = m_docImage_hc;
443 SvLBoxEntry * p = InsertEntry(
444 rText, aImage, aImage, pParent, bChildrenOnDemand, LIST_APPEND,
445 aUserData.release()); // XXX possible leak
446 SetExpandedEntryBmp(p, aHCImage, BMP_COLOR_HIGHCONTRAST);
447 SetCollapsedEntryBmp(p, aHCImage, BMP_COLOR_HIGHCONTRAST);
448 return p;
451 void __EXPORT SFTreeListBox::RequestingChilds( SvLBoxEntry* pEntry )
453 SFEntry* userData = 0;
454 if ( !pEntry )
456 return;
458 userData = (SFEntry*)pEntry->GetUserData();
460 Reference< browse::XBrowseNode > node;
461 Reference< XModel > model;
462 if ( userData && !userData->isLoaded() )
464 node = userData->GetNode();
465 model = userData->GetModel();
466 RequestSubEntries( pEntry, node, model );
467 userData->setLoaded();
471 void __EXPORT SFTreeListBox::ExpandedHdl()
473 /* SvLBoxEntry* pEntry = GetHdlEntry();
474 DBG_ASSERT( pEntry, "Was wurde zugeklappt?" );
476 if ( !IsExpanded( pEntry ) && pEntry->HasChildsOnDemand() )
478 SvLBoxEntry* pChild = FirstChild( pEntry );
479 while ( pChild )
481 GetModel()->Remove( pChild ); // Ruft auch den DTOR
482 pChild = FirstChild( pEntry );
487 // ----------------------------------------------------------------------------
488 // InputDialog ------------------------------------------------------------
489 // ----------------------------------------------------------------------------
490 InputDialog::InputDialog(Window * pParent, USHORT nMode )
491 : ModalDialog( pParent, SVX_RES( RID_DLG_NEWLIB ) ),
492 aText( this, SVX_RES( FT_NEWLIB ) ),
493 aEdit( this, SVX_RES( ED_LIBNAME ) ),
494 aOKButton( this, SVX_RES( PB_OK ) ),
495 aCancelButton( this, SVX_RES( PB_CANCEL ) )
497 aEdit.GrabFocus();
498 if ( nMode == INPUTMODE_NEWLIB )
500 SetText( String( SVX_RES( STR_NEWLIB ) ) );
502 else if ( nMode == INPUTMODE_NEWMACRO )
504 SetText( String( SVX_RES( STR_NEWMACRO ) ) );
505 aText.SetText( String( SVX_RES( STR_FT_NEWMACRO ) ) );
507 else if ( nMode == INPUTMODE_RENAME )
509 SetText( String( SVX_RES( STR_RENAME ) ) );
510 aText.SetText( String( SVX_RES( STR_FT_RENAME ) ) );
512 FreeResource();
514 // some resizing so that the text fits
515 Point point, newPoint;
516 Size siz, newSiz;
517 long gap;
519 USHORT style = TEXT_DRAW_MULTILINE | TEXT_DRAW_TOP |
520 TEXT_DRAW_LEFT | TEXT_DRAW_WORDBREAK;
522 // get dimensions of dialog instructions control
523 point = aText.GetPosPixel();
524 siz = aText.GetSizePixel();
526 // get dimensions occupied by text in the control
527 Rectangle rect =
528 GetTextRect( Rectangle( point, siz ), aText.GetText(), style );
529 newSiz = rect.GetSize();
531 // the gap is the difference between the text width and its control width
532 gap = siz.Height() - newSiz.Height();
534 //resize the text field
535 newSiz = Size( siz.Width(), siz.Height() - gap );
536 aText.SetSizePixel( newSiz );
538 //move the OK & cancel buttons
539 point = aEdit.GetPosPixel();
540 newPoint = Point( point.X(), point.Y() - gap );
541 aEdit.SetPosPixel( newPoint );
545 InputDialog::~InputDialog()
548 // ----------------------------------------------------------------------------
549 // ScriptOrgDialog ------------------------------------------------------------
550 // ----------------------------------------------------------------------------
551 SvxScriptOrgDialog::SvxScriptOrgDialog( Window* pParent, ::rtl::OUString language )
552 : SfxModalDialog( pParent, SVX_RES( RID_DLG_SCRIPTORGANIZER ) ),
553 aScriptsTxt( this, SVX_RES( SF_TXT_SCRIPTS ) ),
554 aScriptsBox( this, SVX_RES( SF_CTRL_SCRIPTSBOX ) ),
555 aRunButton( this, SVX_RES( SF_PB_RUN ) ),
556 aCloseButton( this, SVX_RES( SF_PB_CLOSE ) ),
557 aCreateButton( this, SVX_RES( SF_PB_CREATE ) ),
558 aEditButton( this, SVX_RES( SF_PB_EDIT ) ),
559 aRenameButton(this, SVX_RES( SF_PB_RENAME ) ),
560 aDelButton( this, SVX_RES( SF_PB_DEL ) ),
561 aHelpButton( this, SVX_RES( SF_PB_HELP ) ),
562 m_sLanguage( language ),
563 m_delErrStr( SVX_RES( RID_SVXSTR_DELFAILED ) ),
564 m_delErrTitleStr( SVX_RES( RID_SVXSTR_DELFAILED_TITLE ) ),
565 m_delQueryStr( SVX_RES( RID_SVXSTR_DELQUERY ) ),
566 m_delQueryTitleStr( SVX_RES( RID_SVXSTR_DELQUERY_TITLE ) ) ,
567 m_createErrStr( SVX_RES ( RID_SVXSTR_CREATEFAILED ) ),
568 m_createDupStr( SVX_RES ( RID_SVXSTR_CREATEFAILEDDUP ) ),
569 m_createErrTitleStr( SVX_RES( RID_SVXSTR_CREATEFAILED_TITLE ) ),
570 m_renameErrStr( SVX_RES ( RID_SVXSTR_RENAMEFAILED ) ),
571 m_renameErrTitleStr( SVX_RES( RID_SVXSTR_RENAMEFAILED_TITLE ) )
574 // must be a neater way to deal with the strings than as above
575 // append the language to the dialog title
576 String winTitle( GetText() );
577 winTitle.SearchAndReplace( String::CreateFromAscii( "%MACROLANG" ), language.pData->buffer );
578 SetText( winTitle );
580 aScriptsBox.SetSelectHdl( LINK( this, SvxScriptOrgDialog, ScriptSelectHdl ) );
581 aRunButton.SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
582 aCloseButton.SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
583 aRenameButton.SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
584 aEditButton.SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
585 aDelButton.SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
586 aCreateButton.SetClickHdl( LINK( this, SvxScriptOrgDialog, ButtonHdl ) );
588 aRunButton.Disable();
589 aRenameButton.Disable();
590 aEditButton.Disable();
591 aDelButton.Disable();
592 aCreateButton.Disable();
594 aScriptsBox.Init( m_sLanguage );
595 RestorePreviousSelection();
596 FreeResource();
599 __EXPORT SvxScriptOrgDialog::~SvxScriptOrgDialog()
601 // clear the SelectHdl so that it isn't called during the dtor
602 aScriptsBox.SetSelectHdl( Link() );
605 short SvxScriptOrgDialog::Execute()
608 SfxObjectShell *pDoc = SfxObjectShell::GetFirst();
610 // force load of MSPs for all documents
611 while ( pDoc )
613 Reference< provider::XScriptProviderSupplier > xSPS =
614 Reference< provider::XScriptProviderSupplier >
615 ( pDoc->GetModel(), UNO_QUERY );
616 if ( xSPS.is() )
618 Reference< provider::XScriptProvider > ScriptProvider =
619 xSPS->getScriptProvider();
622 pDoc = SfxObjectShell::GetNext(*pDoc);
624 aScriptsBox.ExpandAllTrees();
626 Window* pPrevDlgParent = Application::GetDefDialogParent();
627 Application::SetDefDialogParent( this );
628 short nRet = ModalDialog::Execute();
629 Application::SetDefDialogParent( pPrevDlgParent );
630 return nRet;
633 void SvxScriptOrgDialog::EnableButton( Button& , BOOL )
637 void SvxScriptOrgDialog::CheckButtons( Reference< browse::XBrowseNode >& node )
639 if ( node.is() )
641 if ( node->getType() == browse::BrowseNodeTypes::SCRIPT)
643 aRunButton.Enable();
645 else
647 aRunButton.Disable();
649 Reference< beans::XPropertySet > xProps( node, UNO_QUERY );
651 if ( !xProps.is() )
653 aEditButton.Disable();
654 aDelButton.Disable();
655 aCreateButton.Disable();
656 aRunButton.Disable();
657 return;
660 ::rtl::OUString sName;
661 sName = String::CreateFromAscii("Editable") ;
663 if ( getBoolProperty( xProps, sName ) )
665 aEditButton.Enable();
667 else
669 aEditButton.Disable();
672 sName = String::CreateFromAscii("Deletable") ;
674 if ( getBoolProperty( xProps, sName ) )
676 aDelButton.Enable();
678 else
680 aDelButton.Disable();
683 sName = String::CreateFromAscii("Creatable") ;
685 if ( getBoolProperty( xProps, sName ) )
687 aCreateButton.Enable();
689 else
691 aCreateButton.Disable();
694 sName = String::CreateFromAscii("Renamable") ;
696 if ( getBoolProperty( xProps, sName ) )
698 aRenameButton.Enable();
700 else
702 aRenameButton.Disable();
705 else
707 // no node info available, disable all configurable actions
708 aDelButton.Disable();
709 aCreateButton.Disable();
710 aEditButton.Disable();
711 aRunButton.Disable();
712 aRenameButton.Disable();
716 IMPL_LINK_INLINE_START( SvxScriptOrgDialog, MacroDoubleClickHdl, SvTreeListBox *, EMPTYARG )
718 return 0;
721 IMPL_LINK_INLINE_END( SvxScriptOrgDialog, MacroDoubleClickHdl, SvTreeListBox *, EMPTYARG )
723 IMPL_LINK( SvxScriptOrgDialog, ScriptSelectHdl, SvTreeListBox *, pBox )
725 if ( !pBox->IsSelected( pBox->GetHdlEntry() ) )
727 return 0;
730 SvLBoxEntry* pEntry = pBox->GetHdlEntry();
732 SFEntry* userData = 0;
733 if ( !pEntry )
735 return 0;
737 userData = (SFEntry*)pEntry->GetUserData();
739 Reference< browse::XBrowseNode > node;
740 if ( userData )
742 node = userData->GetNode();
743 CheckButtons( node );
746 return 0;
749 IMPL_LINK( SvxScriptOrgDialog, ButtonHdl, Button *, pButton )
751 if ( pButton == &aCloseButton )
753 StoreCurrentSelection();
754 EndDialog( 0 );
756 if ( pButton == &aEditButton ||
757 pButton == &aCreateButton ||
758 pButton == &aDelButton ||
759 pButton == &aRunButton ||
760 pButton == &aRenameButton )
763 if ( aScriptsBox.IsSelected( aScriptsBox.GetHdlEntry() ) )
765 SvLBoxEntry* pEntry = aScriptsBox.GetHdlEntry();
766 SFEntry* userData = 0;
767 if ( !pEntry )
769 return 0;
771 userData = (SFEntry*)pEntry->GetUserData();
772 if ( userData )
774 Reference< browse::XBrowseNode > node;
775 Reference< XModel > xModel;
777 node = userData->GetNode();
778 xModel = userData->GetModel();
780 if ( !node.is() )
782 return 0;
785 if ( pButton == &aRunButton )
787 ::rtl::OUString tmpString;
788 Reference< beans::XPropertySet > xProp( node, UNO_QUERY );
789 Reference< provider::XScriptProvider > mspNode;
790 if( !xProp.is() )
792 return 0;
795 if ( xModel.is() )
797 Reference< XEmbeddedScripts > xEmbeddedScripts( xModel, UNO_QUERY);
798 if( !xEmbeddedScripts.is() )
800 return 0;
803 if (!xEmbeddedScripts->getAllowMacroExecution())
805 // Please FIXME: Show a message box if AllowMacroExecution is false
806 return 0;
811 SvLBoxEntry* pParent = aScriptsBox.GetParent( pEntry );
812 while ( pParent && !mspNode.is() )
814 SFEntry* mspUserData = (SFEntry*)pParent->GetUserData();
815 mspNode.set( mspUserData->GetNode() , UNO_QUERY );
816 pParent = aScriptsBox.GetParent( pParent );
818 xProp->getPropertyValue( String::CreateFromAscii("URI" ) ) >>= tmpString;
819 const String scriptURL( tmpString );
821 if ( mspNode.is() )
825 Reference< provider::XScript > xScript(
826 mspNode->getScript( scriptURL ), UNO_QUERY_THROW );
828 const Sequence< Any > args(0);
829 Any aRet;
830 Sequence< sal_Int16 > outIndex;
831 Sequence< Any > outArgs( 0 );
832 aRet = xScript->invoke( args, outIndex, outArgs );
834 catch ( reflection::InvocationTargetException& ite )
836 ::com::sun::star::uno::Any a = makeAny(ite);
837 ShowErrorDialog(a);
839 catch ( provider::ScriptFrameworkErrorException& ite )
841 ::com::sun::star::uno::Any a = makeAny(ite);
842 ShowErrorDialog(a);
844 catch ( RuntimeException& re )
846 ::com::sun::star::uno::Any a = makeAny(re);
847 ShowErrorDialog(a);
849 catch ( Exception& e )
851 ::com::sun::star::uno::Any a = makeAny(e);
852 ShowErrorDialog(a);
855 StoreCurrentSelection();
856 EndDialog( 0 );
858 else if ( pButton == &aEditButton )
860 Reference< script::XInvocation > xInv( node, UNO_QUERY );
861 if ( xInv.is() )
863 StoreCurrentSelection();
864 EndDialog( 0 );
865 Sequence< Any > args(0);
866 Sequence< Any > outArgs( 0 );
867 Sequence< sal_Int16 > outIndex;
870 // ISSUE need code to run script here
871 xInv->invoke( ::rtl::OUString::createFromAscii( "Editable" ), args, outIndex, outArgs );
873 catch( Exception& e )
875 OSL_TRACE("Caught exception trying to invoke %s", ::rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
880 else if ( pButton == &aCreateButton )
882 createEntry( pEntry );
884 else if ( pButton == &aDelButton )
886 deleteEntry( pEntry );
888 else if ( pButton == &aRenameButton )
890 renameEntry( pEntry );
895 return 0;
898 Reference< browse::XBrowseNode > SvxScriptOrgDialog::getBrowseNode( SvLBoxEntry* pEntry )
900 Reference< browse::XBrowseNode > node;
901 if ( pEntry )
903 SFEntry* userData = (SFEntry*)pEntry->GetUserData();
904 if ( userData )
906 node = userData->GetNode();
910 return node;
913 Reference< XModel > SvxScriptOrgDialog::getModel( SvLBoxEntry* pEntry )
915 Reference< XModel > model;
916 if ( pEntry )
918 SFEntry* userData = (SFEntry*)pEntry->GetUserData();
919 if ( userData )
921 model = userData->GetModel();
925 return model;
928 Reference< XInterface >
929 SvxScriptOrgDialog::getDocumentModel( Reference< XComponentContext >& xCtx, ::rtl::OUString& docName )
931 Reference< XInterface > xModel;
932 Reference< lang::XMultiComponentFactory > mcf =
933 xCtx->getServiceManager();
934 Reference< frame::XDesktop > desktop (
935 mcf->createInstanceWithContext(
936 ::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop"), xCtx ),
937 UNO_QUERY );
939 Reference< container::XEnumerationAccess > componentsAccess =
940 desktop->getComponents();
941 Reference< container::XEnumeration > components =
942 componentsAccess->createEnumeration();
943 while (components->hasMoreElements())
945 Reference< frame::XModel > model(
946 components->nextElement(), UNO_QUERY );
947 if ( model.is() )
949 ::rtl::OUString sTdocUrl = ::comphelper::DocumentInfo::getDocumentTitle( model );
950 if( sTdocUrl.equals( docName ) )
952 xModel = model;
953 break;
957 return xModel;
960 void SvxScriptOrgDialog::createEntry( SvLBoxEntry* pEntry )
963 Reference< browse::XBrowseNode > aChildNode;
964 Reference< browse::XBrowseNode > node = getBrowseNode( pEntry );
965 Reference< script::XInvocation > xInv( node, UNO_QUERY );
967 if ( xInv.is() )
969 ::rtl::OUString aNewName;
970 ::rtl::OUString aNewStdName;
971 USHORT nMode = INPUTMODE_NEWLIB;
972 if( aScriptsBox.GetModel()->GetDepth( pEntry ) == 0 )
974 aNewStdName = ::rtl::OUString::createFromAscii( "Library" ) ;
976 else
978 aNewStdName = ::rtl::OUString::createFromAscii( "Macro" ) ;
979 nMode = INPUTMODE_NEWMACRO;
981 //do we need L10N for this? ie somethng like:
982 //String aNewStdName( ResId( STR_STDMODULENAME ) );
983 BOOL bValid = FALSE;
984 USHORT i = 1;
986 Sequence< Reference< browse::XBrowseNode > > childNodes;
987 // no children => ok to create Parcel1 or Script1 without checking
990 if( node->hasChildNodes() == sal_False )
992 aNewName = aNewStdName;
993 aNewName += String::CreateFromInt32( i );
994 bValid = TRUE;
996 else
998 childNodes = node->getChildNodes();
1001 catch ( Exception& )
1003 // ignore, will continue on with empty sequence
1006 ::rtl::OUString extn;
1007 while ( !bValid )
1009 aNewName = aNewStdName;
1010 aNewName += String::CreateFromInt32( i );
1011 BOOL bFound = FALSE;
1012 if(childNodes.getLength() > 0 )
1014 ::rtl::OUString nodeName = childNodes[0]->getName();
1015 sal_Int32 extnPos = nodeName.lastIndexOf( '.' );
1016 if(extnPos>0)
1017 extn = nodeName.copy(extnPos);
1019 for( sal_Int32 index = 0; index < childNodes.getLength(); index++ )
1021 if ( (aNewName+extn).equals( childNodes[index]->getName() ) )
1023 bFound = TRUE;
1024 break;
1027 if( bFound )
1029 i++;
1031 else
1033 bValid = TRUE;
1037 std::auto_ptr< InputDialog > xNewDlg( new InputDialog( static_cast<Window*>(this), nMode ) );
1038 xNewDlg->SetObjectName( aNewName );
1042 if ( xNewDlg->Execute() && xNewDlg->GetObjectName().Len() )
1044 ::rtl::OUString aUserSuppliedName = xNewDlg->GetObjectName();
1045 bValid = TRUE;
1046 for( sal_Int32 index = 0; index < childNodes.getLength(); index++ )
1048 if ( (aUserSuppliedName+extn).equals( childNodes[index]->getName() ) )
1050 bValid = FALSE;
1051 String aError( m_createErrStr );
1052 aError.Append( m_createDupStr );
1053 ErrorBox aErrorBox( static_cast<Window*>(this), WB_OK | RET_OK, aError );
1054 aErrorBox.SetText( m_createErrTitleStr );
1055 aErrorBox.Execute();
1056 xNewDlg->SetObjectName( aNewName );
1057 break;
1060 if( bValid )
1061 aNewName = aUserSuppliedName;
1063 else
1065 // user hit cancel or hit OK with nothing in the editbox
1067 return;
1070 while ( !bValid );
1072 // open up parent node (which ensures it's loaded)
1073 aScriptsBox.RequestingChilds( pEntry );
1075 Sequence< Any > args( 1 );
1076 args[ 0 ] <<= aNewName;
1077 Sequence< Any > outArgs( 0 );
1078 Sequence< sal_Int16 > outIndex;
1081 Any aResult;
1082 aResult = xInv->invoke( ::rtl::OUString::createFromAscii( "Creatable" ), args, outIndex, outArgs );
1083 Reference< browse::XBrowseNode > newNode( aResult, UNO_QUERY );
1084 aChildNode = newNode;
1087 catch( Exception& e )
1089 OSL_TRACE("Caught exception trying to Create %s",
1090 ::rtl::OUStringToOString(
1091 e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
1094 if ( aChildNode.is() )
1096 String aChildName = aChildNode->getName();
1097 SvLBoxEntry* pNewEntry = NULL;
1100 ::rtl::OUString name( aChildName );
1101 Reference<XModel> xDocumentModel = getModel( pEntry );
1103 // ISSUE do we need to remove all entries for parent
1104 // to achieve sort? Just need to determine position
1105 // SvTreeListBox::InsertEntry can take position arg
1106 // -- Basic doesn't do this on create.
1107 // Suppose we could avoid this too. -> created nodes are
1108 // not in alphabetical order
1109 if ( aChildNode->getType() == browse::BrowseNodeTypes::SCRIPT )
1111 pNewEntry = aScriptsBox.insertEntry( aChildName,
1112 IMG_MACRO, pEntry, false, std::auto_ptr< SFEntry >(new SFEntry( OBJTYPE_METHOD, aChildNode,xDocumentModel ) ) );
1115 else
1117 pNewEntry = aScriptsBox.insertEntry( aChildName,
1118 IMG_LIB, pEntry, false, std::auto_ptr< SFEntry >(new SFEntry( OBJTYPE_SCRIPTCONTAINER, aChildNode,xDocumentModel ) ) );
1119 // If the Parent is not loaded then set to
1120 // loaded, this will prevent RequestingChilds ( called
1121 // from vcl via RequestingChilds ) from
1122 // creating new ( duplicate ) children
1123 SFEntry* userData = (SFEntry*)pEntry->GetUserData();
1124 if ( userData && !userData->isLoaded() )
1126 userData->setLoaded();
1129 aScriptsBox.SetCurEntry( pNewEntry );
1130 aScriptsBox.Select( aScriptsBox.GetCurEntry() );
1133 else
1135 //ISSUE L10N & message from exception?
1136 String aError( m_createErrStr );
1137 ErrorBox aErrorBox( static_cast<Window*>(this), WB_OK | RET_OK, aError );
1138 aErrorBox.SetText( m_createErrTitleStr );
1139 aErrorBox.Execute();
1143 void SvxScriptOrgDialog::renameEntry( SvLBoxEntry* pEntry )
1146 Reference< browse::XBrowseNode > aChildNode;
1147 Reference< browse::XBrowseNode > node = getBrowseNode( pEntry );
1148 Reference< script::XInvocation > xInv( node, UNO_QUERY );
1150 if ( xInv.is() )
1152 ::rtl::OUString aNewName = node->getName();
1153 sal_Int32 extnPos = aNewName.lastIndexOf( '.' );
1154 ::rtl::OUString extn;
1155 if(extnPos>0)
1157 extn = aNewName.copy(extnPos);
1158 aNewName = aNewName.copy(0,extnPos);
1160 USHORT nMode = INPUTMODE_RENAME;
1162 std::auto_ptr< InputDialog > xNewDlg( new InputDialog( static_cast<Window*>(this), nMode ) );
1163 xNewDlg->SetObjectName( aNewName );
1165 BOOL bValid;
1168 if ( xNewDlg->Execute() && xNewDlg->GetObjectName().Len() )
1170 ::rtl::OUString aUserSuppliedName = xNewDlg->GetObjectName();
1171 bValid = TRUE;
1173 for( sal_Int32 index = 0; index < childNodes.getLength(); index++ )
1175 if ( (aUserSuppliedName+extn).equals( childNodes[index]->getName() ) )
1177 bValid = FALSE;
1178 String aError( m_createErrStr );
1179 aError.Append( m_createDupStr );
1180 ErrorBox aErrorBox( static_cast<Window*>(this), WB_OK | RET_OK, aError );
1181 aErrorBox.SetText( m_createErrTitleStr );
1182 aErrorBox.Execute();
1183 xNewDlg->SetObjectName( aNewName );
1184 break;
1186 } */
1187 if( bValid )
1188 aNewName = aUserSuppliedName;
1190 else
1192 // user hit cancel or hit OK with nothing in the editbox
1193 return;
1196 while ( !bValid );
1198 Sequence< Any > args( 1 );
1199 args[ 0 ] <<= aNewName;
1200 Sequence< Any > outArgs( 0 );
1201 Sequence< sal_Int16 > outIndex;
1204 Any aResult;
1205 aResult = xInv->invoke( ::rtl::OUString::createFromAscii( "Renamable" ), args, outIndex, outArgs );
1206 Reference< browse::XBrowseNode > newNode( aResult, UNO_QUERY );
1207 aChildNode = newNode;
1210 catch( Exception& e )
1212 OSL_TRACE("Caught exception trying to Rename %s",
1213 ::rtl::OUStringToOString(
1214 e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
1217 if ( aChildNode.is() )
1219 aScriptsBox.SetEntryText( pEntry, aChildNode->getName() );
1220 aScriptsBox.SetCurEntry( pEntry );
1221 aScriptsBox.Select( aScriptsBox.GetCurEntry() );
1224 else
1226 //ISSUE L10N & message from exception?
1227 String aError( m_renameErrStr );
1228 ErrorBox aErrorBox( static_cast<Window*>(this), WB_OK | RET_OK, aError );
1229 aErrorBox.SetText( m_renameErrTitleStr );
1230 aErrorBox.Execute();
1233 void SvxScriptOrgDialog::deleteEntry( SvLBoxEntry* pEntry )
1235 sal_Bool result = sal_False;
1236 Reference< browse::XBrowseNode > node = getBrowseNode( pEntry );
1237 // ISSUE L10N string & can we centre list?
1238 String aQuery( m_delQueryStr );
1239 aQuery.Append( getListOfChildren( node, 0 ) );
1240 QueryBox aQueryBox( static_cast<Window*>(this), WB_YES_NO | WB_DEF_YES, aQuery );
1241 aQueryBox.SetText( m_delQueryTitleStr );
1242 if ( aQueryBox.Execute() == RET_NO )
1244 return;
1247 Reference< script::XInvocation > xInv( node, UNO_QUERY );
1248 if ( xInv.is() )
1250 Sequence< Any > args( 0 );
1251 Sequence< Any > outArgs( 0 );
1252 Sequence< sal_Int16 > outIndex;
1255 Any aResult;
1256 aResult = xInv->invoke( ::rtl::OUString::createFromAscii( "Deletable" ), args, outIndex, outArgs );
1257 aResult >>= result; // or do we just assume true if no exception ?
1259 catch( Exception& e )
1261 OSL_TRACE("Caught exception trying to delete %s",
1262 ::rtl::OUStringToOString(
1263 e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
1267 if ( result == sal_True )
1269 aScriptsBox.deleteTree( pEntry );
1270 aScriptsBox.GetModel()->Remove( pEntry );
1272 else
1274 //ISSUE L10N & message from exception?
1275 ErrorBox aErrorBox( static_cast<Window*>(this), WB_OK | RET_OK, m_delErrStr );
1276 aErrorBox.SetText( m_delErrTitleStr );
1277 aErrorBox.Execute();
1282 BOOL SvxScriptOrgDialog::getBoolProperty( Reference< beans::XPropertySet >& xProps,
1283 ::rtl::OUString& propName )
1285 BOOL result = false;
1288 sal_Bool bTemp = sal_False;
1289 xProps->getPropertyValue( propName ) >>= bTemp;
1290 result = ( bTemp == sal_True );
1292 catch ( Exception& )
1294 return result;
1296 return result;
1299 String SvxScriptOrgDialog::getListOfChildren( Reference< browse::XBrowseNode > node, int depth )
1301 String result;
1302 result.Append( String::CreateFromAscii( "\n" ) );
1303 for( int i=0;i<=depth;i++ )
1305 result.Append( String::CreateFromAscii( "\t" ) );
1307 result.Append( String( node->getName() ) );
1311 if ( node->hasChildNodes() == sal_True )
1313 Sequence< Reference< browse::XBrowseNode > > children
1314 = node->getChildNodes();
1315 for ( sal_Int32 n = 0; n < children.getLength(); n++ )
1317 result.Append( getListOfChildren( children[ n ] , depth+1 ) );
1321 catch ( Exception& )
1323 // ignore, will return an empty string
1326 return result;
1329 Selection_hash SvxScriptOrgDialog::m_lastSelection;
1331 void SvxScriptOrgDialog::StoreCurrentSelection()
1333 String aDescription;
1334 if ( aScriptsBox.IsSelected( aScriptsBox.GetHdlEntry() ) )
1336 SvLBoxEntry* pEntry = aScriptsBox.GetHdlEntry();
1337 while( pEntry )
1339 aDescription.Insert( aScriptsBox.GetEntryText( pEntry ), 0 );
1340 pEntry = aScriptsBox.GetParent( pEntry );
1341 if ( pEntry )
1342 aDescription.Insert( ';', 0 );
1344 ::rtl::OUString sDesc( aDescription );
1345 m_lastSelection[ m_sLanguage ] = sDesc;
1349 void SvxScriptOrgDialog::RestorePreviousSelection()
1351 String aStoredEntry = String( m_lastSelection[ m_sLanguage ] );
1352 if( aStoredEntry.Len() <= 0 )
1353 return;
1354 SvLBoxEntry* pEntry = 0;
1355 USHORT nIndex = 0;
1356 while ( nIndex != STRING_NOTFOUND )
1358 String aTmp( aStoredEntry.GetToken( 0, ';', nIndex ) );
1359 SvLBoxEntry* pTmpEntry = aScriptsBox.FirstChild( pEntry );
1360 ::rtl::OUString debugStr(aTmp);
1361 while ( pTmpEntry )
1363 debugStr = ::rtl::OUString(aScriptsBox.GetEntryText( pTmpEntry ));
1364 if ( aScriptsBox.GetEntryText( pTmpEntry ) == aTmp )
1366 pEntry = pTmpEntry;
1367 break;
1369 pTmpEntry = aScriptsBox.NextSibling( pTmpEntry );
1371 if ( !pTmpEntry )
1372 break;
1373 aScriptsBox.RequestingChilds( pEntry );
1375 aScriptsBox.SetCurEntry( pEntry );
1378 BOOL SFTreeListBox::dialogSort1( Reference< browse::XBrowseNode > node1,
1379 Reference< browse::XBrowseNode > node2 )
1381 ::rtl::OUString userStr = ::rtl::OUString::createFromAscii("user");
1382 ::rtl::OUString shareStr = ::rtl::OUString::createFromAscii("share");
1383 if( node1->getName().equals( userStr ) )
1384 return true;
1385 if( node2->getName().equals( userStr ) )
1386 return false;
1387 if( node1->getName().equals( shareStr ) )
1388 return true;
1389 if( node2->getName().equals( shareStr ) )
1390 return false;
1391 return dialogSort2( node1, node2 );
1394 BOOL SFTreeListBox::dialogSort2( Reference< browse::XBrowseNode > node1,
1395 Reference< browse::XBrowseNode > node2 )
1397 return ( node1->getName().compareTo( node2->getName() ) < 0 );
1400 ::rtl::OUString ReplaceString(
1401 const ::rtl::OUString& source,
1402 const ::rtl::OUString& token,
1403 const ::rtl::OUString& value )
1405 sal_Int32 pos = source.indexOf( token );
1407 if ( pos != -1 && value.getLength() != 0 )
1409 return source.replaceAt( pos, token.getLength(), value );
1411 else
1413 return source;
1417 ::rtl::OUString FormatErrorString(
1418 const ::rtl::OUString& unformatted,
1419 const ::rtl::OUString& language,
1420 const ::rtl::OUString& script,
1421 const ::rtl::OUString& line,
1422 const ::rtl::OUString& type,
1423 const ::rtl::OUString& message )
1425 ::rtl::OUString result = unformatted.copy( 0 );
1427 result = ReplaceString(
1428 result, ::rtl::OUString::createFromAscii( "%LANGUAGENAME" ), language );
1429 result = ReplaceString(
1430 result, ::rtl::OUString::createFromAscii( "%SCRIPTNAME" ), script );
1431 result = ReplaceString(
1432 result, ::rtl::OUString::createFromAscii( "%LINENUMBER" ), line );
1434 if ( type.getLength() != 0 )
1436 result += ::rtl::OUString::createFromAscii( "\n\n" );
1437 result += ::rtl::OUString(String(SVX_RES(RID_SVXSTR_ERROR_TYPE_LABEL)));
1438 result += ::rtl::OUString::createFromAscii( " " );
1439 result += type;
1442 if ( message.getLength() != 0 )
1444 result += ::rtl::OUString::createFromAscii( "\n\n" );
1445 result += ::rtl::OUString(String(SVX_RES(RID_SVXSTR_ERROR_MESSAGE_LABEL)));
1446 result += ::rtl::OUString::createFromAscii( " " );
1447 result += message;
1450 return result;
1453 ::rtl::OUString GetErrorMessage(
1454 const provider::ScriptErrorRaisedException& eScriptError )
1456 ::rtl::OUString unformatted = String( SVX_RES( RID_SVXSTR_ERROR_AT_LINE ) );
1458 ::rtl::OUString unknown = ::rtl::OUString::createFromAscii( "UNKNOWN" );
1459 ::rtl::OUString language = unknown;
1460 ::rtl::OUString script = unknown;
1461 ::rtl::OUString line = unknown;
1462 ::rtl::OUString type = ::rtl::OUString();
1463 ::rtl::OUString message = eScriptError.Message;
1465 if ( eScriptError.language.getLength() != 0 )
1467 language = eScriptError.language;
1470 if ( eScriptError.scriptName.getLength() != 0 )
1472 script = eScriptError.scriptName;
1475 if ( eScriptError.Message.getLength() != 0 )
1477 message = eScriptError.Message;
1479 if ( eScriptError.lineNum != -1 )
1481 line = ::rtl::OUString::valueOf( eScriptError.lineNum );
1482 unformatted = String(
1483 SVX_RES( RID_SVXSTR_ERROR_AT_LINE ) );
1485 else
1487 unformatted = String(
1488 SVX_RES( RID_SVXSTR_ERROR_RUNNING ) );
1491 return FormatErrorString(
1492 unformatted, language, script, line, type, message );
1495 ::rtl::OUString GetErrorMessage(
1496 const provider::ScriptExceptionRaisedException& eScriptException )
1498 ::rtl::OUString unformatted =
1499 String( SVX_RES( RID_SVXSTR_EXCEPTION_AT_LINE ) );
1501 ::rtl::OUString unknown = ::rtl::OUString::createFromAscii( "UNKNOWN" );
1502 ::rtl::OUString language = unknown;
1503 ::rtl::OUString script = unknown;
1504 ::rtl::OUString line = unknown;
1505 ::rtl::OUString type = unknown;
1506 ::rtl::OUString message = eScriptException.Message;
1508 if ( eScriptException.language.getLength() != 0 )
1510 language = eScriptException.language;
1512 if ( eScriptException.scriptName.getLength() != 0 )
1514 script = eScriptException.scriptName;
1517 if ( eScriptException.Message.getLength() != 0 )
1519 message = eScriptException.Message;
1522 if ( eScriptException.lineNum != -1 )
1524 line = ::rtl::OUString::valueOf( eScriptException.lineNum );
1525 unformatted = String(
1526 SVX_RES( RID_SVXSTR_EXCEPTION_AT_LINE ) );
1528 else
1530 unformatted = String(
1531 SVX_RES( RID_SVXSTR_EXCEPTION_RUNNING ) );
1534 if ( eScriptException.exceptionType.getLength() != 0 )
1536 type = eScriptException.exceptionType;
1539 return FormatErrorString(
1540 unformatted, language, script, line, type, message );
1543 ::rtl::OUString GetErrorMessage(
1544 const provider::ScriptFrameworkErrorException& sError )
1546 ::rtl::OUString unformatted = String(
1547 SVX_RES( RID_SVXSTR_FRAMEWORK_ERROR_RUNNING ) );
1549 ::rtl::OUString language =
1550 ::rtl::OUString::createFromAscii( "UNKNOWN" );
1552 ::rtl::OUString script =
1553 ::rtl::OUString::createFromAscii( "UNKNOWN" );
1555 ::rtl::OUString message;
1557 if ( sError.scriptName.getLength() > 0 )
1559 script = sError.scriptName;
1561 if ( sError.language.getLength() > 0 )
1563 language = sError.language;
1565 if ( sError.errorType == provider::ScriptFrameworkErrorType::NOTSUPPORTED )
1567 message = String(
1568 SVX_RES( RID_SVXSTR_ERROR_LANG_NOT_SUPPORTED ) );
1569 message = ReplaceString(
1570 message, ::rtl::OUString::createFromAscii( "%LANGUAGENAME" ), language );
1573 else
1575 message = sError.Message;
1577 return FormatErrorString(
1578 unformatted, language, script, ::rtl::OUString(), ::rtl::OUString(), message );
1581 ::rtl::OUString GetErrorMessage( const RuntimeException& re )
1583 Type t = ::getCppuType( &re );
1584 ::rtl::OUString message = t.getTypeName();
1585 message += re.Message;
1587 return message;
1590 ::rtl::OUString GetErrorMessage( const Exception& e )
1592 Type t = ::getCppuType( &e );
1593 ::rtl::OUString message = t.getTypeName();
1594 message += e.Message;
1596 return message;
1599 ::rtl::OUString GetErrorMessage( const com::sun::star::uno::Any& aException )
1601 ::rtl::OUString exType;
1602 if ( aException.getValueType() ==
1603 ::getCppuType( (const reflection::InvocationTargetException* ) NULL ) )
1605 reflection::InvocationTargetException ite;
1606 aException >>= ite;
1607 if ( ite.TargetException.getValueType() == ::getCppuType( ( const provider::ScriptErrorRaisedException* ) NULL ) )
1609 // Error raised by script
1610 provider::ScriptErrorRaisedException scriptError;
1611 ite.TargetException >>= scriptError;
1612 return GetErrorMessage( scriptError );
1614 else if ( ite.TargetException.getValueType() == ::getCppuType( ( const provider::ScriptExceptionRaisedException* ) NULL ) )
1616 // Exception raised by script
1617 provider::ScriptExceptionRaisedException scriptException;
1618 ite.TargetException >>= scriptException;
1619 return GetErrorMessage( scriptException );
1621 else
1623 // Unknown error, shouldn't happen
1624 // OSL_ASSERT(...)
1628 else if ( aException.getValueType() == ::getCppuType( ( const provider::ScriptFrameworkErrorException* ) NULL ) )
1630 // A Script Framework error has occured
1631 provider::ScriptFrameworkErrorException sfe;
1632 aException >>= sfe;
1633 return GetErrorMessage( sfe );
1636 // unknown exception
1637 Exception e;
1638 RuntimeException rte;
1639 if ( aException >>= rte )
1641 return GetErrorMessage( rte );
1644 aException >>= e;
1645 return GetErrorMessage( e );
1649 SvxScriptErrorDialog::SvxScriptErrorDialog(
1650 Window* , ::com::sun::star::uno::Any aException )
1651 : m_sMessage()
1653 ::vos::OGuard aGuard( Application::GetSolarMutex() );
1654 m_sMessage = GetErrorMessage( aException );
1657 SvxScriptErrorDialog::~SvxScriptErrorDialog()
1661 short SvxScriptErrorDialog::Execute()
1663 // Show Error dialog asynchronously
1665 // Pass a copy of the message to the ShowDialog method as the
1666 // SvxScriptErrorDialog may be deleted before ShowDialog is called
1667 Application::PostUserEvent(
1668 LINK( this, SvxScriptErrorDialog, ShowDialog ),
1669 new rtl::OUString( m_sMessage ) );
1671 return 0;
1674 IMPL_LINK( SvxScriptErrorDialog, ShowDialog, ::rtl::OUString*, pMessage )
1676 ::rtl::OUString message;
1678 if ( pMessage && pMessage->getLength() != 0 )
1680 message = *pMessage;
1682 else
1684 message = String( SVX_RES( RID_SVXSTR_ERROR_TITLE ) );
1687 MessBox* pBox = new WarningBox( NULL, WB_OK, message );
1688 pBox->SetText( SVX_RES( RID_SVXSTR_ERROR_TITLE ) );
1689 pBox->Execute();
1691 if ( pBox ) delete pBox;
1692 if ( pMessage ) delete pMessage;
1694 return 0;