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