1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "dbregister.hxx"
21 #include "dbregistersettings.hxx"
22 #include "connpooloptions.hxx"
23 #include <svl/filenotation.hxx>
25 #include <svtools/editbrowsebox.hxx>
26 #include "svtools/treelistentry.hxx"
28 #include <vcl/field.hxx>
29 #include <vcl/layout.hxx>
30 #include <svl/eitem.hxx>
31 #include <comphelper/processfactory.hxx>
32 #include <com/sun/star/uno/Exception.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
35 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
36 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
37 #include <vcl/msgbox.hxx>
38 #include <svtools/svtabbx.hxx>
39 #include <svl/itemset.hxx>
40 #include "doclinkdialog.hxx"
41 #include <unotools/localfilehelper.hxx>
42 #include "optHeaderTabListbox.hxx"
43 #include <sfx2/docfilt.hxx>
44 #include <dialmgr.hxx>
45 #include "dbregisterednamesconfig.hxx"
46 #include <svx/dialogs.hrc>
49 #define TAB_WIDTH_MIN 10
50 #define TAB_WIDTH2 1000
58 using namespace ::com::sun::star::lang
;
59 using namespace ::com::sun::star::ui::dialogs
;
60 using namespace ::com::sun::star::uno
;
61 using namespace ::svt
;
63 // class RegistrationItemSetHolder -------------------------------------------------
65 RegistrationItemSetHolder::RegistrationItemSetHolder( const SfxItemSet
& _rMasterSet
)
66 :m_aRegistrationItems( _rMasterSet
)
68 DbRegisteredNamesConfig::GetOptions( m_aRegistrationItems
);
71 RegistrationItemSetHolder::~RegistrationItemSetHolder()
75 // class DatabaseRegistrationDialog ------------------------------------------------
77 DatabaseRegistrationDialog::DatabaseRegistrationDialog( Window
* pParent
, const SfxItemSet
& rInAttrs
)
78 : RegistrationItemSetHolder(rInAttrs
)
79 , SfxSingleTabDialog(pParent
, getRegistrationItems())
81 SfxTabPage
* page
= DbRegistrationOptionsPage::Create(get_content_area(), getRegistrationItems());
83 SetText(page
->get
<VclFrame
>("frame1")->get_label());
86 DatabaseRegistrationDialog::~DatabaseRegistrationDialog()
90 short DatabaseRegistrationDialog::Execute()
92 short result
= SfxSingleTabDialog::Execute();
93 if ( result
== RET_OK
)
95 DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
96 if ( GetOutputItemSet() )
97 DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
102 // class DbRegistrationOptionsPage --------------------------------------------------
104 DbRegistrationOptionsPage::DbRegistrationOptionsPage( Window
* pParent
, const SfxItemSet
& rSet
) :
106 SfxTabPage( pParent
, "DbRegisterPage", "cui/ui/dbregisterpage.ui", rSet
),
108 aTypeText ( CUI_RES( RID_SVXSTR_TYPE
) ),
109 aPathText ( CUI_RES( RID_SVXSTR_PATH
) ),
111 m_pCurEntry ( NULL
),
113 m_bModified ( false )
115 get(m_pPathCtrl
, "pathctrl");
116 Size
aControlSize(248, 147);
117 aControlSize
= LogicToPixel(aControlSize
, MAP_APPFONT
);
118 m_pPathCtrl
->set_width_request(aControlSize
.Width());
119 m_pPathCtrl
->set_height_request(aControlSize
.Height());
122 get(m_pEdit
, "edit");
123 get(m_pDelete
, "delete");
125 m_pNew
->SetClickHdl( LINK( this, DbRegistrationOptionsPage
, NewHdl
) );
126 m_pEdit
->SetClickHdl( LINK( this, DbRegistrationOptionsPage
, EditHdl
) );
127 m_pDelete
->SetClickHdl( LINK( this, DbRegistrationOptionsPage
, DeleteHdl
) );
129 Size aBoxSize
= m_pPathCtrl
->GetOutputSizePixel();
131 WinBits nBits
= WB_SORT
| WB_HSCROLL
| WB_CLIPCHILDREN
| WB_TABSTOP
;
132 pPathBox
= new ::svx::OptHeaderTabListBox( *m_pPathCtrl
, nBits
);
134 HeaderBar
&rBar
= pPathBox
->GetTheHeaderBar();
136 rBar
.SetSelectHdl( LINK( this, DbRegistrationOptionsPage
, HeaderSelect_Impl
) );
137 rBar
.SetEndDragHdl( LINK( this, DbRegistrationOptionsPage
, HeaderEndDrag_Impl
) );
139 aSz
.Width() = TAB_WIDTH1
;
140 rBar
.InsertItem( ITEMID_TYPE
, aTypeText
,
141 LogicToPixel( aSz
, MapMode( MAP_APPFONT
) ).Width(),
142 HIB_LEFT
| HIB_VCENTER
| HIB_CLICKABLE
| HIB_UPARROW
);
143 aSz
.Width() = TAB_WIDTH2
;
144 rBar
.InsertItem( ITEMID_PATH
, aPathText
,
145 LogicToPixel( aSz
, MapMode( MAP_APPFONT
) ).Width(),
146 HIB_LEFT
| HIB_VCENTER
);
148 static long aTabs
[] = {3, 0, TAB_WIDTH1
, TAB_WIDTH1
+ TAB_WIDTH2
};
149 Size aHeadSize
= rBar
.GetSizePixel();
151 pPathBox
->SetStyle( pPathBox
->GetStyle()|nBits
);
152 pPathBox
->SetDoubleClickHdl( LINK( this, DbRegistrationOptionsPage
, EditHdl
) );
153 pPathBox
->SetSelectHdl( LINK( this, DbRegistrationOptionsPage
, PathSelect_Impl
) );
154 pPathBox
->SetSelectionMode( SINGLE_SELECTION
);
155 pPathBox
->SetPosSizePixel( Point( 0, aHeadSize
.Height() ),
156 Size( aBoxSize
.Width(), aBoxSize
.Height() - aHeadSize
.Height() ) );
157 pPathBox
->SvSimpleTable::SetTabs( aTabs
, MAP_APPFONT
);
158 pPathBox
->SetHighlightRange();
160 pPathBox
->SetHelpId( HID_DBPATH_CTL_PATH
);
161 rBar
.SetHelpId( HID_DBPATH_HEADERBAR
);
163 pPathBox
->ShowTable();
168 DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
170 for ( sal_uInt16 i
= 0; i
< pPathBox
->GetEntryCount(); ++i
)
171 delete static_cast< DatabaseRegistration
* >( pPathBox
->GetEntry(i
)->GetUserData() );
177 SfxTabPage
* DbRegistrationOptionsPage::Create( Window
* pParent
,
178 const SfxItemSet
& rAttrSet
)
180 return ( new DbRegistrationOptionsPage( pParent
, rAttrSet
) );
185 bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet
& rCoreSet
)
187 // the settings for the single drivers
188 bool bModified
= false;
189 DatabaseRegistrations aRegistrations
;
190 sal_uLong nCount
= pPathBox
->GetEntryCount();
191 for ( sal_uLong i
= 0; i
< nCount
; ++i
)
193 SvTreeListEntry
* pEntry
= pPathBox
->GetEntry(i
);
194 DatabaseRegistration
* pRegistration
= static_cast< DatabaseRegistration
* >( pEntry
->GetUserData() );
195 if ( pRegistration
&& !pRegistration
->sLocation
.isEmpty() )
197 OUString
sName( pPathBox
->GetEntryText( pEntry
, 0 ) );
198 OFileNotation
aTransformer( pRegistration
->sLocation
);
199 aRegistrations
[ sName
] = DatabaseRegistration( aTransformer
.get( OFileNotation::N_URL
), pRegistration
->bReadOnly
);
202 if ( m_nOldCount
!= aRegistrations
.size() || m_bModified
)
204 rCoreSet
.Put(DatabaseMapItem( SID_SB_DB_REGISTER
, aRegistrations
), SID_SB_DB_REGISTER
);
213 void DbRegistrationOptionsPage::Reset( const SfxItemSet
& rSet
)
215 // the settings for the single drivers
216 SFX_ITEMSET_GET( rSet
, pRegistrations
, DatabaseMapItem
, SID_SB_DB_REGISTER
, true );
217 if ( !pRegistrations
)
222 const DatabaseRegistrations
& rRegistrations
= pRegistrations
->getRegistrations();
223 m_nOldCount
= rRegistrations
.size();
224 DatabaseRegistrations::const_iterator aIter
= rRegistrations
.begin();
225 DatabaseRegistrations::const_iterator aEnd
= rRegistrations
.end();
226 for ( ; aIter
!= aEnd
; ++aIter
)
228 OFileNotation
aTransformer( aIter
->second
.sLocation
);
229 insertNewEntry( aIter
->first
, aTransformer
.get( OFileNotation::N_SYSTEM
), aIter
->second
.bReadOnly
);
232 OUString aUserData
= GetUserData();
233 if ( !aUserData
.isEmpty() )
235 HeaderBar
&rBar
= pPathBox
->GetTheHeaderBar();
237 // restore column width
238 rBar
.SetItemSize( ITEMID_TYPE
, aUserData
.getToken(0, ';').toInt32() );
239 HeaderEndDrag_Impl( &rBar
);
240 // restore sort direction
241 bool bUp
= aUserData
.getToken(1, ';').toInt32() != 0;
242 HeaderBarItemBits nBits
= rBar
.GetItemBits(ITEMID_TYPE
);
246 nBits
&= ~HIB_UPARROW
;
247 nBits
|= HIB_DOWNARROW
;
251 nBits
&= ~HIB_DOWNARROW
;
252 nBits
|= HIB_UPARROW
;
254 rBar
.SetItemBits( ITEMID_TYPE
, nBits
);
255 HeaderSelect_Impl( &rBar
);
261 void DbRegistrationOptionsPage::FillUserData()
263 HeaderBar
&rBar
= pPathBox
->GetTheHeaderBar();
265 OUString aUserData
= OUString::number( rBar
.GetItemSize( ITEMID_TYPE
) ) + ";";
266 HeaderBarItemBits nBits
= rBar
.GetItemBits( ITEMID_TYPE
);
267 bool bUp
= ( ( nBits
& HIB_UPARROW
) == HIB_UPARROW
);
268 aUserData
+= (bUp
? OUString("1") : OUString("0"));
269 SetUserData( aUserData
);
273 IMPL_LINK_NOARG(DbRegistrationOptionsPage
, DeleteHdl
)
275 SvTreeListEntry
* pEntry
= pPathBox
->FirstSelected();
278 QueryBox
aQuery(this,CUI_RES(QUERY_DELETE_CONFIRM
));
279 if ( aQuery
.Execute() == RET_YES
)
280 pPathBox
->GetModel()->Remove(pEntry
);
285 IMPL_LINK_NOARG(DbRegistrationOptionsPage
, NewHdl
)
287 OUString sNewName
,sNewLocation
;
288 openLinkDialog(sNewName
,sNewLocation
);
294 IMPL_LINK_NOARG(DbRegistrationOptionsPage
, EditHdl
)
296 SvTreeListEntry
* pEntry
= pPathBox
->GetCurEntry();
300 DatabaseRegistration
* pOldRegistration
= static_cast< DatabaseRegistration
* >( pEntry
->GetUserData() );
301 if ( !pOldRegistration
|| pOldRegistration
->bReadOnly
)
304 OUString sOldName
= pPathBox
->GetEntryText(pEntry
,0);
305 m_pCurEntry
= pEntry
;
306 openLinkDialog( sOldName
, pOldRegistration
->sLocation
, pEntry
);
314 IMPL_LINK( DbRegistrationOptionsPage
, HeaderSelect_Impl
, HeaderBar
*, pBar
)
318 if (!pBar
|| pBar
->GetCurItemId() != ITEMID_TYPE
)
321 HeaderBarItemBits nBits
= pBar
->GetItemBits(ITEMID_TYPE
);
322 bool bUp
= ( ( nBits
& HIB_UPARROW
) == HIB_UPARROW
);
323 SvSortMode eMode
= SortAscending
;
327 nBits
&= ~HIB_UPARROW
;
328 nBits
|= HIB_DOWNARROW
;
329 eMode
= SortDescending
;
333 nBits
&= ~HIB_DOWNARROW
;
334 nBits
|= HIB_UPARROW
;
336 pBar
->SetItemBits( ITEMID_TYPE
, nBits
);
337 SvTreeList
* pModel
= pPathBox
->GetModel();
338 pModel
->SetSortMode( eMode
);
345 IMPL_LINK( DbRegistrationOptionsPage
, HeaderEndDrag_Impl
, HeaderBar
*, pBar
)
349 if (!pBar
|| !pBar
->GetCurItemId())
352 if ( !pBar
->IsItemMode() )
355 sal_uInt16 nTabs
= pBar
->GetItemCount();
357 long nWidth
= pBar
->GetItemSize(ITEMID_TYPE
);
358 long nBarWidth
= pBar
->GetSizePixel().Width();
360 if(nWidth
< TAB_WIDTH_MIN
)
361 pBar
->SetItemSize( ITEMID_TYPE
, TAB_WIDTH_MIN
);
362 else if ( ( nBarWidth
- nWidth
) < TAB_WIDTH_MIN
)
363 pBar
->SetItemSize( ITEMID_TYPE
, nBarWidth
- TAB_WIDTH_MIN
);
365 for ( sal_uInt16 i
= 1; i
<= nTabs
; ++i
)
367 long _nWidth
= pBar
->GetItemSize(i
);
368 aSz
.Width() = _nWidth
+ nTmpSz
;
370 pPathBox
->SetTab( i
, PixelToLogic( aSz
, MapMode(MAP_APPFONT
) ).Width(), MAP_APPFONT
);
377 IMPL_LINK_NOARG(DbRegistrationOptionsPage
, PathSelect_Impl
)
379 SvTreeListEntry
* pEntry
= pPathBox
->FirstSelected();
381 bool bReadOnly
= true;
384 DatabaseRegistration
* pRegistration
= static_cast< DatabaseRegistration
* >( pEntry
->GetUserData() );
385 bReadOnly
= pRegistration
->bReadOnly
;
388 m_pEdit
->Enable( !bReadOnly
);
389 m_pDelete
->Enable( !bReadOnly
);
393 void DbRegistrationOptionsPage::insertNewEntry( const OUString
& _sName
,const OUString
& _sLocation
, const bool _bReadOnly
)
395 OUString
aStr( _sName
);
399 SvTreeListEntry
* pEntry
= NULL
;
402 Image
aLocked( CUI_RES( RID_SVXBMP_LOCK
) );
403 pEntry
= pPathBox
->InsertEntry( aStr
, aLocked
, aLocked
);
407 pEntry
= pPathBox
->InsertEntry( aStr
);
410 pEntry
->SetUserData( new DatabaseRegistration( _sLocation
, _bReadOnly
) );
414 void DbRegistrationOptionsPage::openLinkDialog(const OUString
& _sOldName
,const OUString
& _sOldLocation
,SvTreeListEntry
* _pEntry
)
416 ODocumentLinkDialog
aDlg(this,_pEntry
== NULL
);
418 aDlg
.setLink(_sOldName
,_sOldLocation
);
419 aDlg
.setNameValidator(LINK( this, DbRegistrationOptionsPage
, NameValidator
) );
421 if ( aDlg
.Execute() == RET_OK
)
423 OUString sNewName
,sNewLocation
;
424 aDlg
.getLink(sNewName
,sNewLocation
);
425 if ( _pEntry
== NULL
|| sNewName
!= _sOldName
|| sNewLocation
!= _sOldLocation
)
429 delete static_cast< DatabaseRegistration
* >( _pEntry
->GetUserData() );
430 pPathBox
->GetModel()->Remove( _pEntry
);
432 insertNewEntry( sNewName
, sNewLocation
, false );
438 IMPL_LINK( DbRegistrationOptionsPage
, NameValidator
, OUString
*, _pName
)
442 sal_uLong nCount
= pPathBox
->GetEntryCount();
443 for ( sal_uLong i
= 0; i
< nCount
; ++i
)
445 SvTreeListEntry
* pEntry
= pPathBox
->GetEntry(i
);
446 if ( (!m_pCurEntry
|| m_pCurEntry
!= pEntry
) && pPathBox
->GetEntryText(pEntry
,0) == *_pName
)
457 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */