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/strings.hrc>
26 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
27 #include <com/sun/star/container/XNameAccess.hpp>
28 #include <com/sun/star/uno/XComponentContext.hpp>
30 #include <comphelper/namedvaluecollection.hxx>
31 #include <rtl/ustrbuf.hxx>
32 #include <sfx2/filedlghelper.hxx>
33 #include <svl/filenotation.hxx>
34 #include <svtools/inettbc.hxx>
35 #include <comphelper/diagnose_ex.hxx>
36 #include <unotools/confignode.hxx>
37 #include <unotools/ucbhelper.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vcl/weld.hxx>
43 using ::com::sun::star::uno::Sequence
;
44 using ::com::sun::star::uno::Reference
;
45 using ::com::sun::star::uno::XComponentContext
;
46 using ::com::sun::star::container::XNameAccess
;
47 using ::com::sun::star::uno::UNO_QUERY_THROW
;
48 using ::com::sun::star::uno::Exception
;
50 namespace TemplateDescription
= ::com::sun::star::ui::dialogs::TemplateDescription
;
52 class DatabaseLocationInputController_Impl
55 DatabaseLocationInputController_Impl(
56 const Reference
<XComponentContext
>& _rContext
,
57 SvtURLBox
& _rLocationInput
,
58 weld::Button
& _rBrowseButton
,
59 weld::Window
& _rDialog
63 void setURL( const OUString
& _rURL
);
64 OUString
getURL() const;
67 void impl_initFilterProperties_nothrow();
68 void impl_onBrowseButtonClicked();
69 OUString
impl_getCurrentURL() const;
71 DECL_LINK( OnButtonAction
, weld::Button
&, void );
74 const Reference
<XComponentContext
> m_xContext
;
75 SvtURLBox
& m_rLocationInput
;
76 weld::Window
& m_rDialog
;
77 Sequence
< OUString
> m_aFilterExtensions
;
78 OUString m_sFilterUIName
;
79 bool m_bNeedExistenceCheck
;
82 DatabaseLocationInputController_Impl::DatabaseLocationInputController_Impl(const Reference
<XComponentContext
>& _rContext
,
83 SvtURLBox
& _rLocationInput
, weld::Button
& _rBrowseButton
, weld::Window
& _rDialog
)
84 :m_xContext( _rContext
)
85 ,m_rLocationInput( _rLocationInput
)
86 ,m_rDialog( _rDialog
)
87 ,m_bNeedExistenceCheck( true )
89 impl_initFilterProperties_nothrow();
91 // forward the allowed extensions to the input control
92 OUStringBuffer aExtensionList
;
93 for (auto const& extension
: m_aFilterExtensions
)
95 aExtensionList
.append( extension
+ ";" );
97 m_rLocationInput
.SetFilter( aExtensionList
.makeStringAndClear() );
98 _rBrowseButton
.connect_clicked(LINK(this, DatabaseLocationInputController_Impl
, OnButtonAction
));
101 bool DatabaseLocationInputController_Impl::prepareCommit()
103 OUString
sURL( impl_getCurrentURL() );
104 if ( sURL
.isEmpty() )
107 // check if the name exists
108 if ( m_bNeedExistenceCheck
)
110 if ( ::utl::UCBContentHelper::Exists( sURL
) )
112 std::unique_ptr
<weld::MessageDialog
> xQueryBox(Application::CreateMessageDialog(m_rLocationInput
.getWidget(),
113 VclMessageType::Question
, VclButtonsType::YesNo
,
114 SvxResId(RID_STR_ALREADYEXISTOVERWRITE
)));
115 if (xQueryBox
->run() != RET_YES
)
123 void DatabaseLocationInputController_Impl::setURL( const OUString
& _rURL
)
125 ::svt::OFileNotation
aTransformer( _rURL
);
126 m_rLocationInput
.set_entry_text( aTransformer
.get( ::svt::OFileNotation::N_SYSTEM
) );
129 OUString
DatabaseLocationInputController_Impl::getURL() const
131 return impl_getCurrentURL();
134 void DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow()
138 // get the name of the default filter for database documents
139 ::utl::OConfigurationTreeRoot
aConfig(
140 ::utl::OConfigurationTreeRoot::createWithComponentContext(
142 u
"/org.openoffice.Setup/Office/Factories/com.sun.star.sdb.OfficeDatabaseDocument"_ustr
144 OUString sDatabaseFilter
;
145 OSL_VERIFY( aConfig
.getNodeValue( u
"ooSetupFactoryActualFilter"_ustr
) >>= sDatabaseFilter
);
147 // get the type this filter is responsible for
148 Reference
< XNameAccess
> xFilterFactory(
149 m_xContext
->getServiceManager()->createInstanceWithContext(u
"com.sun.star.document.FilterFactory"_ustr
, m_xContext
),
151 ::comphelper::NamedValueCollection
aFilterProperties( xFilterFactory
->getByName( sDatabaseFilter
) );
152 OUString sDocumentType
= aFilterProperties
.getOrDefault( u
"Type"_ustr
, OUString() );
154 // get the extension(s) for this type
155 Reference
< XNameAccess
> xTypeDetection(
156 m_xContext
->getServiceManager()->createInstanceWithContext(u
"com.sun.star.document.TypeDetection"_ustr
, m_xContext
),
159 ::comphelper::NamedValueCollection
aTypeProperties( xTypeDetection
->getByName( sDocumentType
) );
160 m_aFilterExtensions
= aTypeProperties
.getOrDefault( u
"Extensions"_ustr
, m_aFilterExtensions
);
161 m_sFilterUIName
= aTypeProperties
.getOrDefault( u
"UIName"_ustr
, m_sFilterUIName
);
163 catch( const Exception
& )
165 DBG_UNHANDLED_EXCEPTION("svx");
168 // ensure we have at least one extension
169 OSL_ENSURE( m_aFilterExtensions
.hasElements(),
170 "DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow: unable to determine the file extension(s)!" );
171 if ( !m_aFilterExtensions
.hasElements() )
173 m_aFilterExtensions
= { u
"*.odb"_ustr
};
177 IMPL_LINK_NOARG(DatabaseLocationInputController_Impl
, OnButtonAction
, weld::Button
&, void)
179 impl_onBrowseButtonClicked();
182 OUString
DatabaseLocationInputController_Impl::impl_getCurrentURL() const
184 OUString
sCurrentFile( m_rLocationInput
.get_active_text() );
185 if ( !sCurrentFile
.isEmpty() )
187 ::svt::OFileNotation
aCurrentFile( sCurrentFile
);
188 sCurrentFile
= aCurrentFile
.get( ::svt::OFileNotation::N_URL
);
193 void DatabaseLocationInputController_Impl::impl_onBrowseButtonClicked()
195 ::sfx2::FileDialogHelper
aFileDlg(
196 TemplateDescription::FILESAVE_AUTOEXTENSION
,
197 FileDialogFlags::NONE
,
200 aFileDlg
.SetDisplayDirectory( impl_getCurrentURL() );
202 aFileDlg
.AddFilter( m_sFilterUIName
, "*." + m_aFilterExtensions
[0] );
203 aFileDlg
.SetCurrentFilter( m_sFilterUIName
);
205 if ( aFileDlg
.Execute() == ERRCODE_NONE
)
207 INetURLObject
aURL( aFileDlg
.GetPath() );
208 if( aURL
.GetProtocol() != INetProtocol::NotValid
)
210 ::svt::OFileNotation
aFileNotation( aURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
) );
211 m_rLocationInput
.set_entry_text(aFileNotation
.get(::svt::OFileNotation::N_SYSTEM
));
212 m_rLocationInput
.trigger_changed();
213 // the dialog already checked for the file's existence, so we don't need to, again
214 m_bNeedExistenceCheck
= false;
219 DatabaseLocationInputController::DatabaseLocationInputController( const Reference
<XComponentContext
>& _rContext
,
220 SvtURLBox
& _rLocationInput
, weld::Button
& _rBrowseButton
, weld::Window
& _rDialog
)
221 :m_pImpl( new DatabaseLocationInputController_Impl( _rContext
, _rLocationInput
, _rBrowseButton
, _rDialog
) )
225 DatabaseLocationInputController::~DatabaseLocationInputController()
229 bool DatabaseLocationInputController::prepareCommit()
231 return m_pImpl
->prepareCommit();
234 void DatabaseLocationInputController::setURL( const OUString
& _rURL
)
236 m_pImpl
->setURL( _rURL
);
239 OUString
DatabaseLocationInputController::getURL() const
241 return m_pImpl
->getURL();
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */