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 : ,SfxSingleTabDialog( 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 = SfxSingleTabDialog::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 : pHeaderBar ( NULL ),
117 : pPathBox ( NULL ),
118 : m_pCurEntry ( NULL ),
119 : m_nOldCount ( 0 ),
120 0 : m_bModified ( sal_False )
121 : {
122 0 : m_aNew.SetClickHdl( LINK( this, DbRegistrationOptionsPage, NewHdl ) );
123 0 : m_aEdit.SetClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
124 0 : m_aDelete.SetClickHdl( LINK( this, DbRegistrationOptionsPage, DeleteHdl ) );
125 :
126 0 : Size aBoxSize = aPathCtrl.GetOutputSizePixel();
127 :
128 :
129 0 : WinBits nBits = WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP;
130 0 : pPathBox = new ::svx::OptHeaderTabListBox( &aPathCtrl, nBits );
131 :
132 0 : pHeaderBar = new HeaderBar( &aPathCtrl, WB_BUTTONSTYLE | WB_BOTTOMBORDER );
133 0 : pHeaderBar->SetPosSizePixel( Point( 0, 0 ), Size( aBoxSize.Width(), 16 ) );
134 0 : pHeaderBar->SetSelectHdl( LINK( this, DbRegistrationOptionsPage, HeaderSelect_Impl ) );
135 0 : pHeaderBar->SetEndDragHdl( LINK( this, DbRegistrationOptionsPage, HeaderEndDrag_Impl ) );
136 0 : Size aSz;
137 0 : aSz.Width() = TAB_WIDTH1;
138 : pHeaderBar->InsertItem( ITEMID_TYPE, aTypeText.GetText(),
139 0 : LogicToPixel( aSz, MapMode( MAP_APPFONT ) ).Width(),
140 0 : HIB_LEFT | HIB_VCENTER | HIB_CLICKABLE | HIB_UPARROW );
141 0 : aSz.Width() = TAB_WIDTH2;
142 : pHeaderBar->InsertItem( ITEMID_PATH, aPathText.GetText(),
143 0 : LogicToPixel( aSz, MapMode( MAP_APPFONT ) ).Width(),
144 0 : HIB_LEFT | HIB_VCENTER );
145 :
146 : static long nTabs[] = {3, 0, TAB_WIDTH1, TAB_WIDTH1 + TAB_WIDTH2 };
147 0 : Size aHeadSize = pHeaderBar->GetSizePixel();
148 :
149 0 : aPathCtrl.SetFocusControl( pPathBox );
150 0 : pPathBox->SetStyle( pPathBox->GetStyle()|nBits );
151 0 : pPathBox->SetDoubleClickHdl( LINK( this, DbRegistrationOptionsPage, EditHdl ) );
152 0 : pPathBox->SetSelectHdl( LINK( this, DbRegistrationOptionsPage, PathSelect_Impl ) );
153 0 : pPathBox->SetSelectionMode( SINGLE_SELECTION );
154 0 : pPathBox->SetPosSizePixel( Point( 0, aHeadSize.Height() ),
155 0 : Size( aBoxSize.Width(), aBoxSize.Height() - aHeadSize.Height() ) );
156 0 : pPathBox->SetTabs( &nTabs[0], MAP_APPFONT );
157 0 : pPathBox->InitHeaderBar( pHeaderBar );
158 0 : pPathBox->SetHighlightRange();
159 :
160 0 : pPathBox->SetHelpId( HID_DBPATH_CTL_PATH );
161 0 : pHeaderBar->SetHelpId( HID_DBPATH_HEADERBAR );
162 :
163 0 : pPathBox->Show();
164 0 : pHeaderBar->Show();
165 :
166 0 : FreeResource();
167 0 : }
168 :
169 : // -----------------------------------------------------------------------
170 :
171 0 : DbRegistrationOptionsPage::~DbRegistrationOptionsPage()
172 : {
173 : // #110603# do not grab focus to a destroyed window !!!
174 0 : aPathCtrl.SetFocusControl( NULL );
175 :
176 0 : pHeaderBar->Hide();
177 0 : for ( sal_uInt16 i = 0; i < pPathBox->GetEntryCount(); ++i )
178 0 : delete static_cast< DatabaseRegistration* >( pPathBox->GetEntry(i)->GetUserData() );
179 0 : delete pPathBox;
180 0 : delete pHeaderBar;
181 0 : }
182 :
183 : // -----------------------------------------------------------------------
184 :
185 0 : SfxTabPage* DbRegistrationOptionsPage::Create( Window* pParent,
186 : const SfxItemSet& rAttrSet )
187 : {
188 0 : return ( new DbRegistrationOptionsPage( pParent, rAttrSet ) );
189 : }
190 :
191 : // -----------------------------------------------------------------------
192 :
193 0 : sal_Bool DbRegistrationOptionsPage::FillItemSet( SfxItemSet& rCoreSet )
194 : {
195 : // the settings for the single drivers
196 0 : sal_Bool bModified = sal_False;
197 0 : DatabaseRegistrations aRegistrations;
198 0 : sal_uLong nCount = pPathBox->GetEntryCount();
199 0 : for ( sal_uLong i = 0; i < nCount; ++i )
200 : {
201 0 : SvTreeListEntry* pEntry = pPathBox->GetEntry(i);
202 0 : DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
203 0 : if ( pRegistration && !pRegistration->sLocation.isEmpty() )
204 : {
205 0 : ::rtl::OUString sName( pPathBox->GetEntryText( pEntry, 0 ) );
206 0 : OFileNotation aTransformer( pRegistration->sLocation );
207 0 : aRegistrations[ sName ] = DatabaseRegistration( aTransformer.get( OFileNotation::N_URL ), pRegistration->bReadOnly );
208 : }
209 : }
210 0 : if ( m_nOldCount != aRegistrations.size() || m_bModified )
211 : {
212 0 : rCoreSet.Put(DatabaseMapItem( SID_SB_DB_REGISTER, aRegistrations ), SID_SB_DB_REGISTER);
213 0 : bModified = sal_True;
214 : }
215 :
216 0 : return bModified;
217 : }
218 :
219 : // -----------------------------------------------------------------------
220 :
221 0 : void DbRegistrationOptionsPage::Reset( const SfxItemSet& rSet )
222 : {
223 : // the settings for the single drivers
224 0 : SFX_ITEMSET_GET( rSet, pRegistrations, DatabaseMapItem, SID_SB_DB_REGISTER, sal_True );
225 0 : if ( !pRegistrations )
226 0 : return;
227 :
228 0 : pPathBox->Clear();
229 :
230 0 : const DatabaseRegistrations& rRegistrations = pRegistrations->getRegistrations();
231 0 : m_nOldCount = rRegistrations.size();
232 0 : DatabaseRegistrations::const_iterator aIter = rRegistrations.begin();
233 0 : DatabaseRegistrations::const_iterator aEnd = rRegistrations.end();
234 0 : for ( ; aIter != aEnd; ++aIter )
235 : {
236 0 : OFileNotation aTransformer( aIter->second.sLocation );
237 0 : insertNewEntry( aIter->first, aTransformer.get( OFileNotation::N_SYSTEM ), aIter->second.bReadOnly );
238 0 : }
239 :
240 0 : String aUserData = GetUserData();
241 0 : if ( aUserData.Len() )
242 : {
243 : // restore column width
244 0 : pHeaderBar->SetItemSize( ITEMID_TYPE, aUserData.GetToken(0).ToInt32() );
245 0 : HeaderEndDrag_Impl( NULL );
246 : // restore sort direction
247 0 : sal_Bool bUp = (sal_Bool)(sal_uInt16)aUserData.GetToken(1).ToInt32();
248 0 : HeaderBarItemBits nBits = pHeaderBar->GetItemBits(ITEMID_TYPE);
249 :
250 0 : if ( bUp )
251 : {
252 0 : nBits &= ~HIB_UPARROW;
253 0 : nBits |= HIB_DOWNARROW;
254 : }
255 : else
256 : {
257 0 : nBits &= ~HIB_DOWNARROW;
258 0 : nBits |= HIB_UPARROW;
259 : }
260 0 : pHeaderBar->SetItemBits( ITEMID_TYPE, nBits );
261 0 : HeaderSelect_Impl( NULL );
262 0 : }
263 : }
264 :
265 : // -----------------------------------------------------------------------
266 :
267 0 : void DbRegistrationOptionsPage::FillUserData()
268 : {
269 0 : String aUserData = String::CreateFromInt32( pHeaderBar->GetItemSize( ITEMID_TYPE ) );
270 0 : aUserData += ';';
271 0 : HeaderBarItemBits nBits = pHeaderBar->GetItemBits( ITEMID_TYPE );
272 0 : sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
273 0 : aUserData += (bUp ? '1' : '0');
274 0 : SetUserData( aUserData );
275 0 : }
276 : // -----------------------------------------------------------------------
277 :
278 0 : IMPL_LINK_NOARG(DbRegistrationOptionsPage, DeleteHdl)
279 : {
280 0 : SvTreeListEntry* pEntry = pPathBox->FirstSelected();
281 0 : if ( pEntry )
282 : {
283 0 : QueryBox aQuery(this,CUI_RES(QUERY_DELETE_CONFIRM));
284 0 : if ( aQuery.Execute() == RET_YES )
285 0 : pPathBox->GetModel()->Remove(pEntry);
286 : }
287 0 : return 0;
288 : }
289 : // -----------------------------------------------------------------------
290 0 : IMPL_LINK_NOARG(DbRegistrationOptionsPage, NewHdl)
291 : {
292 0 : String sNewName,sNewLocation;
293 0 : openLinkDialog(sNewName,sNewLocation);
294 0 : return 0;
295 : }
296 :
297 : // -----------------------------------------------------------------------
298 :
299 0 : IMPL_LINK_NOARG(DbRegistrationOptionsPage, EditHdl)
300 : {
301 0 : SvTreeListEntry* pEntry = pPathBox->GetCurEntry();
302 0 : if ( !pEntry )
303 0 : return 0L;
304 :
305 0 : DatabaseRegistration* pOldRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
306 0 : if ( !pOldRegistration || pOldRegistration->bReadOnly )
307 0 : return 0L;
308 :
309 0 : String sOldName = pPathBox->GetEntryText(pEntry,0);
310 0 : m_pCurEntry = pEntry;
311 0 : openLinkDialog( sOldName, pOldRegistration->sLocation, pEntry );
312 0 : m_pCurEntry = NULL;
313 :
314 0 : return 1L;
315 : }
316 :
317 : // -----------------------------------------------------------------------
318 :
319 0 : IMPL_LINK( DbRegistrationOptionsPage, HeaderSelect_Impl, HeaderBar*, pBar )
320 : {
321 0 : if ( pBar && pBar->GetCurItemId() != ITEMID_TYPE )
322 0 : return 0;
323 :
324 0 : HeaderBarItemBits nBits = pHeaderBar->GetItemBits(ITEMID_TYPE);
325 0 : sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
326 0 : SvSortMode eMode = SortAscending;
327 :
328 0 : if ( bUp )
329 : {
330 0 : nBits &= ~HIB_UPARROW;
331 0 : nBits |= HIB_DOWNARROW;
332 0 : eMode = SortDescending;
333 : }
334 : else
335 : {
336 0 : nBits &= ~HIB_DOWNARROW;
337 0 : nBits |= HIB_UPARROW;
338 : }
339 0 : pHeaderBar->SetItemBits( ITEMID_TYPE, nBits );
340 0 : SvTreeList* pModel = pPathBox->GetModel();
341 0 : pModel->SetSortMode( eMode );
342 0 : pModel->Resort();
343 0 : return 1;
344 : }
345 :
346 : // -----------------------------------------------------------------------
347 :
348 0 : IMPL_LINK( DbRegistrationOptionsPage, HeaderEndDrag_Impl, HeaderBar*, pBar )
349 : {
350 0 : if ( pBar && !pBar->GetCurItemId() )
351 0 : return 0;
352 :
353 0 : if ( !pHeaderBar->IsItemMode() )
354 : {
355 0 : Size aSz;
356 0 : sal_uInt16 nTabs = pHeaderBar->GetItemCount();
357 0 : long nTmpSz = 0;
358 0 : long nWidth = pHeaderBar->GetItemSize(ITEMID_TYPE);
359 0 : long nBarWidth = pHeaderBar->GetSizePixel().Width();
360 :
361 0 : if(nWidth < TAB_WIDTH_MIN)
362 0 : pHeaderBar->SetItemSize( ITEMID_TYPE, TAB_WIDTH_MIN);
363 0 : else if ( ( nBarWidth - nWidth ) < TAB_WIDTH_MIN )
364 0 : pHeaderBar->SetItemSize( ITEMID_TYPE, nBarWidth - TAB_WIDTH_MIN );
365 :
366 0 : for ( sal_uInt16 i = 1; i <= nTabs; ++i )
367 : {
368 0 : long _nWidth = pHeaderBar->GetItemSize(i);
369 0 : aSz.Width() = _nWidth + nTmpSz;
370 0 : nTmpSz += _nWidth;
371 0 : pPathBox->SetTab( i, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
372 : }
373 : }
374 0 : return 1;
375 : }
376 : // -----------------------------------------------------------------------
377 :
378 0 : IMPL_LINK_NOARG(DbRegistrationOptionsPage, PathSelect_Impl)
379 : {
380 0 : SvTreeListEntry* pEntry = pPathBox->FirstSelected();
381 :
382 0 : bool bReadOnly = true;
383 0 : if ( pEntry )
384 : {
385 0 : DatabaseRegistration* pRegistration = static_cast< DatabaseRegistration* >( pEntry->GetUserData() );
386 0 : bReadOnly = pRegistration->bReadOnly;
387 : }
388 :
389 0 : m_aEdit.Enable( !bReadOnly );
390 0 : m_aDelete.Enable( !bReadOnly );
391 0 : return 0;
392 : }
393 : // -----------------------------------------------------------------------------
394 0 : void DbRegistrationOptionsPage::insertNewEntry( const ::rtl::OUString& _sName,const ::rtl::OUString& _sLocation, const bool _bReadOnly )
395 : {
396 0 : String aStr( _sName );
397 0 : aStr += '\t';
398 0 : aStr += String(_sLocation);
399 :
400 0 : SvTreeListEntry* pEntry = NULL;
401 0 : if ( _bReadOnly )
402 : {
403 0 : Image aLocked( CUI_RES( RID_SVXBMP_LOCK ) );
404 0 : pEntry = pPathBox->InsertEntry( aStr, aLocked, aLocked );
405 : }
406 : else
407 : {
408 0 : pEntry = pPathBox->InsertEntry( aStr );
409 : }
410 :
411 0 : pEntry->SetUserData( new DatabaseRegistration( _sLocation, _bReadOnly ) );
412 0 : }
413 :
414 : // -----------------------------------------------------------------------------
415 0 : void DbRegistrationOptionsPage::openLinkDialog(const String& _sOldName,const String& _sOldLocation,SvTreeListEntry* _pEntry)
416 : {
417 0 : ODocumentLinkDialog aDlg(this,_pEntry == NULL);
418 :
419 0 : aDlg.set(_sOldName,_sOldLocation);
420 0 : aDlg.setNameValidator(LINK( this, DbRegistrationOptionsPage, NameValidator ) );
421 :
422 0 : if ( aDlg.Execute() == RET_OK )
423 : {
424 0 : String sNewName,sNewLocation;
425 0 : aDlg.get(sNewName,sNewLocation);
426 0 : if ( _pEntry == NULL || sNewName != _sOldName || sNewLocation != _sOldLocation )
427 : {
428 0 : if ( _pEntry )
429 : {
430 0 : delete static_cast< DatabaseRegistration* >( _pEntry->GetUserData() );
431 0 : pPathBox->GetModel()->Remove( _pEntry );
432 : }
433 0 : insertNewEntry( sNewName, sNewLocation, false );
434 0 : m_bModified = sal_True;
435 0 : }
436 0 : }
437 0 : }
438 : // -----------------------------------------------------------------------------
439 0 : IMPL_LINK( DbRegistrationOptionsPage, NameValidator, String*, _pName )
440 : {
441 0 : if ( _pName )
442 : {
443 0 : sal_uLong nCount = pPathBox->GetEntryCount();
444 0 : for ( sal_uLong i = 0; i < nCount; ++i )
445 : {
446 0 : SvTreeListEntry* pEntry = pPathBox->GetEntry(i);
447 0 : if ( (!m_pCurEntry || m_pCurEntry != pEntry) && pPathBox->GetEntryText(pEntry,0) == *_pName )
448 0 : return 0L;
449 : }
450 : }
451 0 : return 1L;
452 : }
453 : //........................................................................
454 3 : } // namespace svx
455 : //........................................................................
456 :
457 :
458 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|