bump product version to 5.0.4.1
[LibreOffice.git] / cui / source / options / dbregister.cxx
blobdc58d2c2e034bc0ec03e27f50cad86947c2503c5
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 "dbregistersettings.hxx"
22 #include "connpooloptions.hxx"
23 #include <svl/filenotation.hxx>
24 #include "helpid.hrc"
25 #include <svtools/editbrowsebox.hxx>
26 #include "svtools/treelistentry.hxx"
27 #include <cuires.hrc>
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>
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
54 namespace svx
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( vcl::Window* pParent, const SfxItemSet& rInAttrs )
78 : RegistrationItemSetHolder(rInAttrs)
79 , SfxSingleTabDialog(pParent, getRegistrationItems())
81 VclPtr<SfxTabPage> page = DbRegistrationOptionsPage::Create(get_content_area(), &getRegistrationItems());
82 SetTabPage(page);
83 SetText(page->get<VclFrame>("frame1")->get_label());
86 short DatabaseRegistrationDialog::Execute()
88 short result = SfxSingleTabDialog::Execute();
89 if ( result == RET_OK )
91 DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
92 if ( GetOutputItemSet() )
93 DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
95 return result;
98 // class DbRegistrationOptionsPage --------------------------------------------------
100 DbRegistrationOptionsPage::DbRegistrationOptionsPage( vcl::Window* pParent, const SfxItemSet& rSet ) :
102 SfxTabPage( pParent, "DbRegisterPage", "cui/ui/dbregisterpage.ui", &rSet ),
104 aTypeText ( CUI_RES( RID_SVXSTR_TYPE ) ),
105 aPathText ( CUI_RES( RID_SVXSTR_PATH ) ),
106 pPathBox ( NULL ),
107 m_pCurEntry ( NULL ),
108 m_nOldCount ( 0 ),
109 m_bModified ( false )
111 get(m_pPathCtrl, "pathctrl");
112 Size aControlSize(248, 147);
113 aControlSize = LogicToPixel(aControlSize, MAP_APPFONT);
114 m_pPathCtrl->set_width_request(aControlSize.Width());
115 m_pPathCtrl->set_height_request(aControlSize.Height());
117 get(m_pNew, "new");
118 get(m_pEdit, "edit");
119 get(m_pDelete, "delete");
121 m_pNew->SetClickHdl( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
122 m_pEdit->SetClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
123 m_pDelete->SetClickHdl( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );
125 Size aBoxSize = m_pPathCtrl->GetOutputSizePixel();
127 WinBits nBits = WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP;
128 pPathBox = VclPtr<svx::OptHeaderTabListBox>::Create( *m_pPathCtrl, nBits );
130 HeaderBar &rBar = pPathBox->GetTheHeaderBar();
132 rBar.SetSelectHdl( LINK( this, DbRegistrationOptionsPage, HeaderSelect_Impl ) );
133 rBar.SetEndDragHdl( LINK( this, DbRegistrationOptionsPage, HeaderEndDrag_Impl ) );
134 Size aSz;
135 aSz.Width() = TAB_WIDTH1;
136 rBar.InsertItem( ITEMID_TYPE, aTypeText,
137 LogicToPixel( aSz, MapMode( MAP_APPFONT ) ).Width(),
138 HeaderBarItemBits::LEFT | HeaderBarItemBits::VCENTER | HeaderBarItemBits::CLICKABLE | HeaderBarItemBits::UPARROW );
139 aSz.Width() = TAB_WIDTH2;
140 rBar.InsertItem( ITEMID_PATH, aPathText,
141 LogicToPixel( aSz, MapMode( MAP_APPFONT ) ).Width(),
142 HeaderBarItemBits::LEFT | HeaderBarItemBits::VCENTER );
144 static long aTabs[] = {3, 0, TAB_WIDTH1, TAB_WIDTH1 + TAB_WIDTH2 };
145 Size aHeadSize = rBar.GetSizePixel();
147 pPathBox->SetStyle( pPathBox->GetStyle()|nBits );
148 pPathBox->SetDoubleClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
149 pPathBox->SetSelectHdl( LINK( this, DbRegistrationOptionsPage, PathSelect_Impl ) );
150 pPathBox->SetSelectionMode( SINGLE_SELECTION );
151 pPathBox->SetPosSizePixel( Point( 0, aHeadSize.Height() ),
152 Size( aBoxSize.Width(), aBoxSize.Height() - aHeadSize.Height() ) );
153 pPathBox->SvSimpleTable::SetTabs( aTabs, MAP_APPFONT );
154 pPathBox->SetHighlightRange();
156 pPathBox->SetHelpId( HID_DBPATH_CTL_PATH );
157 rBar.SetHelpId( HID_DBPATH_HEADERBAR );
159 pPathBox->ShowTable();
164 DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
166 disposeOnce();
169 void DbRegistrationOptionsPage::dispose()
171 for ( sal_uInt16 i = 0; i < pPathBox->GetEntryCount(); ++i )
172 delete static_cast< DatabaseRegistration* >( pPathBox->GetEntry(i)->GetUserData() );
173 pPathBox.disposeAndClear();
174 m_pPathCtrl.clear();
175 m_pNew.clear();
176 m_pEdit.clear();
177 m_pDelete.clear();
178 SfxTabPage::dispose();
183 VclPtr<SfxTabPage> DbRegistrationOptionsPage::Create( vcl::Window* pParent,
184 const SfxItemSet* rAttrSet )
186 return VclPtr<DbRegistrationOptionsPage>::Create( pParent, *rAttrSet );
191 bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet* rCoreSet )
193 // the settings for the single drivers
194 bool bModified = false;
195 DatabaseRegistrations aRegistrations;
196 sal_uLong nCount = pPathBox->GetEntryCount();
197 for ( sal_uLong i = 0; i < nCount; ++i )
199 SvTreeListEntry* pEntry = pPathBox->GetEntry(i);
200 DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
201 if ( pRegistration && !pRegistration->sLocation.isEmpty() )
203 OUString sName( SvTabListBox::GetEntryText( pEntry, 0 ) );
204 OFileNotation aTransformer( pRegistration->sLocation );
205 aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
208 if ( m_nOldCount != aRegistrations.size() || m_bModified )
210 rCoreSet->Put(DatabaseMapItem( SID_SB_DB_REGISTER, aRegistrations ), SID_SB_DB_REGISTER);
211 bModified = true;
214 return bModified;
219 void DbRegistrationOptionsPage::Reset( const SfxItemSet* rSet )
221 // the settings for the single drivers
222 SFX_ITEMSET_GET( *rSet, pRegistrations, DatabaseMapItem, SID_SB_DB_REGISTER, true );
223 if ( !pRegistrations )
224 return;
226 pPathBox->Clear();
228 const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
229 m_nOldCount = rRegistrations.size();
230 DatabaseRegistrations::const_iterator aIter = rRegistrations.begin();
231 DatabaseRegistrations::const_iterator aEnd = rRegistrations.end();
232 for ( ; aIter != aEnd; ++aIter )
234 OFileNotation aTransformer( aIter->second.sLocation );
235 insertNewEntry( aIter->first, aTransformer.get( OFileNotation::N_SYSTEM ), aIter->second.bReadOnly );
238 OUString aUserData = GetUserData();
239 if ( !aUserData.isEmpty() )
241 HeaderBar &rBar = pPathBox->GetTheHeaderBar();
243 // restore column width
244 rBar.SetItemSize( ITEMID_TYPE, aUserData.getToken(0, ';').toInt32() );
245 HeaderEndDrag_Impl( &rBar );
246 // restore sort direction
247 bool bUp = aUserData.getToken(1, ';').toInt32() != 0;
248 HeaderBarItemBits nBits = rBar.GetItemBits(ITEMID_TYPE);
250 if ( bUp )
252 nBits &= ~HeaderBarItemBits::UPARROW;
253 nBits |= HeaderBarItemBits::DOWNARROW;
255 else
257 nBits &= ~HeaderBarItemBits::DOWNARROW;
258 nBits |= HeaderBarItemBits::UPARROW;
260 rBar.SetItemBits( ITEMID_TYPE, nBits );
261 HeaderSelect_Impl( &rBar );
265 void DbRegistrationOptionsPage::FillUserData()
267 HeaderBar &rBar = pPathBox->GetTheHeaderBar();
269 OUString aUserData = OUString::number( rBar.GetItemSize( ITEMID_TYPE ) ) + ";";
270 HeaderBarItemBits nBits = rBar.GetItemBits( ITEMID_TYPE );
271 bool bUp = ( ( nBits & HeaderBarItemBits::UPARROW ) == HeaderBarItemBits::UPARROW );
272 aUserData += (bUp ? OUString("1") : OUString("0"));
273 SetUserData( aUserData );
276 IMPL_LINK_NOARG(DbRegistrationOptionsPage, DeleteHdl)
278 SvTreeListEntry* pEntry = pPathBox->FirstSelected();
279 if ( pEntry )
281 ScopedVclPtrInstance< MessageDialog > aQuery(this, CUI_RES(RID_SVXSTR_QUERY_DELETE_CONFIRM), VCL_MESSAGE_QUESTION, VCL_BUTTONS_YES_NO);
282 if ( aQuery->Execute() == RET_YES )
283 pPathBox->GetModel()->Remove(pEntry);
285 return 0;
288 IMPL_LINK_NOARG(DbRegistrationOptionsPage, NewHdl)
290 OUString sNewName,sNewLocation;
291 openLinkDialog(sNewName,sNewLocation);
292 return 0;
295 IMPL_LINK_NOARG(DbRegistrationOptionsPage, EditHdl)
297 SvTreeListEntry* pEntry = pPathBox->GetCurEntry();
298 if ( !pEntry )
299 return 0L;
301 DatabaseRegistration* pOldRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
302 if ( !pOldRegistration || pOldRegistration->bReadOnly )
303 return 0L;
305 OUString sOldName = SvTabListBox::GetEntryText(pEntry,0);
306 m_pCurEntry = pEntry;
307 openLinkDialog( sOldName, pOldRegistration->sLocation, pEntry );
308 m_pCurEntry = NULL;
310 return 1L;
315 IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, HeaderBar*, pBar )
317 assert(pBar);
319 if (!pBar || pBar->GetCurItemId() != ITEMID_TYPE)
320 return 0;
322 HeaderBarItemBits nBits = pBar->GetItemBits(ITEMID_TYPE);
323 bool bUp = ( ( nBits & HeaderBarItemBits::UPARROW ) == HeaderBarItemBits::UPARROW );
324 SvSortMode eMode = SortAscending;
326 if ( bUp )
328 nBits &= ~HeaderBarItemBits::UPARROW;
329 nBits |= HeaderBarItemBits::DOWNARROW;
330 eMode = SortDescending;
332 else
334 nBits &= ~HeaderBarItemBits::DOWNARROW;
335 nBits |= HeaderBarItemBits::UPARROW;
337 pBar->SetItemBits( ITEMID_TYPE, nBits );
338 SvTreeList* pModel = pPathBox->GetModel();
339 pModel->SetSortMode( eMode );
340 pModel->Resort();
341 return 1;
346 IMPL_LINK( DbRegistrationOptionsPage, HeaderEndDrag_Impl, HeaderBar*, pBar )
348 assert(pBar);
350 if (!pBar || !pBar->GetCurItemId())
351 return 0;
353 if ( !pBar->IsItemMode() )
355 Size aSz;
356 sal_uInt16 nTabs = pBar->GetItemCount();
357 long nTmpSz = 0;
358 long nWidth = pBar->GetItemSize(ITEMID_TYPE);
359 long nBarWidth = pBar->GetSizePixel().Width();
361 if(nWidth < TAB_WIDTH_MIN)
362 pBar->SetItemSize( ITEMID_TYPE, TAB_WIDTH_MIN);
363 else if ( ( nBarWidth - nWidth ) < TAB_WIDTH_MIN )
364 pBar->SetItemSize( ITEMID_TYPE, nBarWidth - TAB_WIDTH_MIN );
366 for ( sal_uInt16 i = 1; i <= nTabs; ++i )
368 long _nWidth = pBar->GetItemSize(i);
369 aSz.Width() = _nWidth + nTmpSz;
370 nTmpSz += _nWidth;
371 pPathBox->SetTab( i, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
374 return 1;
378 IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathSelect_Impl)
380 SvTreeListEntry* pEntry = pPathBox->FirstSelected();
382 bool bReadOnly = true;
383 if ( pEntry )
385 DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
386 bReadOnly = pRegistration->bReadOnly;
389 m_pEdit->Enable( !bReadOnly );
390 m_pDelete->Enable( !bReadOnly );
391 return 0;
394 void DbRegistrationOptionsPage::insertNewEntry( const OUString& _sName,const OUString& _sLocation, const bool _bReadOnly )
396 OUString aStr( _sName );
397 aStr += "\t";
398 aStr += _sLocation;
400 SvTreeListEntry* pEntry = NULL;
401 if ( _bReadOnly )
403 Image aLocked( CUI_RES( RID_SVXBMP_LOCK ) );
404 pEntry = pPathBox->InsertEntry( aStr, aLocked, aLocked );
406 else
408 pEntry = pPathBox->InsertEntry( aStr );
411 pEntry->SetUserData( new DatabaseRegistration( _sLocation, _bReadOnly ) );
415 void DbRegistrationOptionsPage::openLinkDialog(const OUString& _sOldName,const OUString& _sOldLocation,SvTreeListEntry* _pEntry)
417 ScopedVclPtrInstance< ODocumentLinkDialog > aDlg(this,_pEntry == nullptr);
419 aDlg->setLink(_sOldName,_sOldLocation);
420 aDlg->setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
422 if ( aDlg->Execute() == RET_OK )
424 OUString sNewName,sNewLocation;
425 aDlg->getLink(sNewName,sNewLocation);
426 if ( _pEntry == NULL || sNewName != _sOldName || sNewLocation != _sOldLocation )
428 if ( _pEntry )
430 delete static_cast< DatabaseRegistration* >( _pEntry->GetUserData() );
431 pPathBox->GetModel()->Remove( _pEntry );
433 insertNewEntry( sNewName, sNewLocation, false );
434 m_bModified = true;
439 IMPL_LINK( DbRegistrationOptionsPage, NameValidator, OUString*, _pName )
441 if ( _pName )
443 sal_uLong nCount = pPathBox->GetEntryCount();
444 for ( sal_uLong i = 0; i < nCount; ++i )
446 SvTreeListEntry* pEntry = pPathBox->GetEntry(i);
447 if ( (!m_pCurEntry || m_pCurEntry != pEntry) && SvTabListBox::GetEntryText(pEntry,0) == *_pName )
448 return 0L;
451 return 1L;
458 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */