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