masterfix DEV300: #i10000# build fix
[LibreOffice.git] / cui / source / options / dbregister.cxx
blobb8892ffcbd3fba19a9dca1dd3633387a82b57d7b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
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>
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
68 //........................................................................
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;
75 // class RegistrationItemSetHolder -------------------------------------------------
77 RegistrationItemSetHolder::RegistrationItemSetHolder( const SfxItemSet& _rMasterSet )
78 :m_aRegistrationItems( _rMasterSet )
80 DbRegisteredNamesConfig::GetOptions( m_aRegistrationItems );
83 RegistrationItemSetHolder::~RegistrationItemSetHolder()
87 // class DatabaseRegistrationDialog ------------------------------------------------
89 DatabaseRegistrationDialog::DatabaseRegistrationDialog( Window* pParent, const SfxItemSet& rInAttrs )
90 :RegistrationItemSetHolder( rInAttrs )
91 ,SfxSingleTabDialog( pParent, getRegistrationItems(), RID_SFXPAGE_DBREGISTER )
93 SfxTabPage* page = DbRegistrationOptionsPage::Create( this, getRegistrationItems() );
95 SetTabPage( page );
96 SetText( page->GetText() );
99 DatabaseRegistrationDialog::~DatabaseRegistrationDialog()
103 short DatabaseRegistrationDialog::Execute()
105 short result = SfxSingleTabDialog::Execute();
106 if ( result == RET_OK )
108 DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
109 if ( GetOutputItemSet() )
110 DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
112 return result;
115 // class DbRegistrationOptionsPage --------------------------------------------------
117 DbRegistrationOptionsPage::DbRegistrationOptionsPage( Window* pParent, const SfxItemSet& rSet ) :
119 SfxTabPage( pParent, CUI_RES( RID_SFXPAGE_DBREGISTER), rSet ),
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 )
134 m_aNew.SetClickHdl( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
135 m_aEdit.SetClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
136 m_aDelete.SetClickHdl( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );
138 Size aBoxSize = aPathCtrl.GetOutputSizePixel();
141 WinBits nBits = WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP;
142 pPathBox = new ::svx::OptHeaderTabListBox( &aPathCtrl, nBits );
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 );
158 static long nTabs[] = {3, 0, TAB_WIDTH1, TAB_WIDTH1 + TAB_WIDTH2 };
159 Size aHeadSize = pHeaderBar->GetSizePixel();
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();
172 pPathBox->SetHelpId( HID_DBPATH_CTL_PATH );
173 pHeaderBar->SetHelpId( HID_DBPATH_HEADERBAR );
175 pPathBox->Show();
176 pHeaderBar->Show();
178 FreeResource();
181 // -----------------------------------------------------------------------
183 DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
185 // #110603# do not grab focus to a destroyed window !!!
186 aPathCtrl.SetFocusControl( NULL );
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;
195 // -----------------------------------------------------------------------
197 SfxTabPage* DbRegistrationOptionsPage::Create( Window* pParent,
198 const SfxItemSet& rAttrSet )
200 return ( new DbRegistrationOptionsPage( pParent, rAttrSet ) );
203 // -----------------------------------------------------------------------
205 sal_Bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet& rCoreSet )
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 )
213 SvLBoxEntry* pEntry = pPathBox->GetEntry(i);
214 DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
215 if ( pRegistration && pRegistration->sLocation.getLength() )
217 ::rtl::OUString sName( pPathBox->GetEntryText( pEntry, 0 ) );
218 OFileNotation aTransformer( pRegistration->sLocation );
219 aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
222 if ( m_nOldCount != aRegistrations.size() || m_bModified )
224 rCoreSet.Put(DatabaseMapItem( SID_SB_DB_REGISTER, aRegistrations ), SID_SB_DB_REGISTER);
225 bModified = sal_True;
228 return bModified;
231 // -----------------------------------------------------------------------
233 void DbRegistrationOptionsPage::Reset( const SfxItemSet& rSet )
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;
240 pPathBox->Clear();
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 )
248 OFileNotation aTransformer( aIter->second.sLocation );
249 insertNewEntry( aIter->first, aTransformer.get( OFileNotation::N_SYSTEM ), aIter->second.bReadOnly );
252 String aUserData = GetUserData();
253 if ( aUserData.Len() )
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);
262 if ( bUp )
264 nBits &= ~HIB_UPARROW;
265 nBits |= HIB_DOWNARROW;
267 else
269 nBits &= ~HIB_DOWNARROW;
270 nBits |= HIB_UPARROW;
272 pHeaderBar->SetItemBits( ITEMID_TYPE, nBits );
273 HeaderSelect_Impl( NULL );
277 // -----------------------------------------------------------------------
279 void DbRegistrationOptionsPage::FillUserData()
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 );
288 // -----------------------------------------------------------------------
290 IMPL_LINK( DbRegistrationOptionsPage, DeleteHdl, void *, EMPTYARG )
292 SvLBoxEntry* pEntry = pPathBox->FirstSelected();
293 if ( pEntry )
295 QueryBox aQuery(this,CUI_RES(QUERY_DELETE_CONFIRM));
296 if ( aQuery.Execute() == RET_YES )
297 pPathBox->GetModel()->Remove(pEntry);
299 return 0;
301 // -----------------------------------------------------------------------
302 IMPL_LINK( DbRegistrationOptionsPage, NewHdl, void *, EMPTYARG )
304 String sNewName,sNewLocation;
305 openLinkDialog(sNewName,sNewLocation);
306 return 0;
309 // -----------------------------------------------------------------------
311 IMPL_LINK( DbRegistrationOptionsPage, EditHdl, void *, EMPTYARG )
313 SvLBoxEntry* pEntry = pPathBox->GetCurEntry();
314 if ( !pEntry )
315 return 0L;
317 DatabaseRegistration* pOldRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
318 if ( !pOldRegistration || pOldRegistration->bReadOnly )
319 return 0L;
321 String sOldName = pPathBox->GetEntryText(pEntry,0);
322 m_pCurEntry = pEntry;
323 openLinkDialog( sOldName, pOldRegistration->sLocation, pEntry );
324 m_pCurEntry = NULL;
326 return 1L;
329 // -----------------------------------------------------------------------
331 IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, HeaderBar*, pBar )
333 if ( pBar && pBar->GetCurItemId() != ITEMID_TYPE )
334 return 0;
336 HeaderBarItemBits nBits = pHeaderBar->GetItemBits(ITEMID_TYPE);
337 sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
338 SvSortMode eMode = SortAscending;
340 if ( bUp )
342 nBits &= ~HIB_UPARROW;
343 nBits |= HIB_DOWNARROW;
344 eMode = SortDescending;
346 else
348 nBits &= ~HIB_DOWNARROW;
349 nBits |= HIB_UPARROW;
351 pHeaderBar->SetItemBits( ITEMID_TYPE, nBits );
352 SvTreeList* pModel = pPathBox->GetModel();
353 pModel->SetSortMode( eMode );
354 pModel->Resort();
355 return 1;
358 // -----------------------------------------------------------------------
360 IMPL_LINK( DbRegistrationOptionsPage, HeaderEndDrag_Impl, HeaderBar*, pBar )
362 if ( pBar && !pBar->GetCurItemId() )
363 return 0;
365 if ( !pHeaderBar->IsItemMode() )
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();
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 );
378 for ( sal_uInt16 i = 1; i <= nTabs; ++i )
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 );
386 return 1;
388 // -----------------------------------------------------------------------
390 IMPL_LINK( DbRegistrationOptionsPage, PathSelect_Impl, SvTabListBox *, EMPTYARG )
392 SvLBoxEntry* pEntry = pPathBox->FirstSelected();
394 bool bReadOnly = true;
395 if ( pEntry )
397 DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
398 bReadOnly = pRegistration->bReadOnly;
401 m_aEdit.Enable( !bReadOnly );
402 m_aDelete.Enable( !bReadOnly );
403 return 0;
405 // -----------------------------------------------------------------------------
406 void DbRegistrationOptionsPage::insertNewEntry( const ::rtl::OUString& _sName,const ::rtl::OUString& _sLocation, const bool _bReadOnly )
408 String aStr( _sName );
409 aStr += '\t';
410 aStr += String(_sLocation);
412 SvLBoxEntry* pEntry = NULL;
413 if ( _bReadOnly )
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 );
419 else
421 pEntry = pPathBox->InsertEntry( aStr );
424 pEntry->SetUserData( new DatabaseRegistration( _sLocation, _bReadOnly ) );
427 // -----------------------------------------------------------------------------
428 void DbRegistrationOptionsPage::openLinkDialog(const String& _sOldName,const String& _sOldLocation,SvLBoxEntry* _pEntry)
430 ODocumentLinkDialog aDlg(this,_pEntry == NULL);
432 aDlg.set(_sOldName,_sOldLocation);
433 aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
435 if ( aDlg.Execute() == RET_OK )
437 String sNewName,sNewLocation;
438 aDlg.get(sNewName,sNewLocation);
439 if ( _pEntry == NULL || sNewName != _sOldName || sNewLocation != _sOldLocation )
441 if ( _pEntry )
443 delete static_cast< DatabaseRegistration* >( _pEntry->GetUserData() );
444 pPathBox->GetModel()->Remove( _pEntry );
446 insertNewEntry( sNewName, sNewLocation, false );
447 m_bModified = sal_True;
451 // -----------------------------------------------------------------------------
452 IMPL_LINK( DbRegistrationOptionsPage, NameValidator, String*, _pName )
454 if ( _pName )
456 sal_uLong nCount = pPathBox->GetEntryCount();
457 for ( sal_uLong i = 0; i < nCount; ++i )
459 SvLBoxEntry* pEntry = pPathBox->GetEntry(i);
460 if ( (!m_pCurEntry || m_pCurEntry != pEntry) && pPathBox->GetEntryText(pEntry,0) == *_pName )
461 return 0L;
464 return 1L;
466 //........................................................................
467 } // namespace svx
468 //........................................................................