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