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 .
21 #include "svx/databaselocationinput.hxx"
22 #include "svx/dialmgr.hxx"
24 #include "svx/fmresids.hrc"
26 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
28 #include <comphelper/namedvaluecollection.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <sfx2/filedlghelper.hxx>
31 #include <svtools/urlcontrol.hxx>
32 #include <svl/filenotation.hxx>
33 #include <tools/diagnose_ex.h>
34 #include <unotools/confignode.hxx>
35 #include <unotools/ucbhelper.hxx>
36 #include <vcl/button.hxx>
37 #include <vcl/msgbox.hxx>
44 using ::com::sun::star::uno::Sequence
;
45 using ::com::sun::star::uno::Reference
;
46 using ::com::sun::star::uno::XComponentContext
;
47 using ::com::sun::star::container::XNameAccess
;
48 using ::com::sun::star::uno::UNO_QUERY_THROW
;
49 using ::com::sun::star::uno::Exception
;
51 namespace TemplateDescription
= ::com::sun::star::ui::dialogs::TemplateDescription
;
53 class DatabaseLocationInputController_Impl
56 DatabaseLocationInputController_Impl(
57 const Reference
<XComponentContext
>& _rContext
,
58 ::svt::OFileURLControl
& _rLocationInput
,
59 PushButton
& _rBrowseButton
61 ~DatabaseLocationInputController_Impl();
64 void setURL( const OUString
& _rURL
);
65 OUString
getURL() const;
68 void impl_initFilterProperties_nothrow();
69 void impl_onBrowseButtonClicked();
70 void impl_onLocationModified();
71 OUString
impl_getCurrentURL() const;
73 DECL_LINK( OnControlAction
, VclWindowEvent
* );
76 const Reference
<XComponentContext
> m_xContext
;
77 ::svt::OFileURLControl
& m_rLocationInput
;
78 PushButton
& m_rBrowseButton
;
79 Sequence
< OUString
> m_aFilterExtensions
;
80 OUString m_sFilterUIName
;
81 bool m_bNeedExistenceCheck
;
85 DatabaseLocationInputController_Impl::DatabaseLocationInputController_Impl( const Reference
<XComponentContext
>& _rContext
,
86 ::svt::OFileURLControl
& _rLocationInput
, PushButton
& _rBrowseButton
)
87 :m_xContext( _rContext
)
88 ,m_rLocationInput( _rLocationInput
)
89 ,m_rBrowseButton( _rBrowseButton
)
90 ,m_aFilterExtensions()
92 ,m_bNeedExistenceCheck( true )
94 impl_initFilterProperties_nothrow();
96 // forward the allowed extensions to the input control
97 OUStringBuffer aExtensionList
;
98 for ( const OUString
* pExtension
= m_aFilterExtensions
.getConstArray();
99 pExtension
!= m_aFilterExtensions
.getConstArray() + m_aFilterExtensions
.getLength();
103 aExtensionList
.append( *pExtension
);
104 aExtensionList
.append( ';' );
106 m_rLocationInput
.SetFilter( aExtensionList
.makeStringAndClear() );
108 m_rBrowseButton
.AddEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
109 m_rLocationInput
.AddEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
113 DatabaseLocationInputController_Impl::~DatabaseLocationInputController_Impl()
115 m_rBrowseButton
.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
116 m_rLocationInput
.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
120 bool DatabaseLocationInputController_Impl::prepareCommit()
122 OUString
sURL( impl_getCurrentURL() );
123 if ( sURL
.isEmpty() )
126 // check if the name exists
127 if ( m_bNeedExistenceCheck
)
129 if ( ::utl::UCBContentHelper::Exists( sURL
) )
131 ScopedVclPtrInstance
< QueryBox
> aBox( m_rLocationInput
.GetSystemWindow(), WB_YES_NO
, SVX_RESSTR(RID_STR_ALREADYEXISTOVERWRITE
) );
132 if ( aBox
->Execute() != RET_YES
)
141 void DatabaseLocationInputController_Impl::setURL( const OUString
& _rURL
)
143 ::svt::OFileNotation
aTransformer( _rURL
);
144 m_rLocationInput
.SetText( aTransformer
.get( ::svt::OFileNotation::N_SYSTEM
) );
148 OUString
DatabaseLocationInputController_Impl::getURL() const
150 return impl_getCurrentURL();
154 void DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow()
158 // get the name of the default filter for database documents
159 ::utl::OConfigurationTreeRoot
aConfig(
160 ::utl::OConfigurationTreeRoot::createWithComponentContext(
162 OUString( "/org.openoffice.Setup/Office/Factories/com.sun.star.sdb.OfficeDatabaseDocument" )
164 OUString sDatabaseFilter
;
165 OSL_VERIFY( aConfig
.getNodeValue( "ooSetupFactoryActualFilter" ) >>= sDatabaseFilter
);
167 // get the type this filter is responsible for
168 Reference
< XNameAccess
> xFilterFactory(
169 m_xContext
->getServiceManager()->createInstanceWithContext("com.sun.star.document.FilterFactory", m_xContext
),
171 ::comphelper::NamedValueCollection
aFilterProperties( xFilterFactory
->getByName( sDatabaseFilter
) );
172 OUString sDocumentType
= aFilterProperties
.getOrDefault( "Type", OUString() );
174 // get the extension(s) for this type
175 Reference
< XNameAccess
> xTypeDetection(
176 m_xContext
->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext
),
179 ::comphelper::NamedValueCollection
aTypeProperties( xTypeDetection
->getByName( sDocumentType
) );
180 m_aFilterExtensions
= aTypeProperties
.getOrDefault( "Extensions", m_aFilterExtensions
);
181 m_sFilterUIName
= aTypeProperties
.getOrDefault( "UIName", m_sFilterUIName
);
183 catch( const Exception
& )
185 DBG_UNHANDLED_EXCEPTION();
188 // ensure we have at least one extension
189 OSL_ENSURE( m_aFilterExtensions
.getLength(),
190 "DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow: unable to determine the file extension(s)!" );
191 if ( m_aFilterExtensions
.getLength() == 0 )
193 m_aFilterExtensions
.realloc(1);
194 m_aFilterExtensions
[0] = "*.odb";
199 IMPL_LINK( DatabaseLocationInputController_Impl
, OnControlAction
, VclWindowEvent
*, _pEvent
)
201 if ( ( _pEvent
->GetWindow() == &m_rBrowseButton
)
202 && ( _pEvent
->GetId() == VCLEVENT_BUTTON_CLICK
)
205 impl_onBrowseButtonClicked();
208 if ( ( _pEvent
->GetWindow() == &m_rLocationInput
)
209 && ( _pEvent
->GetId() == VCLEVENT_EDIT_MODIFY
)
212 impl_onLocationModified();
219 OUString
DatabaseLocationInputController_Impl::impl_getCurrentURL() const
221 OUString
sCurrentFile( m_rLocationInput
.GetText() );
222 if ( !sCurrentFile
.isEmpty() )
224 ::svt::OFileNotation
aCurrentFile( sCurrentFile
);
225 sCurrentFile
= aCurrentFile
.get( ::svt::OFileNotation::N_URL
);
231 void DatabaseLocationInputController_Impl::impl_onBrowseButtonClicked()
233 ::sfx2::FileDialogHelper
aFileDlg(
234 TemplateDescription::FILESAVE_AUTOEXTENSION
,
236 m_rLocationInput
.GetSystemWindow()
238 aFileDlg
.SetDisplayDirectory( impl_getCurrentURL() );
240 aFileDlg
.AddFilter( m_sFilterUIName
, OUStringBuffer().appendAscii( "*." ).append( m_aFilterExtensions
[0] ).makeStringAndClear() );
241 aFileDlg
.SetCurrentFilter( m_sFilterUIName
);
243 if ( aFileDlg
.Execute() == ERRCODE_NONE
)
245 INetURLObject
aURL( aFileDlg
.GetPath() );
246 if( aURL
.GetProtocol() != INetProtocol::NotValid
)
248 ::svt::OFileNotation
aFileNotation( aURL
.GetMainURL( INetURLObject::NO_DECODE
) );
249 m_rLocationInput
.SetText( aFileNotation
.get( ::svt::OFileNotation::N_SYSTEM
) );
250 m_rLocationInput
.GetModifyHdl().Call( &m_rLocationInput
);
251 // the dialog already checked for the file's existence, so we don't need to, again
252 m_bNeedExistenceCheck
= false;
258 void DatabaseLocationInputController_Impl::impl_onLocationModified()
260 m_bNeedExistenceCheck
= true;
263 DatabaseLocationInputController::DatabaseLocationInputController( const Reference
<XComponentContext
>& _rContext
,
264 ::svt::OFileURLControl
& _rLocationInput
, PushButton
& _rBrowseButton
)
265 :m_pImpl( new DatabaseLocationInputController_Impl( _rContext
, _rLocationInput
, _rBrowseButton
) )
270 DatabaseLocationInputController::~DatabaseLocationInputController()
275 bool DatabaseLocationInputController::prepareCommit()
277 return m_pImpl
->prepareCommit();
281 void DatabaseLocationInputController::setURL( const OUString
& _rURL
)
283 m_pImpl
->setURL( _rURL
);
287 OUString
DatabaseLocationInputController::getURL() const
289 return m_pImpl
->getURL();
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */