Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "sqlcommanddesign.hxx"
21 : #include "formstrings.hxx"
22 : #include "formresid.hrc"
23 : #include "modulepcr.hxx"
24 : #include "unourl.hxx"
25 :
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/XTitle.hpp>
30 : #include <com/sun/star/frame/XComponentLoader.hpp>
31 : #include <com/sun/star/frame/XController.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/frame/XFramesSupplier.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/beans/XPropertySetInfo.hpp>
40 : #include <com/sun/star/sdb/CommandType.hpp>
41 :
42 : #include <svtools/localresaccess.hxx>
43 : #include <tools/diagnose_ex.h>
44 : #include <osl/diagnose.h>
45 :
46 : //........................................................................
47 : namespace pcr
48 : {
49 : //........................................................................
50 :
51 : /** === begin UNO using === **/
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::uno::Any;
83 : /** === end UNO using === **/
84 : namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
85 : namespace CommandType = ::com::sun::star::sdb::CommandType;
86 :
87 : //====================================================================
88 : //= ISQLCommandAdapter
89 : //====================================================================
90 : //--------------------------------------------------------------------
91 0 : ISQLCommandAdapter::~ISQLCommandAdapter()
92 : {
93 0 : }
94 :
95 : //====================================================================
96 : //= SQLCommandDesigner
97 : //====================================================================
98 : //--------------------------------------------------------------------
99 0 : SQLCommandDesigner::SQLCommandDesigner( const Reference< XComponentContext >& _rxContext,
100 : const ::rtl::Reference< ISQLCommandAdapter >& _rxPropertyAdapter,
101 : const ::dbtools::SharedConnection& _rConnection, const Link& _rCloseLink )
102 : :m_xContext( _rxContext )
103 : ,m_xConnection( _rConnection )
104 : ,m_xObjectAdapter( _rxPropertyAdapter )
105 0 : ,m_aCloseLink( _rCloseLink )
106 : {
107 0 : if ( m_xContext.is() )
108 0 : m_xORB = m_xContext->getServiceManager();
109 0 : if ( !m_xORB.is() || !_rxPropertyAdapter.is() || !m_xConnection.is() )
110 0 : throw NullPointerException();
111 :
112 0 : impl_doOpenDesignerFrame_nothrow();
113 0 : }
114 :
115 : //--------------------------------------------------------------------
116 0 : SQLCommandDesigner::~SQLCommandDesigner()
117 : {
118 0 : }
119 :
120 : //--------------------------------------------------------------------
121 0 : void SAL_CALL SQLCommandDesigner::propertyChange( const PropertyChangeEvent& Event ) throw (RuntimeException)
122 : {
123 : OSL_ENSURE( m_xDesigner.is() && ( Event.Source == m_xDesigner ), "SQLCommandDesigner::propertyChange: where did this come from?" );
124 :
125 0 : if ( m_xDesigner.is() && ( Event.Source == m_xDesigner ) )
126 : {
127 : try
128 : {
129 0 : if ( PROPERTY_ACTIVECOMMAND == Event.PropertyName )
130 : {
131 0 : ::rtl::OUString sCommand;
132 0 : OSL_VERIFY( Event.NewValue >>= sCommand );
133 0 : m_xObjectAdapter->setSQLCommand( sCommand );
134 : }
135 0 : else if ( PROPERTY_ESCAPE_PROCESSING == Event.PropertyName )
136 : {
137 0 : sal_Bool bEscapeProcessing( sal_False );
138 0 : OSL_VERIFY( Event.NewValue >>= bEscapeProcessing );
139 0 : m_xObjectAdapter->setEscapeProcessing( bEscapeProcessing );
140 : }
141 : }
142 0 : catch( const RuntimeException& ) { throw; }
143 0 : catch( const Exception& )
144 : {
145 : // not allowed to leave, so silence it
146 : DBG_UNHANDLED_EXCEPTION();
147 : }
148 : }
149 0 : }
150 :
151 : //--------------------------------------------------------------------
152 0 : void SAL_CALL SQLCommandDesigner::disposing( const EventObject& Source ) throw (RuntimeException)
153 : {
154 0 : if ( m_xDesigner.is() && ( Source.Source == m_xDesigner ) )
155 : {
156 0 : impl_designerClosed_nothrow();
157 0 : m_xDesigner.clear();
158 : }
159 0 : }
160 :
161 : //--------------------------------------------------------------------
162 0 : void SQLCommandDesigner::dispose()
163 : {
164 0 : if ( impl_isDisposed() )
165 0 : return;
166 :
167 0 : if ( isActive() )
168 0 : impl_closeDesigner_nothrow();
169 :
170 0 : m_xConnection.clear();
171 0 : m_xContext.clear();
172 0 : m_xORB.clear();
173 : }
174 :
175 : //--------------------------------------------------------------------
176 0 : void SQLCommandDesigner::impl_checkDisposed_throw() const
177 : {
178 0 : if ( impl_isDisposed() )
179 0 : throw DisposedException();
180 0 : }
181 :
182 : //--------------------------------------------------------------------
183 0 : void SQLCommandDesigner::raise() const
184 : {
185 0 : impl_checkDisposed_throw();
186 0 : impl_raise_nothrow();
187 0 : }
188 :
189 : //------------------------------------------------------------------------
190 0 : bool SQLCommandDesigner::suspend() const
191 : {
192 0 : impl_checkDisposed_throw();
193 0 : return impl_trySuspendDesigner_nothrow();
194 : }
195 :
196 : //--------------------------------------------------------------------
197 0 : void SQLCommandDesigner::impl_raise_nothrow() const
198 : {
199 : OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_raise_nothrow: not active!" );
200 0 : if ( !isActive() )
201 0 : return;
202 :
203 : try
204 : {
205 : // activate the frame for this component
206 0 : Reference< XFrame > xFrame( m_xDesigner->getFrame(), UNO_QUERY_THROW );
207 0 : Reference< XWindow > xWindow( xFrame->getContainerWindow(), UNO_QUERY_THROW );
208 0 : Reference< XTopWindow > xTopWindow( xWindow, UNO_QUERY_THROW );
209 :
210 0 : xTopWindow->toFront();
211 0 : xWindow->setFocus();
212 : }
213 0 : catch( const Exception& )
214 : {
215 : DBG_UNHANDLED_EXCEPTION();
216 : }
217 : }
218 :
219 : //--------------------------------------------------------------------
220 0 : void SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow()
221 : {
222 : OSL_PRECOND( !isActive(),
223 : "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: already active!" );
224 : OSL_PRECOND( m_xConnection.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: this will crash!" );
225 0 : osl_atomic_increment(&m_refCount);
226 :
227 : try
228 : {
229 : // for various reasons, we don't want the new frame to appear in the desktop's frame list
230 : // thus, we create a blank frame at the desktop, remove it from the desktop's frame list
231 : // immediately, and then load the component into this blank (and now parent-less) frame
232 0 : Reference< XComponentLoader > xLoader( impl_createEmptyParentlessTask_nothrow(), UNO_QUERY_THROW );
233 0 : Sequence< PropertyValue > aArgs( 5 );
234 0 : aArgs[0].Name = PROPERTY_ACTIVE_CONNECTION;
235 0 : aArgs[0].Value <<= m_xConnection.getTyped();
236 :
237 0 : aArgs[1].Name = PROPERTY_COMMAND;
238 0 : aArgs[1].Value <<= m_xObjectAdapter->getSQLCommand();
239 0 : aArgs[2].Name = PROPERTY_COMMANDTYPE;
240 0 : aArgs[2].Value <<= (sal_Int32)CommandType::COMMAND;
241 0 : aArgs[3].Name = PROPERTY_ESCAPE_PROCESSING;
242 0 : aArgs[3].Value <<= m_xObjectAdapter->getEscapeProcessing();
243 :
244 0 : aArgs[4].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "GraphicalDesign" ) );
245 0 : aArgs[4].Value <<= m_xObjectAdapter->getEscapeProcessing();
246 :
247 0 : Reference< XComponent > xQueryDesign = xLoader->loadComponentFromURL(
248 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".component:DB/QueryDesign" ) ),
249 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_self" ) ),
250 : FrameSearchFlag::TASKS | FrameSearchFlag::CREATE,
251 : aArgs
252 0 : );
253 :
254 : // remember this newly loaded component - we need to care for it e.g. when we're suspended
255 0 : m_xDesigner = m_xDesigner.query( xQueryDesign );
256 : OSL_ENSURE( m_xDesigner.is() || !xQueryDesign.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the component is expected to be a controller!" );
257 0 : if ( m_xDesigner.is() )
258 : {
259 0 : Reference< XPropertySet > xQueryDesignProps( m_xDesigner, UNO_QUERY );
260 : OSL_ENSURE( xQueryDesignProps.is(), "SQLCommandDesigner::impl_doOpenDesignerFrame_nothrow: the controller should have properties!" );
261 0 : if ( xQueryDesignProps.is() )
262 : {
263 0 : xQueryDesignProps->addPropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
264 0 : xQueryDesignProps->addPropertyChangeListener( PROPERTY_ESCAPE_PROCESSING, this );
265 0 : }
266 : }
267 :
268 : // get the frame which we just opened and set it's title
269 0 : Reference< XTitle> xTitle(xQueryDesign,UNO_QUERY);
270 0 : if ( xTitle.is() )
271 : {
272 0 : ::svt::OLocalResourceAccess aEnumStrings( PcrRes( RID_RSC_ENUM_COMMAND_TYPE ), RSC_RESOURCE );
273 0 : ::rtl::OUString sDisplayName = String( PcrRes( CommandType::COMMAND + 1 ) );
274 0 : xTitle->setTitle( sDisplayName );
275 0 : }
276 : }
277 0 : catch( const Exception& )
278 : {
279 : DBG_UNHANDLED_EXCEPTION();
280 0 : m_xDesigner.clear();
281 : }
282 0 : osl_atomic_decrement(&m_refCount);
283 0 : }
284 :
285 : //------------------------------------------------------------------------
286 0 : Reference< XFrame > SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow( ) const
287 : {
288 : OSL_PRECOND( m_xORB.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: this will crash!" );
289 :
290 0 : Reference< XFrame > xFrame;
291 : try
292 : {
293 0 : Reference< XInterface > xDesktop ( m_xORB->createInstanceWithContext( SERVICE_DESKTOP, m_xContext ) );
294 0 : Reference< XFrame > xDesktopFrame ( xDesktop, UNO_QUERY_THROW );
295 0 : Reference< XFramesSupplier > xSuppDesktopFrames( xDesktopFrame, UNO_QUERY_THROW );
296 :
297 0 : Reference< XFrames > xDesktopFramesCollection( xSuppDesktopFrames->getFrames(), UNO_QUERY_THROW );
298 0 : xFrame = xDesktopFrame->findFrame( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_blank" ) ), FrameSearchFlag::CREATE );
299 : OSL_ENSURE( xFrame.is(), "SQLCommandDesigner::impl_createEmptyParentlessTask_nothrow: could not create an empty frame!" );
300 0 : xDesktopFramesCollection->remove( xFrame );
301 : }
302 0 : catch( const Exception& )
303 : {
304 : DBG_UNHANDLED_EXCEPTION();
305 : }
306 0 : return xFrame;
307 : }
308 :
309 : //------------------------------------------------------------------------
310 0 : void SQLCommandDesigner::impl_designerClosed_nothrow()
311 : {
312 0 : if ( m_aCloseLink.IsSet() )
313 0 : m_aCloseLink.Call( this );
314 0 : }
315 :
316 : //------------------------------------------------------------------------
317 0 : void SQLCommandDesigner::impl_closeDesigner_nothrow()
318 : {
319 : OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_closeDesigner_nothrow: invalid calle!" );
320 : // close it
321 : try
322 : {
323 : // do not listen anymore ....
324 0 : Reference< XPropertySet > xProps( m_xDesigner, UNO_QUERY );
325 0 : if ( xProps.is() )
326 0 : xProps->removePropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
327 :
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( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:CloseDoc" ) ),
333 0 : Reference< XMultiServiceFactory >( m_xORB, UNO_QUERY ) );
334 :
335 0 : Reference< XDispatchProvider > xProvider( m_xDesigner->getFrame(), UNO_QUERY_THROW );
336 0 : Reference< XDispatch > xDispatch( xProvider->queryDispatch( aCloseURL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_top" ) ), FrameSearchFlag::SELF ) );
337 : OSL_ENSURE( xDispatch.is(), "SQLCommandDesigner::impl_closeDesigner_nothrow: no dispatcher for the CloseDoc command!" );
338 0 : if ( xDispatch.is() )
339 : {
340 0 : xDispatch->dispatch( aCloseURL, Sequence< PropertyValue >( ) );
341 : }
342 : else
343 : {
344 : // fallback: use the XCloseable::close (with all possible disadvantages)
345 0 : Reference< XCloseable > xClose( m_xDesigner->getFrame(), UNO_QUERY );
346 0 : if ( xClose.is() )
347 0 : xClose->close( sal_True );
348 0 : }
349 : }
350 0 : catch( const Exception& )
351 : {
352 : DBG_UNHANDLED_EXCEPTION();
353 : }
354 :
355 0 : m_xDesigner.clear();
356 0 : }
357 :
358 : //------------------------------------------------------------------------
359 0 : bool SQLCommandDesigner::impl_trySuspendDesigner_nothrow() const
360 : {
361 : OSL_PRECOND( isActive(), "SQLCommandDesigner::impl_trySuspendDesigner_nothrow: no active designer, this will crash!" );
362 0 : sal_Bool bAllow = sal_True;
363 : try
364 : {
365 0 : bAllow = m_xDesigner->suspend( sal_True );
366 : }
367 0 : catch( const Exception& )
368 : {
369 : DBG_UNHANDLED_EXCEPTION();
370 : }
371 0 : return bAllow;
372 : }
373 :
374 : //........................................................................
375 : } // namespace pcr
376 : //........................................................................
377 :
378 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|