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