Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include <basic/basmgr.hxx>
21 : : #include <basic/sbmod.hxx>
22 : : #include <basic/sbx.hxx>
23 : : #include <bastype2.hxx>
24 : : #include <basobj.hxx>
25 : : #include <baside2.hrc>
26 : : #include <iderid.hxx>
27 : : #include <bastypes.hxx>
28 : : #include <basdoc.hxx>
29 : : #include <com/sun/star/script/XLibraryContainer.hpp>
30 : : #include <com/sun/star/script/XLibraryContainerPassword.hpp>
31 : : #include <deque>
32 : : #include <sfx2/docfac.hxx>
33 : :
34 : : using namespace ::com::sun::star::uno;
35 : : using namespace ::com::sun::star;
36 : :
37 : :
38 : : typedef std::deque< SvLBoxEntry* > EntryArray;
39 : :
40 : :
41 : 0 : void BasicTreeListBox::RequestingChildren( SvLBoxEntry* pEntry )
42 : : {
43 : 0 : BasicEntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
44 : 0 : ScriptDocument aDocument( aDesc.GetDocument() );
45 : : OSL_ENSURE( aDocument.isAlive(), "BasicTreeListBox::RequestingChildren: invalid document!" );
46 : 0 : if ( !aDocument.isAlive() )
47 : 0 : return;
48 : :
49 : 0 : LibraryLocation eLocation( aDesc.GetLocation() );
50 : 0 : BasicEntryType eType( aDesc.GetType() );
51 : :
52 : 0 : if ( eType == OBJ_TYPE_DOCUMENT )
53 : : {
54 : 0 : ImpCreateLibEntries( pEntry, aDocument, eLocation );
55 : : }
56 : 0 : else if ( eType == OBJ_TYPE_LIBRARY )
57 : : {
58 : 0 : ::rtl::OUString aOULibName( aDesc.GetLibName() );
59 : :
60 : : // check password
61 : 0 : bool bOK = true;
62 : 0 : Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
63 : 0 : if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
64 : : {
65 : 0 : Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
66 : 0 : if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
67 : : {
68 : 0 : ::rtl::OUString aPassword;
69 : 0 : bOK = QueryPassword( xModLibContainer, aOULibName, aPassword );
70 : 0 : }
71 : : }
72 : :
73 : 0 : if ( bOK )
74 : : {
75 : : // load module library
76 : 0 : bool bModLibLoaded = false;
77 : 0 : if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
78 : : {
79 : 0 : if ( !xModLibContainer->isLibraryLoaded( aOULibName ) )
80 : : {
81 : 0 : EnterWait();
82 : 0 : xModLibContainer->loadLibrary( aOULibName );
83 : 0 : LeaveWait();
84 : : }
85 : 0 : bModLibLoaded = xModLibContainer->isLibraryLoaded( aOULibName );
86 : : }
87 : :
88 : : // load dialog library
89 : 0 : bool bDlgLibLoaded = false;
90 : 0 : Reference< script::XLibraryContainer > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
91 : 0 : if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) )
92 : : {
93 : 0 : if ( !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
94 : : {
95 : 0 : EnterWait();
96 : 0 : xDlgLibContainer->loadLibrary( aOULibName );
97 : 0 : LeaveWait();
98 : : }
99 : 0 : bDlgLibLoaded = xDlgLibContainer->isLibraryLoaded( aOULibName );
100 : : }
101 : :
102 : 0 : if ( bModLibLoaded || bDlgLibLoaded )
103 : : {
104 : : // create the sub entries
105 : 0 : ImpCreateLibSubEntries( pEntry, aDocument, aOULibName );
106 : :
107 : : // exchange image
108 : 0 : bool bDlgMode = ( nMode & BROWSEMODE_DIALOGS ) && !( nMode & BROWSEMODE_MODULES );
109 : 0 : Image aImage( IDEResId( bDlgMode ? RID_IMG_DLGLIB : RID_IMG_MODLIB ) );
110 : 0 : SetEntryBitmaps( pEntry, aImage );
111 : : }
112 : : else
113 : : {
114 : : OSL_FAIL( "BasicTreeListBox::RequestingChildren: Error loading library!" );
115 : 0 : }
116 : 0 : }
117 : : }
118 : 0 : else if ( eType == OBJ_TYPE_DOCUMENT_OBJECTS
119 : : || eType == OBJ_TYPE_USERFORMS
120 : : || eType == OBJ_TYPE_NORMAL_MODULES
121 : : || eType == OBJ_TYPE_CLASS_MODULES )
122 : : {
123 : 0 : ::rtl::OUString aLibName( aDesc.GetLibName() );
124 : 0 : ImpCreateLibSubSubEntriesInVBAMode( pEntry, aDocument, aLibName );
125 : : }
126 : : else {
127 : : OSL_FAIL( "BasicTreeListBox::RequestingChildren: Unknown Type!" );
128 : 0 : }
129 : : }
130 : :
131 : 0 : void BasicTreeListBox::ExpandedHdl()
132 : : {
133 : 0 : SvLBoxEntry* pEntry = GetHdlEntry();
134 : : DBG_ASSERT( pEntry, "Was wurde zugeklappt?" );
135 : :
136 : 0 : if ( !IsExpanded( pEntry ) && pEntry->HasChildrenOnDemand() )
137 : : {
138 : 0 : SvLBoxEntry* pChild = FirstChild( pEntry );
139 : 0 : while ( pChild )
140 : : {
141 : 0 : GetModel()->Remove( pChild ); // does also call the DTOR
142 : 0 : pChild = FirstChild( pEntry );
143 : : }
144 : : }
145 : 0 : }
146 : :
147 : 0 : void BasicTreeListBox::ScanAllEntries()
148 : : {
149 : 0 : ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
150 : 0 : ScanEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
151 : :
152 : 0 : ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
153 : 0 : for ( ScriptDocuments::const_iterator doc = aDocuments.begin();
154 : 0 : doc != aDocuments.end();
155 : : ++doc
156 : : )
157 : : {
158 : 0 : if ( doc->isAlive() )
159 : 0 : ScanEntry( *doc, LIBRARY_LOCATION_DOCUMENT );
160 : 0 : }
161 : 0 : }
162 : :
163 : 0 : SbxVariable* BasicTreeListBox::FindVariable( SvLBoxEntry* pEntry )
164 : : {
165 : 0 : if ( !pEntry )
166 : 0 : return 0;
167 : :
168 : 0 : ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
169 : 0 : EntryArray aEntries;
170 : :
171 : 0 : while ( pEntry )
172 : : {
173 : 0 : sal_uInt16 nDepth = GetModel()->GetDepth( pEntry );
174 : 0 : switch ( nDepth )
175 : : {
176 : : case 4:
177 : : case 3:
178 : : case 2:
179 : : case 1:
180 : : {
181 : 0 : aEntries.push_front( pEntry );
182 : : }
183 : 0 : break;
184 : : case 0:
185 : : {
186 : 0 : aDocument = static_cast<BasicDocumentEntry*>(pEntry->GetUserData())->GetDocument();
187 : : }
188 : 0 : break;
189 : : }
190 : 0 : pEntry = GetParent( pEntry );
191 : : }
192 : :
193 : 0 : SbxVariable* pVar = 0;
194 : 0 : bool bDocumentObjects = false;
195 : 0 : if ( !aEntries.empty() )
196 : : {
197 : 0 : for ( size_t n = 0; n < aEntries.size(); n++ )
198 : : {
199 : 0 : SvLBoxEntry* pLE = aEntries[n];
200 : : DBG_ASSERT( pLE, "Can not find entry in array" );
201 : 0 : BasicEntry* pBE = static_cast<BasicEntry*>(pLE->GetUserData());
202 : : DBG_ASSERT( pBE, "The data in the entry not found!" );
203 : 0 : String aName( GetEntryText( pLE ) );
204 : :
205 : 0 : switch ( pBE->GetType() )
206 : : {
207 : : case OBJ_TYPE_LIBRARY:
208 : : {
209 : 0 : if (BasicManager* pBasMgr = aDocument.getBasicManager())
210 : 0 : pVar = pBasMgr->GetLib( aName );
211 : : }
212 : 0 : break;
213 : : case OBJ_TYPE_MODULE:
214 : : {
215 : : DBG_ASSERT(dynamic_cast<StarBASIC*>(pVar), "FindVariable: invalid Basic");
216 : : // extract the module name from the string like "Sheet1 (Example1)"
217 : 0 : if( bDocumentObjects )
218 : : {
219 : 0 : sal_uInt16 nIndex = 0;
220 : 0 : aName = aName.GetToken( 0, ' ', nIndex );
221 : : }
222 : 0 : pVar = static_cast<StarBASIC*>(pVar)->FindModule( aName );
223 : : }
224 : 0 : break;
225 : : case OBJ_TYPE_METHOD:
226 : : {
227 : : DBG_ASSERT(dynamic_cast<SbxObject*>(pVar), "FindVariable: invalid modul/object");
228 : 0 : pVar = static_cast<SbxObject*>(pVar)->GetMethods()->Find(aName, SbxCLASS_METHOD);
229 : : }
230 : 0 : break;
231 : : case OBJ_TYPE_DIALOG:
232 : : {
233 : : // sbx dialogs removed
234 : : }
235 : 0 : break;
236 : : case OBJ_TYPE_DOCUMENT_OBJECTS:
237 : 0 : bDocumentObjects = true;
238 : : case OBJ_TYPE_USERFORMS:
239 : : case OBJ_TYPE_NORMAL_MODULES:
240 : : case OBJ_TYPE_CLASS_MODULES:
241 : : {
242 : : // skip, to find the child entry.
243 : 0 : continue;
244 : : }
245 : : default:
246 : : {
247 : : OSL_FAIL( "FindVariable: Unbekannter Typ!" );
248 : 0 : pVar = 0;
249 : : }
250 : 0 : break;
251 : : }
252 : 0 : if ( !pVar )
253 : : break;
254 : 0 : }
255 : : }
256 : :
257 : 0 : return pVar;
258 : : }
259 : :
260 : 0 : BasicEntryDescriptor BasicTreeListBox::GetEntryDescriptor( SvLBoxEntry* pEntry )
261 : : {
262 : 0 : ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
263 : 0 : LibraryLocation eLocation = LIBRARY_LOCATION_UNKNOWN;
264 : 0 : String aLibName;
265 : 0 : String aLibSubName;
266 : 0 : String aName;
267 : 0 : String aMethodName;
268 : 0 : BasicEntryType eType = OBJ_TYPE_UNKNOWN;
269 : :
270 : 0 : if ( !pEntry )
271 : 0 : return BasicEntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
272 : :
273 : 0 : EntryArray aEntries;
274 : :
275 : 0 : while ( pEntry )
276 : : {
277 : 0 : sal_uInt16 nDepth = GetModel()->GetDepth( pEntry );
278 : 0 : switch ( nDepth )
279 : : {
280 : : case 4:
281 : : case 3:
282 : : case 2:
283 : : case 1:
284 : : {
285 : 0 : aEntries.push_front( pEntry );
286 : : }
287 : 0 : break;
288 : : case 0:
289 : : {
290 : 0 : if (BasicDocumentEntry* pBasicDocumentEntry = static_cast<BasicDocumentEntry*>(pEntry->GetUserData()))
291 : : {
292 : 0 : aDocument = pBasicDocumentEntry->GetDocument();
293 : 0 : eLocation = pBasicDocumentEntry->GetLocation();
294 : 0 : eType = OBJ_TYPE_DOCUMENT;
295 : : }
296 : : }
297 : 0 : break;
298 : : }
299 : 0 : pEntry = GetParent( pEntry );
300 : : }
301 : :
302 : 0 : if ( !aEntries.empty() )
303 : : {
304 : 0 : for ( size_t n = 0; n < aEntries.size(); n++ )
305 : : {
306 : 0 : SvLBoxEntry* pLE = aEntries[n];
307 : : DBG_ASSERT( pLE, "Entrie im Array nicht gefunden" );
308 : 0 : BasicEntry* pBE = static_cast<BasicEntry*>(pLE->GetUserData());
309 : : DBG_ASSERT( pBE, "Keine Daten im Eintrag gefunden!" );
310 : :
311 : 0 : switch ( pBE->GetType() )
312 : : {
313 : : case OBJ_TYPE_LIBRARY:
314 : : {
315 : 0 : aLibName = GetEntryText( pLE );
316 : 0 : eType = pBE->GetType();
317 : : }
318 : 0 : break;
319 : : case OBJ_TYPE_MODULE:
320 : : {
321 : 0 : aName = GetEntryText( pLE );
322 : 0 : eType = pBE->GetType();
323 : : }
324 : 0 : break;
325 : : case OBJ_TYPE_METHOD:
326 : : {
327 : 0 : aMethodName = GetEntryText( pLE );
328 : 0 : eType = pBE->GetType();
329 : : }
330 : 0 : break;
331 : : case OBJ_TYPE_DIALOG:
332 : : {
333 : 0 : aName = GetEntryText( pLE );
334 : 0 : eType = pBE->GetType();
335 : : }
336 : 0 : break;
337 : : case OBJ_TYPE_DOCUMENT_OBJECTS:
338 : : case OBJ_TYPE_USERFORMS:
339 : : case OBJ_TYPE_NORMAL_MODULES:
340 : : case OBJ_TYPE_CLASS_MODULES:
341 : : {
342 : 0 : aLibSubName = GetEntryText( pLE );
343 : 0 : eType = pBE->GetType();
344 : : }
345 : 0 : break;
346 : : default:
347 : : {
348 : : OSL_FAIL( "GetEntryDescriptor: Unbekannter Typ!" );
349 : 0 : eType = OBJ_TYPE_UNKNOWN;
350 : : }
351 : 0 : break;
352 : : }
353 : :
354 : 0 : if ( eType == OBJ_TYPE_UNKNOWN )
355 : 0 : break;
356 : : }
357 : : }
358 : :
359 : 0 : return BasicEntryDescriptor( aDocument, eLocation, aLibName, aLibSubName, aName, aMethodName, eType );
360 : : }
361 : :
362 : 0 : BasicIDEType BasicTreeListBox::ConvertType( BasicEntryType eType )
363 : : {
364 : 0 : BasicIDEType nType = static_cast<BasicIDEType>(OBJ_TYPE_UNKNOWN);
365 : :
366 : 0 : switch ( eType )
367 : : {
368 : : case OBJ_TYPE_DOCUMENT:
369 : : {
370 : 0 : nType = BASICIDE_TYPE_SHELL;
371 : : }
372 : 0 : break;
373 : : case OBJ_TYPE_LIBRARY:
374 : : {
375 : 0 : nType = BASICIDE_TYPE_LIBRARY;
376 : : }
377 : 0 : break;
378 : : case OBJ_TYPE_MODULE:
379 : : {
380 : 0 : nType = BASICIDE_TYPE_MODULE;
381 : : }
382 : 0 : break;
383 : : case OBJ_TYPE_DIALOG:
384 : : {
385 : 0 : nType = BASICIDE_TYPE_DIALOG;
386 : : }
387 : 0 : break;
388 : : case OBJ_TYPE_METHOD:
389 : : {
390 : 0 : nType = BASICIDE_TYPE_METHOD;
391 : : }
392 : 0 : break;
393 : : default: ;
394 : : }
395 : :
396 : 0 : return nType;
397 : : }
398 : :
399 : 0 : bool BasicTreeListBox::IsValidEntry( SvLBoxEntry* pEntry )
400 : : {
401 : 0 : bool bIsValid = false;
402 : :
403 : 0 : BasicEntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
404 : 0 : ScriptDocument aDocument( aDesc.GetDocument() );
405 : 0 : LibraryLocation eLocation( aDesc.GetLocation() );
406 : 0 : String aLibName( aDesc.GetLibName() );
407 : 0 : String aName( aDesc.GetName() );
408 : 0 : String aMethodName( aDesc.GetMethodName() );
409 : 0 : BasicEntryType eType( aDesc.GetType() );
410 : :
411 : 0 : switch ( eType )
412 : : {
413 : : case OBJ_TYPE_DOCUMENT:
414 : : {
415 : 0 : bIsValid = aDocument.isAlive()
416 : 0 : && (aDocument.isApplication()
417 : 0 : || GetRootEntryName(aDocument, eLocation).equals(GetEntryText(pEntry)));
418 : : }
419 : 0 : break;
420 : : case OBJ_TYPE_LIBRARY:
421 : : {
422 : 0 : bIsValid = aDocument.hasLibrary( E_SCRIPTS, aLibName ) || aDocument.hasLibrary( E_DIALOGS, aLibName );
423 : : }
424 : 0 : break;
425 : : case OBJ_TYPE_MODULE:
426 : : {
427 : 0 : bIsValid = aDocument.hasModule( aLibName, aName );
428 : : }
429 : 0 : break;
430 : : case OBJ_TYPE_DIALOG:
431 : : {
432 : 0 : bIsValid = aDocument.hasDialog( aLibName, aName );
433 : : }
434 : 0 : break;
435 : : case OBJ_TYPE_METHOD:
436 : : {
437 : 0 : bIsValid = BasicIDE::HasMethod( aDocument, aLibName, aName, aMethodName );
438 : : }
439 : 0 : break;
440 : : case OBJ_TYPE_DOCUMENT_OBJECTS:
441 : : case OBJ_TYPE_USERFORMS:
442 : : case OBJ_TYPE_NORMAL_MODULES:
443 : : case OBJ_TYPE_CLASS_MODULES:
444 : : {
445 : 0 : bIsValid = true;
446 : : }
447 : 0 : break;
448 : : default: ;
449 : : }
450 : :
451 : 0 : return bIsValid;
452 : : }
453 : :
454 : 0 : SbModule* BasicTreeListBox::FindModule( SvLBoxEntry* pEntry )
455 : : {
456 : 0 : return dynamic_cast<SbModule*>(FindVariable(pEntry));
457 : : }
458 : :
459 : 0 : SvLBoxEntry* BasicTreeListBox::FindRootEntry( const ScriptDocument& rDocument, LibraryLocation eLocation )
460 : : {
461 : : OSL_ENSURE( rDocument.isValid(), "BasicTreeListBox::FindRootEntry: invalid document!" );
462 : 0 : sal_uLong nRootPos = 0;
463 : 0 : SvLBoxEntry* pRootEntry = GetEntry( nRootPos );
464 : 0 : while ( pRootEntry )
465 : : {
466 : : DBG_ASSERT( static_cast<BasicEntry*>(pRootEntry->GetUserData())->GetType() == OBJ_TYPE_DOCUMENT, "Kein Shelleintrag?" );
467 : 0 : BasicDocumentEntry* pBDEntry = static_cast<BasicDocumentEntry*>(pRootEntry->GetUserData());
468 : 0 : if (pBDEntry && pBDEntry->GetDocument() == rDocument && pBDEntry->GetLocation() == eLocation)
469 : 0 : return pRootEntry;
470 : 0 : pRootEntry = GetEntry( ++nRootPos );
471 : : }
472 : 0 : return 0;
473 : : }
474 : :
475 : 0 : ::rtl::OUString CreateMgrAndLibStr( const ::rtl::OUString& rMgrName, const ::rtl::OUString& rLibName )
476 : : {
477 : 0 : ::rtl::OUStringBuffer aName;
478 : 0 : aName.append('[');
479 : 0 : aName.append(rMgrName);
480 : 0 : aName.append(']');
481 : 0 : aName.append('.');
482 : 0 : aName.append(rLibName);
483 : 0 : return aName.makeStringAndClear();
484 : : }
485 : :
486 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|