Bump for 3.6-28
[LibreOffice.git] / extensions / source / propctrlr / sqlcommanddesign.cxx
blob46274df60ca919a4db24f9bc4fbac6ae5a9e6fcf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
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.
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).
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.
27 ************************************************************************/
29 #include "sqlcommanddesign.hxx"
30 #include "formstrings.hxx"
31 #include "formresid.hrc"
32 #include "modulepcr.hxx"
33 #include "unourl.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/awt/XWindow.hpp>
37 #include <com/sun/star/awt/XTopWindow.hpp>
38 #include <com/sun/star/uno/Sequence.hxx>
39 #include <com/sun/star/frame/XTitle.hpp>
40 #include <com/sun/star/frame/XComponentLoader.hpp>
41 #include <com/sun/star/frame/XController.hpp>
42 #include <com/sun/star/lang/NullPointerException.hpp>
43 #include <com/sun/star/lang/DisposedException.hpp>
44 #include <com/sun/star/frame/FrameSearchFlag.hpp>
45 #include <com/sun/star/frame/XFramesSupplier.hpp>
46 #include <com/sun/star/sdbc/XConnection.hpp>
47 #include <com/sun/star/util/XCloseable.hpp>
48 #include <com/sun/star/frame/XDispatchProvider.hpp>
49 #include <com/sun/star/beans/XPropertySetInfo.hpp>
50 #include <com/sun/star/sdb/CommandType.hpp>
51 /** === end UNO includes === **/
53 #include <svtools/localresaccess.hxx>
54 #include <tools/diagnose_ex.h>
55 #include <osl/diagnose.h>
57 //........................................................................
58 namespace pcr
60 //........................................................................
62 /** === begin UNO using === **/
63 using ::com::sun::star::uno::Reference;
64 using ::com::sun::star::lang::XMultiComponentFactory;
65 using ::com::sun::star::beans::PropertyChangeEvent;
66 using ::com::sun::star::uno::RuntimeException;
67 using ::com::sun::star::frame::XFrame;
68 using ::com::sun::star::awt::XTopWindow;
69 using ::com::sun::star::awt::XWindow;
70 using ::com::sun::star::uno::Exception;
71 using ::com::sun::star::uno::UNO_QUERY_THROW;
72 using ::com::sun::star::uno::UNO_QUERY;
73 using ::com::sun::star::beans::PropertyValue;
74 using ::com::sun::star::uno::Sequence;
75 using ::com::sun::star::lang::XComponent;
76 using ::com::sun::star::frame::XComponentLoader;
77 using ::com::sun::star::beans::XPropertySet;
78 using ::com::sun::star::beans::XPropertySetInfo;
79 using ::com::sun::star::frame::XController;
80 using ::com::sun::star::frame::XTitle;
81 using ::com::sun::star::lang::EventObject;
82 using ::com::sun::star::lang::NullPointerException;
83 using ::com::sun::star::lang::DisposedException;
84 using ::com::sun::star::uno::makeAny;
85 using ::com::sun::star::uno::XComponentContext;
86 using ::com::sun::star::frame::XFramesSupplier;
87 using ::com::sun::star::frame::XFrames;
88 using ::com::sun::star::util::XCloseable;
89 using ::com::sun::star::uno::TypeClass_STRING;
90 using ::com::sun::star::lang::XMultiServiceFactory;
91 using ::com::sun::star::frame::XDispatchProvider;
92 using ::com::sun::star::frame::XDispatch;
93 using ::com::sun::star::uno::Any;
94 /** === end UNO using === **/
95 namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
96 namespace CommandType = ::com::sun::star::sdb::CommandType;
98 //====================================================================
99 //= ISQLCommandAdapter
100 //====================================================================
101 //--------------------------------------------------------------------
102 ISQLCommandAdapter::~ISQLCommandAdapter()
106 //====================================================================
107 //= SQLCommandDesigner
108 //====================================================================
109 //--------------------------------------------------------------------
110 SQLCommandDesigner::SQLCommandDesigner( const Reference< XComponentContext >& _rxContext,
111 const ::rtl::Reference< ISQLCommandAdapter >& _rxPropertyAdapter,
112 const ::dbtools::SharedConnection& _rConnection, const Link& _rCloseLink )
113 :m_xContext( _rxContext )
114 ,m_xConnection( _rConnection )
115 ,m_xObjectAdapter( _rxPropertyAdapter )
116 ,m_aCloseLink( _rCloseLink )
118 if ( m_xContext.is() )
119 m_xORB = m_xContext->getServiceManager();
120 if ( !m_xORB.is() || !_rxPropertyAdapter.is() || !m_xConnection.is() )
121 throw NullPointerException();
123 impl_doOpenDesignerFrame_nothrow();
126 //--------------------------------------------------------------------
127 SQLCommandDesigner::~SQLCommandDesigner()
131 //--------------------------------------------------------------------
132 void SAL_CALL SQLCommandDesigner::propertyChange( const PropertyChangeEvent& Event ) throw (RuntimeException)
134 OSL_ENSURE( m_xDesigner.is() && ( Event.Source == m_xDesigner ), "SQLCommandDesigner::propertyChange: where did this come from?" );
136 if ( m_xDesigner.is() && ( Event.Source == m_xDesigner ) )
140 if ( PROPERTY_ACTIVECOMMAND == Event.PropertyName )
142 ::rtl::OUString sCommand;
143 OSL_VERIFY( Event.NewValue >>= sCommand );
144 m_xObjectAdapter->setSQLCommand( sCommand );
146 else if ( PROPERTY_ESCAPE_PROCESSING == Event.PropertyName )
148 sal_Bool bEscapeProcessing( sal_False );
149 OSL_VERIFY( Event.NewValue >>= bEscapeProcessing );
150 m_xObjectAdapter->setEscapeProcessing( bEscapeProcessing );
153 catch( const RuntimeException& ) { throw; }
154 catch( const Exception& )
156 // not allowed to leave, so silence it
157 DBG_UNHANDLED_EXCEPTION();
162 //--------------------------------------------------------------------
163 void SAL_CALL SQLCommandDesigner::disposing( const EventObject& Source ) throw (RuntimeException)
165 if ( m_xDesigner.is() && ( Source.Source == m_xDesigner ) )
167 impl_designerClosed_nothrow();
168 m_xDesigner.clear();
172 //--------------------------------------------------------------------
173 void SQLCommandDesigner::dispose()
175 if ( impl_isDisposed() )
176 return;
178 if ( isActive() )
179 impl_closeDesigner_nothrow();
181 m_xConnection.clear();
182 m_xContext.clear();
183 m_xORB.clear();
186 //--------------------------------------------------------------------
187 void SQLCommandDesigner::impl_checkDisposed_throw() const
189 if ( impl_isDisposed() )
190 throw DisposedException();
193 //--------------------------------------------------------------------
194 void SQLCommandDesigner::raise() const
196 impl_checkDisposed_throw();
197 impl_raise_nothrow();
200 //------------------------------------------------------------------------
201 bool SQLCommandDesigner::suspend() const
203 impl_checkDisposed_throw();
204 return impl_trySuspendDesigner_nothrow();
207 //--------------------------------------------------------------------
208 void SQLCommandDesigner::impl_raise_nothrow() const
210 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_raise_nothrow: not active!" );
211 if ( !isActive() )
212 return;
216 // activate the frame for this component
217 Reference< XFrame > xFrame( m_xDesigner->getFrame(), UNO_QUERY_THROW );
218 Reference< XWindow > xWindow( xFrame->getContainerWindow(), UNO_QUERY_THROW );
219 Reference< XTopWindow > xTopWindow( xWindow, UNO_QUERY_THROW );
221 xTopWindow->toFront();
222 xWindow->setFocus();
224 catch( const Exception& )
226 DBG_UNHANDLED_EXCEPTION();
230 //--------------------------------------------------------------------
231 void SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow()
233 OSL_PRECOND( !isActive(),
234 "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: already active!" );
235 OSL_PRECOND( m_xConnection.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: this will crash!" );
236 osl_incrementInterlockedCount(&m_refCount);
240 // for various reasons, we don't want the new frame to appear in the desktop's frame list
241 // thus, we create a blank frame at the desktop, remove it from the desktop's frame list
242 // immediately, and then load the component into this blank (and now parent-less) frame
243 Reference< XComponentLoader > xLoader( impl_createEmptyParentlessTask_nothrow(), UNO_QUERY_THROW );
244 Sequence< PropertyValue > aArgs( 5 );
245 aArgs[0].Name = PROPERTY_ACTIVE_CONNECTION;
246 aArgs[0].Value <<= m_xConnection.getTyped();
248 aArgs[1].Name = PROPERTY_COMMAND;
249 aArgs[1].Value <<= m_xObjectAdapter->getSQLCommand();
250 aArgs[2].Name = PROPERTY_COMMANDTYPE;
251 aArgs[2].Value <<= (sal_Int32)CommandType::COMMAND;
252 aArgs[3].Name = PROPERTY_ESCAPE_PROCESSING;
253 aArgs[3].Value <<= m_xObjectAdapter->getEscapeProcessing();
255 aArgs[4].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "GraphicalDesign" ) );
256 aArgs[4].Value <<= m_xObjectAdapter->getEscapeProcessing();
258 Reference< XComponent > xQueryDesign = xLoader->loadComponentFromURL(
259 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".component:DB/QueryDesign" ) ),
260 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_self" ) ),
261 FrameSearchFlag::TASKS | FrameSearchFlag::CREATE,
262 aArgs
265 // remember this newly loaded component - we need to care for it e.g. when we're suspended
266 m_xDesigner = m_xDesigner.query( xQueryDesign );
267 OSL_ENSURE( m_xDesigner.is() || !xQueryDesign.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the component is expected to be a controller!" );
268 if ( m_xDesigner.is() )
270 Reference< XPropertySet > xQueryDesignProps( m_xDesigner, UNO_QUERY );
271 OSL_ENSURE( xQueryDesignProps.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the controller should have properties!" );
272 if ( xQueryDesignProps.is() )
274 xQueryDesignProps->addPropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
275 xQueryDesignProps->addPropertyChangeListener( PROPERTY_ESCAPE_PROCESSING, this );
279 // get the frame which we just opened and set it's title
280 Reference< XTitle> xTitle(xQueryDesign,UNO_QUERY);
281 if ( xTitle.is() )
283 ::svt::OLocalResourceAccess aEnumStrings( PcrRes( RID_RSC_ENUM_COMMAND_TYPE ), RSC_RESOURCE );
284 ::rtl::OUString sDisplayName = String( PcrRes( CommandType::COMMAND + 1 ) );
285 xTitle->setTitle( sDisplayName );
288 catch( const Exception& )
290 DBG_UNHANDLED_EXCEPTION();
291 m_xDesigner.clear();
293 osl_decrementInterlockedCount(&m_refCount);
296 //------------------------------------------------------------------------
297 Reference< XFrame > SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow( ) const
299 OSL_PRECOND( m_xORB.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: this will crash!" );
301 Reference< XFrame > xFrame;
304 Reference< XInterface > xDesktop ( m_xORB->createInstanceWithContext( SERVICE_DESKTOP, m_xContext ) );
305 Reference< XFrame > xDesktopFrame ( xDesktop, UNO_QUERY_THROW );
306 Reference< XFramesSupplier > xSuppDesktopFrames( xDesktopFrame, UNO_QUERY_THROW );
308 Reference< XFrames > xDesktopFramesCollection( xSuppDesktopFrames->getFrames(), UNO_QUERY_THROW );
309 xFrame = xDesktopFrame->findFrame( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_blank" ) ), FrameSearchFlag::CREATE );
310 OSL_ENSURE( xFrame.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: could not create an empty frame!" );
311 xDesktopFramesCollection->remove( xFrame );
313 catch( const Exception& )
315 DBG_UNHANDLED_EXCEPTION();
317 return xFrame;
320 //------------------------------------------------------------------------
321 void SQLCommandDesigner::impl_designerClosed_nothrow()
323 if ( m_aCloseLink.IsSet() )
324 m_aCloseLink.Call( this );
327 //------------------------------------------------------------------------
328 void SQLCommandDesigner::impl_closeDesigner_nothrow()
330 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_closeDesigner_nothrow: invalid calle!" );
331 // close it
334 // do not listen anymore ....
335 Reference< XPropertySet > xProps( m_xDesigner, UNO_QUERY );
336 if ( xProps.is() )
337 xProps->removePropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
339 // we need to close the frame via the "user interface", by dispatching a close command,
340 // instead of calling XCloseable::close directly. The latter method would also close
341 // the frame, but not care for things like shutting down the office when the last
342 // frame is gone ...
343 const UnoURL aCloseURL( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:CloseDoc" ) ),
344 Reference< XMultiServiceFactory >( m_xORB, UNO_QUERY ) );
346 Reference< XDispatchProvider > xProvider( m_xDesigner->getFrame(), UNO_QUERY_THROW );
347 Reference< XDispatch > xDispatch( xProvider->queryDispatch( aCloseURL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_top" ) ), FrameSearchFlag::SELF ) );
348 OSL_ENSURE( xDispatch.is(), "SQLCommandDesigner::impl_closeDesigner_nothrow: no dispatcher for the CloseDoc command!" );
349 if ( xDispatch.is() )
351 xDispatch->dispatch( aCloseURL, Sequence< PropertyValue >( ) );
353 else
355 // fallback: use the XCloseable::close (with all possible disadvantages)
356 Reference< XCloseable > xClose( m_xDesigner->getFrame(), UNO_QUERY );
357 if ( xClose.is() )
358 xClose->close( sal_True );
361 catch( const Exception& )
363 DBG_UNHANDLED_EXCEPTION();
366 m_xDesigner.clear();
369 //------------------------------------------------------------------------
370 bool SQLCommandDesigner::impl_trySuspendDesigner_nothrow() const
372 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_trySuspendDesigner_nothrow: no active designer, this will crash!" );
373 sal_Bool bAllow = sal_True;
376 bAllow = m_xDesigner->suspend( sal_True );
378 catch( const Exception& )
380 DBG_UNHANDLED_EXCEPTION();
382 return bAllow;
385 //........................................................................
386 } // namespace pcr
387 //........................................................................
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */