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