1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dbregister.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #ifdef SVX_DLLIMPLEMENTATION
35 #undef SVX_DLLIMPLEMENTATION
38 #include "dbregister.hxx"
39 #include "dbregister.hrc"
40 #include "dbregistersettings.hxx"
41 #include "connpooloptions.hxx"
42 #ifndef SVTOOLS_FILENOTATION_HXX_
43 #include <svtools/filenotation.hxx>
45 #include <svx/dialogs.hrc>
46 #include <svx/svxids.hrc>
48 #include <svtools/editbrowsebox.hxx>
49 #include <svx/dialogs.hrc>
50 #include <vcl/field.hxx>
51 #include <svtools/eitem.hxx>
52 #include <comphelper/processfactory.hxx>
53 #include <com/sun/star/uno/Exception.hpp>
54 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
55 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
56 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
57 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
58 #include <vcl/msgbox.hxx>
59 #include <svtools/svtabbx.hxx>
61 #include <svtools/itemset.hxx>
62 #include "doclinkdialog.hxx"
63 #include <tools/urlobj.hxx>
64 #include <unotools/localfilehelper.hxx>
65 #include "optHeaderTabListbox.hxx"
66 #include <sfx2/docfilt.hxx>
67 #include <svx/dialmgr.hxx>
68 #include "dbregisterednamesconfig.hxx"
71 #define TAB_WIDTH_MIN 10
72 #define TAB_WIDTH2 1000
75 //........................................................................
78 //........................................................................
80 using namespace ::com::sun::star::lang
;
81 using namespace ::com::sun::star::ui::dialogs
;
82 using namespace ::com::sun::star::uno
;
83 using namespace ::svt
;
85 // class RegistrationItemSetHolder -------------------------------------------------
87 RegistrationItemSetHolder::RegistrationItemSetHolder( const SfxItemSet
& _rMasterSet
)
88 :m_aRegistrationItems( _rMasterSet
)
90 DbRegisteredNamesConfig::GetOptions( m_aRegistrationItems
);
93 RegistrationItemSetHolder::~RegistrationItemSetHolder()
97 // class DatabaseRegistrationDialog ------------------------------------------------
99 DatabaseRegistrationDialog::DatabaseRegistrationDialog( Window
* pParent
, const SfxItemSet
& rInAttrs
)
100 :RegistrationItemSetHolder( rInAttrs
)
101 ,SfxSingleTabDialog( pParent
, getRegistrationItems(), RID_SFXPAGE_DBREGISTER
)
103 SfxTabPage
* page
= DbRegistrationOptionsPage::Create( this, getRegistrationItems() );
106 SetText( page
->GetText() );
109 DatabaseRegistrationDialog::~DatabaseRegistrationDialog()
113 short DatabaseRegistrationDialog::Execute()
115 short result
= SfxSingleTabDialog::Execute();
116 if ( result
== RET_OK
)
118 DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
119 if ( GetOutputItemSet() )
120 DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
125 // class DbRegistrationOptionsPage --------------------------------------------------
127 DbRegistrationOptionsPage::DbRegistrationOptionsPage( Window
* pParent
, const SfxItemSet
& rSet
) :
129 SfxTabPage( pParent
, SVX_RES( RID_SFXPAGE_DBREGISTER
), rSet
),
131 aTypeText ( this, SVX_RES( FT_TYPE
) ),
132 aPathText ( this, SVX_RES( FT_PATH
) ),
133 aPathCtrl ( this, SVX_RES( LB_PATH
) ),
134 m_aNew ( this, SVX_RES( BTN_NEW
) ),
135 m_aEdit ( this, SVX_RES( BTN_EDIT
) ),
136 m_aDelete ( this, SVX_RES( BTN_DELETE
) ),
137 aStdBox ( this, SVX_RES( GB_STD
) ),
140 m_pCurEntry ( NULL
),
142 m_bModified ( FALSE
)
144 m_aNew
.SetClickHdl( LINK( this, DbRegistrationOptionsPage
, NewHdl
) );
145 m_aEdit
.SetClickHdl( LINK( this, DbRegistrationOptionsPage
, EditHdl
) );
146 m_aDelete
.SetClickHdl( LINK( this, DbRegistrationOptionsPage
, DeleteHdl
) );
148 Size aBoxSize
= aPathCtrl
.GetOutputSizePixel();
151 WinBits nBits
= WB_SORT
| WB_HSCROLL
| WB_CLIPCHILDREN
| WB_TABSTOP
;
152 pPathBox
= new ::svx::OptHeaderTabListBox( &aPathCtrl
, nBits
);
154 pHeaderBar
= new HeaderBar( &aPathCtrl
, WB_BUTTONSTYLE
| WB_BOTTOMBORDER
);
155 pHeaderBar
->SetPosSizePixel( Point( 0, 0 ), Size( aBoxSize
.Width(), 16 ) );
156 pHeaderBar
->SetSelectHdl( LINK( this, DbRegistrationOptionsPage
, HeaderSelect_Impl
) );
157 pHeaderBar
->SetEndDragHdl( LINK( this, DbRegistrationOptionsPage
, HeaderEndDrag_Impl
) );
159 aSz
.Width() = TAB_WIDTH1
;
160 pHeaderBar
->InsertItem( ITEMID_TYPE
, aTypeText
.GetText(),
161 LogicToPixel( aSz
, MapMode( MAP_APPFONT
) ).Width(),
162 HIB_LEFT
| HIB_VCENTER
| HIB_CLICKABLE
| HIB_UPARROW
);
163 aSz
.Width() = TAB_WIDTH2
;
164 pHeaderBar
->InsertItem( ITEMID_PATH
, aPathText
.GetText(),
165 LogicToPixel( aSz
, MapMode( MAP_APPFONT
) ).Width(),
166 HIB_LEFT
| HIB_VCENTER
);
168 static long nTabs
[] = {3, 0, TAB_WIDTH1
, TAB_WIDTH1
+ TAB_WIDTH2
};
169 Size aHeadSize
= pHeaderBar
->GetSizePixel();
171 aPathCtrl
.SetFocusControl( pPathBox
);
172 pPathBox
->SetWindowBits( nBits
);
173 pPathBox
->SetDoubleClickHdl( LINK( this, DbRegistrationOptionsPage
, EditHdl
) );
174 pPathBox
->SetSelectHdl( LINK( this, DbRegistrationOptionsPage
, PathSelect_Impl
) );
175 pPathBox
->SetSelectionMode( SINGLE_SELECTION
);
176 pPathBox
->SetPosSizePixel( Point( 0, aHeadSize
.Height() ),
177 Size( aBoxSize
.Width(), aBoxSize
.Height() - aHeadSize
.Height() ) );
178 pPathBox
->SetTabs( &nTabs
[0], MAP_APPFONT
);
179 pPathBox
->InitHeaderBar( pHeaderBar
);
180 pPathBox
->SetHighlightRange();
182 pPathBox
->SetHelpId( HID_DBPATH_CTL_PATH
);
183 pHeaderBar
->SetHelpId( HID_DBPATH_HEADERBAR
);
191 // -----------------------------------------------------------------------
193 DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
195 // #110603# do not grab focus to a destroyed window !!!
196 aPathCtrl
.SetFocusControl( NULL
);
199 for ( USHORT i
= 0; i
< pPathBox
->GetEntryCount(); ++i
)
200 delete static_cast<String
*>(pPathBox
->GetEntry(i
)->GetUserData());
205 // -----------------------------------------------------------------------
207 SfxTabPage
* DbRegistrationOptionsPage::Create( Window
* pParent
,
208 const SfxItemSet
& rAttrSet
)
210 return ( new DbRegistrationOptionsPage( pParent
, rAttrSet
) );
213 // -----------------------------------------------------------------------
215 BOOL
DbRegistrationOptionsPage::FillItemSet( SfxItemSet
& rCoreSet
)
217 // the settings for the single drivers
218 sal_Bool bModified
= sal_False
;
219 TNameLocationMap aMap
;
220 ULONG nCount
= pPathBox
->GetEntryCount();
221 for ( ULONG i
= 0; i
< nCount
; ++i
)
223 SvLBoxEntry
* pEntry
= pPathBox
->GetEntry(i
);
224 String
* pPath
= static_cast<String
*>(pEntry
->GetUserData());
225 if ( pPath
&& pPath
->Len() )
227 OFileNotation
aTransformer(*pPath
);
228 aMap
.insert(TNameLocationMap::value_type(::rtl::OUString(pPathBox
->GetEntryText(pEntry
,0)),aTransformer
.get(OFileNotation::N_URL
)));
231 if ( m_nOldCount
!= aMap
.size() || m_bModified
)
233 rCoreSet
.Put(DatabaseMapItem(SID_SB_DB_REGISTER
, aMap
), SID_SB_DB_REGISTER
);
234 bModified
= sal_True
;
240 // -----------------------------------------------------------------------
242 void DbRegistrationOptionsPage::Reset( const SfxItemSet
& rSet
)
244 // the settings for the single drivers
245 SFX_ITEMSET_GET( rSet
, pSettings
, DatabaseMapItem
, SID_SB_DB_REGISTER
, sal_True
);
249 // TabListBox f"ullen
252 const TNameLocationMap
& rMap
= pSettings
->getSettings();
253 m_nOldCount
= rMap
.size();
254 TNameLocationMap::const_iterator aIter
= rMap
.begin();
255 TNameLocationMap::const_iterator aEnd
= rMap
.end();
256 for (; aIter
!= aEnd
; ++aIter
)
258 OFileNotation
aTransformer(aIter
->second
);
259 insertNewEntry(aIter
->first
,aTransformer
.get(OFileNotation::N_SYSTEM
));
262 String aUserData
= GetUserData();
264 if ( aUserData
.Len() )
266 // Spaltenbreite restaurieren
267 pHeaderBar
->SetItemSize( ITEMID_TYPE
, aUserData
.GetToken(0).ToInt32() );
268 HeaderEndDrag_Impl( NULL
);
269 // Sortierrichtung restaurieren
270 BOOL bUp
= (BOOL
)(USHORT
)aUserData
.GetToken(1).ToInt32();
271 HeaderBarItemBits nBits
= pHeaderBar
->GetItemBits(ITEMID_TYPE
);
275 nBits
&= ~HIB_UPARROW
;
276 nBits
|= HIB_DOWNARROW
;
280 nBits
&= ~HIB_DOWNARROW
;
281 nBits
|= HIB_UPARROW
;
283 pHeaderBar
->SetItemBits( ITEMID_TYPE
, nBits
);
284 HeaderSelect_Impl( NULL
);
289 // -----------------------------------------------------------------------
291 void DbRegistrationOptionsPage::FillUserData()
293 String aUserData
= String::CreateFromInt32( pHeaderBar
->GetItemSize( ITEMID_TYPE
) );
295 HeaderBarItemBits nBits
= pHeaderBar
->GetItemBits( ITEMID_TYPE
);
296 BOOL bUp
= ( ( nBits
& HIB_UPARROW
) == HIB_UPARROW
);
297 aUserData
+= bUp
? '1' : '0';
298 SetUserData( aUserData
);
300 // -----------------------------------------------------------------------
302 IMPL_LINK( DbRegistrationOptionsPage
, DeleteHdl
, void *, EMPTYARG
)
304 SvLBoxEntry
* pEntry
= pPathBox
->FirstSelected();
307 QueryBox
aQuery(this,SVX_RES(QUERY_DELETE_CONFIRM
));
308 if ( aQuery
.Execute() == RET_YES
)
309 pPathBox
->GetModel()->Remove(pEntry
);
313 // -----------------------------------------------------------------------
314 IMPL_LINK( DbRegistrationOptionsPage
, NewHdl
, void *, EMPTYARG
)
316 String sNewName
,sNewLocation
;
317 openLinkDialog(sNewName
,sNewLocation
);
321 // -----------------------------------------------------------------------
323 IMPL_LINK( DbRegistrationOptionsPage
, EditHdl
, void *, EMPTYARG
)
325 SvLBoxEntry
* pEntry
= pPathBox
->GetCurEntry();
328 String
* pOldLocation
= static_cast<String
*>(pEntry
->GetUserData());
329 String sOldName
= pPathBox
->GetEntryText(pEntry
,0);
330 m_pCurEntry
= pEntry
;
331 openLinkDialog(sOldName
,*pOldLocation
,pEntry
);
338 // -----------------------------------------------------------------------
340 IMPL_LINK( DbRegistrationOptionsPage
, HeaderSelect_Impl
, HeaderBar
*, pBar
)
342 if ( pBar
&& pBar
->GetCurItemId() != ITEMID_TYPE
)
345 HeaderBarItemBits nBits
= pHeaderBar
->GetItemBits(ITEMID_TYPE
);
346 BOOL bUp
= ( ( nBits
& HIB_UPARROW
) == HIB_UPARROW
);
347 SvSortMode eMode
= SortAscending
;
351 nBits
&= ~HIB_UPARROW
;
352 nBits
|= HIB_DOWNARROW
;
353 eMode
= SortDescending
;
357 nBits
&= ~HIB_DOWNARROW
;
358 nBits
|= HIB_UPARROW
;
360 pHeaderBar
->SetItemBits( ITEMID_TYPE
, nBits
);
361 SvTreeList
* pModel
= pPathBox
->GetModel();
362 pModel
->SetSortMode( eMode
);
367 // -----------------------------------------------------------------------
369 IMPL_LINK( DbRegistrationOptionsPage
, HeaderEndDrag_Impl
, HeaderBar
*, pBar
)
371 if ( pBar
&& !pBar
->GetCurItemId() )
374 if ( !pHeaderBar
->IsItemMode() )
377 USHORT nTabs
= pHeaderBar
->GetItemCount();
379 long nWidth
= pHeaderBar
->GetItemSize(ITEMID_TYPE
);
380 long nBarWidth
= pHeaderBar
->GetSizePixel().Width();
382 if(nWidth
< TAB_WIDTH_MIN
)
383 pHeaderBar
->SetItemSize( ITEMID_TYPE
, TAB_WIDTH_MIN
);
384 else if ( ( nBarWidth
- nWidth
) < TAB_WIDTH_MIN
)
385 pHeaderBar
->SetItemSize( ITEMID_TYPE
, nBarWidth
- TAB_WIDTH_MIN
);
387 for ( USHORT i
= 1; i
<= nTabs
; ++i
)
389 long _nWidth
= pHeaderBar
->GetItemSize(i
);
390 aSz
.Width() = _nWidth
+ nTmpSz
;
392 pPathBox
->SetTab( i
, PixelToLogic( aSz
, MapMode(MAP_APPFONT
) ).Width(), MAP_APPFONT
);
397 // -----------------------------------------------------------------------
399 IMPL_LINK( DbRegistrationOptionsPage
, PathSelect_Impl
, SvTabListBox
*, EMPTYARG
)
406 SvLBoxEntry
* pEntry
= pPathBox
->FirstSelected();
408 m_aEdit
.Enable( pEntry
!= NULL
);
409 m_aDelete
.Enable( pEntry
!= NULL
);
412 // -----------------------------------------------------------------------------
413 void DbRegistrationOptionsPage::insertNewEntry(const ::rtl::OUString
& _sName
,const ::rtl::OUString
& _sLocation
)
415 String
aStr( _sName
);
417 aStr
+= String(_sLocation
);
418 SvLBoxEntry
* pEntry
= pPathBox
->InsertEntry( aStr
);
419 String
* pLocation
= new String( _sLocation
);
420 pEntry
->SetUserData( pLocation
);
422 // -----------------------------------------------------------------------------
423 String
DbRegistrationOptionsPage::getFileLocation(const String
& _sLocation
)
427 rtl::OUString
aService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FilePicker" ) );
428 Reference
< XMultiServiceFactory
> xFactory( ::comphelper::getProcessServiceFactory() );
429 Reference
< XFilePicker
> xFilePicker( xFactory
->createInstance( aService
), UNO_QUERY
);
430 OSL_ENSURE(xFilePicker
.is() ,"Could create file picker service!");
431 Reference
< XFilterManager
> xFilterManager(xFilePicker
,UNO_QUERY
);
432 static const String s_sDatabaseType
= String::CreateFromAscii("StarOffice XML (Base)");
433 const SfxFilter
* pFilter
= SfxFilter::GetFilterByName( s_sDatabaseType
);
436 xFilterManager
->appendFilter( pFilter
->GetUIName(),pFilter
->GetDefaultExtension());
437 xFilterManager
->setCurrentFilter(pFilter
->GetUIName());
440 INetURLObject
aURL( _sLocation
, INET_PROT_FILE
);
441 xFilePicker
->setMultiSelectionMode(sal_False
);
442 xFilePicker
->setDisplayDirectory( aURL
.GetMainURL( INetURLObject::NO_DECODE
) );
443 short nRet
= xFilePicker
->execute();
445 if ( ExecutableDialogResults::OK
== nRet
)
448 // old path is an URL?
449 INetURLObject
aObj( _sLocation
);
450 FASTBOOL bURL
= ( aObj
.GetProtocol() != INET_PROT_NOT_VALID
);
451 Sequence
< ::rtl::OUString
> aFiles
= xFilePicker
->getFiles();
452 INetURLObject
aNewObj( aFiles
[0] );
453 aNewObj
.removeFinalSlash();
455 // then the new path also an URL else system path
456 String sNewLocation
= bURL
? rtl::OUString(aFiles
[0]) : aNewObj
.getFSysPath( INetURLObject::FSYS_DETECT
);
460 // Unix is case sensitive
461 ( sNewLocation
!= _sLocation
)
463 ( sNewLocation
.CompareIgnoreCaseToAscii( _sLocation
) != COMPARE_EQUAL
)
471 DBG_ERRORFILE( "DbRegistrationOptionsPage::EditLocationHdl: exception from folder picker" );
476 // -----------------------------------------------------------------------------
477 void DbRegistrationOptionsPage::openLinkDialog(const String
& _sOldName
,const String
& _sOldLocation
,SvLBoxEntry
* _pEntry
)
479 ODocumentLinkDialog
aDlg(this,_pEntry
== NULL
);
481 aDlg
.set(_sOldName
,_sOldLocation
);
482 aDlg
.setNameValidator(LINK( this, DbRegistrationOptionsPage
, NameValidator
) );
484 if ( aDlg
.Execute() == RET_OK
)
486 String sNewName
,sNewLocation
;
487 aDlg
.get(sNewName
,sNewLocation
);
488 if ( _pEntry
== NULL
|| sNewName
!= _sOldName
|| sNewLocation
!= _sOldLocation
)
492 delete static_cast<String
*>(_pEntry
->GetUserData());
493 pPathBox
->GetModel()->Remove(_pEntry
);
495 insertNewEntry(sNewName
,sNewLocation
);
496 m_bModified
= sal_True
;
500 // -----------------------------------------------------------------------------
501 IMPL_LINK( DbRegistrationOptionsPage
, NameValidator
, String
*, _pName
)
505 ULONG nCount
= pPathBox
->GetEntryCount();
506 for ( ULONG i
= 0; i
< nCount
; ++i
)
508 SvLBoxEntry
* pEntry
= pPathBox
->GetEntry(i
);
509 if ( (!m_pCurEntry
|| m_pCurEntry
!= pEntry
) && pPathBox
->GetEntryText(pEntry
,0) == *_pName
)
515 //........................................................................
517 //........................................................................