xref: /aoo42x/main/cui/source/options/dbregister.cxx (revision cdf0e10c)
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_cui.hxx"
30 
31 #include "dbregister.hxx"
32 #include "dbregister.hrc"
33 #include "dbregistersettings.hxx"
34 #include "connpooloptions.hxx"
35 #include <svl/filenotation.hxx>
36 #include <cuires.hrc>
37 #include "helpid.hrc"
38 #include <svtools/editbrowsebox.hxx>
39 #include <cuires.hrc>
40 #include <vcl/field.hxx>
41 #include <svl/eitem.hxx>
42 #include <comphelper/processfactory.hxx>
43 #include <com/sun/star/uno/Exception.hpp>
44 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
45 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
46 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
47 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
48 #include <vcl/msgbox.hxx>
49 #include <svtools/svtabbx.hxx>
50 #include <svl/itemset.hxx>
51 #include "doclinkdialog.hxx"
52 #include <tools/urlobj.hxx>
53 #include <unotools/localfilehelper.hxx>
54 #include "optHeaderTabListbox.hxx"
55 #include <sfx2/docfilt.hxx>
56 #include <dialmgr.hxx>
57 #include "dbregisterednamesconfig.hxx"
58 #include <svx/dialogs.hrc>
59 
60 #define TAB_WIDTH1		80
61 #define TAB_WIDTH_MIN   10
62 #define TAB_WIDTH2		1000
63 #define ITEMID_TYPE       1
64 #define ITEMID_PATH       2
65 //........................................................................
66 namespace svx
67 {
68 //........................................................................
69 
70 using namespace ::com::sun::star::lang;
71 using namespace ::com::sun::star::ui::dialogs;
72 using namespace ::com::sun::star::uno;
73 using namespace ::svt;
74 
75 // class RegistrationItemSetHolder  -------------------------------------------------
76 
77 RegistrationItemSetHolder::RegistrationItemSetHolder( const SfxItemSet& _rMasterSet )
78     :m_aRegistrationItems( _rMasterSet )
79 {
80 	DbRegisteredNamesConfig::GetOptions( m_aRegistrationItems );
81 }
82 
83 RegistrationItemSetHolder::~RegistrationItemSetHolder()
84 {
85 }
86 
87 // class DatabaseRegistrationDialog  ------------------------------------------------
88 
89 DatabaseRegistrationDialog::DatabaseRegistrationDialog( Window* pParent, const SfxItemSet& rInAttrs )
90     :RegistrationItemSetHolder( rInAttrs )
91     ,SfxSingleTabDialog( pParent, getRegistrationItems(), RID_SFXPAGE_DBREGISTER )
92 {
93     SfxTabPage* page = DbRegistrationOptionsPage::Create( this, getRegistrationItems() );
94 
95 	SetTabPage( page );
96 	SetText( page->GetText() );
97 }
98 
99 DatabaseRegistrationDialog::~DatabaseRegistrationDialog()
100 {
101 }
102 
103 short DatabaseRegistrationDialog::Execute()
104 {
105     short result = SfxSingleTabDialog::Execute();
106     if ( result == RET_OK )
107     {
108         DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
109         if ( GetOutputItemSet() )
110 	        DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
111     }
112     return result;
113 }
114 
115 // class DbRegistrationOptionsPage --------------------------------------------------
116 
117 DbRegistrationOptionsPage::DbRegistrationOptionsPage( Window* pParent, const SfxItemSet& rSet ) :
118 
119 	SfxTabPage( pParent, CUI_RES( RID_SFXPAGE_DBREGISTER), rSet ),
120 
121 	aStdBox			( this, CUI_RES( GB_STD ) ),
122 	aTypeText		( this, CUI_RES( FT_TYPE ) ),
123 	aPathText		( this, CUI_RES( FT_PATH ) ),
124 	aPathCtrl		( this, CUI_RES( LB_PATH ) ),
125 	m_aNew			( this, CUI_RES( BTN_NEW ) ),
126 	m_aEdit			( this, CUI_RES( BTN_EDIT ) ),
127 	m_aDelete		( this, CUI_RES( BTN_DELETE ) ),
128 	pHeaderBar		( NULL ),
129 	pPathBox		( NULL ),
130 	m_pCurEntry		( NULL ),
131 	m_nOldCount		( 0 ),
132 	m_bModified		( sal_False )
133 {
134 	m_aNew.SetClickHdl( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
135 	m_aEdit.SetClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
136 	m_aDelete.SetClickHdl( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );
137 
138 	Size aBoxSize = aPathCtrl.GetOutputSizePixel();
139 
140 
141 	WinBits nBits = WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP;
142 	pPathBox = new ::svx::OptHeaderTabListBox( &aPathCtrl, nBits );
143 
144     pHeaderBar = new HeaderBar( &aPathCtrl, WB_BUTTONSTYLE | WB_BOTTOMBORDER );
145 	pHeaderBar->SetPosSizePixel( Point( 0, 0 ), Size( aBoxSize.Width(), 16 ) );
146 	pHeaderBar->SetSelectHdl( LINK( this, DbRegistrationOptionsPage, HeaderSelect_Impl ) );
147 	pHeaderBar->SetEndDragHdl( LINK( this, DbRegistrationOptionsPage, HeaderEndDrag_Impl ) );
148 	Size aSz;
149 	aSz.Width() = TAB_WIDTH1;
150 	pHeaderBar->InsertItem( ITEMID_TYPE, aTypeText.GetText(),
151 							LogicToPixel( aSz, MapMode( MAP_APPFONT ) ).Width(),
152 							HIB_LEFT | HIB_VCENTER | HIB_CLICKABLE | HIB_UPARROW );
153 	aSz.Width() = TAB_WIDTH2;
154 	pHeaderBar->InsertItem( ITEMID_PATH, aPathText.GetText(),
155 							LogicToPixel( aSz, MapMode( MAP_APPFONT ) ).Width(),
156 							HIB_LEFT | HIB_VCENTER );
157 
158 	static long nTabs[] = {3, 0, TAB_WIDTH1, TAB_WIDTH1 + TAB_WIDTH2 };
159 	Size aHeadSize = pHeaderBar->GetSizePixel();
160 
161 	aPathCtrl.SetFocusControl( pPathBox );
162 	pPathBox->SetStyle( pPathBox->GetStyle()|nBits );
163 	pPathBox->SetDoubleClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
164 	pPathBox->SetSelectHdl( LINK( this, DbRegistrationOptionsPage, PathSelect_Impl ) );
165 	pPathBox->SetSelectionMode( SINGLE_SELECTION );
166 	pPathBox->SetPosSizePixel( Point( 0, aHeadSize.Height() ),
167 							   Size( aBoxSize.Width(), aBoxSize.Height() - aHeadSize.Height() ) );
168 	pPathBox->SetTabs( &nTabs[0], MAP_APPFONT );
169 	pPathBox->InitHeaderBar( pHeaderBar );
170 	pPathBox->SetHighlightRange();
171 
172 	pPathBox->SetHelpId( HID_DBPATH_CTL_PATH );
173 	pHeaderBar->SetHelpId( HID_DBPATH_HEADERBAR );
174 
175 	pPathBox->Show();
176 	pHeaderBar->Show();
177 
178 	FreeResource();
179 }
180 
181 // -----------------------------------------------------------------------
182 
183 DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
184 {
185     // #110603# do not grab focus to a destroyed window !!!
186     aPathCtrl.SetFocusControl( NULL );
187 
188 	pHeaderBar->Hide();
189 	for ( sal_uInt16 i = 0; i < pPathBox->GetEntryCount(); ++i )
190 		delete static_cast< DatabaseRegistration* >( pPathBox->GetEntry(i)->GetUserData() );
191 	delete pPathBox;
192 	delete pHeaderBar;
193 }
194 
195 // -----------------------------------------------------------------------
196 
197 SfxTabPage*	DbRegistrationOptionsPage::Create( Window* pParent,
198 									const SfxItemSet& rAttrSet )
199 {
200 	return ( new DbRegistrationOptionsPage( pParent, rAttrSet ) );
201 }
202 
203 // -----------------------------------------------------------------------
204 
205 sal_Bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet& rCoreSet )
206 {
207 	// the settings for the single drivers
208 	sal_Bool bModified = sal_False;
209 	DatabaseRegistrations aRegistrations;
210 	sal_uLong nCount = pPathBox->GetEntryCount();
211 	for ( sal_uLong i = 0; i < nCount; ++i )
212 	{
213 		SvLBoxEntry* pEntry = pPathBox->GetEntry(i);
214 		DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
215         if ( pRegistration && pRegistration->sLocation.getLength() )
216 		{
217             ::rtl::OUString sName( pPathBox->GetEntryText( pEntry, 0 ) );
218             OFileNotation aTransformer( pRegistration->sLocation );
219             aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
220 		}
221 	}
222 	if ( m_nOldCount != aRegistrations.size() || m_bModified )
223 	{
224 		rCoreSet.Put(DatabaseMapItem( SID_SB_DB_REGISTER, aRegistrations ), SID_SB_DB_REGISTER);
225 		bModified = sal_True;
226 	}
227 
228 	return bModified;
229 }
230 
231 // -----------------------------------------------------------------------
232 
233 void DbRegistrationOptionsPage::Reset( const SfxItemSet& rSet )
234 {
235 	// the settings for the single drivers
236 	SFX_ITEMSET_GET( rSet, pRegistrations, DatabaseMapItem, SID_SB_DB_REGISTER, sal_True );
237     if ( !pRegistrations )
238         return;
239 
240 	pPathBox->Clear();
241 
242 	const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
243 	m_nOldCount = rRegistrations.size();
244 	DatabaseRegistrations::const_iterator aIter = rRegistrations.begin();
245 	DatabaseRegistrations::const_iterator aEnd = rRegistrations.end();
246 	for ( ; aIter != aEnd; ++aIter )
247 	{
248 		OFileNotation aTransformer( aIter->second.sLocation );
249 		insertNewEntry( aIter->first, aTransformer.get( OFileNotation::N_SYSTEM ), aIter->second.bReadOnly );
250 	}
251 
252 	String aUserData = GetUserData();
253 	if ( aUserData.Len() )
254 	{
255 		// Spaltenbreite restaurieren
256 		pHeaderBar->SetItemSize( ITEMID_TYPE, aUserData.GetToken(0).ToInt32() );
257 		HeaderEndDrag_Impl( NULL );
258 		// Sortierrichtung restaurieren
259 		sal_Bool bUp = (sal_Bool)(sal_uInt16)aUserData.GetToken(1).ToInt32();
260 		HeaderBarItemBits nBits	= pHeaderBar->GetItemBits(ITEMID_TYPE);
261 
262 		if ( bUp )
263 		{
264 			nBits &= ~HIB_UPARROW;
265 			nBits |= HIB_DOWNARROW;
266 		}
267 		else
268 		{
269 			nBits &= ~HIB_DOWNARROW;
270 			nBits |= HIB_UPARROW;
271 		}
272 		pHeaderBar->SetItemBits( ITEMID_TYPE, nBits );
273 		HeaderSelect_Impl( NULL );
274 	}
275 }
276 
277 // -----------------------------------------------------------------------
278 
279 void DbRegistrationOptionsPage::FillUserData()
280 {
281 	String aUserData = String::CreateFromInt32( pHeaderBar->GetItemSize( ITEMID_TYPE ) );
282 	aUserData += ';';
283 	HeaderBarItemBits nBits	= pHeaderBar->GetItemBits( ITEMID_TYPE );
284 	sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
285 	aUserData += bUp ? '1' : '0';
286 	SetUserData( aUserData );
287 }
288 // -----------------------------------------------------------------------
289 
290 IMPL_LINK( DbRegistrationOptionsPage, DeleteHdl, void *, EMPTYARG )
291 {
292 	SvLBoxEntry* pEntry = pPathBox->FirstSelected();
293 	if ( pEntry )
294 	{
295 		QueryBox aQuery(this,CUI_RES(QUERY_DELETE_CONFIRM));
296 		if ( aQuery.Execute() == RET_YES )
297 			pPathBox->GetModel()->Remove(pEntry);
298 	}
299 	return 0;
300 }
301 // -----------------------------------------------------------------------
302 IMPL_LINK( DbRegistrationOptionsPage, NewHdl, void *, EMPTYARG )
303 {
304 	String sNewName,sNewLocation;
305 	openLinkDialog(sNewName,sNewLocation);
306 	return 0;
307 }
308 
309 // -----------------------------------------------------------------------
310 
311 IMPL_LINK( DbRegistrationOptionsPage, EditHdl, void *, EMPTYARG )
312 {
313 	SvLBoxEntry* pEntry = pPathBox->GetCurEntry();
314 	if ( !pEntry )
315         return 0L;
316 
317 	DatabaseRegistration* pOldRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
318     if ( !pOldRegistration || pOldRegistration->bReadOnly )
319         return 0L;
320 
321 	String sOldName = pPathBox->GetEntryText(pEntry,0);
322 	m_pCurEntry = pEntry;
323 	openLinkDialog( sOldName, pOldRegistration->sLocation, pEntry );
324 	m_pCurEntry = NULL;
325 
326 	return 1L;
327 }
328 
329 // -----------------------------------------------------------------------
330 
331 IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, HeaderBar*, pBar )
332 {
333 	if ( pBar && pBar->GetCurItemId() != ITEMID_TYPE )
334 		return 0;
335 
336 	HeaderBarItemBits nBits	= pHeaderBar->GetItemBits(ITEMID_TYPE);
337 	sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
338 	SvSortMode eMode = SortAscending;
339 
340 	if ( bUp )
341 	{
342 		nBits &= ~HIB_UPARROW;
343 		nBits |= HIB_DOWNARROW;
344 		eMode = SortDescending;
345 	}
346 	else
347 	{
348 		nBits &= ~HIB_DOWNARROW;
349 		nBits |= HIB_UPARROW;
350 	}
351 	pHeaderBar->SetItemBits( ITEMID_TYPE, nBits );
352 	SvTreeList* pModel = pPathBox->GetModel();
353 	pModel->SetSortMode( eMode );
354 	pModel->Resort();
355 	return 1;
356 }
357 
358 // -----------------------------------------------------------------------
359 
360 IMPL_LINK( DbRegistrationOptionsPage, HeaderEndDrag_Impl, HeaderBar*, pBar )
361 {
362 	if ( pBar && !pBar->GetCurItemId() )
363 		return 0;
364 
365 	if ( !pHeaderBar->IsItemMode() )
366 	{
367 		Size aSz;
368 		sal_uInt16 nTabs = pHeaderBar->GetItemCount();
369 		long nTmpSz = 0;
370 		long nWidth = pHeaderBar->GetItemSize(ITEMID_TYPE);
371 		long nBarWidth = pHeaderBar->GetSizePixel().Width();
372 
373         if(nWidth < TAB_WIDTH_MIN)
374             pHeaderBar->SetItemSize( ITEMID_TYPE, TAB_WIDTH_MIN);
375         else if ( ( nBarWidth - nWidth ) < TAB_WIDTH_MIN )
376             pHeaderBar->SetItemSize( ITEMID_TYPE, nBarWidth - TAB_WIDTH_MIN );
377 
378 		for ( sal_uInt16 i = 1; i <= nTabs; ++i )
379 		{
380             long _nWidth = pHeaderBar->GetItemSize(i);
381             aSz.Width() =  _nWidth + nTmpSz;
382             nTmpSz += _nWidth;
383 			pPathBox->SetTab( i, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
384 		}
385 	}
386 	return 1;
387 }
388 // -----------------------------------------------------------------------
389 
390 IMPL_LINK( DbRegistrationOptionsPage, PathSelect_Impl, SvTabListBox *, EMPTYARG )
391 {
392 	SvLBoxEntry* pEntry = pPathBox->FirstSelected();
393 
394     bool bReadOnly = true;
395     if ( pEntry )
396     {
397 	    DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
398         bReadOnly = pRegistration->bReadOnly;
399     }
400 
401     m_aEdit.Enable( !bReadOnly );
402 	m_aDelete.Enable( !bReadOnly );
403 	return 0;
404 }
405 // -----------------------------------------------------------------------------
406 void DbRegistrationOptionsPage::insertNewEntry( const ::rtl::OUString& _sName,const ::rtl::OUString& _sLocation, const bool _bReadOnly )
407 {
408 	String aStr( _sName );
409 	aStr += '\t';
410 	aStr += String(_sLocation);
411 
412     SvLBoxEntry* pEntry = NULL;
413     if ( _bReadOnly )
414     {
415         sal_Bool bHighContrast = pPathBox->GetDisplayBackground().GetColor().IsDark();
416         Image aLocked( CUI_RES( bHighContrast ? RID_SVXBMP_LOCK_HC : RID_SVXBMP_LOCK ) );
417         pEntry = pPathBox->InsertEntry( aStr, aLocked, aLocked );
418     }
419     else
420     {
421         pEntry = pPathBox->InsertEntry( aStr );
422     }
423 
424 	pEntry->SetUserData( new DatabaseRegistration( _sLocation, _bReadOnly ) );
425 }
426 
427 // -----------------------------------------------------------------------------
428 void DbRegistrationOptionsPage::openLinkDialog(const String& _sOldName,const String& _sOldLocation,SvLBoxEntry* _pEntry)
429 {
430 	ODocumentLinkDialog aDlg(this,_pEntry == NULL);
431 
432 	aDlg.set(_sOldName,_sOldLocation);
433 	aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
434 
435 	if ( aDlg.Execute() == RET_OK )
436 	{
437 		String sNewName,sNewLocation;
438 		aDlg.get(sNewName,sNewLocation);
439 		if ( _pEntry == NULL || sNewName != _sOldName || sNewLocation != _sOldLocation )
440 		{
441 			if ( _pEntry )
442 			{
443 				delete static_cast< DatabaseRegistration* >( _pEntry->GetUserData() );
444 				pPathBox->GetModel()->Remove( _pEntry );
445 			}
446 			insertNewEntry( sNewName, sNewLocation, false );
447 			m_bModified = sal_True;
448 		}
449 	}
450 }
451 // -----------------------------------------------------------------------------
452 IMPL_LINK( DbRegistrationOptionsPage, NameValidator, String*, _pName )
453 {
454 	if ( _pName )
455 	{
456 		sal_uLong nCount = pPathBox->GetEntryCount();
457 		for ( sal_uLong i = 0; i < nCount; ++i )
458 		{
459 			SvLBoxEntry* pEntry = pPathBox->GetEntry(i);
460 			if ( (!m_pCurEntry || m_pCurEntry != pEntry) && pPathBox->GetEntryText(pEntry,0) == *_pName )
461 				return 0L;
462 		}
463 	}
464 	return 1L;
465 }
466 //........................................................................
467 }	// namespace svx
468 //........................................................................
469 
470 
471