Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / extensions / source / propctrlr / sqlcommanddesign.cxx
blob0158e92160f74387b8c36026941940227f7590f7
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 <command.hrc>
23 #include <strings.hrc>
24 #include "modulepcr.hxx"
25 #include "unourl.hxx"
27 #include <com/sun/star/awt/XWindow.hpp>
28 #include <com/sun/star/awt/XTopWindow.hpp>
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <com/sun/star/frame/Desktop.hpp>
31 #include <com/sun/star/frame/XTitle.hpp>
32 #include <com/sun/star/frame/XComponentLoader.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/sdbc/XConnection.hpp>
37 #include <com/sun/star/util/XCloseable.hpp>
38 #include <com/sun/star/frame/XDispatchProvider.hpp>
39 #include <com/sun/star/sdb/CommandType.hpp>
41 #include <tools/diagnose_ex.h>
42 #include <osl/diagnose.h>
45 namespace pcr
49 using ::com::sun::star::uno::Reference;
50 using ::com::sun::star::beans::PropertyChangeEvent;
51 using ::com::sun::star::uno::RuntimeException;
52 using ::com::sun::star::frame::XFrame;
53 using ::com::sun::star::awt::XTopWindow;
54 using ::com::sun::star::awt::XWindow;
55 using ::com::sun::star::uno::Exception;
56 using ::com::sun::star::uno::UNO_QUERY_THROW;
57 using ::com::sun::star::uno::UNO_QUERY;
58 using ::com::sun::star::beans::PropertyValue;
59 using ::com::sun::star::uno::Sequence;
60 using ::com::sun::star::lang::XComponent;
61 using ::com::sun::star::frame::XComponentLoader;
62 using ::com::sun::star::beans::XPropertySet;
63 using ::com::sun::star::frame::XTitle;
64 using ::com::sun::star::lang::EventObject;
65 using ::com::sun::star::lang::NullPointerException;
66 using ::com::sun::star::lang::DisposedException;
67 using ::com::sun::star::uno::XComponentContext;
68 using ::com::sun::star::frame::XFrames;
69 using ::com::sun::star::util::XCloseable;
70 using ::com::sun::star::lang::XMultiServiceFactory;
71 using ::com::sun::star::frame::XDispatchProvider;
72 using ::com::sun::star::frame::XDispatch;
73 using ::com::sun::star::frame::Desktop;
74 using ::com::sun::star::frame::XDesktop2;
76 namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
77 namespace CommandType = ::com::sun::star::sdb::CommandType;
80 //= ISQLCommandAdapter
83 ISQLCommandAdapter::~ISQLCommandAdapter()
88 //= SQLCommandDesigner
91 SQLCommandDesigner::SQLCommandDesigner( const Reference< XComponentContext >& _rxContext,
92 const ::rtl::Reference< ISQLCommandAdapter >& _rxPropertyAdapter,
93 const ::dbtools::SharedConnection& _rConnection, const Link<SQLCommandDesigner&,void>& _rCloseLink )
94 :m_xContext( _rxContext )
95 ,m_xConnection( _rConnection )
96 ,m_xObjectAdapter( _rxPropertyAdapter )
97 ,m_aCloseLink( _rCloseLink )
99 if ( m_xContext.is() )
100 m_xORB = m_xContext->getServiceManager();
101 if ( !m_xORB.is() || !_rxPropertyAdapter.is() || !m_xConnection.is() )
102 throw NullPointerException();
104 impl_doOpenDesignerFrame_nothrow();
108 SQLCommandDesigner::~SQLCommandDesigner()
113 void SAL_CALL SQLCommandDesigner::propertyChange( const PropertyChangeEvent& Event )
115 OSL_ENSURE( m_xDesigner.is() && ( Event.Source == m_xDesigner ), "SQLCommandDesigner::propertyChange: where did this come from?" );
117 if ( m_xDesigner.is() && ( Event.Source == m_xDesigner ) )
121 if ( PROPERTY_ACTIVECOMMAND == Event.PropertyName )
123 OUString sCommand;
124 OSL_VERIFY( Event.NewValue >>= sCommand );
125 m_xObjectAdapter->setSQLCommand( sCommand );
127 else if ( PROPERTY_ESCAPE_PROCESSING == Event.PropertyName )
129 bool bEscapeProcessing( false );
130 OSL_VERIFY( Event.NewValue >>= bEscapeProcessing );
131 m_xObjectAdapter->setEscapeProcessing( bEscapeProcessing );
134 catch( const RuntimeException& ) { throw; }
135 catch( const Exception& )
137 // not allowed to leave, so silence it
138 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
144 void SAL_CALL SQLCommandDesigner::disposing( const EventObject& Source )
146 if ( m_xDesigner.is() && ( Source.Source == m_xDesigner ) )
148 m_aCloseLink.Call( *this );
149 m_xDesigner.clear();
154 void SQLCommandDesigner::dispose()
156 if ( impl_isDisposed() )
157 return;
159 if ( isActive() )
160 impl_closeDesigner_nothrow();
162 m_xConnection.clear();
163 m_xContext.clear();
164 m_xORB.clear();
168 void SQLCommandDesigner::impl_checkDisposed_throw() const
170 if ( impl_isDisposed() )
171 throw DisposedException();
175 void SQLCommandDesigner::raise() const
177 impl_checkDisposed_throw();
178 impl_raise_nothrow();
182 bool SQLCommandDesigner::suspend() const
184 impl_checkDisposed_throw();
185 return impl_trySuspendDesigner_nothrow();
189 void SQLCommandDesigner::impl_raise_nothrow() const
191 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_raise_nothrow: not active!" );
192 if ( !isActive() )
193 return;
197 // activate the frame for this component
198 Reference< XFrame > xFrame( m_xDesigner->getFrame(), css::uno::UNO_SET_THROW );
199 Reference< XWindow > xWindow( xFrame->getContainerWindow(), css::uno::UNO_SET_THROW );
200 Reference< XTopWindow > xTopWindow( xWindow, UNO_QUERY_THROW );
202 xTopWindow->toFront();
203 xWindow->setFocus();
205 catch( const Exception& )
207 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
212 void SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow()
214 OSL_PRECOND( !isActive(),
215 "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: already active!" );
216 OSL_PRECOND( m_xConnection.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: this will crash!" );
217 osl_atomic_increment(&m_refCount);
221 // for various reasons, we don't want the new frame to appear in the desktop's frame list
222 // thus, we create a blank frame at the desktop, remove it from the desktop's frame list
223 // immediately, and then load the component into this blank (and now parent-less) frame
224 Reference< XComponentLoader > xLoader( impl_createEmptyParentlessTask_nothrow(), UNO_QUERY_THROW );
225 Sequence< PropertyValue > aArgs( 5 );
226 aArgs[0].Name = PROPERTY_ACTIVE_CONNECTION;
227 aArgs[0].Value <<= m_xConnection.getTyped();
229 aArgs[1].Name = PROPERTY_COMMAND;
230 aArgs[1].Value <<= m_xObjectAdapter->getSQLCommand();
231 aArgs[2].Name = PROPERTY_COMMANDTYPE;
232 aArgs[2].Value <<= sal_Int32(CommandType::COMMAND);
233 aArgs[3].Name = PROPERTY_ESCAPE_PROCESSING;
234 aArgs[3].Value <<= m_xObjectAdapter->getEscapeProcessing();
236 aArgs[4].Name = "GraphicalDesign";
237 aArgs[4].Value <<= m_xObjectAdapter->getEscapeProcessing();
239 Reference< XComponent > xQueryDesign = xLoader->loadComponentFromURL(
240 ".component:DB/QueryDesign",
241 "_self",
242 FrameSearchFlag::TASKS | FrameSearchFlag::CREATE,
243 aArgs
246 // remember this newly loaded component - we need to care for it e.g. when we're suspended
247 m_xDesigner.set(xQueryDesign, css::uno::UNO_QUERY);
248 OSL_ENSURE( m_xDesigner.is() || !xQueryDesign.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the component is expected to be a controller!" );
249 if ( m_xDesigner.is() )
251 Reference< XPropertySet > xQueryDesignProps( m_xDesigner, UNO_QUERY );
252 OSL_ENSURE( xQueryDesignProps.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the controller should have properties!" );
253 if ( xQueryDesignProps.is() )
255 xQueryDesignProps->addPropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
256 xQueryDesignProps->addPropertyChangeListener( PROPERTY_ESCAPE_PROCESSING, this );
260 // get the frame which we just opened and set its title
261 Reference< XTitle> xTitle(xQueryDesign,UNO_QUERY);
262 if ( xTitle.is() )
264 OUString sDisplayName = PcrRes(RID_RSC_ENUM_COMMAND_TYPE[CommandType::COMMAND]);
265 xTitle->setTitle(sDisplayName);
268 catch( const Exception& )
270 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
271 m_xDesigner.clear();
273 osl_atomic_decrement(&m_refCount);
277 Reference< XFrame > SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow( ) const
279 OSL_PRECOND( m_xORB.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: this will crash!" );
281 Reference< XFrame > xFrame;
284 Reference< XDesktop2 > xDesktop = Desktop::create(m_xContext);
286 Reference< XFrames > xDesktopFramesCollection( xDesktop->getFrames(), css::uno::UNO_SET_THROW );
287 xFrame = xDesktop->findFrame( "_blank", FrameSearchFlag::CREATE );
288 OSL_ENSURE( xFrame.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: could not create an empty frame!" );
289 xDesktopFramesCollection->remove( xFrame );
291 catch( const Exception& )
293 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
295 return xFrame;
299 void SQLCommandDesigner::impl_closeDesigner_nothrow()
301 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_closeDesigner_nothrow: invalid call!" );
302 // close it
305 // do not listen anymore...
306 Reference< XPropertySet > xProps( m_xDesigner, UNO_QUERY );
307 if ( xProps.is() )
308 xProps->removePropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
310 // we need to close the frame via the "user interface", by dispatching a close command,
311 // instead of calling XCloseable::close directly. The latter method would also close
312 // the frame, but not care for things like shutting down the office when the last
313 // frame is gone ...
314 const UnoURL aCloseURL( ".uno:CloseDoc",
315 Reference< XMultiServiceFactory >( m_xORB, UNO_QUERY ) );
317 Reference< XDispatchProvider > xProvider( m_xDesigner->getFrame(), UNO_QUERY_THROW );
318 Reference< XDispatch > xDispatch( xProvider->queryDispatch( aCloseURL, "_top", FrameSearchFlag::SELF ) );
319 OSL_ENSURE( xDispatch.is(), "SQLCommandDesigner::impl_closeDesigner_nothrow: no dispatcher for the CloseDoc command!" );
320 if ( xDispatch.is() )
322 xDispatch->dispatch( aCloseURL, Sequence< PropertyValue >( ) );
324 else
326 // fallback: use the XCloseable::close (with all possible disadvantages)
327 Reference< XCloseable > xClose( m_xDesigner->getFrame(), UNO_QUERY );
328 if ( xClose.is() )
329 xClose->close( true );
332 catch( const Exception& )
334 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
337 m_xDesigner.clear();
341 bool SQLCommandDesigner::impl_trySuspendDesigner_nothrow() const
343 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_trySuspendDesigner_nothrow: no active designer, this will crash!" );
344 bool bAllow = true;
347 bAllow = m_xDesigner->suspend( true );
349 catch( const Exception& )
351 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
353 return bAllow;
357 } // namespace pcr
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */