update credits
[LibreOffice.git] / cui / source / options / dbregister.cxx
blob25d34e63161da085b03b5ef49cb8223503533f83
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "dbregister.hrc"
22 #include "dbregistersettings.hxx"
23 #include "connpooloptions.hxx"
24 #include <svl/filenotation.hxx>
25 #include "helpid.hrc"
26 #include <svtools/editbrowsebox.hxx>
27 #include "svtools/treelistentry.hxx"
28 #include <cuires.hrc>
29 #include <vcl/field.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>
48 #define TAB_WIDTH1 80
49 #define TAB_WIDTH_MIN 10
50 #define TAB_WIDTH2 1000
51 #define ITEMID_TYPE 1
52 #define ITEMID_PATH 2
53 //........................................................................
54 namespace svx
56 //........................................................................
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 ,SfxNoLayoutSingleTabDialog( pParent, getRegistrationItems(), RID_SFXPAGE_DBREGISTER )
81 SfxTabPage* page = DbRegistrationOptionsPage::Create( this, getRegistrationItems() );
83 SetTabPage( page );
84 SetText( page->GetText() );
87 DatabaseRegistrationDialog::~DatabaseRegistrationDialog()
91 short DatabaseRegistrationDialog::Execute()
93 short result = SfxNoLayoutSingleTabDialog::Execute();
94 if ( result == RET_OK )
96 DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
97 if ( GetOutputItemSet() )
98 DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
100 return result;
103 // class DbRegistrationOptionsPage --------------------------------------------------
105 DbRegistrationOptionsPage::DbRegistrationOptionsPage( Window* pParent, const SfxItemSet& rSet ) :
107 SfxTabPage( pParent, CUI_RES( RID_SFXPAGE_DBREGISTER), rSet ),
109 aStdBox ( this, CUI_RES( GB_STD ) ),
110 aTypeText ( this, CUI_RES( FT_TYPE ) ),
111 aPathText ( this, CUI_RES( FT_PATH ) ),
112 aPathCtrl ( this, CUI_RES( LB_PATH ) ),
113 m_aNew ( this, CUI_RES( BTN_NEW ) ),
114 m_aEdit ( this, CUI_RES( BTN_EDIT ) ),
115 m_aDelete ( this, CUI_RES( BTN_DELETE ) ),
116 pPathBox ( NULL ),
117 m_pCurEntry ( NULL ),
118 m_nOldCount ( 0 ),
119 m_bModified ( sal_False )
121 m_aNew.SetClickHdl( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
122 m_aEdit.SetClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
123 m_aDelete.SetClickHdl( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );
125 Size aBoxSize = aPathCtrl.GetOutputSizePixel();
128 WinBits nBits = WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP;
129 pPathBox = new ::svx::OptHeaderTabListBox( aPathCtrl, nBits );
131 HeaderBar &rBar = pPathBox->GetTheHeaderBar();
133 rBar.SetSelectHdl( LINK( this, DbRegistrationOptionsPage, HeaderSelect_Impl ) );
134 rBar.SetEndDragHdl( LINK( this, DbRegistrationOptionsPage, HeaderEndDrag_Impl ) );
135 Size aSz;
136 aSz.Width() = TAB_WIDTH1;
137 rBar.InsertItem( ITEMID_TYPE, aTypeText.GetText(),
138 LogicToPixel( aSz, MapMode( MAP_APPFONT ) ).Width(),
139 HIB_LEFT | HIB_VCENTER | HIB_CLICKABLE | HIB_UPARROW );
140 aSz.Width() = TAB_WIDTH2;
141 rBar.InsertItem( ITEMID_PATH, aPathText.GetText(),
142 LogicToPixel( aSz, MapMode( MAP_APPFONT ) ).Width(),
143 HIB_LEFT | HIB_VCENTER );
145 static long aTabs[] = {3, 0, TAB_WIDTH1, TAB_WIDTH1 + TAB_WIDTH2 };
146 Size aHeadSize = rBar.GetSizePixel();
148 pPathBox->SetStyle( pPathBox->GetStyle()|nBits );
149 pPathBox->SetDoubleClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
150 pPathBox->SetSelectHdl( LINK( this, DbRegistrationOptionsPage, PathSelect_Impl ) );
151 pPathBox->SetSelectionMode( SINGLE_SELECTION );
152 pPathBox->SetPosSizePixel( Point( 0, aHeadSize.Height() ),
153 Size( aBoxSize.Width(), aBoxSize.Height() - aHeadSize.Height() ) );
154 pPathBox->SvxSimpleTable::SetTabs( aTabs, MAP_APPFONT );
155 pPathBox->SetHighlightRange();
157 pPathBox->SetHelpId( HID_DBPATH_CTL_PATH );
158 rBar.SetHelpId( HID_DBPATH_HEADERBAR );
160 pPathBox->ShowTable();
162 FreeResource();
165 // -----------------------------------------------------------------------
167 DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
169 for ( sal_uInt16 i = 0; i < pPathBox->GetEntryCount(); ++i )
170 delete static_cast< DatabaseRegistration* >( pPathBox->GetEntry(i)->GetUserData() );
171 delete pPathBox;
174 // -----------------------------------------------------------------------
176 SfxTabPage* DbRegistrationOptionsPage::Create( Window* pParent,
177 const SfxItemSet& rAttrSet )
179 return ( new DbRegistrationOptionsPage( pParent, rAttrSet ) );
182 // -----------------------------------------------------------------------
184 sal_Bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet& rCoreSet )
186 // the settings for the single drivers
187 sal_Bool bModified = sal_False;
188 DatabaseRegistrations aRegistrations;
189 sal_uLong nCount = pPathBox->GetEntryCount();
190 for ( sal_uLong i = 0; i < nCount; ++i )
192 SvTreeListEntry* pEntry = pPathBox->GetEntry(i);
193 DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
194 if ( pRegistration && !pRegistration->sLocation.isEmpty() )
196 OUString sName( pPathBox->GetEntryText( pEntry, 0 ) );
197 OFileNotation aTransformer( pRegistration->sLocation );
198 aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
201 if ( m_nOldCount != aRegistrations.size() || m_bModified )
203 rCoreSet.Put(DatabaseMapItem( SID_SB_DB_REGISTER, aRegistrations ), SID_SB_DB_REGISTER);
204 bModified = sal_True;
207 return bModified;
210 // -----------------------------------------------------------------------
212 void DbRegistrationOptionsPage::Reset( const SfxItemSet& rSet )
214 // the settings for the single drivers
215 SFX_ITEMSET_GET( rSet, pRegistrations, DatabaseMapItem, SID_SB_DB_REGISTER, sal_True );
216 if ( !pRegistrations )
217 return;
219 pPathBox->Clear();
221 const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
222 m_nOldCount = rRegistrations.size();
223 DatabaseRegistrations::const_iterator aIter = rRegistrations.begin();
224 DatabaseRegistrations::const_iterator aEnd = rRegistrations.end();
225 for ( ; aIter != aEnd; ++aIter )
227 OFileNotation aTransformer( aIter->second.sLocation );
228 insertNewEntry( aIter->first, aTransformer.get( OFileNotation::N_SYSTEM ), aIter->second.bReadOnly );
231 String aUserData = GetUserData();
232 if ( aUserData.Len() )
234 HeaderBar &rBar = pPathBox->GetTheHeaderBar();
236 // restore column width
237 rBar.SetItemSize( ITEMID_TYPE, aUserData.GetToken(0).ToInt32() );
238 HeaderEndDrag_Impl( &rBar );
239 // restore sort direction
240 sal_Bool bUp = (sal_Bool)(sal_uInt16)aUserData.GetToken(1).ToInt32();
241 HeaderBarItemBits nBits = rBar.GetItemBits(ITEMID_TYPE);
243 if ( bUp )
245 nBits &= ~HIB_UPARROW;
246 nBits |= HIB_DOWNARROW;
248 else
250 nBits &= ~HIB_DOWNARROW;
251 nBits |= HIB_UPARROW;
253 rBar.SetItemBits( ITEMID_TYPE, nBits );
254 HeaderSelect_Impl( &rBar );
258 // -----------------------------------------------------------------------
260 void DbRegistrationOptionsPage::FillUserData()
262 HeaderBar &rBar = pPathBox->GetTheHeaderBar();
264 OUString aUserData = OUString::number( rBar.GetItemSize( ITEMID_TYPE ) ) + ";";
265 HeaderBarItemBits nBits = rBar.GetItemBits( ITEMID_TYPE );
266 sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
267 aUserData += (bUp ? OUString("1") : OUString("0"));
268 SetUserData( aUserData );
270 // -----------------------------------------------------------------------
272 IMPL_LINK_NOARG(DbRegistrationOptionsPage, DeleteHdl)
274 SvTreeListEntry* pEntry = pPathBox->FirstSelected();
275 if ( pEntry )
277 QueryBox aQuery(this,CUI_RES(QUERY_DELETE_CONFIRM));
278 if ( aQuery.Execute() == RET_YES )
279 pPathBox->GetModel()->Remove(pEntry);
281 return 0;
283 // -----------------------------------------------------------------------
284 IMPL_LINK_NOARG(DbRegistrationOptionsPage, NewHdl)
286 String sNewName,sNewLocation;
287 openLinkDialog(sNewName,sNewLocation);
288 return 0;
291 // -----------------------------------------------------------------------
293 IMPL_LINK_NOARG(DbRegistrationOptionsPage, EditHdl)
295 SvTreeListEntry* pEntry = pPathBox->GetCurEntry();
296 if ( !pEntry )
297 return 0L;
299 DatabaseRegistration* pOldRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
300 if ( !pOldRegistration || pOldRegistration->bReadOnly )
301 return 0L;
303 String sOldName = pPathBox->GetEntryText(pEntry,0);
304 m_pCurEntry = pEntry;
305 openLinkDialog( sOldName, pOldRegistration->sLocation, pEntry );
306 m_pCurEntry = NULL;
308 return 1L;
311 // -----------------------------------------------------------------------
313 IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, HeaderBar*, pBar )
315 assert(pBar);
317 if ( pBar && pBar->GetCurItemId() != ITEMID_TYPE )
318 return 0;
320 HeaderBarItemBits nBits = pBar->GetItemBits(ITEMID_TYPE);
321 sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
322 SvSortMode eMode = SortAscending;
324 if ( bUp )
326 nBits &= ~HIB_UPARROW;
327 nBits |= HIB_DOWNARROW;
328 eMode = SortDescending;
330 else
332 nBits &= ~HIB_DOWNARROW;
333 nBits |= HIB_UPARROW;
335 pBar->SetItemBits( ITEMID_TYPE, nBits );
336 SvTreeList* pModel = pPathBox->GetModel();
337 pModel->SetSortMode( eMode );
338 pModel->Resort();
339 return 1;
342 // -----------------------------------------------------------------------
344 IMPL_LINK( DbRegistrationOptionsPage, HeaderEndDrag_Impl, HeaderBar*, pBar )
346 assert(pBar);
348 if ( pBar && !pBar->GetCurItemId() )
349 return 0;
351 if ( !pBar->IsItemMode() )
353 Size aSz;
354 sal_uInt16 nTabs = pBar->GetItemCount();
355 long nTmpSz = 0;
356 long nWidth = pBar->GetItemSize(ITEMID_TYPE);
357 long nBarWidth = pBar->GetSizePixel().Width();
359 if(nWidth < TAB_WIDTH_MIN)
360 pBar->SetItemSize( ITEMID_TYPE, TAB_WIDTH_MIN);
361 else if ( ( nBarWidth - nWidth ) < TAB_WIDTH_MIN )
362 pBar->SetItemSize( ITEMID_TYPE, nBarWidth - TAB_WIDTH_MIN );
364 for ( sal_uInt16 i = 1; i <= nTabs; ++i )
366 long _nWidth = pBar->GetItemSize(i);
367 aSz.Width() = _nWidth + nTmpSz;
368 nTmpSz += _nWidth;
369 pPathBox->SetTab( i, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
372 return 1;
374 // -----------------------------------------------------------------------
376 IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathSelect_Impl)
378 SvTreeListEntry* pEntry = pPathBox->FirstSelected();
380 bool bReadOnly = true;
381 if ( pEntry )
383 DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
384 bReadOnly = pRegistration->bReadOnly;
387 m_aEdit.Enable( !bReadOnly );
388 m_aDelete.Enable( !bReadOnly );
389 return 0;
391 // -----------------------------------------------------------------------------
392 void DbRegistrationOptionsPage::insertNewEntry( const OUString& _sName,const OUString& _sLocation, const bool _bReadOnly )
394 String aStr( _sName );
395 aStr += '\t';
396 aStr += String(_sLocation);
398 SvTreeListEntry* pEntry = NULL;
399 if ( _bReadOnly )
401 Image aLocked( CUI_RES( RID_SVXBMP_LOCK ) );
402 pEntry = pPathBox->InsertEntry( aStr, aLocked, aLocked );
404 else
406 pEntry = pPathBox->InsertEntry( aStr );
409 pEntry->SetUserData( new DatabaseRegistration( _sLocation, _bReadOnly ) );
412 // -----------------------------------------------------------------------------
413 void DbRegistrationOptionsPage::openLinkDialog(const String& _sOldName,const String& _sOldLocation,SvTreeListEntry* _pEntry)
415 ODocumentLinkDialog aDlg(this,_pEntry == NULL);
417 aDlg.set(_sOldName,_sOldLocation);
418 aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
420 if ( aDlg.Execute() == RET_OK )
422 String sNewName,sNewLocation;
423 aDlg.get(sNewName,sNewLocation);
424 if ( _pEntry == NULL || sNewName != _sOldName || sNewLocation != _sOldLocation )
426 if ( _pEntry )
428 delete static_cast< DatabaseRegistration* >( _pEntry->GetUserData() );
429 pPathBox->GetModel()->Remove( _pEntry );
431 insertNewEntry( sNewName, sNewLocation, false );
432 m_bModified = sal_True;
436 // -----------------------------------------------------------------------------
437 IMPL_LINK( DbRegistrationOptionsPage, NameValidator, String*, _pName )
439 if ( _pName )
441 sal_uLong nCount = pPathBox->GetEntryCount();
442 for ( sal_uLong i = 0; i < nCount; ++i )
444 SvTreeListEntry* pEntry = pPathBox->GetEntry(i);
445 if ( (!m_pCurEntry || m_pCurEntry != pEntry) && pPathBox->GetEntryText(pEntry,0) == *_pName )
446 return 0L;
449 return 1L;
451 //........................................................................
452 } // namespace svx
453 //........................................................................
456 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */