update dev300-m58
[ooovba.git] / extensions / source / propctrlr / sqlcommanddesign.cxx
blobefd2eea07182e3b9ef8d8ec7d02b3601acd200e0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sqlcommanddesign.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "sqlcommanddesign.hxx"
34 #include "formstrings.hxx"
35 #ifndef _EXTENSIONS_FORMCTRLR_PROPRESID_HRC_
36 #include "formresid.hrc"
37 #endif
38 #ifndef _EXTENSIONS_PROPCTRLR_MODULEPRC_HXX_
39 #include "modulepcr.hxx"
40 #endif
41 #include "unourl.hxx"
43 /** === begin UNO includes === **/
44 #include <com/sun/star/awt/XWindow.hpp>
45 #include <com/sun/star/awt/XTopWindow.hpp>
46 #include <com/sun/star/uno/Sequence.hxx>
47 #include <com/sun/star/frame/XTitle.hpp>
48 #include <com/sun/star/frame/XComponentLoader.hpp>
49 #include <com/sun/star/frame/XController.hpp>
50 #include <com/sun/star/lang/NullPointerException.hpp>
51 #include <com/sun/star/lang/DisposedException.hpp>
52 #include <com/sun/star/frame/FrameSearchFlag.hpp>
53 #include <com/sun/star/frame/XFramesSupplier.hpp>
54 #include <com/sun/star/sdbc/XConnection.hpp>
55 #include <com/sun/star/util/XCloseable.hpp>
56 #include <com/sun/star/frame/XDispatchProvider.hpp>
57 #include <com/sun/star/beans/XPropertySetInfo.hpp>
58 #include <com/sun/star/sdb/CommandType.hpp>
59 /** === end UNO includes === **/
61 #include <svtools/localresaccess.hxx>
62 #include <tools/diagnose_ex.h>
63 #include <osl/diagnose.h>
65 //........................................................................
66 namespace pcr
68 //........................................................................
70 /** === begin UNO using === **/
71 using ::com::sun::star::uno::Reference;
72 using ::com::sun::star::lang::XMultiComponentFactory;
73 using ::com::sun::star::beans::PropertyChangeEvent;
74 using ::com::sun::star::uno::RuntimeException;
75 using ::com::sun::star::frame::XFrame;
76 using ::com::sun::star::awt::XTopWindow;
77 using ::com::sun::star::awt::XWindow;
78 using ::com::sun::star::uno::Exception;
79 using ::com::sun::star::uno::UNO_QUERY_THROW;
80 using ::com::sun::star::uno::UNO_QUERY;
81 using ::com::sun::star::beans::PropertyValue;
82 using ::com::sun::star::uno::Sequence;
83 using ::com::sun::star::lang::XComponent;
84 using ::com::sun::star::frame::XComponentLoader;
85 using ::com::sun::star::beans::XPropertySet;
86 using ::com::sun::star::beans::XPropertySetInfo;
87 using ::com::sun::star::frame::XController;
88 using ::com::sun::star::frame::XTitle;
89 using ::com::sun::star::lang::EventObject;
90 using ::com::sun::star::lang::NullPointerException;
91 using ::com::sun::star::lang::DisposedException;
92 using ::com::sun::star::uno::makeAny;
93 using ::com::sun::star::uno::XComponentContext;
94 using ::com::sun::star::frame::XFramesSupplier;
95 using ::com::sun::star::frame::XFrames;
96 using ::com::sun::star::util::XCloseable;
97 using ::com::sun::star::uno::TypeClass_STRING;
98 using ::com::sun::star::lang::XMultiServiceFactory;
99 using ::com::sun::star::frame::XDispatchProvider;
100 using ::com::sun::star::frame::XDispatch;
101 using ::com::sun::star::uno::Any;
102 /** === end UNO using === **/
103 namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
104 namespace CommandType = ::com::sun::star::sdb::CommandType;
106 //====================================================================
107 //= ISQLCommandAdapter
108 //====================================================================
109 //--------------------------------------------------------------------
110 ISQLCommandAdapter::~ISQLCommandAdapter()
114 //====================================================================
115 //= SQLCommandDesigner
116 //====================================================================
117 //--------------------------------------------------------------------
118 SQLCommandDesigner::SQLCommandDesigner( const Reference< XComponentContext >& _rxContext,
119 const ::rtl::Reference< ISQLCommandAdapter >& _rxPropertyAdapter,
120 const ::dbtools::SharedConnection& _rConnection, const Link& _rCloseLink )
121 :m_xContext( _rxContext )
122 ,m_xConnection( _rConnection )
123 ,m_xObjectAdapter( _rxPropertyAdapter )
124 ,m_aCloseLink( _rCloseLink )
126 if ( m_xContext.is() )
127 m_xORB = m_xContext->getServiceManager();
128 if ( !m_xORB.is() || !_rxPropertyAdapter.is() || !m_xConnection.is() )
129 throw NullPointerException();
131 impl_doOpenDesignerFrame_nothrow();
134 //--------------------------------------------------------------------
135 SQLCommandDesigner::~SQLCommandDesigner()
139 //--------------------------------------------------------------------
140 void SAL_CALL SQLCommandDesigner::propertyChange( const PropertyChangeEvent& Event ) throw (RuntimeException)
142 OSL_ENSURE( m_xDesigner.is() && ( Event.Source == m_xDesigner ), "SQLCommandDesigner::propertyChange: where did this come from?" );
144 if ( m_xDesigner.is() && ( Event.Source == m_xDesigner ) )
148 if ( PROPERTY_ACTIVECOMMAND == Event.PropertyName )
150 ::rtl::OUString sCommand;
151 OSL_VERIFY( Event.NewValue >>= sCommand );
152 m_xObjectAdapter->setSQLCommand( sCommand );
154 else if ( PROPERTY_ESCAPE_PROCESSING == Event.PropertyName )
156 sal_Bool bEscapeProcessing( sal_False );
157 OSL_VERIFY( Event.NewValue >>= bEscapeProcessing );
158 m_xObjectAdapter->setEscapeProcessing( bEscapeProcessing );
161 catch( const RuntimeException& ) { throw; }
162 catch( const Exception& )
164 // not allowed to leave, so silence it
165 DBG_UNHANDLED_EXCEPTION();
170 //--------------------------------------------------------------------
171 void SAL_CALL SQLCommandDesigner::disposing( const EventObject& Source ) throw (RuntimeException)
173 if ( m_xDesigner.is() && ( Source.Source == m_xDesigner ) )
175 impl_designerClosed_nothrow();
176 m_xDesigner.clear();
180 //--------------------------------------------------------------------
181 void SQLCommandDesigner::dispose()
183 if ( impl_isDisposed() )
184 return;
186 if ( isActive() )
187 impl_closeDesigner_nothrow();
189 m_xConnection.clear();
190 m_xContext.clear();
191 m_xORB.clear();
194 //--------------------------------------------------------------------
195 void SQLCommandDesigner::impl_checkDisposed_throw() const
197 if ( impl_isDisposed() )
198 throw DisposedException();
201 //--------------------------------------------------------------------
202 void SQLCommandDesigner::raise() const
204 impl_checkDisposed_throw();
205 impl_raise_nothrow();
208 //------------------------------------------------------------------------
209 bool SQLCommandDesigner::suspend() const
211 impl_checkDisposed_throw();
212 return impl_trySuspendDesigner_nothrow();
215 //--------------------------------------------------------------------
216 void SQLCommandDesigner::impl_raise_nothrow() const
218 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_raise_nothrow: not active!" );
219 if ( !isActive() )
220 return;
224 // activate the frame for this component
225 Reference< XFrame > xFrame( m_xDesigner->getFrame(), UNO_QUERY_THROW );
226 Reference< XWindow > xWindow( xFrame->getContainerWindow(), UNO_QUERY_THROW );
227 Reference< XTopWindow > xTopWindow( xWindow, UNO_QUERY_THROW );
229 xTopWindow->toFront();
230 xWindow->setFocus();
232 catch( const Exception& )
234 DBG_UNHANDLED_EXCEPTION();
238 //--------------------------------------------------------------------
239 void SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow()
241 OSL_PRECOND( !isActive(),
242 "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: already active!" );
243 OSL_PRECOND( m_xConnection.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: this will crash!" );
244 osl_incrementInterlockedCount(&m_refCount);
248 // for various reasons, we don't want the new frame to appear in the desktop's frame list
249 // thus, we create a blank frame at the desktop, remove it from the desktop's frame list
250 // immediately, and then load the component into this blank (and now parent-less) frame
251 Reference< XComponentLoader > xLoader( impl_createEmptyParentlessTask_nothrow(), UNO_QUERY_THROW );
252 Sequence< PropertyValue > aArgs( 5 );
253 aArgs[0].Name = PROPERTY_ACTIVE_CONNECTION;
254 aArgs[0].Value <<= m_xConnection.getTyped();
256 aArgs[1].Name = PROPERTY_COMMAND;
257 aArgs[1].Value <<= m_xObjectAdapter->getSQLCommand();
258 aArgs[2].Name = PROPERTY_COMMANDTYPE;
259 aArgs[2].Value <<= (sal_Int32)CommandType::COMMAND;
260 aArgs[3].Name = PROPERTY_ESCAPE_PROCESSING;
261 aArgs[3].Value <<= m_xObjectAdapter->getEscapeProcessing();
263 aArgs[4].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "GraphicalDesign" ) );
264 aArgs[4].Value <<= m_xObjectAdapter->getEscapeProcessing();
266 Reference< XComponent > xQueryDesign = xLoader->loadComponentFromURL(
267 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".component:DB/QueryDesign" ) ),
268 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_self" ) ),
269 FrameSearchFlag::TASKS | FrameSearchFlag::CREATE,
270 aArgs
273 // remember this newly loaded component - we need to care for it e.g. when we're suspended
274 m_xDesigner = m_xDesigner.query( xQueryDesign );
275 OSL_ENSURE( m_xDesigner.is() || !xQueryDesign.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the component is expected to be a controller!" );
276 if ( m_xDesigner.is() )
278 Reference< XPropertySet > xQueryDesignProps( m_xDesigner, UNO_QUERY );
279 OSL_ENSURE( xQueryDesignProps.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the controller should have properties!" );
280 if ( xQueryDesignProps.is() )
282 xQueryDesignProps->addPropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
283 xQueryDesignProps->addPropertyChangeListener( PROPERTY_ESCAPE_PROCESSING, this );
287 // get the frame which we just opened and set it's title
288 Reference< XTitle> xTitle(xQueryDesign,UNO_QUERY);
289 if ( xTitle.is() )
291 ::svt::OLocalResourceAccess aEnumStrings( PcrRes( RID_RSC_ENUM_COMMAND_TYPE ), RSC_RESOURCE );
292 ::rtl::OUString sDisplayName = String( PcrRes( CommandType::COMMAND + 1 ) );
293 xTitle->setTitle( sDisplayName );
296 catch( const Exception& )
298 DBG_UNHANDLED_EXCEPTION();
299 m_xDesigner.clear();
301 osl_decrementInterlockedCount(&m_refCount);
304 //------------------------------------------------------------------------
305 Reference< XFrame > SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow( ) const
307 OSL_PRECOND( m_xORB.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: this will crash!" );
309 Reference< XFrame > xFrame;
312 Reference< XInterface > xDesktop ( m_xORB->createInstanceWithContext( SERVICE_DESKTOP, m_xContext ) );
313 Reference< XFrame > xDesktopFrame ( xDesktop, UNO_QUERY_THROW );
314 Reference< XFramesSupplier > xSuppDesktopFrames( xDesktopFrame, UNO_QUERY_THROW );
316 Reference< XFrames > xDesktopFramesCollection( xSuppDesktopFrames->getFrames(), UNO_QUERY_THROW );
317 xFrame = xDesktopFrame->findFrame( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_blank" ) ), FrameSearchFlag::CREATE );
318 OSL_ENSURE( xFrame.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: could not create an empty frame!" );
319 xDesktopFramesCollection->remove( xFrame );
321 catch( const Exception& )
323 DBG_UNHANDLED_EXCEPTION();
325 return xFrame;
328 //------------------------------------------------------------------------
329 void SQLCommandDesigner::impl_designerClosed_nothrow()
331 if ( m_aCloseLink.IsSet() )
332 m_aCloseLink.Call( this );
335 //------------------------------------------------------------------------
336 void SQLCommandDesigner::impl_closeDesigner_nothrow()
338 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_closeDesigner_nothrow: invalid calle!" );
339 // close it
342 // do not listen anymore ....
343 Reference< XPropertySet > xProps( m_xDesigner, UNO_QUERY );
344 if ( xProps.is() )
345 xProps->removePropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
347 // we need to close the frame via the "user interface", by dispatching a close command,
348 // instead of calling XCloseable::close directly. The latter method would also close
349 // the frame, but not care for things like shutting down the office when the last
350 // frame is gone ...
351 const UnoURL aCloseURL( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:CloseDoc" ) ),
352 Reference< XMultiServiceFactory >( m_xORB, UNO_QUERY ) );
354 Reference< XDispatchProvider > xProvider( m_xDesigner->getFrame(), UNO_QUERY_THROW );
355 Reference< XDispatch > xDispatch( xProvider->queryDispatch( aCloseURL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_top" ) ), FrameSearchFlag::SELF ) );
356 OSL_ENSURE( xDispatch.is(), "SQLCommandDesigner::impl_closeDesigner_nothrow: no dispatcher for the CloseDoc command!" );
357 if ( xDispatch.is() )
359 xDispatch->dispatch( aCloseURL, Sequence< PropertyValue >( ) );
361 else
363 // fallback: use the XCloseable::close (with all possible disadvantages)
364 Reference< XCloseable > xClose( m_xDesigner->getFrame(), UNO_QUERY );
365 if ( xClose.is() )
366 xClose->close( sal_True );
369 catch( const Exception& )
371 DBG_UNHANDLED_EXCEPTION();
374 m_xDesigner.clear();
377 //------------------------------------------------------------------------
378 bool SQLCommandDesigner::impl_trySuspendDesigner_nothrow() const
380 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_trySuspendDesigner_nothrow: no active designer, this will crash!" );
381 sal_Bool bAllow = sal_True;
384 bAllow = m_xDesigner->suspend( sal_True );
386 catch( const Exception& )
388 DBG_UNHANDLED_EXCEPTION();
390 return bAllow;
393 //........................................................................
394 } // namespace pcr
395 //........................................................................