Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
47 :
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
55 : {
56 : //........................................................................
57 :
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;
62 :
63 : // class RegistrationItemSetHolder -------------------------------------------------
64 :
65 0 : RegistrationItemSetHolder::RegistrationItemSetHolder( const SfxItemSet& _rMasterSet )
66 0 : :m_aRegistrationItems( _rMasterSet )
67 : {
68 0 : DbRegisteredNamesConfig::GetOptions( m_aRegistrationItems );
69 0 : }
70 :
71 0 : RegistrationItemSetHolder::~RegistrationItemSetHolder()
72 : {
73 0 : }
74 :
75 : // class DatabaseRegistrationDialog ------------------------------------------------
76 :
77 0 : DatabaseRegistrationDialog::DatabaseRegistrationDialog( Window* pParent, const SfxItemSet& rInAttrs )
78 : :RegistrationItemSetHolder( rInAttrs )
79 0 : ,SfxNoLayoutSingleTabDialog( pParent, getRegistrationItems(), RID_SFXPAGE_DBREGISTER )
80 : {
81 0 : SfxTabPage* page = DbRegistrationOptionsPage::Create( this, getRegistrationItems() );
82 :
83 0 : SetTabPage( page );
84 0 : SetText( page->GetText() );
85 0 : }
86 :
87 0 : DatabaseRegistrationDialog::~DatabaseRegistrationDialog()
88 : {
89 0 : }
90 :
91 0 : short DatabaseRegistrationDialog::Execute()
92 : {
93 0 : short result = SfxNoLayoutSingleTabDialog::Execute();
94 0 : if ( result == RET_OK )
95 : {
96 : DBG_ASSERT( GetOutputItemSet(), "DatabaseRegistrationDialog::Execute: no output items!" );
97 0 : if ( GetOutputItemSet() )
98 0 : DbRegisteredNamesConfig::SetOptions( *GetOutputItemSet() );
99 : }
100 0 : return result;
101 : }
102 :
103 : // class DbRegistrationOptionsPage --------------------------------------------------
104 :
105 0 : DbRegistrationOptionsPage::DbRegistrationOptionsPage( Window* pParent, const SfxItemSet& rSet ) :
106 :
107 0 : SfxTabPage( pParent, CUI_RES( RID_SFXPAGE_DBREGISTER), rSet ),
108 :
109 0 : aStdBox ( this, CUI_RES( GB_STD ) ),
110 0 : aTypeText ( this, CUI_RES( FT_TYPE ) ),
111 0 : aPathText ( this, CUI_RES( FT_PATH ) ),
112 0 : aPathCtrl ( this, CUI_RES( LB_PATH ) ),
113 0 : m_aNew ( this, CUI_RES( BTN_NEW ) ),
114 0 : m_aEdit ( this, CUI_RES( BTN_EDIT ) ),
115 0 : m_aDelete ( this, CUI_RES( BTN_DELETE ) ),
116 : pPathBox ( NULL ),
117 : m_pCurEntry ( NULL ),
118 : m_nOldCount ( 0 ),
119 0 : m_bModified ( sal_False )
120 : {
121 0 : m_aNew.SetClickHdl( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
122 0 : m_aEdit.SetClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
123 0 : m_aDelete.SetClickHdl( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );
124 :
125 0 : Size aBoxSize = aPathCtrl.GetOutputSizePixel();
126 :
127 :
128 0 : WinBits nBits = WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP;
129 0 : pPathBox = new ::svx::OptHeaderTabListBox( aPathCtrl, nBits );
130 :
131 0 : HeaderBar &rBar = pPathBox->GetTheHeaderBar();
132 :
133 0 : rBar.SetSelectHdl( LINK( this, DbRegistrationOptionsPage, HeaderSelect_Impl ) );
134 0 : rBar.SetEndDragHdl( LINK( this, DbRegistrationOptionsPage, HeaderEndDrag_Impl ) );
135 0 : Size aSz;
136 0 : aSz.Width() = TAB_WIDTH1;
137 0 : rBar.InsertItem( ITEMID_TYPE, aTypeText.GetText(),
138 0 : LogicToPixel( aSz, MapMode( MAP_APPFONT ) ).Width(),
139 0 : HIB_LEFT | HIB_VCENTER | HIB_CLICKABLE | HIB_UPARROW );
140 0 : aSz.Width() = TAB_WIDTH2;
141 0 : rBar.InsertItem( ITEMID_PATH, aPathText.GetText(),
142 0 : LogicToPixel( aSz, MapMode( MAP_APPFONT ) ).Width(),
143 0 : HIB_LEFT | HIB_VCENTER );
144 :
145 : static long aTabs[] = {3, 0, TAB_WIDTH1, TAB_WIDTH1 + TAB_WIDTH2 };
146 0 : Size aHeadSize = rBar.GetSizePixel();
147 :
148 0 : pPathBox->SetStyle( pPathBox->GetStyle()|nBits );
149 0 : pPathBox->SetDoubleClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
150 0 : pPathBox->SetSelectHdl( LINK( this, DbRegistrationOptionsPage, PathSelect_Impl ) );
151 0 : pPathBox->SetSelectionMode( SINGLE_SELECTION );
152 0 : pPathBox->SetPosSizePixel( Point( 0, aHeadSize.Height() ),
153 0 : Size( aBoxSize.Width(), aBoxSize.Height() - aHeadSize.Height() ) );
154 0 : pPathBox->SvxSimpleTable::SetTabs( aTabs, MAP_APPFONT );
155 0 : pPathBox->SetHighlightRange();
156 :
157 0 : pPathBox->SetHelpId( HID_DBPATH_CTL_PATH );
158 0 : rBar.SetHelpId( HID_DBPATH_HEADERBAR );
159 :
160 0 : pPathBox->ShowTable();
161 :
162 0 : FreeResource();
163 0 : }
164 :
165 : // -----------------------------------------------------------------------
166 :
167 0 : DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
168 : {
169 0 : for ( sal_uInt16 i = 0; i < pPathBox->GetEntryCount(); ++i )
170 0 : delete static_cast< DatabaseRegistration* >( pPathBox->GetEntry(i)->GetUserData() );
171 0 : delete pPathBox;
172 0 : }
173 :
174 : // -----------------------------------------------------------------------
175 :
176 0 : SfxTabPage* DbRegistrationOptionsPage::Create( Window* pParent,
177 : const SfxItemSet& rAttrSet )
178 : {
179 0 : return ( new DbRegistrationOptionsPage( pParent, rAttrSet ) );
180 : }
181 :
182 : // -----------------------------------------------------------------------
183 :
184 0 : sal_Bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet& rCoreSet )
185 : {
186 : // the settings for the single drivers
187 0 : sal_Bool bModified = sal_False;
188 0 : DatabaseRegistrations aRegistrations;
189 0 : sal_uLong nCount = pPathBox->GetEntryCount();
190 0 : for ( sal_uLong i = 0; i < nCount; ++i )
191 : {
192 0 : SvTreeListEntry* pEntry = pPathBox->GetEntry(i);
193 0 : DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
194 0 : if ( pRegistration && !pRegistration->sLocation.isEmpty() )
195 : {
196 0 : OUString sName( pPathBox->GetEntryText( pEntry, 0 ) );
197 0 : OFileNotation aTransformer( pRegistration->sLocation );
198 0 : aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
199 : }
200 : }
201 0 : if ( m_nOldCount != aRegistrations.size() || m_bModified )
202 : {
203 0 : rCoreSet.Put(DatabaseMapItem( SID_SB_DB_REGISTER, aRegistrations ), SID_SB_DB_REGISTER);
204 0 : bModified = sal_True;
205 : }
206 :
207 0 : return bModified;
208 : }
209 :
210 : // -----------------------------------------------------------------------
211 :
212 0 : void DbRegistrationOptionsPage::Reset( const SfxItemSet& rSet )
213 : {
214 : // the settings for the single drivers
215 0 : SFX_ITEMSET_GET( rSet, pRegistrations, DatabaseMapItem, SID_SB_DB_REGISTER, sal_True );
216 0 : if ( !pRegistrations )
217 0 : return;
218 :
219 0 : pPathBox->Clear();
220 :
221 0 : const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
222 0 : m_nOldCount = rRegistrations.size();
223 0 : DatabaseRegistrations::const_iterator aIter = rRegistrations.begin();
224 0 : DatabaseRegistrations::const_iterator aEnd = rRegistrations.end();
225 0 : for ( ; aIter != aEnd; ++aIter )
226 : {
227 0 : OFileNotation aTransformer( aIter->second.sLocation );
228 0 : insertNewEntry( aIter->first, aTransformer.get( OFileNotation::N_SYSTEM ), aIter->second.bReadOnly );
229 0 : }
230 :
231 0 : String aUserData = GetUserData();
232 0 : if ( aUserData.Len() )
233 : {
234 0 : HeaderBar &rBar = pPathBox->GetTheHeaderBar();
235 :
236 : // restore column width
237 0 : rBar.SetItemSize( ITEMID_TYPE, aUserData.GetToken(0).ToInt32() );
238 0 : HeaderEndDrag_Impl( &rBar );
239 : // restore sort direction
240 0 : sal_Bool bUp = (sal_Bool)(sal_uInt16)aUserData.GetToken(1).ToInt32();
241 0 : HeaderBarItemBits nBits = rBar.GetItemBits(ITEMID_TYPE);
242 :
243 0 : if ( bUp )
244 : {
245 0 : nBits &= ~HIB_UPARROW;
246 0 : nBits |= HIB_DOWNARROW;
247 : }
248 : else
249 : {
250 0 : nBits &= ~HIB_DOWNARROW;
251 0 : nBits |= HIB_UPARROW;
252 : }
253 0 : rBar.SetItemBits( ITEMID_TYPE, nBits );
254 0 : HeaderSelect_Impl( &rBar );
255 0 : }
256 : }
257 :
258 : // -----------------------------------------------------------------------
259 :
260 0 : void DbRegistrationOptionsPage::FillUserData()
261 : {
262 0 : HeaderBar &rBar = pPathBox->GetTheHeaderBar();
263 :
264 0 : OUString aUserData = OUString::number( rBar.GetItemSize( ITEMID_TYPE ) ) + ";";
265 0 : HeaderBarItemBits nBits = rBar.GetItemBits( ITEMID_TYPE );
266 0 : sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
267 0 : aUserData += (bUp ? OUString("1") : OUString("0"));
268 0 : SetUserData( aUserData );
269 0 : }
270 : // -----------------------------------------------------------------------
271 :
272 0 : IMPL_LINK_NOARG(DbRegistrationOptionsPage, DeleteHdl)
273 : {
274 0 : SvTreeListEntry* pEntry = pPathBox->FirstSelected();
275 0 : if ( pEntry )
276 : {
277 0 : QueryBox aQuery(this,CUI_RES(QUERY_DELETE_CONFIRM));
278 0 : if ( aQuery.Execute() == RET_YES )
279 0 : pPathBox->GetModel()->Remove(pEntry);
280 : }
281 0 : return 0;
282 : }
283 : // -----------------------------------------------------------------------
284 0 : IMPL_LINK_NOARG(DbRegistrationOptionsPage, NewHdl)
285 : {
286 0 : String sNewName,sNewLocation;
287 0 : openLinkDialog(sNewName,sNewLocation);
288 0 : return 0;
289 : }
290 :
291 : // -----------------------------------------------------------------------
292 :
293 0 : IMPL_LINK_NOARG(DbRegistrationOptionsPage, EditHdl)
294 : {
295 0 : SvTreeListEntry* pEntry = pPathBox->GetCurEntry();
296 0 : if ( !pEntry )
297 0 : return 0L;
298 :
299 0 : DatabaseRegistration* pOldRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
300 0 : if ( !pOldRegistration || pOldRegistration->bReadOnly )
301 0 : return 0L;
302 :
303 0 : String sOldName = pPathBox->GetEntryText(pEntry,0);
304 0 : m_pCurEntry = pEntry;
305 0 : openLinkDialog( sOldName, pOldRegistration->sLocation, pEntry );
306 0 : m_pCurEntry = NULL;
307 :
308 0 : return 1L;
309 : }
310 :
311 : // -----------------------------------------------------------------------
312 :
313 0 : IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, HeaderBar*, pBar )
314 : {
315 : assert(pBar);
316 :
317 0 : if ( pBar && pBar->GetCurItemId() != ITEMID_TYPE )
318 0 : return 0;
319 :
320 0 : HeaderBarItemBits nBits = pBar->GetItemBits(ITEMID_TYPE);
321 0 : sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
322 0 : SvSortMode eMode = SortAscending;
323 :
324 0 : if ( bUp )
325 : {
326 0 : nBits &= ~HIB_UPARROW;
327 0 : nBits |= HIB_DOWNARROW;
328 0 : eMode = SortDescending;
329 : }
330 : else
331 : {
332 0 : nBits &= ~HIB_DOWNARROW;
333 0 : nBits |= HIB_UPARROW;
334 : }
335 0 : pBar->SetItemBits( ITEMID_TYPE, nBits );
336 0 : SvTreeList* pModel = pPathBox->GetModel();
337 0 : pModel->SetSortMode( eMode );
338 0 : pModel->Resort();
339 0 : return 1;
340 : }
341 :
342 : // -----------------------------------------------------------------------
343 :
344 0 : IMPL_LINK( DbRegistrationOptionsPage, HeaderEndDrag_Impl, HeaderBar*, pBar )
345 : {
346 : assert(pBar);
347 :
348 0 : if ( pBar && !pBar->GetCurItemId() )
349 0 : return 0;
350 :
351 0 : if ( !pBar->IsItemMode() )
352 : {
353 0 : Size aSz;
354 0 : sal_uInt16 nTabs = pBar->GetItemCount();
355 0 : long nTmpSz = 0;
356 0 : long nWidth = pBar->GetItemSize(ITEMID_TYPE);
357 0 : long nBarWidth = pBar->GetSizePixel().Width();
358 :
359 0 : if(nWidth < TAB_WIDTH_MIN)
360 0 : pBar->SetItemSize( ITEMID_TYPE, TAB_WIDTH_MIN);
361 0 : else if ( ( nBarWidth - nWidth ) < TAB_WIDTH_MIN )
362 0 : pBar->SetItemSize( ITEMID_TYPE, nBarWidth - TAB_WIDTH_MIN );
363 :
364 0 : for ( sal_uInt16 i = 1; i <= nTabs; ++i )
365 : {
366 0 : long _nWidth = pBar->GetItemSize(i);
367 0 : aSz.Width() = _nWidth + nTmpSz;
368 0 : nTmpSz += _nWidth;
369 0 : pPathBox->SetTab( i, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
370 : }
371 : }
372 0 : return 1;
373 : }
374 : // -----------------------------------------------------------------------
375 :
376 0 : IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathSelect_Impl)
377 : {
378 0 : SvTreeListEntry* pEntry = pPathBox->FirstSelected();
379 :
380 0 : bool bReadOnly = true;
381 0 : if ( pEntry )
382 : {
383 0 : DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
384 0 : bReadOnly = pRegistration->bReadOnly;
385 : }
386 :
387 0 : m_aEdit.Enable( !bReadOnly );
388 0 : m_aDelete.Enable( !bReadOnly );
389 0 : return 0;
390 : }
391 : // -----------------------------------------------------------------------------
392 0 : void DbRegistrationOptionsPage::insertNewEntry( const OUString& _sName,const OUString& _sLocation, const bool _bReadOnly )
393 : {
394 0 : String aStr( _sName );
395 0 : aStr += '\t';
396 0 : aStr += String(_sLocation);
397 :
398 0 : SvTreeListEntry* pEntry = NULL;
399 0 : if ( _bReadOnly )
400 : {
401 0 : Image aLocked( CUI_RES( RID_SVXBMP_LOCK ) );
402 0 : pEntry = pPathBox->InsertEntry( aStr, aLocked, aLocked );
403 : }
404 : else
405 : {
406 0 : pEntry = pPathBox->InsertEntry( aStr );
407 : }
408 :
409 0 : pEntry->SetUserData( new DatabaseRegistration( _sLocation, _bReadOnly ) );
410 0 : }
411 :
412 : // -----------------------------------------------------------------------------
413 0 : void DbRegistrationOptionsPage::openLinkDialog(const String& _sOldName,const String& _sOldLocation,SvTreeListEntry* _pEntry)
414 : {
415 0 : ODocumentLinkDialog aDlg(this,_pEntry == NULL);
416 :
417 0 : aDlg.set(_sOldName,_sOldLocation);
418 0 : aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
419 :
420 0 : if ( aDlg.Execute() == RET_OK )
421 : {
422 0 : String sNewName,sNewLocation;
423 0 : aDlg.get(sNewName,sNewLocation);
424 0 : if ( _pEntry == NULL || sNewName != _sOldName || sNewLocation != _sOldLocation )
425 : {
426 0 : if ( _pEntry )
427 : {
428 0 : delete static_cast< DatabaseRegistration* >( _pEntry->GetUserData() );
429 0 : pPathBox->GetModel()->Remove( _pEntry );
430 : }
431 0 : insertNewEntry( sNewName, sNewLocation, false );
432 0 : m_bModified = sal_True;
433 0 : }
434 0 : }
435 0 : }
436 : // -----------------------------------------------------------------------------
437 0 : IMPL_LINK( DbRegistrationOptionsPage, NameValidator, String*, _pName )
438 : {
439 0 : if ( _pName )
440 : {
441 0 : sal_uLong nCount = pPathBox->GetEntryCount();
442 0 : for ( sal_uLong i = 0; i < nCount; ++i )
443 : {
444 0 : SvTreeListEntry* pEntry = pPathBox->GetEntry(i);
445 0 : if ( (!m_pCurEntry || m_pCurEntry != pEntry) && pPathBox->GetEntryText(pEntry,0) == *_pName )
446 0 : return 0L;
447 : }
448 : }
449 0 : return 1L;
450 : }
451 : //........................................................................
452 0 : } // namespace svx
453 : //........................................................................
454 :
455 :
456 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|