bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / propctrlr / sqlcommanddesign.cxx
blob753ab0eea7f9768172a1d33860ed03618948817d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sqlcommanddesign.hxx"
21 #include "formstrings.hxx"
22 #include "formresid.hrc"
23 #include "modulepcr.hxx"
24 #include "unourl.hxx"
26 #include <com/sun/star/awt/XWindow.hpp>
27 #include <com/sun/star/awt/XTopWindow.hpp>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <com/sun/star/frame/Desktop.hpp>
30 #include <com/sun/star/frame/XTitle.hpp>
31 #include <com/sun/star/frame/XComponentLoader.hpp>
32 #include <com/sun/star/frame/XController.hpp>
33 #include <com/sun/star/lang/NullPointerException.hpp>
34 #include <com/sun/star/lang/DisposedException.hpp>
35 #include <com/sun/star/frame/FrameSearchFlag.hpp>
36 #include <com/sun/star/frame/XFramesSupplier.hpp>
37 #include <com/sun/star/sdbc/XConnection.hpp>
38 #include <com/sun/star/util/XCloseable.hpp>
39 #include <com/sun/star/frame/XDispatchProvider.hpp>
40 #include <com/sun/star/beans/XPropertySetInfo.hpp>
41 #include <com/sun/star/sdb/CommandType.hpp>
43 #include <svtools/localresaccess.hxx>
44 #include <tools/diagnose_ex.h>
45 #include <osl/diagnose.h>
48 namespace pcr
52 using ::com::sun::star::uno::Reference;
53 using ::com::sun::star::lang::XMultiComponentFactory;
54 using ::com::sun::star::beans::PropertyChangeEvent;
55 using ::com::sun::star::uno::RuntimeException;
56 using ::com::sun::star::frame::XFrame;
57 using ::com::sun::star::awt::XTopWindow;
58 using ::com::sun::star::awt::XWindow;
59 using ::com::sun::star::uno::Exception;
60 using ::com::sun::star::uno::UNO_QUERY_THROW;
61 using ::com::sun::star::uno::UNO_QUERY;
62 using ::com::sun::star::beans::PropertyValue;
63 using ::com::sun::star::uno::Sequence;
64 using ::com::sun::star::lang::XComponent;
65 using ::com::sun::star::frame::XComponentLoader;
66 using ::com::sun::star::beans::XPropertySet;
67 using ::com::sun::star::beans::XPropertySetInfo;
68 using ::com::sun::star::frame::XController;
69 using ::com::sun::star::frame::XTitle;
70 using ::com::sun::star::lang::EventObject;
71 using ::com::sun::star::lang::NullPointerException;
72 using ::com::sun::star::lang::DisposedException;
73 using ::com::sun::star::uno::makeAny;
74 using ::com::sun::star::uno::XComponentContext;
75 using ::com::sun::star::frame::XFramesSupplier;
76 using ::com::sun::star::frame::XFrames;
77 using ::com::sun::star::util::XCloseable;
78 using ::com::sun::star::uno::TypeClass_STRING;
79 using ::com::sun::star::lang::XMultiServiceFactory;
80 using ::com::sun::star::frame::XDispatchProvider;
81 using ::com::sun::star::frame::XDispatch;
82 using ::com::sun::star::frame::Desktop;
83 using ::com::sun::star::frame::XDesktop2;
84 using ::com::sun::star::uno::Any;
86 namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
87 namespace CommandType = ::com::sun::star::sdb::CommandType;
90 //= ISQLCommandAdapter
93 ISQLCommandAdapter::~ISQLCommandAdapter()
98 //= SQLCommandDesigner
101 SQLCommandDesigner::SQLCommandDesigner( const Reference< XComponentContext >& _rxContext,
102 const ::rtl::Reference< ISQLCommandAdapter >& _rxPropertyAdapter,
103 const ::dbtools::SharedConnection& _rConnection, const Link<>& _rCloseLink )
104 :m_xContext( _rxContext )
105 ,m_xConnection( _rConnection )
106 ,m_xObjectAdapter( _rxPropertyAdapter )
107 ,m_aCloseLink( _rCloseLink )
109 if ( m_xContext.is() )
110 m_xORB = m_xContext->getServiceManager();
111 if ( !m_xORB.is() || !_rxPropertyAdapter.is() || !m_xConnection.is() )
112 throw NullPointerException();
114 impl_doOpenDesignerFrame_nothrow();
118 SQLCommandDesigner::~SQLCommandDesigner()
123 void SAL_CALL SQLCommandDesigner::propertyChange( const PropertyChangeEvent& Event ) throw (RuntimeException, std::exception)
125 OSL_ENSURE( m_xDesigner.is() && ( Event.Source == m_xDesigner ), "SQLCommandDesigner::propertyChange: where did this come from?" );
127 if ( m_xDesigner.is() && ( Event.Source == m_xDesigner ) )
131 if ( PROPERTY_ACTIVECOMMAND == Event.PropertyName )
133 OUString sCommand;
134 OSL_VERIFY( Event.NewValue >>= sCommand );
135 m_xObjectAdapter->setSQLCommand( sCommand );
137 else if ( PROPERTY_ESCAPE_PROCESSING == Event.PropertyName )
139 bool bEscapeProcessing( false );
140 OSL_VERIFY( Event.NewValue >>= bEscapeProcessing );
141 m_xObjectAdapter->setEscapeProcessing( bEscapeProcessing );
144 catch( const RuntimeException& ) { throw; }
145 catch( const Exception& )
147 // not allowed to leave, so silence it
148 DBG_UNHANDLED_EXCEPTION();
154 void SAL_CALL SQLCommandDesigner::disposing( const EventObject& Source ) throw (RuntimeException, std::exception)
156 if ( m_xDesigner.is() && ( Source.Source == m_xDesigner ) )
158 impl_designerClosed_nothrow();
159 m_xDesigner.clear();
164 void SQLCommandDesigner::dispose()
166 if ( impl_isDisposed() )
167 return;
169 if ( isActive() )
170 impl_closeDesigner_nothrow();
172 m_xConnection.clear();
173 m_xContext.clear();
174 m_xORB.clear();
178 void SQLCommandDesigner::impl_checkDisposed_throw() const
180 if ( impl_isDisposed() )
181 throw DisposedException();
185 void SQLCommandDesigner::raise() const
187 impl_checkDisposed_throw();
188 impl_raise_nothrow();
192 bool SQLCommandDesigner::suspend() const
194 impl_checkDisposed_throw();
195 return impl_trySuspendDesigner_nothrow();
199 void SQLCommandDesigner::impl_raise_nothrow() const
201 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_raise_nothrow: not active!" );
202 if ( !isActive() )
203 return;
207 // activate the frame for this component
208 Reference< XFrame > xFrame( m_xDesigner->getFrame(), UNO_QUERY_THROW );
209 Reference< XWindow > xWindow( xFrame->getContainerWindow(), UNO_QUERY_THROW );
210 Reference< XTopWindow > xTopWindow( xWindow, UNO_QUERY_THROW );
212 xTopWindow->toFront();
213 xWindow->setFocus();
215 catch( const Exception& )
217 DBG_UNHANDLED_EXCEPTION();
222 void SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow()
224 OSL_PRECOND( !isActive(),
225 "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: already active!" );
226 OSL_PRECOND( m_xConnection.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: this will crash!" );
227 osl_atomic_increment(&m_refCount);
231 // for various reasons, we don't want the new frame to appear in the desktop's frame list
232 // thus, we create a blank frame at the desktop, remove it from the desktop's frame list
233 // immediately, and then load the component into this blank (and now parent-less) frame
234 Reference< XComponentLoader > xLoader( impl_createEmptyParentlessTask_nothrow(), UNO_QUERY_THROW );
235 Sequence< PropertyValue > aArgs( 5 );
236 aArgs[0].Name = PROPERTY_ACTIVE_CONNECTION;
237 aArgs[0].Value <<= m_xConnection.getTyped();
239 aArgs[1].Name = PROPERTY_COMMAND;
240 aArgs[1].Value <<= m_xObjectAdapter->getSQLCommand();
241 aArgs[2].Name = PROPERTY_COMMANDTYPE;
242 aArgs[2].Value <<= (sal_Int32)CommandType::COMMAND;
243 aArgs[3].Name = PROPERTY_ESCAPE_PROCESSING;
244 aArgs[3].Value <<= m_xObjectAdapter->getEscapeProcessing();
246 aArgs[4].Name = "GraphicalDesign";
247 aArgs[4].Value <<= m_xObjectAdapter->getEscapeProcessing();
249 Reference< XComponent > xQueryDesign = xLoader->loadComponentFromURL(
250 OUString( ".component:DB/QueryDesign" ),
251 OUString( "_self" ),
252 FrameSearchFlag::TASKS | FrameSearchFlag::CREATE,
253 aArgs
256 // remember this newly loaded component - we need to care for it e.g. when we're suspended
257 m_xDesigner.set(xQueryDesign, css::uno::UNO_QUERY);
258 OSL_ENSURE( m_xDesigner.is() || !xQueryDesign.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the component is expected to be a controller!" );
259 if ( m_xDesigner.is() )
261 Reference< XPropertySet > xQueryDesignProps( m_xDesigner, UNO_QUERY );
262 OSL_ENSURE( xQueryDesignProps.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the controller should have properties!" );
263 if ( xQueryDesignProps.is() )
265 xQueryDesignProps->addPropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
266 xQueryDesignProps->addPropertyChangeListener( PROPERTY_ESCAPE_PROCESSING, this );
270 // get the frame which we just opened and set its title
271 Reference< XTitle> xTitle(xQueryDesign,UNO_QUERY);
272 if ( xTitle.is() )
274 ::svt::OLocalResourceAccess aEnumStrings( PcrRes( RID_RSC_ENUM_COMMAND_TYPE ), RSC_RESOURCE );
275 OUString sDisplayName = PcrRes(CommandType::COMMAND + 1).toString();
276 xTitle->setTitle( sDisplayName );
279 catch( const Exception& )
281 DBG_UNHANDLED_EXCEPTION();
282 m_xDesigner.clear();
284 osl_atomic_decrement(&m_refCount);
288 Reference< XFrame > SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow( ) const
290 OSL_PRECOND( m_xORB.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: this will crash!" );
292 Reference< XFrame > xFrame;
295 Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
297 Reference< XFrames > xDesktopFramesCollection( xDesktop->getFrames(), UNO_QUERY_THROW );
298 xFrame = xDesktop->findFrame( OUString( "_blank" ), FrameSearchFlag::CREATE );
299 OSL_ENSURE( xFrame.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: could not create an empty frame!" );
300 xDesktopFramesCollection->remove( xFrame );
302 catch( const Exception& )
304 DBG_UNHANDLED_EXCEPTION();
306 return xFrame;
310 void SQLCommandDesigner::impl_designerClosed_nothrow()
312 if ( m_aCloseLink.IsSet() )
313 m_aCloseLink.Call( this );
317 void SQLCommandDesigner::impl_closeDesigner_nothrow()
319 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_closeDesigner_nothrow: invalid calle!" );
320 // close it
323 // do not listen anymore ....
324 Reference< XPropertySet > xProps( m_xDesigner, UNO_QUERY );
325 if ( xProps.is() )
326 xProps->removePropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
328 // we need to close the frame via the "user interface", by dispatching a close command,
329 // instead of calling XCloseable::close directly. The latter method would also close
330 // the frame, but not care for things like shutting down the office when the last
331 // frame is gone ...
332 const UnoURL aCloseURL( OUString( ".uno:CloseDoc" ),
333 Reference< XMultiServiceFactory >( m_xORB, UNO_QUERY ) );
335 Reference< XDispatchProvider > xProvider( m_xDesigner->getFrame(), UNO_QUERY_THROW );
336 Reference< XDispatch > xDispatch( xProvider->queryDispatch( aCloseURL, OUString( "_top" ), FrameSearchFlag::SELF ) );
337 OSL_ENSURE( xDispatch.is(), "SQLCommandDesigner::impl_closeDesigner_nothrow: no dispatcher for the CloseDoc command!" );
338 if ( xDispatch.is() )
340 xDispatch->dispatch( aCloseURL, Sequence< PropertyValue >( ) );
342 else
344 // fallback: use the XCloseable::close (with all possible disadvantages)
345 Reference< XCloseable > xClose( m_xDesigner->getFrame(), UNO_QUERY );
346 if ( xClose.is() )
347 xClose->close( sal_True );
350 catch( const Exception& )
352 DBG_UNHANDLED_EXCEPTION();
355 m_xDesigner.clear();
359 bool SQLCommandDesigner::impl_trySuspendDesigner_nothrow() const
361 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_trySuspendDesigner_nothrow: no active designer, this will crash!" );
362 bool bAllow = true;
365 bAllow = m_xDesigner->suspend( sal_True );
367 catch( const Exception& )
369 DBG_UNHANDLED_EXCEPTION();
371 return bAllow;
375 } // namespace pcr
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */