Bump version to 6.4-15
[LibreOffice.git] / svx / source / form / databaselocationinput.cxx
blob6d49c7dd2eff4ab6dc2505301c53f9402107bf9c
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 <tools/diagnose_ex.h>
36 #include <unotools/confignode.hxx>
37 #include <unotools/ucbhelper.hxx>
38 #include <vcl/button.hxx>
39 #include <vcl/svapp.hxx>
40 #include <vcl/weld.hxx>
42 namespace svx
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
55 public:
56 DatabaseLocationInputController_Impl(
57 const Reference<XComponentContext>& _rContext,
58 URLBox& _rLocationInput,
59 weld::Button& _rBrowseButton,
60 weld::Window& _rDialog
63 bool prepareCommit();
64 void setURL( const OUString& _rURL );
65 OUString getURL() const;
67 private:
68 void impl_initFilterProperties_nothrow();
69 void impl_onBrowseButtonClicked();
70 OUString impl_getCurrentURL() const;
72 DECL_LINK( OnButtonAction, weld::Button&, void );
74 private:
75 const Reference<XComponentContext> m_xContext;
76 URLBox& m_rLocationInput;
77 weld::Window& m_rDialog;
78 Sequence< OUString > m_aFilterExtensions;
79 OUString m_sFilterUIName;
80 bool m_bNeedExistenceCheck;
83 DatabaseLocationInputController_Impl::DatabaseLocationInputController_Impl(const Reference<XComponentContext>& _rContext,
84 URLBox& _rLocationInput, weld::Button& _rBrowseButton, weld::Window& _rDialog)
85 :m_xContext( _rContext )
86 ,m_rLocationInput( _rLocationInput )
87 ,m_rDialog( _rDialog )
88 ,m_aFilterExtensions()
89 ,m_sFilterUIName()
90 ,m_bNeedExistenceCheck( true )
92 impl_initFilterProperties_nothrow();
94 // forward the allowed extensions to the input control
95 OUStringBuffer aExtensionList;
96 for ( auto const & extension : std::as_const(m_aFilterExtensions) )
98 aExtensionList.append( extension );
99 aExtensionList.append( ';' );
101 m_rLocationInput.SetFilter( aExtensionList.makeStringAndClear() );
102 _rBrowseButton.connect_clicked(LINK(this, DatabaseLocationInputController_Impl, OnButtonAction));
105 bool DatabaseLocationInputController_Impl::prepareCommit()
107 OUString sURL( impl_getCurrentURL() );
108 if ( sURL.isEmpty() )
109 return false;
111 // check if the name exists
112 if ( m_bNeedExistenceCheck )
114 if ( ::utl::UCBContentHelper::Exists( sURL ) )
116 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(m_rLocationInput.getWidget(),
117 VclMessageType::Question, VclButtonsType::YesNo,
118 SvxResId(RID_STR_ALREADYEXISTOVERWRITE)));
119 if (xQueryBox->run() != RET_YES)
120 return false;
124 return true;
127 void DatabaseLocationInputController_Impl::setURL( const OUString& _rURL )
129 ::svt::OFileNotation aTransformer( _rURL );
130 m_rLocationInput.set_entry_text( aTransformer.get( ::svt::OFileNotation::N_SYSTEM ) );
133 OUString DatabaseLocationInputController_Impl::getURL() const
135 return impl_getCurrentURL();
138 void DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow()
142 // get the name of the default filter for database documents
143 ::utl::OConfigurationTreeRoot aConfig(
144 ::utl::OConfigurationTreeRoot::createWithComponentContext(
145 m_xContext,
146 "/org.openoffice.Setup/Office/Factories/com.sun.star.sdb.OfficeDatabaseDocument"
147 ) );
148 OUString sDatabaseFilter;
149 OSL_VERIFY( aConfig.getNodeValue( "ooSetupFactoryActualFilter" ) >>= sDatabaseFilter );
151 // get the type this filter is responsible for
152 Reference< XNameAccess > xFilterFactory(
153 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.FilterFactory", m_xContext),
154 UNO_QUERY_THROW );
155 ::comphelper::NamedValueCollection aFilterProperties( xFilterFactory->getByName( sDatabaseFilter ) );
156 OUString sDocumentType = aFilterProperties.getOrDefault( "Type", OUString() );
158 // get the extension(s) for this type
159 Reference< XNameAccess > xTypeDetection(
160 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext),
161 UNO_QUERY_THROW );
163 ::comphelper::NamedValueCollection aTypeProperties( xTypeDetection->getByName( sDocumentType ) );
164 m_aFilterExtensions = aTypeProperties.getOrDefault( "Extensions", m_aFilterExtensions );
165 m_sFilterUIName = aTypeProperties.getOrDefault( "UIName", m_sFilterUIName );
167 catch( const Exception& )
169 DBG_UNHANDLED_EXCEPTION("svx");
172 // ensure we have at least one extension
173 OSL_ENSURE( m_aFilterExtensions.hasElements(),
174 "DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow: unable to determine the file extension(s)!" );
175 if ( !m_aFilterExtensions.hasElements() )
177 m_aFilterExtensions.realloc(1);
178 m_aFilterExtensions[0] = "*.odb";
182 IMPL_LINK_NOARG(DatabaseLocationInputController_Impl, OnButtonAction, weld::Button&, void)
184 impl_onBrowseButtonClicked();
187 OUString DatabaseLocationInputController_Impl::impl_getCurrentURL() const
189 OUString sCurrentFile( m_rLocationInput.get_active_text() );
190 if ( !sCurrentFile.isEmpty() )
192 ::svt::OFileNotation aCurrentFile( sCurrentFile );
193 sCurrentFile = aCurrentFile.get( ::svt::OFileNotation::N_URL );
195 return sCurrentFile;
198 void DatabaseLocationInputController_Impl::impl_onBrowseButtonClicked()
200 ::sfx2::FileDialogHelper aFileDlg(
201 TemplateDescription::FILESAVE_AUTOEXTENSION,
202 FileDialogFlags::NONE,
203 &m_rDialog
205 aFileDlg.SetDisplayDirectory( impl_getCurrentURL() );
207 aFileDlg.AddFilter( m_sFilterUIName, "*." + m_aFilterExtensions[0] );
208 aFileDlg.SetCurrentFilter( m_sFilterUIName );
210 if ( aFileDlg.Execute() == ERRCODE_NONE )
212 INetURLObject aURL( aFileDlg.GetPath() );
213 if( aURL.GetProtocol() != INetProtocol::NotValid )
215 ::svt::OFileNotation aFileNotation( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
216 m_rLocationInput.set_entry_text(aFileNotation.get(::svt::OFileNotation::N_SYSTEM));
217 m_rLocationInput.trigger_changed();
218 // the dialog already checked for the file's existence, so we don't need to, again
219 m_bNeedExistenceCheck = false;
224 DatabaseLocationInputController::DatabaseLocationInputController( const Reference<XComponentContext>& _rContext,
225 URLBox& _rLocationInput, weld::Button& _rBrowseButton, weld::Window& _rDialog )
226 :m_pImpl( new DatabaseLocationInputController_Impl( _rContext, _rLocationInput, _rBrowseButton, _rDialog ) )
230 DatabaseLocationInputController::~DatabaseLocationInputController()
234 bool DatabaseLocationInputController::prepareCommit()
236 return m_pImpl->prepareCommit();
239 void DatabaseLocationInputController::setURL( const OUString& _rURL )
241 m_pImpl->setURL( _rURL );
244 OUString DatabaseLocationInputController::getURL() const
246 return m_pImpl->getURL();
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */