LibreOffice Module svx (master)  1
databaselocationinput.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
22 #include <svx/dialmgr.hxx>
23 
24 #include <svx/strings.hrc>
25 
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>
29 
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/svapp.hxx>
39 #include <vcl/weld.hxx>
40 
41 namespace svx
42 {
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;
49 
50  namespace TemplateDescription = ::com::sun::star::ui::dialogs::TemplateDescription;
51 
53  {
54  public:
56  const Reference<XComponentContext>& _rContext,
57  SvtURLBox& _rLocationInput,
58  weld::Button& _rBrowseButton,
59  weld::Window& _rDialog
60  );
61 
62  bool prepareCommit();
63  void setURL( const OUString& _rURL );
64  OUString getURL() const;
65 
66  private:
69  OUString impl_getCurrentURL() const;
70 
71  DECL_LINK( OnButtonAction, weld::Button&, void );
72 
73  private:
78  OUString m_sFilterUIName;
80  };
81 
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 )
88  {
90 
91  // forward the allowed extensions to the input control
92  OUStringBuffer aExtensionList;
93  for ( auto const & extension : std::as_const(m_aFilterExtensions) )
94  {
95  aExtensionList.append( extension );
96  aExtensionList.append( ';' );
97  }
98  m_rLocationInput.SetFilter( aExtensionList.makeStringAndClear() );
99  _rBrowseButton.connect_clicked(LINK(this, DatabaseLocationInputController_Impl, OnButtonAction));
100  }
101 
103  {
104  OUString sURL( impl_getCurrentURL() );
105  if ( sURL.isEmpty() )
106  return false;
107 
108  // check if the name exists
109  if ( m_bNeedExistenceCheck )
110  {
111  if ( ::utl::UCBContentHelper::Exists( sURL ) )
112  {
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;
118  }
119  }
120 
121  return true;
122  }
123 
124  void DatabaseLocationInputController_Impl::setURL( const OUString& _rURL )
125  {
126  ::svt::OFileNotation aTransformer( _rURL );
128  }
129 
131  {
132  return impl_getCurrentURL();
133  }
134 
136  {
137  try
138  {
139  // get the name of the default filter for database documents
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 );
147 
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() );
154 
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 );
159 
160  ::comphelper::NamedValueCollection aTypeProperties( xTypeDetection->getByName( sDocumentType ) );
161  m_aFilterExtensions = aTypeProperties.getOrDefault( "Extensions", m_aFilterExtensions );
162  m_sFilterUIName = aTypeProperties.getOrDefault( "UIName", m_sFilterUIName );
163  }
164  catch( const Exception& )
165  {
167  }
168 
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() )
173  {
174  m_aFilterExtensions.realloc(1);
175  m_aFilterExtensions[0] = "*.odb";
176  }
177  }
178 
180  {
181  impl_onBrowseButtonClicked();
182  }
183 
185  {
186  OUString sCurrentFile( m_rLocationInput.get_active_text() );
187  if ( !sCurrentFile.isEmpty() )
188  {
189  ::svt::OFileNotation aCurrentFile( sCurrentFile );
190  sCurrentFile = aCurrentFile.get( ::svt::OFileNotation::N_URL );
191  }
192  return sCurrentFile;
193  }
194 
196  {
197  ::sfx2::FileDialogHelper aFileDlg(
198  TemplateDescription::FILESAVE_AUTOEXTENSION,
199  FileDialogFlags::NONE,
200  &m_rDialog
201  );
203 
204  aFileDlg.AddFilter( m_sFilterUIName, "*." + m_aFilterExtensions[0] );
205  aFileDlg.SetCurrentFilter( m_sFilterUIName );
206 
207  if ( aFileDlg.Execute() == ERRCODE_NONE )
208  {
209  INetURLObject aURL( aFileDlg.GetPath() );
210  if( aURL.GetProtocol() != INetProtocol::NotValid )
211  {
212  ::svt::OFileNotation aFileNotation( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
215  // the dialog already checked for the file's existence, so we don't need to, again
216  m_bNeedExistenceCheck = false;
217  }
218  }
219  }
220 
222  SvtURLBox& _rLocationInput, weld::Button& _rBrowseButton, weld::Window& _rDialog )
223  :m_pImpl( new DatabaseLocationInputController_Impl( _rContext, _rLocationInput, _rBrowseButton, _rDialog ) )
224  {
225  }
226 
228  {
229  }
230 
232  {
233  return m_pImpl->prepareCommit();
234  }
235 
236  void DatabaseLocationInputController::setURL( const OUString& _rURL )
237  {
238  m_pImpl->setURL( _rURL );
239  }
240 
242  {
243  return m_pImpl->getURL();
244  }
245 }
246 
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
UNOTOOLS_DLLPUBLIC bool Exists(OUString const &url)
URL aURL
DatabaseLocationInputController_Impl(const Reference< XComponentContext > &_rContext, SvtURLBox &_rLocationInput, weld::Button &_rBrowseButton, weld::Window &_rDialog)
void AddFilter(const OUString &rFilterName, const OUString &rExtension)
OUString GetPath() const
VALUE_TYPE getOrDefault(const char *_pAsciiValueName, const VALUE_TYPE &_rDefault) const
const Reference< XComponentContext > m_xContext
void trigger_changed()
void SetDisplayDirectory(const OUString &rPath)
static OConfigurationTreeRoot createWithComponentContext(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const OUString &_rPath, sal_Int32 _nDepth=-1, CREATION_MODE _eMode=CM_UPDATABLE)
RET_YES
bool prepareCommit()
prepares committing the database location entered in the input field
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
void SetFilter(const OUString &_sFilter)
void set_entry_text(const OUString &rStr)
IMPL_LINK_NOARG(SuggestionDisplay, SelectSuggestionValueSetHdl, ValueSet *, void)
#define DBG_UNHANDLED_EXCEPTION(...)
void connect_clicked(const Link< Button &, void > &rLink)
DECL_LINK(OnButtonAction, weld::Button &, void)
css::uno::Any getNodeValue(const OUString &_rPath) const noexcept
void SetCurrentFilter(const OUString &rFilter)
OUString getURL() const
returns the current database location, in form of a URL (not a system path)
#define ERRCODE_NONE
void setURL(const OUString &_rURL)
sets the given URL at the input control, after translating it into a system path
OUString get(NOTATION _eOutputNotation) const
weld::ComboBox * getWidget()
Reference< XComponentContext > m_xContext
::std::unique_ptr< DatabaseLocationInputController_Impl > m_pImpl
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
DatabaseLocationInputController(const css::uno::Reference< css::uno::XComponentContext > &_rContext, SvtURLBox &_rLocationInput, weld::Button &_rBrowseButton, weld::Window &_rDialog)
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
OUString get_active_text() const