Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / svx / source / form / databaselocationinput.cxx
blobac1f94d8b72b244261cf67306b41efc32116f0c6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
41 namespace svx
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
54 public:
55 DatabaseLocationInputController_Impl(
56 const Reference<XComponentContext>& _rContext,
57 SvtURLBox& _rLocationInput,
58 weld::Button& _rBrowseButton,
59 weld::Window& _rDialog
62 bool prepareCommit();
63 void setURL( const OUString& _rURL );
64 OUString getURL() const;
66 private:
67 void impl_initFilterProperties_nothrow();
68 void impl_onBrowseButtonClicked();
69 OUString impl_getCurrentURL() const;
71 DECL_LINK( OnButtonAction, weld::Button&, void );
73 private:
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 : std::as_const(m_aFilterExtensions) )
95 aExtensionList.append( extension );
96 aExtensionList.append( ';' );
98 m_rLocationInput.SetFilter( aExtensionList.makeStringAndClear() );
99 _rBrowseButton.connect_clicked(LINK(this, DatabaseLocationInputController_Impl, OnButtonAction));
102 bool DatabaseLocationInputController_Impl::prepareCommit()
104 OUString sURL( impl_getCurrentURL() );
105 if ( sURL.isEmpty() )
106 return false;
108 // check if the name exists
109 if ( m_bNeedExistenceCheck )
111 if ( ::utl::UCBContentHelper::Exists( sURL ) )
113 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(m_rLocationInput.getWidget(),
114 VclMessageType::Question, VclButtonsType::YesNo,
115 SvxResId(RID_STR_ALREADYEXISTOVERWRITE)));
116 if (xQueryBox->run() != RET_YES)
117 return false;
121 return true;
124 void DatabaseLocationInputController_Impl::setURL( const OUString& _rURL )
126 ::svt::OFileNotation aTransformer( _rURL );
127 m_rLocationInput.set_entry_text( aTransformer.get( ::svt::OFileNotation::N_SYSTEM ) );
130 OUString DatabaseLocationInputController_Impl::getURL() const
132 return impl_getCurrentURL();
135 void DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow()
139 // get the name of the default filter for database documents
140 ::utl::OConfigurationTreeRoot aConfig(
141 ::utl::OConfigurationTreeRoot::createWithComponentContext(
142 m_xContext,
143 "/org.openoffice.Setup/Office/Factories/com.sun.star.sdb.OfficeDatabaseDocument"
144 ) );
145 OUString sDatabaseFilter;
146 OSL_VERIFY( aConfig.getNodeValue( "ooSetupFactoryActualFilter" ) >>= sDatabaseFilter );
148 // get the type this filter is responsible for
149 Reference< XNameAccess > xFilterFactory(
150 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.FilterFactory", m_xContext),
151 UNO_QUERY_THROW );
152 ::comphelper::NamedValueCollection aFilterProperties( xFilterFactory->getByName( sDatabaseFilter ) );
153 OUString sDocumentType = aFilterProperties.getOrDefault( "Type", OUString() );
155 // get the extension(s) for this type
156 Reference< XNameAccess > xTypeDetection(
157 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext),
158 UNO_QUERY_THROW );
160 ::comphelper::NamedValueCollection aTypeProperties( xTypeDetection->getByName( sDocumentType ) );
161 m_aFilterExtensions = aTypeProperties.getOrDefault( "Extensions", m_aFilterExtensions );
162 m_sFilterUIName = aTypeProperties.getOrDefault( "UIName", m_sFilterUIName );
164 catch( const Exception& )
166 DBG_UNHANDLED_EXCEPTION("svx");
169 // ensure we have at least one extension
170 OSL_ENSURE( m_aFilterExtensions.hasElements(),
171 "DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow: unable to determine the file extension(s)!" );
172 if ( !m_aFilterExtensions.hasElements() )
174 m_aFilterExtensions = { "*.odb" };
178 IMPL_LINK_NOARG(DatabaseLocationInputController_Impl, OnButtonAction, weld::Button&, void)
180 impl_onBrowseButtonClicked();
183 OUString DatabaseLocationInputController_Impl::impl_getCurrentURL() const
185 OUString sCurrentFile( m_rLocationInput.get_active_text() );
186 if ( !sCurrentFile.isEmpty() )
188 ::svt::OFileNotation aCurrentFile( sCurrentFile );
189 sCurrentFile = aCurrentFile.get( ::svt::OFileNotation::N_URL );
191 return sCurrentFile;
194 void DatabaseLocationInputController_Impl::impl_onBrowseButtonClicked()
196 ::sfx2::FileDialogHelper aFileDlg(
197 TemplateDescription::FILESAVE_AUTOEXTENSION,
198 FileDialogFlags::NONE,
199 &m_rDialog
201 aFileDlg.SetDisplayDirectory( impl_getCurrentURL() );
203 aFileDlg.AddFilter( m_sFilterUIName, "*." + m_aFilterExtensions[0] );
204 aFileDlg.SetCurrentFilter( m_sFilterUIName );
206 if ( aFileDlg.Execute() == ERRCODE_NONE )
208 INetURLObject aURL( aFileDlg.GetPath() );
209 if( aURL.GetProtocol() != INetProtocol::NotValid )
211 ::svt::OFileNotation aFileNotation( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
212 m_rLocationInput.set_entry_text(aFileNotation.get(::svt::OFileNotation::N_SYSTEM));
213 m_rLocationInput.trigger_changed();
214 // the dialog already checked for the file's existence, so we don't need to, again
215 m_bNeedExistenceCheck = false;
220 DatabaseLocationInputController::DatabaseLocationInputController( const Reference<XComponentContext>& _rContext,
221 SvtURLBox& _rLocationInput, weld::Button& _rBrowseButton, weld::Window& _rDialog )
222 :m_pImpl( new DatabaseLocationInputController_Impl( _rContext, _rLocationInput, _rBrowseButton, _rDialog ) )
226 DatabaseLocationInputController::~DatabaseLocationInputController()
230 bool DatabaseLocationInputController::prepareCommit()
232 return m_pImpl->prepareCommit();
235 void DatabaseLocationInputController::setURL( const OUString& _rURL )
237 m_pImpl->setURL( _rURL );
240 OUString DatabaseLocationInputController::getURL() const
242 return m_pImpl->getURL();
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */