1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "modulepcr.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/lang/NullPointerException.hpp>
33 #include <com/sun/star/lang/DisposedException.hpp>
34 #include <com/sun/star/frame/FrameSearchFlag.hpp>
35 #include <com/sun/star/sdbc/XConnection.hpp>
36 #include <com/sun/star/util/XCloseable.hpp>
37 #include <com/sun/star/frame/XDispatchProvider.hpp>
38 #include <com/sun/star/sdb/CommandType.hpp>
40 #include <comphelper/propertyvalue.hxx>
42 #include <comphelper/diagnose_ex.hxx>
43 #include <osl/diagnose.h>
50 using ::com::sun::star::uno::Reference
;
51 using ::com::sun::star::beans::PropertyChangeEvent
;
52 using ::com::sun::star::uno::RuntimeException
;
53 using ::com::sun::star::frame::XFrame
;
54 using ::com::sun::star::awt::XTopWindow
;
55 using ::com::sun::star::awt::XWindow
;
56 using ::com::sun::star::uno::Exception
;
57 using ::com::sun::star::uno::UNO_QUERY_THROW
;
58 using ::com::sun::star::uno::UNO_QUERY
;
59 using ::com::sun::star::beans::PropertyValue
;
60 using ::com::sun::star::uno::Sequence
;
61 using ::com::sun::star::lang::XComponent
;
62 using ::com::sun::star::frame::XComponentLoader
;
63 using ::com::sun::star::beans::XPropertySet
;
64 using ::com::sun::star::frame::XTitle
;
65 using ::com::sun::star::lang::EventObject
;
66 using ::com::sun::star::lang::NullPointerException
;
67 using ::com::sun::star::lang::DisposedException
;
68 using ::com::sun::star::uno::XComponentContext
;
69 using ::com::sun::star::frame::XFrames
;
70 using ::com::sun::star::util::XCloseable
;
71 using ::com::sun::star::lang::XMultiServiceFactory
;
72 using ::com::sun::star::frame::XDispatchProvider
;
73 using ::com::sun::star::frame::XDispatch
;
74 using ::com::sun::star::frame::Desktop
;
75 using ::com::sun::star::frame::XDesktop2
;
77 namespace FrameSearchFlag
= ::com::sun::star::frame::FrameSearchFlag
;
78 namespace CommandType
= ::com::sun::star::sdb::CommandType
;
81 //= ISQLCommandAdapter
84 ISQLCommandAdapter::~ISQLCommandAdapter()
89 //= SQLCommandDesigner
92 SQLCommandDesigner::SQLCommandDesigner( const Reference
< XComponentContext
>& _rxContext
,
93 const ::rtl::Reference
< ISQLCommandAdapter
>& _rxPropertyAdapter
,
94 ::dbtools::SharedConnection _aConnection
, const Link
<SQLCommandDesigner
&,void>& _rCloseLink
)
95 :m_xContext( _rxContext
)
96 ,m_xConnection(std::move( _aConnection
))
97 ,m_xObjectAdapter( _rxPropertyAdapter
)
98 ,m_aCloseLink( _rCloseLink
)
100 if ( m_xContext
.is() )
101 m_xORB
= m_xContext
->getServiceManager();
102 if ( !m_xORB
.is() || !_rxPropertyAdapter
.is() || !m_xConnection
.is() )
103 throw NullPointerException();
105 impl_doOpenDesignerFrame_nothrow();
109 SQLCommandDesigner::~SQLCommandDesigner()
114 void SAL_CALL
SQLCommandDesigner::propertyChange( const PropertyChangeEvent
& Event
)
116 OSL_ENSURE( m_xDesigner
.is() && ( Event
.Source
== m_xDesigner
), "SQLCommandDesigner::propertyChange: where did this come from?" );
118 if ( !(m_xDesigner
.is() && ( Event
.Source
== m_xDesigner
)) )
123 if ( PROPERTY_ACTIVECOMMAND
== Event
.PropertyName
)
126 OSL_VERIFY( Event
.NewValue
>>= sCommand
);
127 m_xObjectAdapter
->setSQLCommand( sCommand
);
129 else if ( PROPERTY_ESCAPE_PROCESSING
== Event
.PropertyName
)
131 bool bEscapeProcessing( false );
132 OSL_VERIFY( Event
.NewValue
>>= bEscapeProcessing
);
133 m_xObjectAdapter
->setEscapeProcessing( bEscapeProcessing
);
136 catch( const RuntimeException
& ) { throw; }
137 catch( const Exception
& )
139 // not allowed to leave, so silence it
140 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
145 void SAL_CALL
SQLCommandDesigner::disposing( const EventObject
& Source
)
147 if ( m_xDesigner
.is() && ( Source
.Source
== m_xDesigner
) )
149 m_aCloseLink
.Call( *this );
155 void SQLCommandDesigner::dispose()
157 if ( impl_isDisposed() )
161 impl_closeDesigner_nothrow();
163 m_xConnection
.clear();
167 m_xObjectAdapter
.clear();
171 void SQLCommandDesigner::impl_checkDisposed_throw() const
173 if ( impl_isDisposed() )
174 throw DisposedException();
178 void SQLCommandDesigner::raise() const
180 impl_checkDisposed_throw();
181 impl_raise_nothrow();
185 bool SQLCommandDesigner::suspend() const
187 impl_checkDisposed_throw();
188 return impl_trySuspendDesigner_nothrow();
192 void SQLCommandDesigner::impl_raise_nothrow() const
194 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_raise_nothrow: not active!" );
200 // activate the frame for this component
201 Reference
< XFrame
> xFrame( m_xDesigner
->getFrame(), css::uno::UNO_SET_THROW
);
202 Reference
< XWindow
> xWindow( xFrame
->getContainerWindow(), css::uno::UNO_SET_THROW
);
203 Reference
< XTopWindow
> xTopWindow( xWindow
, UNO_QUERY_THROW
);
205 xTopWindow
->toFront();
208 catch( const Exception
& )
210 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
215 void SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow()
217 OSL_PRECOND( !isActive(),
218 "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: already active!" );
219 OSL_PRECOND( m_xConnection
.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: this will crash!" );
220 osl_atomic_increment(&m_refCount
);
224 // for various reasons, we don't want the new frame to appear in the desktop's frame list
225 // thus, we create a blank frame at the desktop, remove it from the desktop's frame list
226 // immediately, and then load the component into this blank (and now parent-less) frame
227 Reference
< XComponentLoader
> xLoader( impl_createEmptyParentlessTask_nothrow(), UNO_QUERY_THROW
);
228 const bool bEscapeProcessing
= m_xObjectAdapter
->getEscapeProcessing();
229 Sequence
< PropertyValue
> aArgs
{
230 comphelper::makePropertyValue(PROPERTY_ACTIVE_CONNECTION
, m_xConnection
.getTyped()),
231 comphelper::makePropertyValue(PROPERTY_COMMAND
, m_xObjectAdapter
->getSQLCommand()),
232 comphelper::makePropertyValue(PROPERTY_COMMANDTYPE
, CommandType::COMMAND
),
233 comphelper::makePropertyValue(PROPERTY_ESCAPE_PROCESSING
, bEscapeProcessing
),
234 comphelper::makePropertyValue(u
"GraphicalDesign"_ustr
, bEscapeProcessing
)
237 Reference
< XComponent
> xQueryDesign
= xLoader
->loadComponentFromURL(
238 u
".component:DB/QueryDesign"_ustr
,
240 FrameSearchFlag::TASKS
| FrameSearchFlag::CREATE
,
244 // remember this newly loaded component - we need to care for it e.g. when we're suspended
245 m_xDesigner
.set(xQueryDesign
, css::uno::UNO_QUERY
);
246 OSL_ENSURE( m_xDesigner
.is() || !xQueryDesign
.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the component is expected to be a controller!" );
247 if ( m_xDesigner
.is() )
249 Reference
< XPropertySet
> xQueryDesignProps( m_xDesigner
, UNO_QUERY
);
250 OSL_ENSURE( xQueryDesignProps
.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the controller should have properties!" );
251 if ( xQueryDesignProps
.is() )
253 xQueryDesignProps
->addPropertyChangeListener( PROPERTY_ACTIVECOMMAND
, this );
254 xQueryDesignProps
->addPropertyChangeListener( PROPERTY_ESCAPE_PROCESSING
, this );
258 // get the frame which we just opened and set its title
259 Reference
< XTitle
> xTitle(xQueryDesign
,UNO_QUERY
);
262 OUString sDisplayName
= PcrRes(RID_RSC_ENUM_COMMAND_TYPE
[CommandType::COMMAND
]);
263 xTitle
->setTitle(sDisplayName
);
266 catch( const Exception
& )
268 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
271 osl_atomic_decrement(&m_refCount
);
275 Reference
< XFrame
> SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow( ) const
277 OSL_PRECOND( m_xORB
.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: this will crash!" );
279 Reference
< XFrame
> xFrame
;
282 Reference
< XDesktop2
> xDesktop
= Desktop::create(m_xContext
);
284 Reference
< XFrames
> xDesktopFramesCollection( xDesktop
->getFrames(), css::uno::UNO_SET_THROW
);
285 xFrame
= xDesktop
->findFrame( u
"_blank"_ustr
, FrameSearchFlag::CREATE
);
286 OSL_ENSURE( xFrame
.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: could not create an empty frame!" );
287 xDesktopFramesCollection
->remove( xFrame
);
289 catch( const Exception
& )
291 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
297 void SQLCommandDesigner::impl_closeDesigner_nothrow()
299 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_closeDesigner_nothrow: invalid call!" );
303 // do not listen anymore...
304 Reference
< XPropertySet
> xProps( m_xDesigner
, UNO_QUERY
);
306 xProps
->removePropertyChangeListener( PROPERTY_ACTIVECOMMAND
, this );
308 // we need to close the frame via the "user interface", by dispatching a close command,
309 // instead of calling XCloseable::close directly. The latter method would also close
310 // the frame, but not care for things like shutting down the office when the last
312 const UnoURL
aCloseURL( u
".uno:CloseDoc"_ustr
,
313 Reference
< XMultiServiceFactory
>( m_xORB
, UNO_QUERY
) );
315 Reference
< XDispatchProvider
> xProvider( m_xDesigner
->getFrame(), UNO_QUERY_THROW
);
316 Reference
< XDispatch
> xDispatch( xProvider
->queryDispatch( aCloseURL
, u
"_top"_ustr
, FrameSearchFlag::SELF
) );
317 OSL_ENSURE( xDispatch
.is(), "SQLCommandDesigner::impl_closeDesigner_nothrow: no dispatcher for the CloseDoc command!" );
318 if ( xDispatch
.is() )
320 xDispatch
->dispatch( aCloseURL
, Sequence
< PropertyValue
>( ) );
324 // fallback: use the XCloseable::close (with all possible disadvantages)
325 Reference
< XCloseable
> xClose( m_xDesigner
->getFrame(), UNO_QUERY
);
327 xClose
->close( true );
330 catch( const Exception
& )
332 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
339 bool SQLCommandDesigner::impl_trySuspendDesigner_nothrow() const
341 OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_trySuspendDesigner_nothrow: no active designer, this will crash!" );
345 bAllow
= m_xDesigner
->suspend( true );
347 catch( const Exception
& )
349 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */