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