1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_basctl.hxx"
30 
31 
32 #define GLOBALOVERFLOW
33 
34 #include <ide_pch.hxx>
35 
36 #include <svtools/filedlg.hxx>
37 
38 
39 #include <sot/storinfo.hxx>
40 
41 #include <moduldlg.hrc>
42 #include <moduldlg.hxx>
43 #include <basidesh.hrc>
44 #include <basidesh.hxx>
45 #include <bastypes.hxx>
46 #include <basobj.hxx>
47 #include <baside2.hrc>
48 #include <iderdll.hxx>
49 #include <iderdll2.hxx>
50 #include <svx/passwd.hxx>
51 #include <sbxitem.hxx>
52 #include <basdoc.hxx>
53 #include <ucbhelper/content.hxx>
54 #include "rtl/uri.hxx"
55 #include <tools/urlobj.hxx>
56 #include <tools/diagnose_ex.h>
57 
58 #include <sot/storage.hxx>
59 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
60 #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
61 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
62 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
63 #ifndef _COM_SUN_STAR_SCRIPT_XLIBRYARYCONTAINER2_HPP_
64 #include <com/sun/star/script/XLibraryContainer2.hpp>
65 #endif
66 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
67 #include <com/sun/star/script/XLibraryContainerExport.hpp>
68 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
69 #include "com/sun/star/ucb/XCommandEnvironment.hpp"
70 #include <com/sun/star/ucb/NameClash.hpp>
71 #include "com/sun/star/packages/manifest/XManifestWriter.hpp"
72 #include <unotools/pathoptions.hxx>
73 #include <comphelper/processfactory.hxx>
74 
75 #include <com/sun/star/util/VetoException.hpp>
76 #include <com/sun/star/script/ModuleSizeExceededRequest.hpp>
77 
78 using namespace ::comphelper;
79 using ::rtl::OUString;
80 using namespace ::com::sun::star;
81 using namespace ::com::sun::star::uno;
82 using namespace ::com::sun::star::lang;
83 using namespace ::com::sun::star::ucb;
84 using namespace ::com::sun::star::ui::dialogs;
85 
86 
87 typedef ::cppu::WeakImplHelper1< task::XInteractionHandler > HandlerImpl_BASE;
88 
89 class DummyInteractionHandler  : public HandlerImpl_BASE
90 {
91     Reference< task::XInteractionHandler > m_xHandler;
92 public:
93     DummyInteractionHandler( const Reference< task::XInteractionHandler >& xHandler ) : m_xHandler( xHandler ){}
94 
95     virtual void SAL_CALL handle( const Reference< task::XInteractionRequest >& rRequest ) throw (::com::sun::star::uno::RuntimeException)
96     {
97         if ( m_xHandler.is() )
98         {
99 		script::ModuleSizeExceededRequest aModSizeException;
100 		if ( rRequest->getRequest() >>= aModSizeException )
101 			m_xHandler->handle( rRequest );
102         }
103     }
104 };
105 
106 //----------------------------------------------------------------------------
107 //  BasicLibUserData
108 //----------------------------------------------------------------------------
109 class BasicLibUserData
110 {
111 private:
112     ScriptDocument      m_aDocument;
113 
114 public:
115                     BasicLibUserData( const ScriptDocument& rDocument ) : m_aDocument( rDocument ) { }
116                     virtual 		~BasicLibUserData() {};
117 
118     const ScriptDocument&
119                     GetDocument() const { return m_aDocument; }
120 };
121 
122 
123 //----------------------------------------------------------------------------
124 //  BasicLibLBoxString
125 //----------------------------------------------------------------------------
126 
127 class BasicLibLBoxString : public SvLBoxString
128 {
129 public:
130 	BasicLibLBoxString( SvLBoxEntry* pEntry, sal_uInt16 nFlags,	const String& rTxt ) :
131 		SvLBoxString( pEntry, nFlags, rTxt ) {}
132 
133 	virtual void Paint( const Point& rPos, SvLBox& rDev, sal_uInt16 nFlags, SvLBoxEntry* pEntry );
134 };
135 
136 //----------------------------------------------------------------------------
137 
138 void BasicLibLBoxString::Paint( const Point& rPos, SvLBox& rDev, sal_uInt16, SvLBoxEntry* pEntry )
139 {
140     // Change text color if library is read only:
141     bool bReadOnly = false;
142     if (pEntry && pEntry->GetUserData())
143     {
144         ScriptDocument aDocument(
145             static_cast< BasicLibUserData * >(pEntry->GetUserData())->
146             GetDocument() );
147 
148         rtl::OUString aLibName(
149             static_cast< SvLBoxString * >(pEntry->GetItem(1))->GetText());
150         Reference< script::XLibraryContainer2 > xModLibContainer(
151             aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY);
152         Reference< script::XLibraryContainer2 > xDlgLibContainer(
153             aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY);
154         bReadOnly
155             = (xModLibContainer.is() && xModLibContainer->hasByName(aLibName)
156                && xModLibContainer->isLibraryReadOnly(aLibName))
157             || (xDlgLibContainer.is() && xDlgLibContainer->hasByName(aLibName)
158                 && xDlgLibContainer->isLibraryReadOnly(aLibName));
159     }
160     if (bReadOnly)
161         rDev.DrawCtrlText(rPos, GetText(), 0, STRING_LEN, TEXT_DRAW_DISABLE);
162     else
163         rDev.DrawText(rPos, GetText());
164 }
165 
166 
167 //----------------------------------------------------------------------------
168 //  BasicCheckBox
169 //----------------------------------------------------------------------------
170 
171 BasicCheckBox::BasicCheckBox( Window* pParent, const ResId& rResId )
172     :SvTabListBox( pParent, rResId )
173     ,m_aDocument( ScriptDocument::getApplicationScriptDocument() )
174 {
175 	nMode = LIBMODE_MANAGER;
176 	long aTabs_[] = { 1, 12 };	// Mindestens einen braucht die TabPos...
177 								// 12 wegen der Checkbox
178 	SetTabs( aTabs_ );
179 	Init();
180 }
181 
182 //----------------------------------------------------------------------------
183 
184 __EXPORT BasicCheckBox::~BasicCheckBox()
185 {
186 	delete pCheckButton;
187 
188 	// delete user data
189 	SvLBoxEntry* pEntry = First();
190 	while ( pEntry )
191 	{
192 		delete (BasicLibUserData*)pEntry->GetUserData();
193 		pEntry = Next( pEntry );
194 	}
195 }
196 
197 //----------------------------------------------------------------------------
198 
199 void BasicCheckBox::Init()
200 {
201     pCheckButton = new SvLBoxButtonData(this);
202 
203     if ( nMode == LIBMODE_CHOOSER )
204         EnableCheckButton( pCheckButton );
205     else
206         EnableCheckButton( 0 );
207 
208     SetHighlightRange();
209 }
210 
211 //----------------------------------------------------------------------------
212 
213 void BasicCheckBox::SetMode( sal_uInt16 n )
214 {
215     nMode = n;
216 
217     if ( nMode == LIBMODE_CHOOSER )
218         EnableCheckButton( pCheckButton );
219     else
220         EnableCheckButton( 0 );
221 }
222 
223 //----------------------------------------------------------------------------
224 
225 SvLBoxEntry* BasicCheckBox::DoInsertEntry( const String& rStr, sal_uLong nPos )
226 {
227 	return SvTabListBox::InsertEntryToColumn( rStr, nPos, 0 );
228 }
229 
230 //----------------------------------------------------------------------------
231 
232 SvLBoxEntry* BasicCheckBox::FindEntry( const String& rName )
233 {
234 	sal_uLong nCount = GetEntryCount();
235 	for ( sal_uLong i = 0; i < nCount; i++ )
236 	{
237 		SvLBoxEntry* pEntry = GetEntry( i );
238 		DBG_ASSERT( pEntry, "pEntry?!" );
239 		if ( rName.CompareIgnoreCaseToAscii( GetEntryText( pEntry, 0 ) ) == COMPARE_EQUAL )
240 			return pEntry;
241 	}
242 	return 0;
243 }
244 
245 //----------------------------------------------------------------------------
246 
247 void BasicCheckBox::CheckEntryPos( sal_uLong nPos, sal_Bool bCheck )
248 {
249 	if ( nPos < GetEntryCount() )
250 	{
251 		SvLBoxEntry* pEntry = GetEntry( nPos );
252 
253 		if ( bCheck != GetCheckButtonState( pEntry ) )
254 			SetCheckButtonState( pEntry,
255 								 bCheck
256 									? SvButtonState(SV_BUTTON_CHECKED)
257 									: SvButtonState(SV_BUTTON_UNCHECKED) );
258 	}
259 }
260 
261 //----------------------------------------------------------------------------
262 
263 sal_Bool BasicCheckBox::IsChecked( sal_uLong nPos ) const
264 {
265 	if ( nPos < GetEntryCount() )
266 		return (GetCheckButtonState( GetEntry( nPos ) ) == SV_BUTTON_CHECKED);
267 	return sal_False;
268 }
269 
270 //----------------------------------------------------------------------------
271 
272 void BasicCheckBox::InitEntry( SvLBoxEntry* pEntry, const XubString& rTxt, const Image& rImg1, const Image& rImg2, SvLBoxButtonKind eButtonKind )
273 {
274 	SvTabListBox::InitEntry( pEntry, rTxt, rImg1, rImg2, eButtonKind );
275 
276     if ( nMode == LIBMODE_MANAGER )
277     {
278         // initialize all columns with own string class (column 0 == bitmap)
279         sal_uInt16 nCount = pEntry->ItemCount();
280         for ( sal_uInt16 nCol = 1; nCol < nCount; ++nCol )
281         {
282 		    SvLBoxString* pCol = (SvLBoxString*)pEntry->GetItem( nCol );
283 		    BasicLibLBoxString* pStr = new BasicLibLBoxString( pEntry, 0, pCol->GetText() );
284 		    pEntry->ReplaceItem( pStr, nCol );
285 	    }
286     }
287 }
288 
289 //----------------------------------------------------------------------------
290 
291 sal_Bool __EXPORT BasicCheckBox::EditingEntry( SvLBoxEntry* pEntry, Selection& )
292 {
293 	if ( nMode != LIBMODE_MANAGER )
294 		return sal_False;
295 
296 	DBG_ASSERT( pEntry, "Kein Eintrag?" );
297 
298     // check, if Standard library
299     String aLibName = GetEntryText( pEntry, 0 );
300 	if ( aLibName.EqualsIgnoreCaseAscii( "Standard" ) )
301     {
302 		ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_CANNOTCHANGENAMESTDLIB ) ) ).Execute();
303 		return sal_False;
304 	}
305 
306     // check, if library is readonly
307     ::rtl::OUString aOULibName( aLibName );
308     Reference< script::XLibraryContainer2 > xModLibContainer( m_aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
309     Reference< script::XLibraryContainer2 > xDlgLibContainer( m_aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
310     if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) && !xModLibContainer->isLibraryLink( aOULibName ) ) ||
311          ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) && !xDlgLibContainer->isLibraryLink( aOULibName ) ) )
312     {
313         ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_LIBISREADONLY ) ) ).Execute();
314         return sal_False;
315     }
316 
317 	// i24094: Password verification necessary for renaming
318     sal_Bool bOK = sal_True;
319     if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
320     {
321         // check password
322         Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
323         if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
324         {
325             String aPassword;
326 		    Reference< script::XLibraryContainer > xModLibContainer1( xModLibContainer, UNO_QUERY );
327             bOK = QueryPassword( xModLibContainer1, aLibName, aPassword );
328         }
329         if ( !bOK )
330 	        return sal_False;
331     }
332 
333     // TODO: check if library is reference/link
334 
335     // Prueffen, ob Referenz...
336     /*
337 	sal_uInt16 nLib = pBasMgr->GetLibId( GetEntryText( pEntry, 0 ) );
338 	DBG_ASSERT( nLib != LIB_NOTFOUND, "LibId ?!" );
339 	if ( pBasMgr->IsReference( nLib ) )
340 	{
341 		ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_CANNOTCHANGENAMEREFLIB ) ) ).Execute();
342 		return sal_False;
343 	}
344     */
345 	return sal_True;
346 }
347 
348 //----------------------------------------------------------------------------
349 
350 sal_Bool __EXPORT BasicCheckBox::EditedEntry( SvLBoxEntry* pEntry, const String& rNewText )
351 {
352 	sal_Bool bValid = ( rNewText.Len() <= 30 ) && BasicIDE::IsValidSbxName( rNewText );
353 	String aCurText( GetEntryText( pEntry, 0 ) );
354 	if ( bValid && ( aCurText != rNewText ) )
355 	{
356         try
357         {
358             ::rtl::OUString aOUOldName( aCurText );
359             ::rtl::OUString aOUNewName( rNewText );
360 
361             Reference< script::XLibraryContainer2 > xModLibContainer( m_aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
362             if ( xModLibContainer.is() )
363             {
364                 xModLibContainer->renameLibrary( aOUOldName, aOUNewName );
365             }
366 
367             Reference< script::XLibraryContainer2 > xDlgLibContainer( m_aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
368             if ( xDlgLibContainer.is() )
369             {
370                 xDlgLibContainer->renameLibrary( aOUOldName, aOUNewName );
371             }
372 
373             BasicIDE::MarkDocumentModified( m_aDocument );
374             SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
375             if ( pBindings )
376             {
377                 pBindings->Invalidate( SID_BASICIDE_LIBSELECTOR );
378                 pBindings->Update( SID_BASICIDE_LIBSELECTOR );
379             }
380 		}
381 		catch ( container::ElementExistException& )
382 		{
383 			ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_SBXNAMEALLREADYUSED ) ) ).Execute();
384 			return sal_False;
385         }
386 		catch ( container::NoSuchElementException& )
387 		{
388 			DBG_UNHANDLED_EXCEPTION();
389             return sal_False;
390 		}
391     }
392 
393 	if ( !bValid )
394 	{
395 		if ( rNewText.Len() > 30 )
396 			ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_LIBNAMETOLONG ) ) ).Execute();
397 		else
398 			ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
399 	}
400 
401 	return bValid;
402 }
403 
404 //----------------------------------------------------------------------------
405 // NewObjectDialog
406 //----------------------------------------------------------------------------
407 
408 IMPL_LINK(NewObjectDialog, OkButtonHandler, Button *, EMPTYARG)
409 {
410     if (BasicIDE::IsValidSbxName(aEdit.GetText()))
411         EndDialog(1);
412     else
413     {
414         ErrorBox(this, WB_OK | WB_DEF_OK,
415                  String(IDEResId(RID_STR_BADSBXNAME))).Execute();
416         aEdit.GrabFocus();
417     }
418     return 0;
419 }
420 
421 NewObjectDialog::NewObjectDialog(Window * pParent, sal_uInt16 nMode,
422                                  bool bCheckName)
423 	: ModalDialog( pParent, IDEResId( RID_DLG_NEWLIB ) ),
424 		aText( this, IDEResId( RID_FT_NEWLIB ) ),
425 		aEdit( this, IDEResId( RID_ED_LIBNAME ) ),
426         aOKButton( this, IDEResId( RID_PB_OK ) ),
427 		aCancelButton( this, IDEResId( RID_PB_CANCEL ) )
428 {
429 	FreeResource();
430 	aEdit.GrabFocus();
431 
432 	if ( nMode == NEWOBJECTMODE_LIB )
433 	{
434         SetText( String( IDEResId( RID_STR_NEWLIB ) ) );
435 	}
436 	else if ( nMode == NEWOBJECTMODE_MOD )
437 	{
438 		SetText( String( IDEResId( RID_STR_NEWMOD ) ) );
439 	}
440 	else if ( nMode == NEWOBJECTMODE_METH )
441 	{
442 		SetText( String( IDEResId( RID_STR_NEWMETH ) ) );
443 	}
444 	else
445 	{
446 		SetText( String( IDEResId( RID_STR_NEWDLG ) ) );
447 	}
448 
449     if (bCheckName)
450         aOKButton.SetClickHdl(LINK(this, NewObjectDialog, OkButtonHandler));
451 }
452 
453 //----------------------------------------------------------------------------
454 
455 NewObjectDialog::~NewObjectDialog()
456 {
457 }
458 
459 //----------------------------------------------------------------------------
460 // ExportDialog
461 //----------------------------------------------------------------------------
462 
463 IMPL_LINK(ExportDialog, OkButtonHandler, Button *, EMPTYARG)
464 {
465 	mbExportAsPackage = maExportAsPackageButton.IsChecked();
466 	EndDialog(1);
467     return 0;
468 }
469 
470 ExportDialog::ExportDialog( Window * pParent )
471 	: ModalDialog( pParent, IDEResId( RID_DLG_EXPORT ) ),
472 		maExportAsPackageButton( this, IDEResId( RB_EXPORTASPACKAGE ) ),
473 		maExportAsBasicButton( this, IDEResId( RB_EXPORTASBASIC ) ),
474         maOKButton( this, IDEResId( RID_PB_OK ) ),
475 		maCancelButton( this, IDEResId( RID_PB_CANCEL ) )
476 {
477 	FreeResource();
478 	maExportAsPackageButton.Check();
479 	maOKButton.SetClickHdl(LINK(this, ExportDialog, OkButtonHandler));
480 }
481 
482 //----------------------------------------------------------------------------
483 
484 ExportDialog::~ExportDialog()
485 {
486 }
487 
488 //----------------------------------------------------------------------------
489 //  LibPage
490 //----------------------------------------------------------------------------
491 
492 LibPage::LibPage( Window * pParent )
493     :TabPage( pParent, IDEResId( RID_TP_LIBS ) )
494     ,aBasicsText( this, IDEResId( RID_STR_BASICS ) )
495     ,aBasicsBox( this, IDEResId( RID_LB_BASICS ) )
496     ,aLibText( this, IDEResId( RID_STR_LIB ) )
497     ,aLibBox( this, IDEResId( RID_TRLBOX ) )
498     ,aEditButton( this, IDEResId( RID_PB_EDIT ) )
499     ,aCloseButton( this, IDEResId( RID_PB_CLOSE ) )
500     ,aPasswordButton( this, IDEResId( RID_PB_PASSWORD ) )
501     ,aNewLibButton( this, IDEResId( RID_PB_NEWLIB ) )
502     ,aInsertLibButton( this, IDEResId( RID_PB_APPEND ) )
503     ,aExportButton( this, IDEResId( RID_PB_EXPORT ) )
504     ,aDelButton( this, IDEResId( RID_PB_DELETE ) )
505     ,m_aCurDocument( ScriptDocument::getApplicationScriptDocument() )
506     ,m_eCurLocation( LIBRARY_LOCATION_UNKNOWN )
507 {
508 	FreeResource();
509 	pTabDlg = 0;
510 
511 	aEditButton.SetClickHdl( LINK( this, LibPage, ButtonHdl ) );
512 	aNewLibButton.SetClickHdl( LINK( this, LibPage, ButtonHdl ) );
513 	aPasswordButton.SetClickHdl( LINK( this, LibPage, ButtonHdl ) );
514 	aExportButton.SetClickHdl( LINK( this, LibPage, ButtonHdl ) );
515 	aInsertLibButton.SetClickHdl( LINK( this, LibPage, ButtonHdl ) );
516 	aDelButton.SetClickHdl( LINK( this, LibPage, ButtonHdl ) );
517 	aCloseButton.SetClickHdl( LINK( this, LibPage, ButtonHdl ) );
518 	aLibBox.SetSelectHdl( LINK( this, LibPage, TreeListHighlightHdl ) );
519 
520 	aBasicsBox.SetSelectHdl( LINK( this, LibPage, BasicSelectHdl ) );
521 
522 	aLibBox.SetMode( LIBMODE_MANAGER );
523 	aLibBox.EnableInplaceEditing( sal_True );
524     aLibBox.SetStyle( WB_HSCROLL | WB_BORDER | WB_TABSTOP );
525 	aCloseButton.GrabFocus();
526 
527 	long aTabs[] = { 2, 30, 120 };
528     aLibBox.SetTabs( aTabs, MAP_PIXEL );
529 
530 	FillListBox();
531 	aBasicsBox.SelectEntryPos( 0 );
532 	SetCurLib();
533 
534 	CheckButtons();
535 }
536 
537 //----------------------------------------------------------------------------
538 
539 LibPage::~LibPage()
540 {
541     sal_uInt16 nCount = aBasicsBox.GetEntryCount();
542     for ( sal_uInt16 i = 0; i < nCount; ++i )
543     {
544         BasicDocumentEntry* pEntry = (BasicDocumentEntry*)aBasicsBox.GetEntryData( i );
545         delete pEntry;
546     }
547 }
548 
549 //----------------------------------------------------------------------------
550 
551 void LibPage::CheckButtons()
552 {
553     SvLBoxEntry* pCur = aLibBox.GetCurEntry();
554     if ( pCur )
555     {
556         String aLibName = aLibBox.GetEntryText( pCur, 0 );
557         ::rtl::OUString aOULibName( aLibName );
558         Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
559         Reference< script::XLibraryContainer2 > xDlgLibContainer( m_aCurDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
560 
561         if ( m_eCurLocation == LIBRARY_LOCATION_SHARE )
562         {
563             aPasswordButton.Disable();
564             aNewLibButton.Disable();
565             aInsertLibButton.Disable();
566             aDelButton.Disable();
567         }
568         else if ( aLibName.EqualsIgnoreCaseAscii( "Standard" ) )
569         {
570             aPasswordButton.Disable();
571             aNewLibButton.Enable();
572             aInsertLibButton.Enable();
573             aExportButton.Disable();
574     		aDelButton.Disable();
575 			if ( !aLibBox.HasFocus() )
576 				aCloseButton.GrabFocus();
577         }
578         else if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) ) ||
579                   ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) ) )
580         {
581             aPasswordButton.Disable();
582             aNewLibButton.Enable();
583             aInsertLibButton.Enable();
584             if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) && !xModLibContainer->isLibraryLink( aOULibName ) ) ||
585                  ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) && !xDlgLibContainer->isLibraryLink( aOULibName ) ) )
586                 aDelButton.Disable();
587             else
588                 aDelButton.Enable();
589         }
590         else
591         {
592             if ( xModLibContainer.is() && !xModLibContainer->hasByName( aOULibName ) )
593                 aPasswordButton.Disable();
594             else
595     		    aPasswordButton.Enable();
596 
597             aNewLibButton.Enable();
598             aInsertLibButton.Enable();
599             aExportButton.Enable();
600     		aDelButton.Enable();
601         }
602     }
603 }
604 
605 //----------------------------------------------------------------------------
606 
607 void __EXPORT LibPage::ActivatePage()
608 {
609 	SetCurLib();
610 }
611 
612 //----------------------------------------------------------------------------
613 
614 
615 void __EXPORT LibPage::DeactivatePage()
616 {
617 }
618 
619 //----------------------------------------------------------------------------
620 
621 
622 IMPL_LINK_INLINE_START( LibPage, TreeListHighlightHdl, SvTreeListBox *, pBox )
623 {
624 	if ( pBox->IsSelected( pBox->GetHdlEntry() ) )
625 		CheckButtons();
626 	return 0;
627 }
628 IMPL_LINK_INLINE_END( LibPage, TreeListHighlightHdl, SvTreeListBox *, pBox )
629 
630 //----------------------------------------------------------------------------
631 
632 IMPL_LINK_INLINE_START( LibPage, BasicSelectHdl, ListBox *, pBox )
633 {
634 	(void)pBox;
635 	SetCurLib();
636 	CheckButtons();
637 	return 0;
638 }
639 IMPL_LINK_INLINE_END( LibPage, BasicSelectHdl, ListBox *, pBox )
640 
641 //----------------------------------------------------------------------------
642 
643 IMPL_LINK( LibPage, ButtonHdl, Button *, pButton )
644 {
645 	if ( pButton == &aEditButton )
646 	{
647     	SfxAllItemSet aArgs( SFX_APP()->GetPool() );
648         SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
649         SFX_APP()->ExecuteSlot( aRequest );
650 
651         SfxUsrAnyItem aDocItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, makeAny( m_aCurDocument.getDocumentOrNull() ) );
652 		SvLBoxEntry* pCurEntry = aLibBox.GetCurEntry();
653 		DBG_ASSERT( pCurEntry, "Entry?!" );
654 		String aLibName( aLibBox.GetEntryText( pCurEntry, 0 ) );
655         SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
656         BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
657         SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
658         SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
659         if ( pDispatcher )
660 		{
661 			pDispatcher->Execute( SID_BASICIDE_LIBSELECTED,
662 									SFX_CALLMODE_ASYNCHRON, &aDocItem, &aLibNameItem, 0L );
663 		}
664 		EndTabDialog( 1 );
665         return 0;
666 	}
667 	else if ( pButton == &aNewLibButton )
668 		NewLib();
669 	else if ( pButton == &aInsertLibButton )
670 		InsertLib();
671 	else if ( pButton == &aExportButton )
672 		Export();
673 	else if ( pButton == &aDelButton )
674 		DeleteCurrent();
675 	else if ( pButton == &aCloseButton )
676 	{
677 		EndTabDialog( 0 );
678         return 0;
679 	}
680 	else if ( pButton == &aPasswordButton )
681 	{
682 		SvLBoxEntry* pCurEntry = aLibBox.GetCurEntry();
683         String aLibName( aLibBox.GetEntryText( pCurEntry, 0 ) );
684         ::rtl::OUString aOULibName( aLibName );
685 
686         // load module library (if not loaded)
687         Reference< script::XLibraryContainer > xModLibContainer = m_aCurDocument.getLibraryContainer( E_SCRIPTS );
688 	    if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
689         {
690 			BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
691             if ( pIDEShell )
692                 pIDEShell->GetViewFrame()->GetWindow().EnterWait();
693             xModLibContainer->loadLibrary( aOULibName );
694             if ( pIDEShell )
695                 pIDEShell->GetViewFrame()->GetWindow().LeaveWait();
696         }
697 
698         // load dialog library (if not loaded)
699         Reference< script::XLibraryContainer > xDlgLibContainer = m_aCurDocument.getLibraryContainer( E_DIALOGS );
700         if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
701         {
702 			BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
703             if ( pIDEShell )
704                 pIDEShell->GetViewFrame()->GetWindow().EnterWait();
705             xDlgLibContainer->loadLibrary( aOULibName );
706             if ( pIDEShell )
707                 pIDEShell->GetViewFrame()->GetWindow().LeaveWait();
708         }
709 
710         // check, if library is password protected
711 	    if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
712         {
713             Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
714             if ( xPasswd.is() )
715             {
716                 sal_Bool bProtected = xPasswd->isLibraryPasswordProtected( aOULibName );
717 
718                 // change password dialog
719                 SvxPasswordDialog* pDlg = new SvxPasswordDialog( this, sal_True, !bProtected );
720                 pDlg->SetCheckPasswordHdl( LINK( this, LibPage, CheckPasswordHdl ) );
721 
722 		        if ( pDlg->Execute() == RET_OK )
723 		        {
724                     sal_Bool bNewProtected = xPasswd->isLibraryPasswordProtected( aOULibName );
725 
726 			        if ( bNewProtected != bProtected )
727 			        {
728         		        sal_uLong nPos = (sal_uLong)aLibBox.GetModel()->GetAbsPos( pCurEntry );
729                         aLibBox.GetModel()->Remove( pCurEntry );
730                         ImpInsertLibEntry( aLibName, nPos );
731                         aLibBox.SetCurEntry( aLibBox.GetEntry( nPos ) );
732                     }
733 
734 					BasicIDE::MarkDocumentModified( m_aCurDocument );
735 		        }
736 		        delete pDlg;
737             }
738         }
739     }
740 	CheckButtons();
741 	return 0;
742 }
743 
744 //----------------------------------------------------------------------------
745 
746 IMPL_LINK_INLINE_START( LibPage, CheckPasswordHdl, SvxPasswordDialog *, pDlg )
747 {
748     long nRet = 0;
749 
750 	SvLBoxEntry* pCurEntry = aLibBox.GetCurEntry();
751     ::rtl::OUString aOULibName( aLibBox.GetEntryText( pCurEntry, 0 ) );
752     Reference< script::XLibraryContainerPassword > xPasswd( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
753 
754     if ( xPasswd.is() )
755     {
756         try
757         {
758             ::rtl::OUString aOUOldPassword( pDlg->GetOldPassword() );
759             ::rtl::OUString aOUNewPassword( pDlg->GetNewPassword() );
760             xPasswd->changeLibraryPassword( aOULibName, aOUOldPassword, aOUNewPassword );
761             nRet = 1;
762         }
763         catch (...)
764         {
765         }
766     }
767 
768     return nRet;
769 }
770 IMPL_LINK_INLINE_END( LibPage, CheckPasswordHdl, SvxPasswordDialog *, pDlg )
771 
772 //----------------------------------------------------------------------------
773 
774 void LibPage::NewLib()
775 {
776 	createLibImpl( static_cast<Window*>( this ), m_aCurDocument, &aLibBox, NULL);
777 }
778 
779 //----------------------------------------------------------------------------
780 
781 void LibPage::InsertLib()
782 {
783     // file open dialog
784     Reference< lang::XMultiServiceFactory > xMSF( ::comphelper::getProcessServiceFactory() );
785     Reference < XFilePicker > xFP;
786     if( xMSF.is() )
787     {
788 		Sequence <Any> aServiceType(1);
789 		aServiceType[0] <<= TemplateDescription::FILEOPEN_SIMPLE;
790         xFP = Reference< XFilePicker >( xMSF->createInstanceWithArguments(
791 					::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FilePicker" ) ), aServiceType ), UNO_QUERY );
792     }
793 	xFP->setTitle( String( IDEResId( RID_STR_APPENDLIBS ) ) );
794 
795     // filter
796     ::rtl::OUString aTitle = String( IDEResId( RID_STR_BASIC ) );
797     ::rtl::OUString aFilter;
798     aFilter =  ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "*.sbl;*.xlc;*.xlb" ) );       // library files
799     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.sdw;*.sxw;*.odt" ) );      // text
800     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.vor;*.stw;*.ott" ) );      // text template
801     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.sgl;*.sxg;*.odm" ) );      // master document
802     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.oth" ) );                  // html document template
803     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.sdc;*.sxc;*.ods" ) );      // spreadsheet
804     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.stc;*.ots" ) );            // spreadsheet template
805     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.sda;*.sxd;*.odg" ) );      // drawing
806     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.std;*.otg" ) );            // drawing template
807     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.sdd;*.sxi;*.odp" ) );      // presentation
808     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.sti;*.otp" ) );            // presentation template
809     aFilter += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ";*.sxm;*.odf" ) );            // formula
810     Reference< XFilterManager > xFltMgr(xFP, UNO_QUERY);
811     xFltMgr->appendFilter( aTitle, aFilter );
812 
813     // set display directory and filter
814 	String aPath( IDE_DLL()->GetExtraData()->GetAddLibPath() );
815 	if ( aPath.Len() )
816 	{
817 		xFP->setDisplayDirectory( aPath );
818 	}
819 	else
820 	{
821         // macro path from configuration management
822         xFP->setDisplayDirectory( SvtPathOptions().GetWorkPath() );
823 	}
824 
825 	String aLastFilter( IDE_DLL()->GetExtraData()->GetAddLibFilter() );
826 	if ( aLastFilter.Len() )
827 	{
828 		xFltMgr->setCurrentFilter( aLastFilter );
829 	}
830 	else
831 	{
832 		xFltMgr->setCurrentFilter( String( IDEResId( RID_STR_BASIC ) ) );
833 	}
834 
835     if ( xFP->execute() == RET_OK )
836 	{
837         IDE_DLL()->GetExtraData()->SetAddLibPath( xFP->getDisplayDirectory() );
838 		IDE_DLL()->GetExtraData()->SetAddLibFilter( xFltMgr->getCurrentFilter() );
839 
840         // library containers for import
841         Reference< script::XLibraryContainer2 > xModLibContImport;
842         Reference< script::XLibraryContainer2 > xDlgLibContImport;
843 
844         // file URLs
845 		Sequence< ::rtl::OUString > aFiles = xFP->getFiles();
846         INetURLObject aURLObj( aFiles[0] );
847         INetURLObject aModURLObj( aURLObj );
848         INetURLObject aDlgURLObj( aURLObj );
849 
850         String aBase = aURLObj.getBase();
851         String aModBase = String::CreateFromAscii( "script" );
852         String aDlgBase = String::CreateFromAscii( "dialog" );
853 
854         if ( aBase == aModBase || aBase == aDlgBase )
855         {
856             aModURLObj.setBase( aModBase );
857             aDlgURLObj.setBase( aDlgBase );
858         }
859 
860         if ( xMSF.is() )
861         {
862             Reference< XSimpleFileAccess > xSFA( xMSF->createInstance(
863                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.SimpleFileAccess" ) ) ), UNO_QUERY );
864 
865             if ( xSFA.is() )
866             {
867                 ::rtl::OUString aModURL( aModURLObj.GetMainURL( INetURLObject::NO_DECODE ) );
868                 if ( xSFA->exists( aModURL ) )
869                 {
870 		            Sequence <Any> aSeqModURL(1);
871                     aSeqModURL[0] <<= aModURL;
872                     xModLibContImport = Reference< script::XLibraryContainer2 >( xMSF->createInstanceWithArguments(
873 					            ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.DocumentScriptLibraryContainer" ) ), aSeqModURL ), UNO_QUERY );
874                 }
875 
876                 ::rtl::OUString aDlgURL( aDlgURLObj.GetMainURL( INetURLObject::NO_DECODE ) );
877                 if ( xSFA->exists( aDlgURL ) )
878                 {
879 		            Sequence <Any> aSeqDlgURL(1);
880 		            aSeqDlgURL[0] <<= aDlgURL;
881                     xDlgLibContImport = Reference< script::XLibraryContainer2 >( xMSF->createInstanceWithArguments(
882 					            ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.DocumentDialogLibraryContainer" ) ), aSeqDlgURL ), UNO_QUERY );
883                 }
884             }
885         }
886 
887         if ( xModLibContImport.is() || xDlgLibContImport.is() )
888         {
889 			LibDialog* pLibDlg = 0;
890 
891             Reference< script::XLibraryContainer > xModLibContImp( xModLibContImport, UNO_QUERY );
892             Reference< script::XLibraryContainer > xDlgLibContImp( xDlgLibContImport, UNO_QUERY );
893             Sequence< ::rtl::OUString > aLibNames = BasicIDE::GetMergedLibraryNames( xModLibContImp, xDlgLibContImp );
894             sal_Int32 nLibCount = aLibNames.getLength();
895 	        const ::rtl::OUString* pLibNames = aLibNames.getConstArray();
896             for ( sal_Int32 i = 0 ; i < nLibCount ; i++ )
897             {
898                 // library import dialog
899 			    if ( !pLibDlg )
900 			    {
901 				    pLibDlg = new LibDialog( this );
902 				    pLibDlg->SetStorageName( aURLObj.getName() );
903 				    pLibDlg->GetLibBox().SetMode( LIBMODE_CHOOSER );
904                 }
905 
906                 // libbox entries
907                 String aLibName( pLibNames[ i ] );
908                 String aOULibName( aLibName );
909                 if ( !( ( xModLibContImport.is() && xModLibContImport->hasByName( aOULibName ) && xModLibContImport->isLibraryLink( aOULibName ) ) ||
910                         ( xDlgLibContImport.is() && xDlgLibContImport->hasByName( aOULibName ) && xDlgLibContImport->isLibraryLink( aOULibName ) ) ) )
911                 {
912 			        SvLBoxEntry* pEntry = pLibDlg->GetLibBox().DoInsertEntry( aLibName );
913 			        sal_uInt16 nPos = (sal_uInt16) pLibDlg->GetLibBox().GetModel()->GetAbsPos( pEntry );
914 			        pLibDlg->GetLibBox().CheckEntryPos( nPos, sal_True);
915                 }
916             }
917 
918 			if ( !pLibDlg )
919 				InfoBox( this, String( IDEResId( RID_STR_NOLIBINSTORAGE ) ) ).Execute();
920 			else
921 			{
922 				sal_Bool bChanges = sal_False;
923                 String aExtension( aURLObj.getExtension() );
924                 String aLibExtension( String::CreateFromAscii( "xlb" ) );
925                 String aContExtension( String::CreateFromAscii( "xlc" ) );
926 
927                 // disable reference checkbox for documents and sbls
928                 if ( aExtension != aLibExtension && aExtension != aContExtension )
929                     pLibDlg->EnableReference( sal_False );
930 
931 				if ( pLibDlg->Execute() )
932 				{
933 					sal_uLong nNewPos = aLibBox.GetEntryCount();
934                     sal_Bool bRemove = sal_False;
935                     sal_Bool bReplace = pLibDlg->IsReplace();
936 					sal_Bool bReference = pLibDlg->IsReference();
937 					for ( sal_uInt16 nLib = 0; nLib < pLibDlg->GetLibBox().GetEntryCount(); nLib++ )
938 					{
939 						if ( pLibDlg->GetLibBox().IsChecked( nLib ) )
940 						{
941 							SvLBoxEntry* pEntry = pLibDlg->GetLibBox().GetEntry( nLib );
942 							DBG_ASSERT( pEntry, "Entry?!" );
943 							String aLibName( pLibDlg->GetLibBox().GetEntryText( pEntry, 0 ) );
944                             ::rtl::OUString aOULibName( aLibName );
945                             Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
946                             Reference< script::XLibraryContainer2 > xDlgLibContainer( m_aCurDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
947 
948                             // check, if the library is already existing
949                             if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) ) ||
950                                  ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) ) )
951                             {
952                                 if ( bReplace )
953                                 {
954                                     // check, if the library is the Standard library
955 								    if ( aLibName.EqualsAscii( "Standard" ) )
956                                     {
957 									    ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_REPLACESTDLIB ) ) ).Execute();
958 									    continue;
959 								    }
960 
961                                     // check, if the library is readonly and not a link
962                                     if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) && !xModLibContainer->isLibraryLink( aOULibName ) ) ||
963                                          ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) && !xDlgLibContainer->isLibraryLink( aOULibName ) ) )
964                                     {
965                                         String aErrStr( IDEResId( RID_STR_REPLACELIB ) );
966 									    aErrStr.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "XX" ) ), aLibName );
967 									    aErrStr += '\n';
968 									    aErrStr += String( IDEResId( RID_STR_LIBISREADONLY ) );
969 									    ErrorBox( this, WB_OK | WB_DEF_OK, aErrStr ).Execute();
970                                         continue;
971                                     }
972 
973                                     // remove existing libraries
974                                     bRemove = sal_True;
975                                 }
976 								else
977                                 {
978                                     String aErrStr;
979                                     if ( bReference )
980                                         aErrStr = String( IDEResId( RID_STR_REFNOTPOSSIBLE ) );
981                                     else
982                                         aErrStr = String( IDEResId( RID_STR_IMPORTNOTPOSSIBLE ) );
983 									aErrStr.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "XX" ) ), aLibName );
984 									aErrStr += '\n';
985 									aErrStr += String( IDEResId( RID_STR_SBXNAMEALLREADYUSED ) );
986 									ErrorBox( this, WB_OK | WB_DEF_OK, aErrStr ).Execute();
987 									continue;
988                                 }
989                             }
990 
991                             // check, if the library is password protected
992                             sal_Bool bOK = sal_False;
993                             String aPassword;
994                             if ( xModLibContImport.is() && xModLibContImport->hasByName( aOULibName ) )
995                             {
996                                 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContImport, UNO_QUERY );
997                                 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) && !bReference )
998                                 {
999 				                    bOK = QueryPassword( xModLibContImp, aLibName, aPassword, sal_True, sal_True );
1000 
1001                                     if ( !bOK )
1002                                     {
1003                                         String aErrStr( IDEResId( RID_STR_NOIMPORT ) );
1004 									    aErrStr.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "XX" ) ), aLibName );
1005                                         ErrorBox( this, WB_OK | WB_DEF_OK, aErrStr ).Execute();
1006                                         continue;
1007                                     }
1008                                 }
1009                             }
1010 
1011                             // remove existing libraries
1012                             if ( bRemove )
1013                             {
1014                                 // remove listbox entry
1015                                 SvLBoxEntry* pEntry_ = aLibBox.FindEntry( aLibName );
1016 								if ( pEntry_ )
1017 									aLibBox.SvTreeListBox::GetModel()->Remove( pEntry_ );
1018 
1019                                 // remove module library
1020                                 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
1021                                     xModLibContainer->removeLibrary( aOULibName );
1022 
1023                                 // remove dialog library
1024                                 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) )
1025                                     xDlgLibContainer->removeLibrary( aOULibName );
1026                             }
1027 
1028                             // copy module library
1029                             if ( xModLibContImport.is() && xModLibContImport->hasByName( aOULibName ) && xModLibContainer.is() && !xModLibContainer->hasByName( aOULibName ) )
1030 	                        {
1031                                 Reference< container::XNameContainer > xModLib;
1032                                 if ( bReference )
1033                                 {
1034                                     // storage URL
1035                                     INetURLObject aModStorageURLObj( aModURLObj );
1036                                     if ( aExtension == aContExtension )
1037                                     {
1038                                         sal_Int32 nCount = aModStorageURLObj.getSegmentCount();
1039                                         aModStorageURLObj.insertName( aLibName, false, nCount-1 );
1040                                         aModStorageURLObj.setExtension( aLibExtension );
1041                                         aModStorageURLObj.setFinalSlash();
1042                                     }
1043                                     ::rtl::OUString aModStorageURL( aModStorageURLObj.GetMainURL( INetURLObject::NO_DECODE ) );
1044 
1045                                     // create library link
1046                                     xModLib = Reference< container::XNameContainer >( xModLibContainer->createLibraryLink( aOULibName, aModStorageURL, sal_True ), UNO_QUERY);
1047                                 }
1048                                 else
1049                                 {
1050                                     // create library
1051                                     xModLib = xModLibContainer->createLibrary( aOULibName );
1052                                     if ( xModLib.is() )
1053                                     {
1054                                         // get import library
1055         	                            Reference< container::XNameContainer > xModLibImport;
1056                                         Any aElement = xModLibContImport->getByName( aOULibName );
1057 		                                aElement >>= xModLibImport;
1058 
1059                                         if ( xModLibImport.is() )
1060                                         {
1061                                             // load library
1062     	                                    if ( !xModLibContImport->isLibraryLoaded( aOULibName ) )
1063 	    	                                    xModLibContImport->loadLibrary( aOULibName );
1064 
1065                                             // copy all modules
1066                                             Sequence< ::rtl::OUString > aModNames = xModLibImport->getElementNames();
1067 		                                    sal_Int32 nModCount = aModNames.getLength();
1068 		                                    const ::rtl::OUString* pModNames = aModNames.getConstArray();
1069                                             for ( sal_Int32 i = 0 ; i < nModCount ; i++ )
1070                                             {
1071                                                 ::rtl::OUString aOUModName( pModNames[ i ] );
1072                                                 Any aElement_ = xModLibImport->getByName( aOUModName );
1073                                                 xModLib->insertByName( aOUModName, aElement_ );
1074                                             }
1075 
1076                                             // set password
1077                                             if ( bOK )
1078                                             {
1079                                                 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
1080                                                 if ( xPasswd.is() )
1081                                                 {
1082                                                     try
1083                                                     {
1084                                                         ::rtl::OUString aOUPassword( aPassword );
1085                                                         xPasswd->changeLibraryPassword( aOULibName, ::rtl::OUString(), aOUPassword );
1086                                                     }
1087                                                     catch (...)
1088                                                     {
1089                                                     }
1090                                                 }
1091                                             }
1092                                         }
1093                                     }
1094                                 }
1095                             }
1096 
1097                             // copy dialog library
1098                             if ( xDlgLibContImport.is() && xDlgLibContImport->hasByName( aOULibName ) && xDlgLibContainer.is() && !xDlgLibContainer->hasByName( aOULibName ) )
1099 	                        {
1100                                 Reference< container::XNameContainer > xDlgLib;
1101                                 if ( bReference )
1102                                 {
1103                                     // storage URL
1104                                     INetURLObject aDlgStorageURLObj( aDlgURLObj );
1105                                     if ( aExtension == aContExtension )
1106                                     {
1107                                         sal_Int32 nCount = aDlgStorageURLObj.getSegmentCount();
1108                                         aDlgStorageURLObj.insertName( aLibName, false, nCount - 1 );
1109                                         aDlgStorageURLObj.setExtension( aLibExtension );
1110                                         aDlgStorageURLObj.setFinalSlash();
1111                                     }
1112                                     ::rtl::OUString aDlgStorageURL( aDlgStorageURLObj.GetMainURL( INetURLObject::NO_DECODE ) );
1113 
1114                                     // create library link
1115                                     xDlgLib = Reference< container::XNameContainer >( xDlgLibContainer->createLibraryLink( aOULibName, aDlgStorageURL, sal_True ), UNO_QUERY);
1116                                 }
1117                                 else
1118                                 {
1119                                     // create library
1120                                     xDlgLib = xDlgLibContainer->createLibrary( aOULibName );
1121                                     if ( xDlgLib.is() )
1122                                     {
1123                                         // get import library
1124         	                            Reference< container::XNameContainer > xDlgLibImport;
1125                                         Any aElement = xDlgLibContImport->getByName( aOULibName );
1126 		                                aElement >>= xDlgLibImport;
1127 
1128                                         if ( xDlgLibImport.is() )
1129                                         {
1130                                             // load library
1131     	                                    if ( !xDlgLibContImport->isLibraryLoaded( aOULibName ) )
1132 	    	                                    xDlgLibContImport->loadLibrary( aOULibName );
1133 
1134                                             // copy all dialogs
1135                                             Sequence< ::rtl::OUString > aDlgNames = xDlgLibImport->getElementNames();
1136 		                                    sal_Int32 nDlgCount = aDlgNames.getLength();
1137 		                                    const ::rtl::OUString* pDlgNames = aDlgNames.getConstArray();
1138                                             for ( sal_Int32 i = 0 ; i < nDlgCount ; i++ )
1139                                             {
1140                                                 ::rtl::OUString aOUDlgName( pDlgNames[ i ] );
1141                                                 Any aElement_ = xDlgLibImport->getByName( aOUDlgName );
1142                                                 xDlgLib->insertByName( aOUDlgName, aElement_ );
1143                                             }
1144                                         }
1145                                     }
1146                                 }
1147                             }
1148 
1149                             // insert listbox entry
1150                             ImpInsertLibEntry( aLibName, aLibBox.GetEntryCount() );
1151                             bChanges = sal_True;
1152                         }
1153                     }
1154 
1155                     SvLBoxEntry* pFirstNew = aLibBox.GetEntry( nNewPos );
1156 					if ( pFirstNew )
1157 						aLibBox.SetCurEntry( pFirstNew );
1158                 }
1159 
1160                 delete pLibDlg;
1161 				if ( bChanges )
1162 					BasicIDE::MarkDocumentModified( m_aCurDocument );
1163             }
1164         }
1165 	}
1166 }
1167 
1168 //----------------------------------------------------------------------------
1169 
1170 void LibPage::Export( void )
1171 {
1172 	SvLBoxEntry* pCurEntry = aLibBox.GetCurEntry();
1173 	String aLibName( aLibBox.GetEntryText( pCurEntry, 0 ) );
1174 
1175 	// Password verification
1176 	::rtl::OUString aOULibName( aLibName );
1177 	Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
1178 
1179 	if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
1180 	{
1181 		sal_Bool bOK = sal_True;
1182 
1183 		// check password
1184 		Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
1185 		if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
1186 		{
1187 			String aPassword;
1188 			Reference< script::XLibraryContainer > xModLibContainer1( xModLibContainer, UNO_QUERY );
1189 			bOK = QueryPassword( xModLibContainer1, aLibName, aPassword );
1190 		}
1191 		if ( !bOK )
1192 			return;
1193 	}
1194 
1195 
1196 	Window* pWin = static_cast<Window*>( this );
1197     std::auto_ptr< ExportDialog > xNewDlg( new ExportDialog( pWin ) );
1198 
1199     if ( xNewDlg->Execute() == RET_OK )
1200 	{
1201 		try
1202 		{
1203 			if( xNewDlg->isExportAsPackage() )
1204 				ExportAsPackage( aLibName );
1205 			else
1206 				ExportAsBasic( aLibName );
1207 		}
1208 		catch( util::VetoException& ) // user cancled operation
1209 		{
1210 		}
1211 	}
1212 }
1213 
1214 void LibPage::implExportLib( const String& aLibName, const String& aTargetURL,
1215 	const Reference< task::XInteractionHandler >& Handler )
1216 {
1217 	::rtl::OUString aOULibName( aLibName );
1218 	Reference< script::XLibraryContainerExport > xModLibContainerExport
1219 		( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
1220 	Reference< script::XLibraryContainerExport > xDlgLibContainerExport
1221 		( m_aCurDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
1222 	if ( xModLibContainerExport.is() )
1223 		xModLibContainerExport->exportLibrary( aOULibName, aTargetURL, Handler );
1224 
1225 	if ( xDlgLibContainerExport.is() )
1226 		xDlgLibContainerExport->exportLibrary( aOULibName, aTargetURL, Handler );
1227 }
1228 
1229 
1230 //===========================================================================
1231 // Implementation XCommandEnvironment
1232 
1233 typedef cppu::WeakImplHelper1< XCommandEnvironment > LibCommandEnvironmentHelper;
1234 
1235 class OLibCommandEnvironment : public LibCommandEnvironmentHelper
1236 {
1237 	Reference< task::XInteractionHandler > mxInteraction;
1238 
1239 public:
1240 	OLibCommandEnvironment( Reference< task::XInteractionHandler > xInteraction )
1241 		: mxInteraction( xInteraction )
1242 	{}
1243 
1244     // Methods
1245     virtual Reference< task::XInteractionHandler > SAL_CALL getInteractionHandler()
1246 		throw(RuntimeException);
1247     virtual Reference< XProgressHandler > SAL_CALL getProgressHandler()
1248 		throw(RuntimeException);
1249 };
1250 
1251 Reference< task::XInteractionHandler > OLibCommandEnvironment::getInteractionHandler()
1252 	throw(RuntimeException)
1253 {
1254 	return mxInteraction;
1255 }
1256 
1257 Reference< XProgressHandler > OLibCommandEnvironment::getProgressHandler()
1258 	throw(RuntimeException)
1259 {
1260 	Reference< XProgressHandler > xRet;
1261 	return xRet;
1262 }
1263 
1264 
1265 
1266 void LibPage::ExportAsPackage( const String& aLibName )
1267 {
1268     // file open dialog
1269     Reference< lang::XMultiServiceFactory > xMSF( ::comphelper::getProcessServiceFactory() );
1270 	Reference< task::XInteractionHandler > xHandler;
1271 	Reference< XSimpleFileAccess > xSFA;
1272     Reference < XFilePicker > xFP;
1273     if( xMSF.is() )
1274     {
1275 		xHandler = Reference< task::XInteractionHandler >( xMSF->createInstance
1276 			( DEFINE_CONST_UNICODE("com.sun.star.task.InteractionHandler") ), UNO_QUERY );
1277 
1278 		xSFA = Reference< XSimpleFileAccess > ( xMSF->createInstance(
1279                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.SimpleFileAccess" ) ) ), UNO_QUERY );
1280 		if( !xSFA.is() )
1281 		{
1282 			DBG_ERROR( "No simpleFileAccess" );
1283 			return;
1284 		}
1285 
1286 		Sequence <Any> aServiceType(1);
1287 		aServiceType[0] <<= TemplateDescription::FILESAVE_SIMPLE;
1288         xFP = Reference< XFilePicker >( xMSF->createInstanceWithArguments(
1289 					::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FilePicker" ) ), aServiceType ), UNO_QUERY );
1290     }
1291 	xFP->setTitle( String( IDEResId( RID_STR_EXPORTPACKAGE ) ) );
1292 
1293     // filter
1294     ::rtl::OUString aTitle = String( IDEResId( RID_STR_PACKAGE_BUNDLE ) );
1295     ::rtl::OUString aFilter;
1296     aFilter = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "*.oxt" ) );       // library files
1297     Reference< XFilterManager > xFltMgr(xFP, UNO_QUERY);
1298     xFltMgr->appendFilter( aTitle, aFilter );
1299 
1300     // set display directory and filter
1301 	String aPath( IDE_DLL()->GetExtraData()->GetAddLibPath() );
1302 	if ( aPath.Len() )
1303 	{
1304 		xFP->setDisplayDirectory( aPath );
1305 	}
1306 	else
1307 	{
1308         // macro path from configuration management
1309         xFP->setDisplayDirectory( SvtPathOptions().GetWorkPath() );
1310 	}
1311 	xFltMgr->setCurrentFilter( aTitle );
1312 
1313     if ( xFP->execute() == RET_OK )
1314 	{
1315         IDE_DLL()->GetExtraData()->SetAddLibPath( xFP->getDisplayDirectory() );
1316 
1317 		Sequence< ::rtl::OUString > aFiles = xFP->getFiles();
1318 		INetURLObject aURL( aFiles[0] );
1319 		if( !aURL.getExtension().getLength() )
1320 			aURL.setExtension( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "oxt" ) ) );
1321 
1322 		::rtl::OUString aPackageURL( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
1323 
1324 		String aTmpPath = SvtPathOptions().GetTempPath();
1325 		INetURLObject aInetObj( aTmpPath );
1326 		aInetObj.insertName( aLibName, sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1327 		OUString aSourcePath = aInetObj.GetMainURL( INetURLObject::NO_DECODE );
1328         if( xSFA->exists( aSourcePath ) )
1329 			xSFA->kill( aSourcePath );
1330 		Reference< task::XInteractionHandler > xDummyHandler( new DummyInteractionHandler( xHandler ) );
1331 		implExportLib( aLibName, aTmpPath, xDummyHandler );
1332 
1333 		Reference< XCommandEnvironment > xCmdEnv =
1334 			static_cast<XCommandEnvironment*>( new OLibCommandEnvironment( xHandler ) );
1335 
1336 	    ::ucbhelper::Content sourceContent( aSourcePath, xCmdEnv );
1337 
1338 		::rtl::OUStringBuffer buf;
1339 		buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.zip://") );
1340 		buf.append( ::rtl::Uri::encode( aPackageURL,
1341 										rtl_UriCharClassRegName,
1342 										rtl_UriEncodeIgnoreEscapes,
1343 										RTL_TEXTENCODING_UTF8 ) );
1344 		buf.append( static_cast<sal_Unicode>('/') );
1345 		OUString destFolder( buf.makeStringAndClear() );
1346 
1347         if( xSFA->exists( aPackageURL ) )
1348 			xSFA->kill( aPackageURL );
1349 
1350 		::ucbhelper::Content destFolderContent( destFolder, xCmdEnv );
1351 		destFolderContent.transferContent(
1352 			sourceContent, ::ucbhelper::InsertOperation_COPY,
1353 			OUString(), NameClash::OVERWRITE );
1354 
1355 		INetURLObject aMetaInfInetObj( aTmpPath );
1356 		aMetaInfInetObj.insertName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "META-INF" ) ),
1357 			sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1358 		OUString aMetaInfFolder = aMetaInfInetObj.GetMainURL( INetURLObject::NO_DECODE );
1359         if( xSFA->exists( aMetaInfFolder ) )
1360 			xSFA->kill( aMetaInfFolder );
1361         xSFA->createFolder( aMetaInfFolder );
1362 
1363         ::std::vector< Sequence<beans::PropertyValue> > manifest;
1364         const OUString strMediaType = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) );
1365         const OUString strFullPath = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FullPath" ) );
1366         const OUString strBasicMediaType = ::rtl::OUString
1367 			( RTL_CONSTASCII_USTRINGPARAM( "application/vnd.sun.star.basic-library" ) );
1368 
1369         Sequence<beans::PropertyValue> attribs( 2 );
1370         beans::PropertyValue * pattribs = attribs.getArray();
1371 		pattribs[ 0 ].Name = strFullPath;
1372 		OUString fullPath = aLibName;
1373 		fullPath += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/") );
1374         pattribs[ 0 ].Value <<= fullPath;
1375 		pattribs[ 1 ].Name = strMediaType;
1376         pattribs[ 1 ].Value <<= strBasicMediaType;
1377         manifest.push_back( attribs );
1378 
1379         // write into pipe:
1380         Reference<packages::manifest::XManifestWriter> xManifestWriter( xMSF->createInstance
1381 			( DEFINE_CONST_UNICODE("com.sun.star.packages.manifest.ManifestWriter") ), UNO_QUERY );
1382         Reference<io::XOutputStream> xPipe( xMSF->createInstance
1383 			( DEFINE_CONST_UNICODE("com.sun.star.io.Pipe") ), UNO_QUERY );
1384         xManifestWriter->writeManifestSequence(
1385             xPipe, Sequence< Sequence<beans::PropertyValue> >(
1386                 &manifest[ 0 ], manifest.size() ) );
1387 
1388 		aMetaInfInetObj.insertName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "manifest.xml" ) ),
1389 			sal_True, INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::ENCODE_ALL );
1390 
1391         // write buffered pipe data to content:
1392         ::ucbhelper::Content manifestContent( aMetaInfInetObj.GetMainURL( INetURLObject::NO_DECODE ), xCmdEnv );
1393         manifestContent.writeStream( Reference<io::XInputStream>( xPipe, UNO_QUERY_THROW ), true );
1394 
1395 	    ::ucbhelper::Content MetaInfContent( aMetaInfFolder, xCmdEnv );
1396 		destFolderContent.transferContent(
1397 			MetaInfContent, ::ucbhelper::InsertOperation_COPY,
1398 			OUString(), NameClash::OVERWRITE );
1399 
1400         if( xSFA->exists( aSourcePath ) )
1401 			xSFA->kill( aSourcePath );
1402         if( xSFA->exists( aMetaInfFolder ) )
1403 			xSFA->kill( aMetaInfFolder );
1404 	}
1405 }
1406 
1407 void LibPage::ExportAsBasic( const String& aLibName )
1408 {
1409     // Folder picker
1410     Reference< lang::XMultiServiceFactory > xMSF( ::comphelper::getProcessServiceFactory() );
1411 	Reference< XFolderPicker > xFolderPicker;
1412 	Reference< task::XInteractionHandler > xHandler;
1413     if( xMSF.is() )
1414     {
1415         xFolderPicker = Reference< XFolderPicker >( xMSF->createInstance(
1416 					::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FolderPicker" ) ) ), UNO_QUERY );
1417 
1418 		xHandler = Reference< task::XInteractionHandler >( xMSF->createInstance
1419 			( DEFINE_CONST_UNICODE("com.sun.star.task.InteractionHandler") ), UNO_QUERY );
1420     }
1421 
1422 	if( xFolderPicker.is() )
1423 	{
1424 		xFolderPicker->setTitle( String( IDEResId( RID_STR_EXPORTBASIC ) ) );
1425 
1426 		// set display directory and filter
1427 		String aPath( IDE_DLL()->GetExtraData()->GetAddLibPath() );
1428 		if( !aPath.Len() )
1429 			aPath = SvtPathOptions().GetWorkPath();
1430 
1431 		// INetURLObject aURL(m_sSavePath, INET_PROT_FILE);
1432 		xFolderPicker->setDisplayDirectory( aPath );
1433 		short nRet = xFolderPicker->execute();
1434 		if( nRet == RET_OK )
1435 		{
1436 			String aTargetURL = xFolderPicker->getDirectory();
1437 	        IDE_DLL()->GetExtraData()->SetAddLibPath( aTargetURL );
1438 
1439 			Reference< task::XInteractionHandler > xDummyHandler( new DummyInteractionHandler( xHandler ) );
1440 			implExportLib( aLibName, aTargetURL, xDummyHandler );
1441 		}
1442 	}
1443 }
1444 
1445 //----------------------------------------------------------------------------
1446 
1447 void LibPage::DeleteCurrent()
1448 {
1449     SvLBoxEntry* pCurEntry = aLibBox.GetCurEntry();
1450     String aLibName( aLibBox.GetEntryText( pCurEntry, 0 ) );
1451 
1452     // check, if library is link
1453     sal_Bool bIsLibraryLink = sal_False;
1454     ::rtl::OUString aOULibName( aLibName );
1455     Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
1456     Reference< script::XLibraryContainer2 > xDlgLibContainer( m_aCurDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
1457     if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryLink( aOULibName ) ) ||
1458          ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryLink( aOULibName ) ) )
1459     {
1460         bIsLibraryLink = sal_True;
1461     }
1462 
1463     if ( QueryDelLib( aLibName, bIsLibraryLink, this ) )
1464 	{
1465 		// inform BasicIDE
1466         SfxUsrAnyItem aDocItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, makeAny( m_aCurDocument.getDocumentOrNull() ) );
1467         SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
1468         BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
1469         SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
1470 	    SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
1471         if( pDispatcher )
1472 		{
1473 			pDispatcher->Execute( SID_BASICIDE_LIBREMOVED,
1474 								  SFX_CALLMODE_SYNCHRON, &aDocItem, &aLibNameItem, 0L );
1475 		}
1476 
1477         // remove library from module and dialog library containers
1478         if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
1479             xModLibContainer->removeLibrary( aOULibName );
1480         if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) )
1481             xDlgLibContainer->removeLibrary( aOULibName );
1482 
1483 		((SvLBox&)aLibBox).GetModel()->Remove( pCurEntry );
1484         BasicIDE::MarkDocumentModified( m_aCurDocument );
1485     }
1486 }
1487 
1488 //----------------------------------------------------------------------------
1489 
1490 void LibPage::EndTabDialog( sal_uInt16 nRet )
1491 {
1492 	DBG_ASSERT( pTabDlg, "TabDlg nicht gesetzt!" );
1493 	if ( pTabDlg )
1494 		pTabDlg->EndDialog( nRet );
1495 }
1496 
1497 //----------------------------------------------------------------------------
1498 
1499 void LibPage::FillListBox()
1500 {
1501     InsertListBoxEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
1502     InsertListBoxEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
1503 
1504     ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
1505     for (   ScriptDocuments::const_iterator doc = aDocuments.begin();
1506             doc != aDocuments.end();
1507             ++doc
1508         )
1509     {
1510         InsertListBoxEntry( *doc, LIBRARY_LOCATION_DOCUMENT );
1511     }
1512 }
1513 
1514 //----------------------------------------------------------------------------
1515 
1516 void LibPage::InsertListBoxEntry( const ScriptDocument& rDocument, LibraryLocation eLocation )
1517 {
1518     String aEntryText( rDocument.getTitle( eLocation ) );
1519     sal_uInt16 nPos = aBasicsBox.InsertEntry( aEntryText, LISTBOX_APPEND );
1520     aBasicsBox.SetEntryData( nPos, new BasicDocumentEntry( rDocument, eLocation ) );
1521 }
1522 
1523 //----------------------------------------------------------------------------
1524 
1525 void LibPage::SetCurLib()
1526 {
1527     sal_uInt16 nSelPos = aBasicsBox.GetSelectEntryPos();
1528     BasicDocumentEntry* pEntry = (BasicDocumentEntry*)aBasicsBox.GetEntryData( nSelPos );
1529     if ( pEntry )
1530     {
1531         ScriptDocument aDocument( pEntry->GetDocument() );
1532         DBG_ASSERT( aDocument.isAlive(), "LibPage::SetCurLib: no document, or document is dead!" );
1533         if ( !aDocument.isAlive() )
1534             return;
1535         LibraryLocation eLocation = pEntry->GetLocation();
1536         if ( aDocument != m_aCurDocument || eLocation != m_eCurLocation )
1537         {
1538             m_aCurDocument = aDocument;
1539             m_eCurLocation = eLocation;
1540             aLibBox.SetDocument( aDocument );
1541             aLibBox.Clear();
1542 
1543             // get a sorted list of library names
1544             Sequence< ::rtl::OUString > aLibNames = aDocument.getLibraryNames();
1545             sal_Int32 nLibCount = aLibNames.getLength();
1546             const ::rtl::OUString* pLibNames = aLibNames.getConstArray();
1547 
1548             for ( sal_Int32 i = 0 ; i < nLibCount ; i++ )
1549             {
1550                 String aLibName( pLibNames[ i ] );
1551                 if ( eLocation == aDocument.getLibraryLocation( aLibName ) )
1552                     ImpInsertLibEntry( aLibName, i );
1553             }
1554 
1555             SvLBoxEntry* pEntry_ = aLibBox.FindEntry( String::CreateFromAscii( "Standard" ) );
1556             if ( !pEntry_ )
1557                 pEntry_ = aLibBox.GetEntry( 0 );
1558             aLibBox.SetCurEntry( pEntry_ );
1559         }
1560     }
1561 }
1562 
1563 //----------------------------------------------------------------------------
1564 
1565 SvLBoxEntry* LibPage::ImpInsertLibEntry( const String& rLibName, sal_uLong nPos )
1566 {
1567     // check, if library is password protected
1568     sal_Bool bProtected = sal_False;
1569     ::rtl::OUString aOULibName( rLibName );
1570     Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
1571 	if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
1572     {
1573         Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
1574         if ( xPasswd.is() )
1575         {
1576             bProtected = xPasswd->isLibraryPasswordProtected( aOULibName );
1577         }
1578     }
1579 
1580 	SvLBoxEntry* pNewEntry = aLibBox.DoInsertEntry( rLibName, nPos );
1581     pNewEntry->SetUserData( new BasicLibUserData( m_aCurDocument ) );
1582 
1583     if (bProtected)
1584     {
1585         Image aImage(IDEResId(RID_IMG_LOCKED));
1586         aLibBox.SetExpandedEntryBmp(pNewEntry, aImage, BMP_COLOR_NORMAL);
1587         aLibBox.SetCollapsedEntryBmp(pNewEntry, aImage, BMP_COLOR_NORMAL);
1588         aImage = Image(IDEResId(RID_IMG_LOCKED_HC));
1589         aLibBox.SetExpandedEntryBmp(pNewEntry, aImage,
1590                                     BMP_COLOR_HIGHCONTRAST);
1591         aLibBox.SetCollapsedEntryBmp(pNewEntry, aImage,
1592                                      BMP_COLOR_HIGHCONTRAST);
1593     }
1594 
1595     // check, if library is link
1596 	if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryLink( aOULibName ) )
1597     {
1598         String aLinkURL = xModLibContainer->getLibraryLinkURL( aOULibName );
1599         aLibBox.SetEntryText( aLinkURL, pNewEntry, 1 );
1600     }
1601 
1602 	return pNewEntry;
1603 }
1604 
1605 //----------------------------------------------------------------------------
1606 
1607 // Helper function
1608 void createLibImpl( Window* pWin, const ScriptDocument& rDocument,
1609 				    BasicCheckBox* pLibBox, BasicTreeListBox* pBasicBox )
1610 {
1611     OSL_ENSURE( rDocument.isAlive(), "createLibImpl: invalid document!" );
1612     if ( !rDocument.isAlive() )
1613         return;
1614 
1615     // create library name
1616     String aLibName;
1617 	String aLibStdName( String( RTL_CONSTASCII_USTRINGPARAM( "Library" ) ) );
1618 	//String aLibStdName( IDEResId( RID_STR_STDLIBNAME ) );
1619 	sal_Bool bValid = sal_False;
1620 	sal_uInt16 i = 1;
1621 	while ( !bValid )
1622 	{
1623 		aLibName = aLibStdName;
1624 		aLibName += String::CreateFromInt32( i );
1625         if ( !rDocument.hasLibrary( E_SCRIPTS, aLibName ) && !rDocument.hasLibrary( E_DIALOGS, aLibName ) )
1626             bValid = sal_True;
1627 		i++;
1628 	}
1629 
1630     std::auto_ptr< NewObjectDialog > xNewDlg( new NewObjectDialog( pWin, NEWOBJECTMODE_LIB ) );
1631 	xNewDlg->SetObjectName( aLibName );
1632 
1633     if ( xNewDlg->Execute() )
1634 	{
1635 		if ( xNewDlg->GetObjectName().Len() )
1636 			aLibName = xNewDlg->GetObjectName();
1637 
1638 		if ( aLibName.Len() > 30 )
1639 		{
1640 			ErrorBox( pWin, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_LIBNAMETOLONG ) ) ).Execute();
1641 		}
1642 		else if ( !BasicIDE::IsValidSbxName( aLibName ) )
1643 		{
1644 			ErrorBox( pWin, WB_OK | WB_DEF_OK,
1645 						String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
1646 		}
1647         else if ( rDocument.hasLibrary( E_SCRIPTS, aLibName ) || rDocument.hasLibrary( E_DIALOGS, aLibName ) )
1648 		{
1649 			ErrorBox( pWin, WB_OK | WB_DEF_OK,
1650 						String( IDEResId( RID_STR_SBXNAMEALLREADYUSED2 ) ) ).Execute();
1651 		}
1652 		else
1653 		{
1654             try
1655             {
1656                 // create module and dialog library
1657                 Reference< container::XNameContainer > xModLib( rDocument.getOrCreateLibrary( E_SCRIPTS, aLibName ) );
1658                 Reference< container::XNameContainer > xDlgLib( rDocument.getOrCreateLibrary( E_DIALOGS, aLibName ) );
1659 
1660 				if( pLibBox )
1661 				{
1662 					SvLBoxEntry* pEntry = pLibBox->DoInsertEntry( aLibName );
1663 					pEntry->SetUserData( new BasicLibUserData( rDocument ) );
1664 					pLibBox->SetCurEntry( pEntry );
1665 				}
1666 
1667                 // create a module
1668 				String aModName = rDocument.createObjectName( E_SCRIPTS, aLibName );
1669                 ::rtl::OUString sModuleCode;
1670                 if ( !rDocument.createModule( aLibName, aModName, sal_True, sModuleCode ) )
1671                     throw Exception();
1672 
1673 				SbxItem aSbxItem( SID_BASICIDE_ARG_SBX, rDocument, aLibName, aModName, BASICIDE_TYPE_MODULE );
1674                 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
1675                 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
1676 	            SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
1677                 if( pDispatcher )
1678 				{
1679 					pDispatcher->Execute( SID_BASICIDE_SBXINSERTED,
1680 										  SFX_CALLMODE_SYNCHRON, &aSbxItem, 0L );
1681 				}
1682 
1683 				if( pBasicBox )
1684 				{
1685 					SvLBoxEntry* pEntry = pBasicBox->GetCurEntry();
1686 					SvLBoxEntry* pRootEntry = NULL;
1687 					while( pEntry )
1688 					{
1689 						pRootEntry = pEntry;
1690 						pEntry = pBasicBox->GetParent( pEntry );
1691 					}
1692 
1693                     sal_uInt16 nMode = pBasicBox->GetMode();
1694                     bool bDlgMode = ( nMode & BROWSEMODE_DIALOGS ) && !( nMode & BROWSEMODE_MODULES );
1695                     sal_uInt16 nId = bDlgMode ? RID_IMG_DLGLIB : RID_IMG_MODLIB;
1696                     sal_uInt16 nIdHC = bDlgMode ? RID_IMG_DLGLIB_HC : RID_IMG_MODLIB_HC;
1697 					SvLBoxEntry* pNewLibEntry = pBasicBox->AddEntry(
1698 						aLibName,
1699                         Image( IDEResId( nId ) ),
1700                         Image( IDEResId( nIdHC ) ),
1701                         pRootEntry, false,
1702 						std::auto_ptr< BasicEntry >( new BasicEntry( OBJ_TYPE_LIBRARY ) ) );
1703 					DBG_ASSERT( pNewLibEntry, "InsertEntry fehlgeschlagen!" );
1704 
1705 					if( pNewLibEntry )
1706 					{
1707 						SvLBoxEntry* pEntry_ = pBasicBox->AddEntry(
1708 							aModName,
1709                             Image( IDEResId( RID_IMG_MODULE ) ),
1710                             Image( IDEResId( RID_IMG_MODULE_HC ) ),
1711                             pNewLibEntry, false,
1712 							std::auto_ptr< BasicEntry >( new BasicEntry( OBJ_TYPE_MODULE ) ) );
1713 						DBG_ASSERT( pEntry_, "InsertEntry fehlgeschlagen!" );
1714 						pBasicBox->SetCurEntry( pEntry_ );
1715 						pBasicBox->Select( pBasicBox->GetCurEntry() );		// OV-Bug?!
1716 					}
1717 				}
1718 			}
1719 			catch ( uno::Exception& )
1720 			{
1721 				DBG_UNHANDLED_EXCEPTION();
1722 			}
1723         }
1724 	}
1725 }
1726 
1727 //----------------------------------------------------------------------------
1728 
1729