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