xref: /trunk/main/basctl/source/basicide/bastype2.cxx (revision 0848378b)
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_basctl.hxx"
26 
27 #include <memory>
28 
29 #include "vcl/bitmap.hxx"
30 
31 #include <ide_pch.hxx>
32 
33 
34 #include <basidesh.hrc>
35 #include <bastypes.hxx>
36 #include <bastype2.hxx>
37 #include <basobj.hxx>
38 #include <baside2.hrc>
39 #include <iderid.hxx>
40 #include <tools/urlobj.hxx>
41 #include <tools/diagnose_ex.h>
42 #include <basic/sbx.hxx>
43 #include <svtools/imagemgr.hxx>
44 #include <com/sun/star/script/XLibraryContainer.hpp>
45 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
46 #include <com/sun/star/frame/XModuleManager.hpp>
47 #include <comphelper/processfactory.hxx>
48 #include <comphelper/componentcontext.hxx>
49 #include <map>
50 #include <com/sun/star/script/ModuleType.hpp>
51 #include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
52 #include <com/sun/star/container/XNameContainer.hpp>
53 #include <com/sun/star/lang/XServiceInfo.hpp>
54 #include <com/sun/star/container/XNamed.hpp>
55 
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star;
58 
getObjectName(const uno::Reference<container::XNameContainer> & rLib,const String & rModName,String & rObjName)59 void ModuleInfoHelper::getObjectName( const uno::Reference< container::XNameContainer >& rLib, const String& rModName, String& rObjName )
60 {
61     try
62     {
63                 uno::Reference< script::vba::XVBAModuleInfo > xVBAModuleInfo( rLib, uno::UNO_QUERY );
64                 if ( xVBAModuleInfo.is() && xVBAModuleInfo->hasModuleInfo( rModName ) )
65                 {
66 			script::ModuleInfo aModuleInfo = xVBAModuleInfo->getModuleInfo( rModName );
67 			uno::Any aObject( aModuleInfo.ModuleObject );
68 			uno::Reference< lang::XServiceInfo > xServiceInfo( aObject, uno::UNO_QUERY );
69 			if( xServiceInfo.is() && xServiceInfo->supportsService( rtl::OUString::createFromAscii( "ooo.vba.excel.Worksheet" ) ) )
70 			{
71 				uno::Reference< container::XNamed > xNamed( aObject, uno::UNO_QUERY );
72 				if( xNamed.is() )
73 					rObjName = xNamed->getName();
74 			}
75 		}
76     }
77     catch( uno::Exception& )
78     {
79     }
80 }
81 
getModuleType(const uno::Reference<container::XNameContainer> & rLib,const String & rModName)82 sal_Int32 ModuleInfoHelper::getModuleType(  const uno::Reference< container::XNameContainer >& rLib, const String& rModName )
83 {
84     sal_Int32 nType = script::ModuleType::NORMAL;
85     uno::Reference< script::vba::XVBAModuleInfo > xVBAModuleInfo( rLib, uno::UNO_QUERY );
86     if ( xVBAModuleInfo.is() && xVBAModuleInfo->hasModuleInfo( rModName ) )
87     {
88         script::ModuleInfo aModuleInfo = xVBAModuleInfo->getModuleInfo( rModName );
89         nType = aModuleInfo.ModuleType;
90     }
91     return nType;
92 }
93 
~BasicEntry()94 BasicEntry::~BasicEntry()
95 {
96 }
97 
BasicDocumentEntry(const ScriptDocument & rDocument,LibraryLocation eLocation,BasicEntryType eType)98 BasicDocumentEntry::BasicDocumentEntry( const ScriptDocument& rDocument, LibraryLocation eLocation, BasicEntryType eType )
99     :BasicEntry( eType )
100     ,m_aDocument( rDocument )
101     ,m_eLocation( eLocation )
102 {
103     OSL_ENSURE( m_aDocument.isValid(), "BasicDocumentEntry::BasicDocumentEntry: illegal document!" );
104 }
105 
~BasicDocumentEntry()106 BasicDocumentEntry::~BasicDocumentEntry()
107 {
108 }
109 
BasicLibEntry(const ScriptDocument & rDocument,LibraryLocation eLocation,const String & rLibName,BasicEntryType eType)110 BasicLibEntry::BasicLibEntry( const ScriptDocument& rDocument, LibraryLocation eLocation, const String& rLibName, BasicEntryType eType )
111     :BasicDocumentEntry( rDocument, eLocation, eType )
112     ,m_aLibName( rLibName )
113 {
114 }
115 
~BasicLibEntry()116 BasicLibEntry::~BasicLibEntry()
117 {
118 }
119 
BasicEntryDescriptor()120 BasicEntryDescriptor::BasicEntryDescriptor()
121     :m_aDocument( ScriptDocument::getApplicationScriptDocument() )
122     ,m_eLocation( LIBRARY_LOCATION_UNKNOWN )
123     ,m_eType( OBJ_TYPE_UNKNOWN )
124 {
125 }
126 
BasicEntryDescriptor(const ScriptDocument & rDocument,LibraryLocation eLocation,const String & rLibName,const String & rLibSubName,const String & rName,BasicEntryType eType)127 BasicEntryDescriptor::BasicEntryDescriptor( const ScriptDocument& rDocument, LibraryLocation eLocation, const String& rLibName, const String& rLibSubName, const String& rName, BasicEntryType eType )
128     :m_aDocument( rDocument )
129     ,m_eLocation( eLocation )
130     ,m_aLibName( rLibName )
131     ,m_aLibSubName( rLibSubName )
132     ,m_aName( rName )
133     ,m_eType( eType )
134 {
135     OSL_ENSURE( m_aDocument.isValid(), "BasicEntryDescriptor::BasicEntryDescriptor: invalid document!" );
136 }
137 
BasicEntryDescriptor(const ScriptDocument & rDocument,LibraryLocation eLocation,const String & rLibName,const String & rLibSubName,const String & rName,const String & rMethodName,BasicEntryType eType)138 BasicEntryDescriptor::BasicEntryDescriptor( const ScriptDocument& rDocument, LibraryLocation eLocation, const String& rLibName, const String& rLibSubName, const String& rName, const String& rMethodName, BasicEntryType eType )
139     :m_aDocument( rDocument )
140     ,m_eLocation( eLocation )
141     ,m_aLibName( rLibName )
142     ,m_aLibSubName( rLibSubName )
143     ,m_aName( rName )
144     ,m_aMethodName( rMethodName )
145     ,m_eType( eType )
146 {
147     OSL_ENSURE( m_aDocument.isValid(), "BasicEntryDescriptor::BasicEntryDescriptor: invalid document!" );
148 }
149 
~BasicEntryDescriptor()150 BasicEntryDescriptor::~BasicEntryDescriptor()
151 {
152 }
153 
BasicEntryDescriptor(const BasicEntryDescriptor & rDesc)154 BasicEntryDescriptor::BasicEntryDescriptor( const BasicEntryDescriptor& rDesc )
155     :m_aDocument( rDesc.m_aDocument )
156     ,m_eLocation( rDesc.m_eLocation )
157     ,m_aLibName( rDesc.m_aLibName )
158     ,m_aLibSubName( rDesc.m_aLibSubName )
159     ,m_aName( rDesc.m_aName )
160     ,m_aMethodName( rDesc.m_aMethodName )
161     ,m_eType( rDesc.m_eType )
162 {
163 }
164 
operator =(const BasicEntryDescriptor & rDesc)165 BasicEntryDescriptor& BasicEntryDescriptor::operator=( const BasicEntryDescriptor& rDesc )
166 {
167     m_aDocument = rDesc.m_aDocument;
168     m_eLocation = rDesc.m_eLocation;
169     m_aLibName = rDesc.m_aLibName;
170     m_aLibSubName = rDesc.m_aLibSubName;
171     m_aName = rDesc.m_aName;
172     m_aMethodName = rDesc.m_aMethodName;
173     m_eType = rDesc.m_eType;
174 
175     return *this;
176 }
177 
operator ==(const BasicEntryDescriptor & rDesc) const178 bool BasicEntryDescriptor::operator==( const BasicEntryDescriptor& rDesc ) const
179 {
180     return m_aDocument == rDesc.m_aDocument &&
181            m_eLocation == rDesc.m_eLocation &&
182            m_aLibName == rDesc.m_aLibName &&
183            m_aLibSubName == rDesc.m_aLibSubName &&
184            m_aName == rDesc.m_aName &&
185            m_aMethodName == rDesc.m_aMethodName &&
186            m_eType == rDesc.m_eType;
187 }
188 
BasicTreeListBox(Window * pParent,const ResId & rRes)189 BasicTreeListBox::BasicTreeListBox( Window* pParent, const ResId& rRes ) :
190 	SvTreeListBox( pParent, IDEResId( sal::static_int_cast<sal_uInt16>( rRes.GetId() ) ) ),
191     m_aNotifier( *this )
192 {
193     SetNodeDefaultImages();
194     SetSelectionMode( SINGLE_SELECTION );
195 	nMode = 0xFF;	// Alles
196 }
197 
198 
199 
~BasicTreeListBox()200 BasicTreeListBox::~BasicTreeListBox()
201 {
202     m_aNotifier.dispose();
203 
204 	// UserDaten zerstoeren
205 	SvLBoxEntry* pEntry = First();
206 	while ( pEntry )
207 	{
208 		delete (BasicEntry*)pEntry->GetUserData();
209 		pEntry = Next( pEntry );
210 	}
211 }
212 
ScanEntry(const ScriptDocument & rDocument,LibraryLocation eLocation)213 void BasicTreeListBox::ScanEntry( const ScriptDocument& rDocument, LibraryLocation eLocation )
214 {
215     OSL_ENSURE( rDocument.isAlive(), "BasicTreeListBox::ScanEntry: illegal document!" );
216     if ( !rDocument.isAlive() )
217         return;
218 
219     // can be called multiple times for updating!
220 
221 	// eigentlich prueffen, ob Basic bereits im Baum ?!
222 	SetUpdateMode( sal_False );
223 
224     // level 1: BasicManager (application, document, ...)
225     SvLBoxEntry* pDocumentRootEntry = FindRootEntry( rDocument, eLocation );
226     if ( pDocumentRootEntry && IsExpanded( pDocumentRootEntry ) )
227         ImpCreateLibEntries( pDocumentRootEntry, rDocument, eLocation );
228     if ( !pDocumentRootEntry )
229     {
230         String aRootName( GetRootEntryName( rDocument, eLocation ) );
231         Image aImage;
232         Image aImageHC;
233         GetRootEntryBitmaps( rDocument, aImage, aImageHC );
234         pDocumentRootEntry = AddEntry(
235             aRootName,
236             aImage,
237             aImageHC,
238             0, true,
239             std::auto_ptr< BasicEntry >( new BasicDocumentEntry( rDocument, eLocation ) ) );
240     }
241 
242 	SetUpdateMode( sal_True );
243 }
244 
ImpCreateLibEntries(SvLBoxEntry * pDocumentRootEntry,const ScriptDocument & rDocument,LibraryLocation eLocation)245 void BasicTreeListBox::ImpCreateLibEntries( SvLBoxEntry* pDocumentRootEntry, const ScriptDocument& rDocument, LibraryLocation eLocation )
246 {
247     // get a sorted list of library names
248     Sequence< ::rtl::OUString > aLibNames( rDocument.getLibraryNames() );
249     sal_Int32 nLibCount = aLibNames.getLength();
250 	const ::rtl::OUString* pLibNames = aLibNames.getConstArray();
251 
252     for ( sal_Int32 i = 0 ; i < nLibCount ; i++ )
253 	{
254         String aLibName = pLibNames[ i ];
255 
256         if ( eLocation == rDocument.getLibraryLocation( aLibName ) )
257         {
258             // check, if the module library is loaded
259             sal_Bool bModLibLoaded = sal_False;
260             ::rtl::OUString aOULibName( aLibName );
261             Reference< script::XLibraryContainer > xModLibContainer( rDocument.getLibraryContainer( E_SCRIPTS ) );
262 		    if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryLoaded( aOULibName ) )
263                 bModLibLoaded = sal_True;
264 
265             // check, if the dialog library is loaded
266             sal_Bool bDlgLibLoaded = sal_False;
267             Reference< script::XLibraryContainer > xDlgLibContainer( rDocument.getLibraryContainer( E_DIALOGS ) );
268             if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryLoaded( aOULibName ) )
269                 bDlgLibLoaded = sal_True;
270 
271             sal_Bool bLoaded = bModLibLoaded || bDlgLibLoaded;
272 
273             // if only one of the libraries is loaded, load also the other
274             if ( bLoaded )
275             {
276                 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
277                     xModLibContainer->loadLibrary( aOULibName );
278 
279                 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
280                     xDlgLibContainer->loadLibrary( aOULibName );
281             }
282 
283             // create tree list box entry
284             sal_uInt16 nId, nIdHC;
285             if ( ( nMode & BROWSEMODE_DIALOGS ) && !( nMode & BROWSEMODE_MODULES ) )
286             {
287                 nId = bLoaded ? RID_IMG_DLGLIB : RID_IMG_DLGLIBNOTLOADED;
288                 nIdHC = bLoaded ? RID_IMG_DLGLIB_HC : RID_IMG_DLGLIBNOTLOADED_HC;
289             }
290             else
291             {
292                 nId = bLoaded ? RID_IMG_MODLIB : RID_IMG_MODLIBNOTLOADED;
293                 nIdHC = bLoaded ? RID_IMG_MODLIB_HC : RID_IMG_MODLIBNOTLOADED_HC;
294             }
295 		    SvLBoxEntry* pLibRootEntry = FindEntry( pDocumentRootEntry, aLibName, OBJ_TYPE_LIBRARY );
296             if ( pLibRootEntry )
297             {
298                 SetEntryBitmaps( pLibRootEntry, Image( IDEResId( nId ) ), Image( IDEResId( nIdHC ) ) );
299                 if ( IsExpanded( pLibRootEntry ) )
300 			        ImpCreateLibSubEntries( pLibRootEntry, rDocument, aLibName );
301             }
302             else
303             {
304                 pLibRootEntry = AddEntry(
305                     aLibName,
306                     Image( IDEResId( nId ) ),
307                     Image( IDEResId( nIdHC ) ),
308                     pDocumentRootEntry, true,
309                     std::auto_ptr< BasicEntry >( new BasicEntry( OBJ_TYPE_LIBRARY ) ) );
310             }
311         }
312     }
313 }
314 
ImpCreateLibSubEntries(SvLBoxEntry * pLibRootEntry,const ScriptDocument & rDocument,const String & rLibName)315 void BasicTreeListBox::ImpCreateLibSubEntries( SvLBoxEntry* pLibRootEntry, const ScriptDocument& rDocument, const String& rLibName )
316 {
317     ::rtl::OUString aOULibName( rLibName );
318 
319 	// modules
320     if ( nMode & BROWSEMODE_MODULES )
321     {
322         Reference< script::XLibraryContainer > xModLibContainer( rDocument.getLibraryContainer( E_SCRIPTS ) );
323 
324         if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryLoaded( aOULibName ) )
325         {
326             try
327 		    {
328                 if( rDocument.isInVBAMode() )
329                     ImpCreateLibSubEntriesInVBAMode( pLibRootEntry, rDocument, rLibName );
330                 else
331                 {
332                     // get a sorted list of module names
333                     Sequence< ::rtl::OUString > aModNames = rDocument.getObjectNames( E_SCRIPTS, rLibName );
334                     sal_Int32 nModCount = aModNames.getLength();
335     	            const ::rtl::OUString* pModNames = aModNames.getConstArray();
336 
337                     for ( sal_Int32 i = 0 ; i < nModCount ; i++ )
338     				{
339                         String aModName = pModNames[ i ];
340                         SvLBoxEntry* pModuleEntry = FindEntry( pLibRootEntry, aModName, OBJ_TYPE_MODULE );
341                         if ( !pModuleEntry )
342                             pModuleEntry = AddEntry(
343                                 aModName,
344                                 Image( IDEResId( RID_IMG_MODULE ) ),
345                                 Image( IDEResId( RID_IMG_MODULE_HC ) ),
346                                 pLibRootEntry, false,
347                                 std::auto_ptr< BasicEntry >( new BasicEntry( OBJ_TYPE_MODULE ) ) );
348 
349     					// methods
350     					if ( nMode & BROWSEMODE_SUBS )
351     					{
352                             Sequence< ::rtl::OUString > aNames = BasicIDE::GetMethodNames( rDocument, rLibName, aModName );
353     						sal_Int32 nCount = aNames.getLength();
354     						const ::rtl::OUString* pNames = aNames.getConstArray();
355 
356     						for ( sal_Int32 j = 0 ; j < nCount ; j++ )
357     						{
358     							String aName = pNames[ j ];
359     							SvLBoxEntry* pEntry = FindEntry( pModuleEntry, aName, OBJ_TYPE_METHOD );
360                                 if ( !pEntry )
361                                     pEntry = AddEntry(
362                                         aName,
363                                         Image( IDEResId( RID_IMG_MACRO ) ),
364                                         Image( IDEResId( RID_IMG_MACRO_HC ) ),
365                                         pModuleEntry, false,
366                                         std::auto_ptr< BasicEntry >( new BasicEntry( OBJ_TYPE_METHOD ) ) );
367     						}
368                         }
369     				}
370                 }
371             }
372 		    catch ( const container::NoSuchElementException& )
373 		    {
374                 DBG_UNHANDLED_EXCEPTION();
375 		    }
376         }
377     }
378 
379 	// dialogs
380     if ( nMode & BROWSEMODE_DIALOGS )
381     {
382          Reference< script::XLibraryContainer > xDlgLibContainer( rDocument.getLibraryContainer( E_DIALOGS ) );
383 
384          if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryLoaded( aOULibName ) )
385          {
386             try
387 		    {
388                 // get a sorted list of dialog names
389                 Sequence< ::rtl::OUString > aDlgNames( rDocument.getObjectNames( E_DIALOGS, rLibName ) );
390                 sal_Int32 nDlgCount = aDlgNames.getLength();
391 	            const ::rtl::OUString* pDlgNames = aDlgNames.getConstArray();
392 
393                 for ( sal_Int32 i = 0 ; i < nDlgCount ; i++ )
394 				{
395 					String aDlgName = pDlgNames[ i ];
396 					SvLBoxEntry* pDialogEntry = FindEntry( pLibRootEntry, aDlgName, OBJ_TYPE_DIALOG );
397                     if ( !pDialogEntry )
398                         pDialogEntry = AddEntry(
399                             aDlgName,
400                             Image( IDEResId( RID_IMG_DIALOG ) ),
401                             Image( IDEResId( RID_IMG_DIALOG_HC ) ),
402                             pLibRootEntry, false,
403                             std::auto_ptr< BasicEntry >( new BasicEntry( OBJ_TYPE_DIALOG ) ) );
404 				}
405             }
406 		    catch ( container::NoSuchElementException& )
407 		    {
408 			    DBG_UNHANDLED_EXCEPTION();
409 		    }
410         }
411     }
412 }
413 
ImpCreateLibSubEntriesInVBAMode(SvLBoxEntry * pLibRootEntry,const ScriptDocument & rDocument,const String & rLibName)414 void BasicTreeListBox::ImpCreateLibSubEntriesInVBAMode( SvLBoxEntry* pLibRootEntry, const ScriptDocument& rDocument, const String& rLibName )
415 {
416 
417     ::std::vector< std::pair< BasicEntryType, ::rtl::OUString > > aEntries;
418     aEntries.push_back( ::std::make_pair( OBJ_TYPE_DOCUMENT_OBJECTS, String( IDEResId( RID_STR_DOCUMENT_OBJECTS ) ) ) );
419     aEntries.push_back( ::std::make_pair( OBJ_TYPE_USERFORMS,  String( IDEResId( RID_STR_USERFORMS ) ) ) );
420     aEntries.push_back( ::std::make_pair( OBJ_TYPE_NORMAL_MODULES, String( IDEResId( RID_STR_NORMAL_MODULES ) ) ) );
421     aEntries.push_back( ::std::make_pair( OBJ_TYPE_CLASS_MODULES,  String( IDEResId( RID_STR_CLASS_MODULES ) ) ) );
422 
423     ::std::vector< std::pair< BasicEntryType, ::rtl::OUString > >::iterator iter;
424     for( iter = aEntries.begin(); iter != aEntries.end(); ++iter )
425     {
426         BasicEntryType eType = iter->first;
427         ::rtl::OUString aEntryName = iter->second;
428         SvLBoxEntry* pLibSubRootEntry = FindEntry( pLibRootEntry, aEntryName, eType );
429         if( pLibSubRootEntry )
430         {
431             SetEntryBitmaps( pLibSubRootEntry, Image( IDEResId( RID_IMG_MODLIB ) ), Image( IDEResId( RID_IMG_MODLIB_HC ) ) );
432             if ( IsExpanded( pLibSubRootEntry ) )
433 			    ImpCreateLibSubSubEntriesInVBAMode( pLibSubRootEntry, rDocument, rLibName );
434         }
435         else
436         {
437             pLibSubRootEntry = AddEntry(
438                 aEntryName,
439                 Image( IDEResId( RID_IMG_MODLIB ) ),
440                 Image( IDEResId( RID_IMG_MODLIB_HC ) ),
441                 pLibRootEntry, true,
442                 std::auto_ptr< BasicEntry >( new BasicEntry( eType ) ) );
443         }
444     }
445 }
446 
ImpCreateLibSubSubEntriesInVBAMode(SvLBoxEntry * pLibSubRootEntry,const ScriptDocument & rDocument,const String & rLibName)447 void BasicTreeListBox::ImpCreateLibSubSubEntriesInVBAMode( SvLBoxEntry* pLibSubRootEntry, const ScriptDocument& rDocument, const String& rLibName )
448 {
449     uno::Reference< container::XNameContainer > xLib = rDocument.getOrCreateLibrary( E_SCRIPTS, rLibName );
450     if( !xLib.is() )
451         return;
452 
453     try
454 	{
455         // get a sorted list of module names
456         Sequence< ::rtl::OUString > aModNames = rDocument.getObjectNames( E_SCRIPTS, rLibName );
457         sal_Int32 nModCount = aModNames.getLength();
458     	const ::rtl::OUString* pModNames = aModNames.getConstArray();
459 
460         BasicEntryDescriptor aDesc( GetEntryDescriptor( pLibSubRootEntry ) );
461         BasicEntryType eCurrentType( aDesc.GetType() );
462 
463         for ( sal_Int32 i = 0 ; i < nModCount ; i++ )
464 		{
465             String aModName = pModNames[ i ];
466             BasicEntryType eType = OBJ_TYPE_UNKNOWN;
467             switch( ModuleInfoHelper::getModuleType( xLib, aModName ) )
468             {
469                 case script::ModuleType::DOCUMENT:
470                     eType = OBJ_TYPE_DOCUMENT_OBJECTS;
471                     break;
472                 case script::ModuleType::FORM:
473                     eType = OBJ_TYPE_USERFORMS;
474                     break;
475                 case script::ModuleType::NORMAL:
476                     eType = OBJ_TYPE_NORMAL_MODULES;
477                     break;
478                 case script::ModuleType::CLASS:
479                     eType = OBJ_TYPE_CLASS_MODULES;
480                     break;
481             }
482             if( eType != eCurrentType )
483                 continue;
484 
485 			// display a nice friendly name in the ObjectModule tab,
486            	// combining the objectname and module name, e.g. Sheet1 ( Financials )
487             String aEntryName( aModName );
488             if( eType == OBJ_TYPE_DOCUMENT_OBJECTS )
489             {
490 	       	    String sObjName;
491             	ModuleInfoHelper::getObjectName( xLib, aModName, sObjName );
492 		    	if( sObjName.Len() )
493         	   	{
494 		        	aEntryName.AppendAscii(" (").Append(sObjName).AppendAscii(")");
495             	}
496             }
497             SvLBoxEntry* pModuleEntry = FindEntry( pLibSubRootEntry, aEntryName, OBJ_TYPE_MODULE );
498             if ( !pModuleEntry )
499                 pModuleEntry = AddEntry(
500                     aEntryName,
501                     Image( IDEResId( RID_IMG_MODULE ) ),
502                     Image( IDEResId( RID_IMG_MODULE_HC ) ),
503                     pLibSubRootEntry, false,
504                     std::auto_ptr< BasicEntry >( new BasicEntry( OBJ_TYPE_MODULE ) ) );
505 
506 			// methods
507 			if ( nMode & BROWSEMODE_SUBS )
508 			{
509                 Sequence< ::rtl::OUString > aNames = BasicIDE::GetMethodNames( rDocument, rLibName, aModName );
510 				sal_Int32 nCount = aNames.getLength();
511 				const ::rtl::OUString* pNames = aNames.getConstArray();
512 
513 				for ( sal_Int32 j = 0 ; j < nCount ; j++ )
514 				{
515 					String aName = pNames[ j ];
516 					SvLBoxEntry* pEntry = FindEntry( pModuleEntry, aName, OBJ_TYPE_METHOD );
517                     if ( !pEntry )
518                         pEntry = AddEntry(
519                             aName,
520                             Image( IDEResId( RID_IMG_MACRO ) ),
521                             Image( IDEResId( RID_IMG_MACRO_HC ) ),
522                             pModuleEntry, false,
523                             std::auto_ptr< BasicEntry >( new BasicEntry( OBJ_TYPE_METHOD ) ) );
524     			}
525             }
526 	    }
527     }
528     catch ( const container::NoSuchElementException& )
529 	{
530         DBG_UNHANDLED_EXCEPTION();
531     }
532 }
533 
ImpFindEntry(SvLBoxEntry * pParent,const String & rText)534 SvLBoxEntry* BasicTreeListBox::ImpFindEntry( SvLBoxEntry* pParent, const String& rText )
535 {
536 	sal_uLong nRootPos = 0;
537 	SvLBoxEntry* pEntry = pParent ? FirstChild( pParent ) : GetEntry( nRootPos );
538 	while ( pEntry )
539 	{
540 		if (  GetEntryText( pEntry ) == rText )
541 			return pEntry;
542 
543 		pEntry = pParent ? NextSibling( pEntry ) : GetEntry( ++nRootPos );
544 	}
545 	return 0;
546 }
547 
onDocumentCreated(const ScriptDocument &)548 void BasicTreeListBox::onDocumentCreated( const ScriptDocument& /*_rDocument*/ )
549 {
550     UpdateEntries();
551 }
552 
onDocumentOpened(const ScriptDocument &)553 void BasicTreeListBox::onDocumentOpened( const ScriptDocument& /*_rDocument*/ )
554 {
555     UpdateEntries();
556 }
557 
onDocumentSave(const ScriptDocument &)558 void BasicTreeListBox::onDocumentSave( const ScriptDocument& /*_rDocument*/ )
559 {
560     // not interested in
561 }
562 
onDocumentSaveDone(const ScriptDocument &)563 void BasicTreeListBox::onDocumentSaveDone( const ScriptDocument& /*_rDocument*/ )
564 {
565     // not interested in
566 }
567 
onDocumentSaveAs(const ScriptDocument &)568 void BasicTreeListBox::onDocumentSaveAs( const ScriptDocument& /*_rDocument*/ )
569 {
570     // not interested in
571 }
572 
onDocumentSaveAsDone(const ScriptDocument &)573 void BasicTreeListBox::onDocumentSaveAsDone( const ScriptDocument& /*_rDocument*/ )
574 {
575     UpdateEntries();
576 }
577 
onDocumentClosed(const ScriptDocument &)578 void BasicTreeListBox::onDocumentClosed( const ScriptDocument& /*_rDocument*/ )
579 {
580     UpdateEntries();
581 }
582 
onDocumentTitleChanged(const ScriptDocument &)583 void BasicTreeListBox::onDocumentTitleChanged( const ScriptDocument& /*_rDocument*/ )
584 {
585     // not interested in
586 }
587 
onDocumentModeChanged(const ScriptDocument &)588 void BasicTreeListBox::onDocumentModeChanged( const ScriptDocument& /*_rDocument*/ )
589 {
590     // not interested in
591 }
592 
UpdateEntries()593 void BasicTreeListBox::UpdateEntries()
594 {
595     BasicEntryDescriptor aCurDesc( GetEntryDescriptor( FirstSelected() ) );
596 
597 	// Erstmal die vorhandenen Eintraege auf existens pruefen:
598 	SvLBoxEntry* pLastValid = 0;
599 	SvLBoxEntry* pEntry = First();
600 	while ( pEntry )
601 	{
602 		if ( IsValidEntry( pEntry ) )
603 			pLastValid = pEntry;
604 		else
605 		{
606 			delete (BasicEntry*)pEntry->GetUserData();
607 			GetModel()->Remove( pEntry );
608 		}
609 		pEntry = pLastValid ? Next( pLastValid ) : First();
610 	}
611 
612 	// Jetzt ueber die Basics rennen und in die Zweige eintragen
613 	ScanAllEntries();
614 
615     SetCurrentEntry( aCurDesc );
616 }
617 
CloneEntry(SvLBoxEntry * pSource)618 SvLBoxEntry* __EXPORT BasicTreeListBox::CloneEntry( SvLBoxEntry* pSource )
619 {
620 	SvLBoxEntry* pNew = SvTreeListBox::CloneEntry( pSource );
621 	BasicEntry* pUser = (BasicEntry*)pSource->GetUserData();
622 
623 	DBG_ASSERT( pUser, "User-Daten?!" );
624     DBG_ASSERT( pUser->GetType() != OBJ_TYPE_DOCUMENT, "BasicTreeListBox::CloneEntry: document?!" );
625 
626 	BasicEntry* pNewUser = new BasicEntry( *pUser );
627 	pNew->SetUserData( pNewUser );
628 	return pNew;
629 }
630 
FindEntry(SvLBoxEntry * pParent,const String & rText,BasicEntryType eType)631 SvLBoxEntry* BasicTreeListBox::FindEntry( SvLBoxEntry* pParent, const String& rText, BasicEntryType eType )
632 {
633 	sal_uLong nRootPos = 0;
634 	SvLBoxEntry* pEntry = pParent ? FirstChild( pParent ) : GetEntry( nRootPos );
635 	while ( pEntry )
636 	{
637 		BasicEntry* pBasicEntry = (BasicEntry*)pEntry->GetUserData();
638 		DBG_ASSERT( pBasicEntry, "FindEntry: Kein BasicEntry ?!" );
639 		if ( ( pBasicEntry->GetType() == eType  ) && ( GetEntryText( pEntry ) == rText ) )
640 			return pEntry;
641 
642 		pEntry = pParent ? NextSibling( pEntry ) : GetEntry( ++nRootPos );
643 	}
644 	return 0;
645 }
646 
ExpandingHdl()647 long BasicTreeListBox::ExpandingHdl()
648 {
649 	// Expanding oder Collaps?
650 	sal_Bool bOK = sal_True;
651 	if ( GetModel()->GetDepth( GetHdlEntry() ) == 1 )
652 	{
653         SvLBoxEntry* pCurEntry = GetCurEntry();
654         BasicEntryDescriptor aDesc( GetEntryDescriptor( pCurEntry ) );
655         ScriptDocument aDocument( aDesc.GetDocument() );
656         OSL_ENSURE( aDocument.isAlive(), "BasicTreeListBox::ExpandingHdl: no document, or document is dead!" );
657         if ( aDocument.isAlive() )
658         {
659             String aLibName( aDesc.GetLibName() );
660             String aLibSubName( aDesc.GetLibSubName() );
661             String aName( aDesc.GetName() );
662             String aMethodName( aDesc.GetMethodName() );
663 
664             if ( aLibName.Len() && !aLibSubName.Len() && !aName.Len() && !aMethodName.Len() )
665 		    {
666                 // check password, if library is password protected and not verified
667                 ::rtl::OUString aOULibName( aLibName );
668                 Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
669                 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
670                 {
671                     Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
672                     if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
673                     {
674                         String aPassword;
675 				        bOK = QueryPassword( xModLibContainer, aLibName, aPassword );
676                     }
677                 }
678             }
679         }
680 	}
681 	return bOK;
682 }
683 
IsEntryProtected(SvLBoxEntry * pEntry)684 sal_Bool BasicTreeListBox::IsEntryProtected( SvLBoxEntry* pEntry )
685 {
686 	sal_Bool bProtected = sal_False;
687 	if ( pEntry && ( GetModel()->GetDepth( pEntry ) == 1 ) )
688 	{
689         BasicEntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
690         ScriptDocument aDocument( aDesc.GetDocument() );
691         OSL_ENSURE( aDocument.isAlive(), "BasicTreeListBox::IsEntryProtected: no document, or document is dead!" );
692         if ( aDocument.isAlive() )
693         {
694             ::rtl::OUString aOULibName( aDesc.GetLibName() );
695             Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
696             if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
697             {
698                 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
699                 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
700                 {
701 			        bProtected = sal_True;
702                 }
703             }
704         }
705     }
706 	return bProtected;
707 }
708 
AddEntry(const String & rText,const Image & rImage,const Image & rImageHC,SvLBoxEntry * pParent,bool bChildrenOnDemand,std::auto_ptr<BasicEntry> aUserData)709 SvLBoxEntry* BasicTreeListBox::AddEntry(
710     const String& rText, const Image& rImage, const Image& rImageHC,
711     SvLBoxEntry* pParent, bool bChildrenOnDemand, std::auto_ptr< BasicEntry > aUserData )
712 {
713     SvLBoxEntry* p = InsertEntry(
714         rText, rImage, rImage, pParent, bChildrenOnDemand, LIST_APPEND,
715         aUserData.release() ); // XXX possible leak
716     SetExpandedEntryBmp( p, rImageHC, BMP_COLOR_HIGHCONTRAST );
717     SetCollapsedEntryBmp( p, rImageHC, BMP_COLOR_HIGHCONTRAST );
718     return p;
719 }
720 
SetEntryBitmaps(SvLBoxEntry * pEntry,const Image & rImage,const Image & rImageHC)721 void BasicTreeListBox::SetEntryBitmaps( SvLBoxEntry * pEntry, const Image& rImage, const Image& rImageHC )
722 {
723     SetExpandedEntryBmp( pEntry, rImage, BMP_COLOR_NORMAL );
724     SetCollapsedEntryBmp( pEntry, rImage, BMP_COLOR_NORMAL );
725     SetExpandedEntryBmp( pEntry, rImageHC, BMP_COLOR_HIGHCONTRAST );
726     SetCollapsedEntryBmp( pEntry, rImageHC, BMP_COLOR_HIGHCONTRAST );
727 }
728 
GetLibraryType() const729 LibraryType BasicTreeListBox::GetLibraryType() const
730 {
731     LibraryType eType = LIBRARY_TYPE_ALL;
732     if ( ( nMode & BROWSEMODE_MODULES ) && !( nMode & BROWSEMODE_DIALOGS ) )
733         eType = LIBRARY_TYPE_MODULE;
734     else if ( !( nMode & BROWSEMODE_MODULES ) && ( nMode & BROWSEMODE_DIALOGS ) )
735         eType = LIBRARY_TYPE_DIALOG;
736     return eType;
737 }
738 
GetRootEntryName(const ScriptDocument & rDocument,LibraryLocation eLocation) const739 String BasicTreeListBox::GetRootEntryName( const ScriptDocument& rDocument, LibraryLocation eLocation ) const
740 {
741     return rDocument.getTitle( eLocation, GetLibraryType() );
742 }
743 
GetRootEntryBitmaps(const ScriptDocument & rDocument,Image & rImage,Image & rImageHC)744 void BasicTreeListBox::GetRootEntryBitmaps( const ScriptDocument& rDocument, Image& rImage, Image& rImageHC )
745 {
746     OSL_ENSURE( rDocument.isValid(), "BasicTreeListBox::GetRootEntryBitmaps: illegal document!" );
747     if ( !rDocument.isValid() )
748         return;
749 
750     if ( rDocument.isDocument() )
751     {
752         ::rtl::OUString sFactoryURL;
753         ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
754         Reference< ::com::sun::star::frame::XModuleManager > xModuleManager;
755         if ( aContext.createComponent( "com.sun.star.frame.ModuleManager", xModuleManager ) )
756         {
757             try
758             {
759                 ::rtl::OUString sModule( xModuleManager->identify( rDocument.getDocument() ) );
760                 Reference< container::XNameAccess > xModuleConfig( xModuleManager, UNO_QUERY );
761                 if ( xModuleConfig.is() )
762                 {
763                     Sequence< beans::PropertyValue > aModuleDescr;
764                     xModuleConfig->getByName( sModule ) >>= aModuleDescr;
765                     sal_Int32 nCount = aModuleDescr.getLength();
766                     const beans::PropertyValue* pModuleDescr = aModuleDescr.getConstArray();
767                     for ( sal_Int32 i = 0; i < nCount; ++i )
768                     {
769                         if ( pModuleDescr[ i ].Name.equalsAsciiL(
770                             RTL_CONSTASCII_STRINGPARAM( "ooSetupFactoryEmptyDocumentURL" ) ) )
771                         {
772                             pModuleDescr[ i ].Value >>= sFactoryURL;
773                             break;
774                         }
775                     }
776                 }
777             }
778             catch( const Exception& )
779             {
780             	DBG_UNHANDLED_EXCEPTION();
781             }
782         }
783 
784         if ( !sFactoryURL.isEmpty() )
785         {
786             rImage = SvFileInformationManager::GetFileImage( INetURLObject( sFactoryURL ),
787                 sal_False /* small */,
788                 sal_False /* normal */ );
789 
790             rImageHC = SvFileInformationManager::GetFileImage( INetURLObject( sFactoryURL ),
791                 sal_False /* small */,
792                 sal_True /* high contrast */ );
793         }
794         else
795         {
796             // default icon
797             rImage = Image( IDEResId( RID_IMG_DOCUMENT ) );
798             rImageHC = Image( IDEResId( RID_IMG_DOCUMENT_HC ) );
799         }
800     }
801     else
802     {
803         rImage = Image( IDEResId( RID_IMG_INSTALLATION ) );
804         rImageHC = Image( IDEResId( RID_IMG_INSTALLATION_HC ) );
805     }
806 }
807 
SetCurrentEntry(BasicEntryDescriptor & rDesc)808 void BasicTreeListBox::SetCurrentEntry( BasicEntryDescriptor& rDesc )
809 {
810     SvLBoxEntry* pCurEntry = 0;
811     BasicEntryDescriptor aDesc( rDesc );
812     if ( aDesc.GetType() == OBJ_TYPE_UNKNOWN )
813     {
814         aDesc = BasicEntryDescriptor(
815             ScriptDocument::getApplicationScriptDocument(),
816             LIBRARY_LOCATION_USER, String::CreateFromAscii( "Standard" ),
817             String(), String::CreateFromAscii( "." ), OBJ_TYPE_UNKNOWN );
818     }
819     ScriptDocument aDocument( aDesc.GetDocument() );
820     OSL_ENSURE( aDocument.isValid(), "BasicTreeListBox::SetCurrentEntry: invalid document!" );
821     LibraryLocation eLocation( aDesc.GetLocation() );
822     SvLBoxEntry* pRootEntry = FindRootEntry( aDocument, eLocation );
823     if ( pRootEntry )
824     {
825         pCurEntry = pRootEntry;
826         String aLibName( aDesc.GetLibName() );
827         if ( aLibName.Len() )
828         {
829             Expand( pRootEntry );
830             SvLBoxEntry* pLibEntry = FindEntry( pRootEntry, aLibName, OBJ_TYPE_LIBRARY );
831             if ( pLibEntry )
832             {
833                 pCurEntry = pLibEntry;
834                 String aLibSubName( aDesc.GetLibSubName() );
835                 if( aLibSubName.Len() )
836                 {
837                     Expand( pLibEntry );
838                     SvLBoxEntry* pLibSubEntry = ImpFindEntry( pLibEntry, aLibSubName );
839                     if( pLibSubEntry )
840                     {
841                         pCurEntry = pLibSubEntry;
842                     }
843                 }
844                 String aName( aDesc.GetName() );
845                 if ( aName.Len() )
846                 {
847                     Expand( pCurEntry );
848                     BasicEntryType eType = OBJ_TYPE_MODULE;
849                     if ( aDesc.GetType() == OBJ_TYPE_DIALOG )
850                         eType = OBJ_TYPE_DIALOG;
851                     SvLBoxEntry* pEntry = FindEntry( pCurEntry, aName, eType );
852                     if ( pEntry )
853                     {
854                         pCurEntry = pEntry;
855                         String aMethodName( aDesc.GetMethodName() );
856                         if ( aMethodName.Len() )
857                         {
858                             Expand( pEntry );
859                             SvLBoxEntry* pSubEntry = FindEntry( pEntry, aMethodName, OBJ_TYPE_METHOD );
860                             if ( pSubEntry )
861                             {
862                                 pCurEntry = pSubEntry;
863                             }
864                             else
865                             {
866                                 pSubEntry = FirstChild( pEntry );
867                                 if ( pSubEntry )
868                                     pCurEntry = pSubEntry;
869                             }
870                         }
871                     }
872                     else
873                     {
874                         pEntry = FirstChild( pLibEntry );
875                         if ( pEntry )
876                             pCurEntry = pEntry;
877                     }
878                 }
879             }
880             else
881             {
882                 pLibEntry = FirstChild( pRootEntry );
883                 if ( pLibEntry )
884                     pCurEntry = pLibEntry;
885             }
886         }
887     }
888     else
889     {
890         pRootEntry = First();
891         if ( pRootEntry )
892             pCurEntry = pRootEntry;
893     }
894 
895     SetCurEntry( pCurEntry );
896 }
897