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
;
54 //= DatabaseLocationInputController_Impl
56 class DatabaseLocationInputController_Impl
59 DatabaseLocationInputController_Impl(
60 const Reference
<XComponentContext
>& _rContext
,
61 ::svt::OFileURLControl
& _rLocationInput
,
62 PushButton
& _rBrowseButton
64 ~DatabaseLocationInputController_Impl();
67 void setURL( const OUString
& _rURL
);
68 OUString
getURL() const;
71 void impl_initFilterProperties_nothrow();
72 void impl_onBrowseButtonClicked();
73 void impl_onLocationModified();
74 OUString
impl_getCurrentURL() const;
76 DECL_LINK( OnControlAction
, VclWindowEvent
* );
79 const Reference
<XComponentContext
> m_xContext
;
80 ::svt::OFileURLControl
& m_rLocationInput
;
81 PushButton
& m_rBrowseButton
;
82 Sequence
< OUString
> m_aFilterExtensions
;
83 OUString m_sFilterUIName
;
84 bool m_bNeedExistenceCheck
;
88 DatabaseLocationInputController_Impl::DatabaseLocationInputController_Impl( const Reference
<XComponentContext
>& _rContext
,
89 ::svt::OFileURLControl
& _rLocationInput
, PushButton
& _rBrowseButton
)
90 :m_xContext( _rContext
)
91 ,m_rLocationInput( _rLocationInput
)
92 ,m_rBrowseButton( _rBrowseButton
)
93 ,m_aFilterExtensions()
95 ,m_bNeedExistenceCheck( true )
97 impl_initFilterProperties_nothrow();
99 // forward the allowed extensions to the input control
100 OUStringBuffer aExtensionList
;
101 for ( const OUString
* pExtension
= m_aFilterExtensions
.getConstArray();
102 pExtension
!= m_aFilterExtensions
.getConstArray() + m_aFilterExtensions
.getLength();
106 aExtensionList
.append( *pExtension
);
107 aExtensionList
.append( ';' );
109 m_rLocationInput
.SetFilter( aExtensionList
.makeStringAndClear() );
111 m_rBrowseButton
.AddEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
112 m_rLocationInput
.AddEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
116 DatabaseLocationInputController_Impl::~DatabaseLocationInputController_Impl()
118 m_rBrowseButton
.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
119 m_rLocationInput
.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
123 bool DatabaseLocationInputController_Impl::prepareCommit()
125 OUString
sURL( impl_getCurrentURL() );
126 if ( sURL
.isEmpty() )
129 // check if the name exists
130 if ( m_bNeedExistenceCheck
)
132 if ( ::utl::UCBContentHelper::Exists( sURL
) )
134 QueryBox
aBox( m_rLocationInput
.GetSystemWindow(), WB_YES_NO
, SVX_RESSTR(RID_STR_ALREADYEXISTOVERWRITE
) );
135 if ( aBox
.Execute() != RET_YES
)
144 void DatabaseLocationInputController_Impl::setURL( const OUString
& _rURL
)
146 ::svt::OFileNotation
aTransformer( _rURL
);
147 m_rLocationInput
.SetText( aTransformer
.get( ::svt::OFileNotation::N_SYSTEM
) );
151 OUString
DatabaseLocationInputController_Impl::getURL() const
153 return impl_getCurrentURL();
157 void DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow()
161 // get the name of the default filter for database documents
162 ::utl::OConfigurationTreeRoot
aConfig(
163 ::utl::OConfigurationTreeRoot::createWithComponentContext(
165 OUString( "/org.openoffice.Setup/Office/Factories/com.sun.star.sdb.OfficeDatabaseDocument" )
167 OUString sDatabaseFilter
;
168 OSL_VERIFY( aConfig
.getNodeValue( "ooSetupFactoryActualFilter" ) >>= sDatabaseFilter
);
170 // get the type this filter is responsible for
171 Reference
< XNameAccess
> xFilterFactory(
172 m_xContext
->getServiceManager()->createInstanceWithContext("com.sun.star.document.FilterFactory", m_xContext
),
174 ::comphelper::NamedValueCollection
aFilterProperties( xFilterFactory
->getByName( sDatabaseFilter
) );
175 OUString sDocumentType
= aFilterProperties
.getOrDefault( "Type", OUString() );
177 // get the extension(s) for this type
178 Reference
< XNameAccess
> xTypeDetection(
179 m_xContext
->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext
),
182 ::comphelper::NamedValueCollection
aTypeProperties( xTypeDetection
->getByName( sDocumentType
) );
183 m_aFilterExtensions
= aTypeProperties
.getOrDefault( "Extensions", m_aFilterExtensions
);
184 m_sFilterUIName
= aTypeProperties
.getOrDefault( "UIName", m_sFilterUIName
);
186 catch( const Exception
& )
188 DBG_UNHANDLED_EXCEPTION();
191 // ensure we have at least one extension
192 OSL_ENSURE( m_aFilterExtensions
.getLength(),
193 "DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow: unable to determine the file extension(s)!" );
194 if ( m_aFilterExtensions
.getLength() == 0 )
196 m_aFilterExtensions
.realloc(1);
197 m_aFilterExtensions
[0] = "*.odb";
202 IMPL_LINK( DatabaseLocationInputController_Impl
, OnControlAction
, VclWindowEvent
*, _pEvent
)
204 if ( ( _pEvent
->GetWindow() == &m_rBrowseButton
)
205 && ( _pEvent
->GetId() == VCLEVENT_BUTTON_CLICK
)
208 impl_onBrowseButtonClicked();
211 if ( ( _pEvent
->GetWindow() == &m_rLocationInput
)
212 && ( _pEvent
->GetId() == VCLEVENT_EDIT_MODIFY
)
215 impl_onLocationModified();
222 OUString
DatabaseLocationInputController_Impl::impl_getCurrentURL() const
224 OUString
sCurrentFile( m_rLocationInput
.GetText() );
225 if ( !sCurrentFile
.isEmpty() )
227 ::svt::OFileNotation
aCurrentFile( sCurrentFile
);
228 sCurrentFile
= aCurrentFile
.get( ::svt::OFileNotation::N_URL
);
234 void DatabaseLocationInputController_Impl::impl_onBrowseButtonClicked()
236 ::sfx2::FileDialogHelper
aFileDlg(
237 TemplateDescription::FILESAVE_AUTOEXTENSION
,
239 m_rLocationInput
.GetSystemWindow()
241 aFileDlg
.SetDisplayDirectory( impl_getCurrentURL() );
243 aFileDlg
.AddFilter( m_sFilterUIName
, OUStringBuffer().appendAscii( "*." ).append( m_aFilterExtensions
[0] ).makeStringAndClear() );
244 aFileDlg
.SetCurrentFilter( m_sFilterUIName
);
246 if ( aFileDlg
.Execute() == ERRCODE_NONE
)
248 INetURLObject
aURL( aFileDlg
.GetPath() );
249 if( aURL
.GetProtocol() != INET_PROT_NOT_VALID
)
251 ::svt::OFileNotation
aFileNotation( aURL
.GetMainURL( INetURLObject::NO_DECODE
) );
252 m_rLocationInput
.SetText( aFileNotation
.get( ::svt::OFileNotation::N_SYSTEM
) );
253 m_rLocationInput
.GetModifyHdl().Call( &m_rLocationInput
);
254 // the dialog already checked for the file's existence, so we don't need to, again
255 m_bNeedExistenceCheck
= false;
261 void DatabaseLocationInputController_Impl::impl_onLocationModified()
263 m_bNeedExistenceCheck
= true;
267 //= DatabaseLocationInputController
270 DatabaseLocationInputController::DatabaseLocationInputController( const Reference
<XComponentContext
>& _rContext
,
271 ::svt::OFileURLControl
& _rLocationInput
, PushButton
& _rBrowseButton
)
272 :m_pImpl( new DatabaseLocationInputController_Impl( _rContext
, _rLocationInput
, _rBrowseButton
) )
277 DatabaseLocationInputController::~DatabaseLocationInputController()
282 bool DatabaseLocationInputController::prepareCommit()
284 return m_pImpl
->prepareCommit();
288 void DatabaseLocationInputController::setURL( const OUString
& _rURL
)
290 m_pImpl
->setURL( _rURL
);
294 OUString
DatabaseLocationInputController::getURL() const
296 return m_pImpl
->getURL();
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */