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