Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / uno / dbinteraction.cxx
blobf0612991f4b947e7775214820d9f0538e6389f1a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "dbinteraction.hxx"
31 #include "dbu_reghelper.hxx"
32 #include <tools/diagnose_ex.h>
33 #include <osl/diagnose.h>
34 #include <vcl/msgbox.hxx>
35 #include <connectivity/dbexception.hxx>
36 #include "sqlmessage.hxx"
37 #include <com/sun/star/task/XInteractionApprove.hpp>
38 #include <com/sun/star/task/XInteractionDisapprove.hpp>
39 #include <com/sun/star/task/XInteractionRetry.hpp>
40 #include <com/sun/star/task/XInteractionAbort.hpp>
41 #include <com/sun/star/sdb/XInteractionSupplyParameters.hpp>
42 #include <com/sun/star/sdb/XInteractionDocumentSave.hpp>
43 #include <sfx2/QuerySaveDocument.hxx>
44 #include "dbu_uno.hrc"
45 #include "paramdialog.hxx"
46 #include <vcl/svapp.hxx>
47 #include <osl/mutex.hxx>
48 #include "CollectionView.hxx"
49 #include "UITools.hxx"
52 //==========================================================================
54 extern "C" void SAL_CALL createRegistryInfo_OInteractionHandler()
56 static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::SQLExceptionInteractionHandler > aSQLExceptionInteractionHandler_AutoRegistration;
57 static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::LegacyInteractionHandler > aLegacyInteractionHandler_AutoRegistration;
60 //.........................................................................
61 namespace dbaui
63 //.........................................................................
64 using namespace ::com::sun::star::uno;
65 using namespace ::com::sun::star::ucb;
66 using namespace ::com::sun::star::sdb;
67 using namespace ::com::sun::star::lang;
68 using namespace ::com::sun::star::task;
69 using namespace ::com::sun::star::beans;
70 using namespace ::dbtools;
72 //=========================================================================
73 //= BasicInteractionHandler
74 //=========================================================================
75 //-------------------------------------------------------------------------
76 BasicInteractionHandler::BasicInteractionHandler( const Reference< XMultiServiceFactory >& _rxORB, const bool i_bFallbackToGeneric )
77 :m_xORB( _rxORB )
78 ,m_bFallbackToGeneric( i_bFallbackToGeneric )
80 OSL_ENSURE( !m_bFallbackToGeneric,
81 "BasicInteractionHandler::BasicInteractionHandler: enabling legacy behavior, there should be no clients of this anymore!" );
84 //-------------------------------------------------------------------------
85 ::sal_Bool SAL_CALL BasicInteractionHandler::handleInteractionRequest( const Reference< XInteractionRequest >& i_rRequest ) throw (RuntimeException)
87 return impl_handle_throw( i_rRequest );
90 //-------------------------------------------------------------------------
91 void SAL_CALL BasicInteractionHandler::handle( const Reference< XInteractionRequest >& i_rRequest ) throw(RuntimeException)
93 impl_handle_throw( i_rRequest );
96 //-------------------------------------------------------------------------
97 sal_Bool BasicInteractionHandler::impl_handle_throw( const Reference< XInteractionRequest >& i_Request )
99 Any aRequest( i_Request->getRequest() );
100 OSL_ENSURE(aRequest.hasValue(), "BasicInteractionHandler::handle: invalid request!");
101 if ( !aRequest.hasValue() )
102 // no request -> no handling
103 return sal_False;
105 Sequence< Reference< XInteractionContinuation > > aContinuations( i_Request->getContinuations() );
107 // try to extract an SQLException (or one of it's derived members
108 SQLExceptionInfo aInfo( aRequest );
109 if ( aInfo.isValid() )
111 implHandle( aInfo, aContinuations );
112 return sal_True;
115 ParametersRequest aParamRequest;
116 if ( aRequest >>= aParamRequest )
118 implHandle( aParamRequest, aContinuations );
119 return sal_True;
122 DocumentSaveRequest aDocuRequest;
123 if ( aRequest >>= aDocuRequest )
125 implHandle( aDocuRequest, aContinuations );
126 return sal_True;
129 if ( m_bFallbackToGeneric )
130 return implHandleUnknown( i_Request );
132 return sal_False;
135 //-------------------------------------------------------------------------
136 void BasicInteractionHandler::implHandle(const ParametersRequest& _rParamRequest, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
138 SolarMutexGuard aGuard;
139 // want to open a dialog ....
141 sal_Int32 nAbortPos = getContinuation(ABORT, _rContinuations);
142 sal_Int32 nParamPos = getContinuation(SUPPLY_PARAMETERS, _rContinuations);
144 Reference< XInteractionSupplyParameters > xParamCallback;
145 if (-1 != nParamPos)
146 xParamCallback = Reference< XInteractionSupplyParameters >(_rContinuations[nParamPos], UNO_QUERY);
147 OSL_ENSURE(xParamCallback.is(), "BasicInteractionHandler::implHandle(ParametersRequest): can't set the parameters without an appropriate interaction handler!s");
149 OParameterDialog aDlg(NULL, _rParamRequest.Parameters, _rParamRequest.Connection, m_xORB);
150 sal_Int16 nResult = aDlg.Execute();
153 switch (nResult)
155 case RET_OK:
156 if (xParamCallback.is())
158 xParamCallback->setParameters(aDlg.getValues());
159 xParamCallback->select();
161 break;
162 default:
163 if (-1 != nAbortPos)
164 _rContinuations[nAbortPos]->select();
165 break;
168 catch( const Exception& )
170 DBG_UNHANDLED_EXCEPTION();
174 //-------------------------------------------------------------------------
175 void BasicInteractionHandler::implHandle(const SQLExceptionInfo& _rSqlInfo, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
177 SolarMutexGuard aGuard;
178 // want to open a dialog ....
180 sal_Int32 nApprovePos = getContinuation(APPROVE, _rContinuations);
181 sal_Int32 nDisapprovePos = getContinuation(DISAPPROVE, _rContinuations);
182 sal_Int32 nAbortPos = getContinuation(ABORT, _rContinuations);
183 sal_Int32 nRetryPos = getContinuation(RETRY, _rContinuations);
185 // determine the style of the dialog, dependent on the present continuation types
186 WinBits nDialogStyle = 0;
187 bool bHaveCancel = nAbortPos != -1;
188 // "approve" means "Yes", "disapprove" means "No"
189 // VCL only supports having both (which makes sense ...)
190 if ( ( nApprovePos != -1 ) || ( nDisapprovePos != -1 ) )
191 nDialogStyle = ( bHaveCancel ? WB_YES_NO_CANCEL : WB_YES_NO ) | WB_DEF_YES;
192 else
194 // if there's no yes/no, then use a default OK button
195 nDialogStyle = ( bHaveCancel ? WB_OK_CANCEL : WB_OK ) | WB_DEF_OK;
198 // If there's a "Retry" continuation, have a "Retry" button
199 if ( nRetryPos != -1 )
201 nDialogStyle = WB_RETRY_CANCEL | WB_DEF_RETRY;
204 // execute the dialog
205 OSQLMessageBox aDialog(NULL, _rSqlInfo, nDialogStyle);
206 // TODO: need a way to specify the parent window
207 sal_Int16 nResult = aDialog.Execute();
210 switch (nResult)
212 case RET_YES:
213 case RET_OK:
214 if ( nApprovePos != -1 )
215 _rContinuations[ nApprovePos ]->select();
216 else
217 OSL_ENSURE( nResult != RET_YES, "BasicInteractionHandler::implHandle: no handler for YES!" );
218 break;
220 case RET_NO:
221 if ( nDisapprovePos != -1 )
222 _rContinuations[ nDisapprovePos ]->select();
223 else
224 OSL_FAIL( "BasicInteractionHandler::implHandle: no handler for NO!" );
225 break;
227 case RET_CANCEL:
228 if ( nAbortPos != -1 )
229 _rContinuations[ nAbortPos ]->select();
230 else if ( nDisapprovePos != -1 )
231 _rContinuations[ nDisapprovePos ]->select();
232 else
233 OSL_FAIL( "BasicInteractionHandler::implHandle: no handler for CANCEL!" );
234 break;
235 case RET_RETRY:
236 if ( nRetryPos != -1 )
237 _rContinuations[ nRetryPos ]->select();
238 else
239 OSL_FAIL( "BasicInteractionHandler::implHandle: where does the RETRY come from?" );
240 break;
243 catch( const Exception& )
245 DBG_UNHANDLED_EXCEPTION();
248 //-------------------------------------------------------------------------
249 void BasicInteractionHandler::implHandle(const DocumentSaveRequest& _rDocuRequest, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
251 SolarMutexGuard aGuard;
252 // want to open a dialog ....
254 sal_Int32 nApprovePos = getContinuation(APPROVE, _rContinuations);
255 sal_Int32 nDisApprovePos = getContinuation(DISAPPROVE, _rContinuations);
256 sal_Int32 nAbortPos = getContinuation(ABORT, _rContinuations);
258 short nRet = RET_YES;
259 if ( -1 != nApprovePos )
261 // fragen, ob gespeichert werden soll
262 nRet = ExecuteQuerySaveDocument(NULL,_rDocuRequest.Name);
265 if ( RET_CANCEL == nRet )
267 if (-1 != nAbortPos)
268 _rContinuations[nAbortPos]->select();
269 return;
271 else if ( RET_YES == nRet )
273 sal_Int32 nDocuPos = getContinuation(SUPPLY_DOCUMENTSAVE, _rContinuations);
275 if (-1 != nDocuPos)
277 Reference< XInteractionDocumentSave > xCallback(_rContinuations[nDocuPos], UNO_QUERY);
278 OSL_ENSURE(xCallback.is(), "BasicInteractionHandler::implHandle(DocumentSaveRequest): can't save document without an appropriate interaction handler!s");
280 OCollectionView aDlg(NULL,_rDocuRequest.Content,_rDocuRequest.Name,m_xORB);
281 sal_Int16 nResult = aDlg.Execute();
284 switch (nResult)
286 case RET_OK:
287 if (xCallback.is())
289 xCallback->setName(aDlg.getName(),aDlg.getSelectedFolder());
290 xCallback->select();
292 break;
293 default:
294 if (-1 != nAbortPos)
295 _rContinuations[nAbortPos]->select();
296 break;
299 catch( const Exception& )
301 DBG_UNHANDLED_EXCEPTION();
304 else if ( -1 != nApprovePos )
305 _rContinuations[nApprovePos]->select();
307 else if ( -1 != nDisApprovePos )
308 _rContinuations[nDisApprovePos]->select();
311 //-------------------------------------------------------------------------
312 bool BasicInteractionHandler::implHandleUnknown( const Reference< XInteractionRequest >& _rxRequest )
314 Reference< XInteractionHandler > xFallbackHandler;
315 if ( m_xORB.is() )
316 xFallbackHandler = xFallbackHandler.query( m_xORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.task.InteractionHandler" ) ) ) );
317 if ( xFallbackHandler.is() )
319 xFallbackHandler->handle( _rxRequest );
320 return true;
322 return false;
325 //-------------------------------------------------------------------------
326 sal_Int32 BasicInteractionHandler::getContinuation(Continuation _eCont, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
328 const Reference< XInteractionContinuation >* pContinuations = _rContinuations.getConstArray();
329 for (sal_Int32 i=0; i<_rContinuations.getLength(); ++i, ++pContinuations)
331 switch (_eCont)
333 case APPROVE:
334 if (Reference< XInteractionApprove >(*pContinuations, UNO_QUERY).is())
335 return i;
336 break;
337 case DISAPPROVE:
338 if (Reference< XInteractionDisapprove >(*pContinuations, UNO_QUERY).is())
339 return i;
340 break;
341 case RETRY:
342 if (Reference< XInteractionRetry >(*pContinuations, UNO_QUERY).is())
343 return i;
344 break;
345 case ABORT:
346 if (Reference< XInteractionAbort >(*pContinuations, UNO_QUERY).is())
347 return i;
348 break;
349 case SUPPLY_PARAMETERS:
350 if (Reference< XInteractionSupplyParameters >(*pContinuations, UNO_QUERY).is())
351 return i;
352 break;
353 case SUPPLY_DOCUMENTSAVE:
354 if (Reference< XInteractionDocumentSave >(*pContinuations, UNO_QUERY).is())
355 return i;
356 break;
360 return -1;
363 //==========================================================================
364 //= SQLExceptionInteractionHandler
365 //==========================================================================
366 IMPLEMENT_SERVICE_INFO1_STATIC( SQLExceptionInteractionHandler, "com.sun.star.comp.dbaccess.DatabaseInteractionHandler", "com.sun.star.sdb.DatabaseInteractionHandler" );
368 //==========================================================================
369 //= LegacyInteractionHandler
370 //==========================================================================
371 IMPLEMENT_SERVICE_INFO1_STATIC( LegacyInteractionHandler, "com.sun.star.comp.dbaccess.LegacyInteractionHandler", "com.sun.star.sdb.InteractionHandler" );
373 //.........................................................................
374 } // namespace dbaui
375 //.........................................................................
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */