1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "svx/databaselocationinput.hxx"
28cdf0e10cSrcweir #include "svx/dialmgr.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include "svx/fmresids.hrc"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /** === begin UNO includes === **/
33cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
34cdf0e10cSrcweir /** === end UNO includes === **/
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
37cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
38cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
39cdf0e10cSrcweir #include <sfx2/filedlghelper.hxx>
40cdf0e10cSrcweir #include <svtools/urlcontrol.hxx>
41cdf0e10cSrcweir #include <svl/filenotation.hxx>
42cdf0e10cSrcweir #include <tools/diagnose_ex.h>
43cdf0e10cSrcweir #include <unotools/confignode.hxx>
44cdf0e10cSrcweir #include <unotools/ucbhelper.hxx>
45cdf0e10cSrcweir #include <vcl/button.hxx>
46cdf0e10cSrcweir #include <vcl/msgbox.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //........................................................................
49cdf0e10cSrcweir namespace svx
50cdf0e10cSrcweir {
51cdf0e10cSrcweir //........................................................................
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 	/** === begin UNO using === **/
54cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
55cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
56cdf0e10cSrcweir     using ::com::sun::star::container::XNameAccess;
57cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
58cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
59cdf0e10cSrcweir 	/** === end UNO using === **/
60cdf0e10cSrcweir     namespace TemplateDescription = ::com::sun::star::ui::dialogs::TemplateDescription;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	//====================================================================
63cdf0e10cSrcweir 	//= DatabaseLocationInputController_Impl
64cdf0e10cSrcweir 	//====================================================================
65cdf0e10cSrcweir     class DatabaseLocationInputController_Impl
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir     public:
68cdf0e10cSrcweir         DatabaseLocationInputController_Impl(
69cdf0e10cSrcweir             const ::comphelper::ComponentContext&   _rContext,
70cdf0e10cSrcweir             ::svt::OFileURLControl&                 _rLocationInput,
71cdf0e10cSrcweir             PushButton&                             _rBrowseButton
72cdf0e10cSrcweir         );
73cdf0e10cSrcweir         ~DatabaseLocationInputController_Impl();
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         bool    prepareCommit();
76cdf0e10cSrcweir         void    setURL( const String& _rURL );
77cdf0e10cSrcweir         String  getURL() const;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     private:
80cdf0e10cSrcweir         void    impl_initFilterProperties_nothrow();
81cdf0e10cSrcweir         void    impl_onBrowseButtonClicked();
82cdf0e10cSrcweir         void    impl_onLocationModified();
83cdf0e10cSrcweir         String  impl_getCurrentURL() const;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         DECL_LINK( OnControlAction, VclWindowEvent* );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     private:
88cdf0e10cSrcweir         const ::comphelper::ComponentContext    m_aContext;
89cdf0e10cSrcweir         ::svt::OFileURLControl&                 m_rLocationInput;
90cdf0e10cSrcweir         PushButton&                             m_rBrowseButton;
91cdf0e10cSrcweir         Sequence< ::rtl::OUString >             m_aFilterExtensions;
92cdf0e10cSrcweir         ::rtl::OUString                         m_sFilterUIName;
93cdf0e10cSrcweir         bool                                    m_bNeedExistenceCheck;
94cdf0e10cSrcweir     };
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	//--------------------------------------------------------------------
DatabaseLocationInputController_Impl(const::comphelper::ComponentContext & _rContext,::svt::OFileURLControl & _rLocationInput,PushButton & _rBrowseButton)97cdf0e10cSrcweir     DatabaseLocationInputController_Impl::DatabaseLocationInputController_Impl( const ::comphelper::ComponentContext& _rContext,
98cdf0e10cSrcweir             ::svt::OFileURLControl& _rLocationInput, PushButton& _rBrowseButton )
99cdf0e10cSrcweir         :m_aContext( _rContext )
100cdf0e10cSrcweir         ,m_rLocationInput( _rLocationInput )
101cdf0e10cSrcweir         ,m_rBrowseButton( _rBrowseButton )
102cdf0e10cSrcweir         ,m_aFilterExtensions()
103cdf0e10cSrcweir         ,m_sFilterUIName()
104cdf0e10cSrcweir         ,m_bNeedExistenceCheck( true )
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         impl_initFilterProperties_nothrow();
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         // forward the allowed extensions to the input control
109cdf0e10cSrcweir         ::rtl::OUStringBuffer aExtensionList;
110cdf0e10cSrcweir         for (   const ::rtl::OUString* pExtension = m_aFilterExtensions.getConstArray();
111cdf0e10cSrcweir                 pExtension != m_aFilterExtensions.getConstArray() + m_aFilterExtensions.getLength();
112cdf0e10cSrcweir                 ++pExtension
113cdf0e10cSrcweir             )
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             aExtensionList.append( *pExtension );
116cdf0e10cSrcweir             aExtensionList.append( (sal_Unicode)';' );
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir         m_rLocationInput.SetFilter( aExtensionList.makeStringAndClear() );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         m_rBrowseButton.AddEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
121cdf0e10cSrcweir         m_rLocationInput.AddEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     //--------------------------------------------------------------------
~DatabaseLocationInputController_Impl()125cdf0e10cSrcweir     DatabaseLocationInputController_Impl::~DatabaseLocationInputController_Impl()
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         m_rBrowseButton.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
128cdf0e10cSrcweir         m_rLocationInput.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	//--------------------------------------------------------------------
prepareCommit()132cdf0e10cSrcweir     bool DatabaseLocationInputController_Impl::prepareCommit()
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir 		::rtl::OUString sURL( impl_getCurrentURL() );
135cdf0e10cSrcweir         if ( !sURL.getLength() )
136cdf0e10cSrcweir             return false;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         // check if the name exists
139cdf0e10cSrcweir 		if ( m_bNeedExistenceCheck )
140cdf0e10cSrcweir 		{
141cdf0e10cSrcweir 			if ( ::utl::UCBContentHelper::Exists( sURL ) )
142cdf0e10cSrcweir 			{
143cdf0e10cSrcweir 				QueryBox aBox( m_rLocationInput.GetSystemWindow(), WB_YES_NO, SVX_RES( RID_STR_ALREADYEXISTOVERWRITE ) );
144cdf0e10cSrcweir 				if ( aBox.Execute() != RET_YES )
145cdf0e10cSrcweir 					return false;
146cdf0e10cSrcweir 			}
147cdf0e10cSrcweir 		}
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         return true;
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 	//--------------------------------------------------------------------
setURL(const String & _rURL)153cdf0e10cSrcweir     void DatabaseLocationInputController_Impl::setURL( const String& _rURL )
154cdf0e10cSrcweir     {
155cdf0e10cSrcweir         ::svt::OFileNotation aTransformer( _rURL );
156cdf0e10cSrcweir         m_rLocationInput.SetText( aTransformer.get( ::svt::OFileNotation::N_SYSTEM ) );
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	//--------------------------------------------------------------------
getURL() const160cdf0e10cSrcweir     String DatabaseLocationInputController_Impl::getURL() const
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         return impl_getCurrentURL();
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 	//--------------------------------------------------------------------
impl_initFilterProperties_nothrow()166cdf0e10cSrcweir     void DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow()
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         try
169cdf0e10cSrcweir         {
170cdf0e10cSrcweir             // get the name of the default filter for database documents
171cdf0e10cSrcweir             ::utl::OConfigurationTreeRoot aConfig(
172cdf0e10cSrcweir                 ::utl::OConfigurationTreeRoot::createWithServiceFactory(
173cdf0e10cSrcweir                     m_aContext.getLegacyServiceFactory(),
174cdf0e10cSrcweir                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Setup/Office/Factories/com.sun.star.sdb.OfficeDatabaseDocument" ) )
175cdf0e10cSrcweir             ) );
176cdf0e10cSrcweir             ::rtl::OUString sDatabaseFilter;
177cdf0e10cSrcweir             OSL_VERIFY( aConfig.getNodeValue( "ooSetupFactoryActualFilter" ) >>= sDatabaseFilter );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             // get the type this filter is responsible for
180cdf0e10cSrcweir             Reference< XNameAccess > xFilterFactory(
181cdf0e10cSrcweir                 m_aContext.createComponent( "com.sun.star.document.FilterFactory" ),
182cdf0e10cSrcweir                 UNO_QUERY_THROW );
183cdf0e10cSrcweir             ::comphelper::NamedValueCollection aFilterProperties( xFilterFactory->getByName( sDatabaseFilter ) );
184cdf0e10cSrcweir             ::rtl::OUString sDocumentType = aFilterProperties.getOrDefault( "Type", ::rtl::OUString() );
185cdf0e10cSrcweir 
186cdf0e10cSrcweir             // get the extension(s) for this type
187cdf0e10cSrcweir             Reference< XNameAccess > xTypeDetection(
188cdf0e10cSrcweir                 m_aContext.createComponent( "com.sun.star.document.TypeDetection" ),
189cdf0e10cSrcweir                 UNO_QUERY_THROW );
190cdf0e10cSrcweir 
191cdf0e10cSrcweir             ::comphelper::NamedValueCollection aTypeProperties( xTypeDetection->getByName( sDocumentType ) );
192cdf0e10cSrcweir             m_aFilterExtensions = aTypeProperties.getOrDefault( "Extensions", m_aFilterExtensions );
193cdf0e10cSrcweir             m_sFilterUIName = aTypeProperties.getOrDefault( "UIName", m_sFilterUIName );
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir         catch( const Exception& )
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         // ensure we have at least one extension
201cdf0e10cSrcweir         OSL_ENSURE( m_aFilterExtensions.getLength(),
202cdf0e10cSrcweir             "DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow: unable to determine the file extension(s)!" );
203cdf0e10cSrcweir         if ( m_aFilterExtensions.getLength() == 0 )
204cdf0e10cSrcweir         {
205cdf0e10cSrcweir             m_aFilterExtensions.realloc(1);
206cdf0e10cSrcweir             m_aFilterExtensions[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "*.odb" ) );
207cdf0e10cSrcweir         }
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     // -----------------------------------------------------------------------------
IMPL_LINK(DatabaseLocationInputController_Impl,OnControlAction,VclWindowEvent *,_pEvent)211cdf0e10cSrcweir     IMPL_LINK( DatabaseLocationInputController_Impl, OnControlAction, VclWindowEvent*, _pEvent )
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         if  (   ( _pEvent->GetWindow() == &m_rBrowseButton )
214cdf0e10cSrcweir             &&  ( _pEvent->GetId() == VCLEVENT_BUTTON_CLICK )
215cdf0e10cSrcweir             )
216cdf0e10cSrcweir         {
217cdf0e10cSrcweir             impl_onBrowseButtonClicked();
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         if  (   ( _pEvent->GetWindow() == &m_rLocationInput )
221cdf0e10cSrcweir             &&  ( _pEvent->GetId() == VCLEVENT_EDIT_MODIFY )
222cdf0e10cSrcweir             )
223cdf0e10cSrcweir         {
224cdf0e10cSrcweir             impl_onLocationModified();
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir         return 0L;
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     // -----------------------------------------------------------------------------
impl_getCurrentURL() const231cdf0e10cSrcweir     String DatabaseLocationInputController_Impl::impl_getCurrentURL() const
232cdf0e10cSrcweir     {
233cdf0e10cSrcweir         String sCurrentFile( m_rLocationInput.GetText() );
234cdf0e10cSrcweir         if ( sCurrentFile.Len() )
235cdf0e10cSrcweir         {
236cdf0e10cSrcweir             ::svt::OFileNotation aCurrentFile( sCurrentFile );
237cdf0e10cSrcweir             sCurrentFile = aCurrentFile.get( ::svt::OFileNotation::N_URL );
238cdf0e10cSrcweir         }
239cdf0e10cSrcweir         return sCurrentFile;
240cdf0e10cSrcweir     }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir     // -----------------------------------------------------------------------------
impl_onBrowseButtonClicked()243cdf0e10cSrcweir     void DatabaseLocationInputController_Impl::impl_onBrowseButtonClicked()
244cdf0e10cSrcweir     {
245cdf0e10cSrcweir         ::sfx2::FileDialogHelper aFileDlg(
246cdf0e10cSrcweir             TemplateDescription::FILESAVE_AUTOEXTENSION,
247cdf0e10cSrcweir             WB_STDMODAL | WB_SAVEAS,
248cdf0e10cSrcweir             m_rLocationInput.GetSystemWindow()
249cdf0e10cSrcweir         );
250cdf0e10cSrcweir         aFileDlg.SetDisplayDirectory( impl_getCurrentURL() );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         aFileDlg.AddFilter( m_sFilterUIName, ::rtl::OUStringBuffer().appendAscii( "*." ).append( m_aFilterExtensions[0] ).makeStringAndClear() );
253cdf0e10cSrcweir         aFileDlg.SetCurrentFilter( m_sFilterUIName );
254cdf0e10cSrcweir 
255cdf0e10cSrcweir         if ( aFileDlg.Execute() == ERRCODE_NONE )
256cdf0e10cSrcweir         {
257cdf0e10cSrcweir             INetURLObject aURL( aFileDlg.GetPath() );
258cdf0e10cSrcweir             if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
259cdf0e10cSrcweir             {
260cdf0e10cSrcweir                 ::svt::OFileNotation aFileNotation( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
261cdf0e10cSrcweir                 m_rLocationInput.SetText( aFileNotation.get( ::svt::OFileNotation::N_SYSTEM ) );
262cdf0e10cSrcweir                 m_rLocationInput.GetModifyHdl().Call( &m_rLocationInput );
263cdf0e10cSrcweir                 // the dialog already checked for the file's existence, so we don't need to, again
264cdf0e10cSrcweir                 m_bNeedExistenceCheck = false;
265cdf0e10cSrcweir             }
266cdf0e10cSrcweir         }
267cdf0e10cSrcweir     }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     // -----------------------------------------------------------------------------
impl_onLocationModified()270cdf0e10cSrcweir     void DatabaseLocationInputController_Impl::impl_onLocationModified()
271cdf0e10cSrcweir     {
272cdf0e10cSrcweir         m_bNeedExistenceCheck = true;
273cdf0e10cSrcweir     }
274cdf0e10cSrcweir 
275cdf0e10cSrcweir     //====================================================================
276cdf0e10cSrcweir 	//= DatabaseLocationInputController
277cdf0e10cSrcweir 	//====================================================================
278cdf0e10cSrcweir 	//--------------------------------------------------------------------
DatabaseLocationInputController(const::comphelper::ComponentContext & _rContext,::svt::OFileURLControl & _rLocationInput,PushButton & _rBrowseButton)279cdf0e10cSrcweir     DatabaseLocationInputController::DatabaseLocationInputController( const ::comphelper::ComponentContext& _rContext,
280cdf0e10cSrcweir             ::svt::OFileURLControl& _rLocationInput, PushButton& _rBrowseButton )
281cdf0e10cSrcweir         :m_pImpl( new DatabaseLocationInputController_Impl( _rContext, _rLocationInput, _rBrowseButton ) )
282cdf0e10cSrcweir     {
283cdf0e10cSrcweir     }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 	//--------------------------------------------------------------------
~DatabaseLocationInputController()286cdf0e10cSrcweir     DatabaseLocationInputController::~DatabaseLocationInputController()
287cdf0e10cSrcweir     {
288cdf0e10cSrcweir     }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 	//--------------------------------------------------------------------
prepareCommit()291cdf0e10cSrcweir     bool DatabaseLocationInputController::prepareCommit()
292cdf0e10cSrcweir     {
293cdf0e10cSrcweir         return m_pImpl->prepareCommit();
294cdf0e10cSrcweir     }
295cdf0e10cSrcweir 
296cdf0e10cSrcweir 	//--------------------------------------------------------------------
setURL(const String & _rURL)297cdf0e10cSrcweir     void DatabaseLocationInputController::setURL( const String& _rURL )
298cdf0e10cSrcweir     {
299cdf0e10cSrcweir         m_pImpl->setURL( _rURL );
300cdf0e10cSrcweir     }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 	//--------------------------------------------------------------------
getURL() const303cdf0e10cSrcweir     String DatabaseLocationInputController::getURL() const
304cdf0e10cSrcweir     {
305cdf0e10cSrcweir         return m_pImpl->getURL();
306cdf0e10cSrcweir     }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir //........................................................................
309cdf0e10cSrcweir } // namespace svx
310cdf0e10cSrcweir //........................................................................
311