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