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 "dbinteraction.hxx"
21 : #include "dbu_reghelper.hxx"
22 : #include "uiservices.hxx"
23 : #include <tools/diagnose_ex.h>
24 : #include <osl/diagnose.h>
25 : #include <vcl/msgbox.hxx>
26 : #include <connectivity/dbexception.hxx>
27 : #include "sqlmessage.hxx"
28 : #include <com/sun/star/task/InteractionHandler.hpp>
29 : #include <com/sun/star/task/XInteractionApprove.hpp>
30 : #include <com/sun/star/task/XInteractionDisapprove.hpp>
31 : #include <com/sun/star/task/XInteractionRetry.hpp>
32 : #include <com/sun/star/task/XInteractionAbort.hpp>
33 : #include <com/sun/star/sdb/XInteractionSupplyParameters.hpp>
34 : #include <com/sun/star/sdb/XInteractionDocumentSave.hpp>
35 : #include <sfx2/QuerySaveDocument.hxx>
36 : #include "dbu_uno.hrc"
37 : #include "paramdialog.hxx"
38 : #include <vcl/svapp.hxx>
39 : #include <osl/mutex.hxx>
40 : #include "CollectionView.hxx"
41 : #include "UITools.hxx"
42 : #include <comphelper/processfactory.hxx>
43 :
44 12 : extern "C" void SAL_CALL createRegistryInfo_OInteractionHandler()
45 : {
46 12 : static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::SQLExceptionInteractionHandler > aSQLExceptionInteractionHandler_AutoRegistration;
47 12 : static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::LegacyInteractionHandler > aLegacyInteractionHandler_AutoRegistration;
48 12 : }
49 :
50 : namespace dbaui
51 : {
52 : using namespace ::com::sun::star::uno;
53 : using namespace ::com::sun::star::ucb;
54 : using namespace ::com::sun::star::sdb;
55 : using namespace ::com::sun::star::lang;
56 : using namespace ::com::sun::star::task;
57 : using namespace ::com::sun::star::beans;
58 : using namespace ::dbtools;
59 :
60 : // BasicInteractionHandler
61 4 : BasicInteractionHandler::BasicInteractionHandler( const Reference< XComponentContext >& rxContext, const bool i_bFallbackToGeneric )
62 : :m_xContext( rxContext )
63 4 : ,m_bFallbackToGeneric( i_bFallbackToGeneric )
64 : {
65 : OSL_ENSURE( !m_bFallbackToGeneric,
66 : "BasicInteractionHandler::BasicInteractionHandler: enabling legacy behavior, there should be no clients of this anymore!" );
67 4 : }
68 :
69 0 : sal_Bool SAL_CALL BasicInteractionHandler::handleInteractionRequest( const Reference< XInteractionRequest >& i_rRequest ) throw (RuntimeException, std::exception)
70 : {
71 0 : return impl_handle_throw( i_rRequest );
72 : }
73 :
74 0 : void SAL_CALL BasicInteractionHandler::handle( const Reference< XInteractionRequest >& i_rRequest ) throw(RuntimeException, std::exception)
75 : {
76 0 : impl_handle_throw( i_rRequest );
77 0 : }
78 :
79 0 : bool BasicInteractionHandler::impl_handle_throw( const Reference< XInteractionRequest >& i_Request )
80 : {
81 0 : Any aRequest( i_Request->getRequest() );
82 : OSL_ENSURE(aRequest.hasValue(), "BasicInteractionHandler::handle: invalid request!");
83 0 : if ( !aRequest.hasValue() )
84 : // no request -> no handling
85 0 : return false;
86 :
87 0 : Sequence< Reference< XInteractionContinuation > > aContinuations( i_Request->getContinuations() );
88 :
89 : // try to extract an SQLException (or one of it's derived members
90 0 : SQLExceptionInfo aInfo( aRequest );
91 0 : if ( aInfo.isValid() )
92 : {
93 0 : implHandle( aInfo, aContinuations );
94 0 : return true;
95 : }
96 :
97 0 : ParametersRequest aParamRequest;
98 0 : if ( aRequest >>= aParamRequest )
99 : {
100 0 : implHandle( aParamRequest, aContinuations );
101 0 : return true;
102 : }
103 :
104 0 : DocumentSaveRequest aDocuRequest;
105 0 : if ( aRequest >>= aDocuRequest )
106 : {
107 0 : implHandle( aDocuRequest, aContinuations );
108 0 : return true;
109 : }
110 :
111 0 : if ( m_bFallbackToGeneric )
112 0 : return implHandleUnknown( i_Request );
113 :
114 0 : return false;
115 : }
116 :
117 0 : void BasicInteractionHandler::implHandle(const ParametersRequest& _rParamRequest, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
118 : {
119 0 : SolarMutexGuard aGuard;
120 : // want to open a dialog...
121 :
122 0 : sal_Int32 nAbortPos = getContinuation(ABORT, _rContinuations);
123 0 : sal_Int32 nParamPos = getContinuation(SUPPLY_PARAMETERS, _rContinuations);
124 :
125 0 : Reference< XInteractionSupplyParameters > xParamCallback;
126 0 : if (-1 != nParamPos)
127 0 : xParamCallback = Reference< XInteractionSupplyParameters >(_rContinuations[nParamPos], UNO_QUERY);
128 : OSL_ENSURE(xParamCallback.is(), "BasicInteractionHandler::implHandle(ParametersRequest): can't set the parameters without an appropriate interaction handler!s");
129 :
130 0 : ScopedVclPtrInstance< OParameterDialog > aDlg(nullptr, _rParamRequest.Parameters, _rParamRequest.Connection, m_xContext);
131 0 : sal_Int16 nResult = aDlg->Execute();
132 : try
133 : {
134 0 : switch (nResult)
135 : {
136 : case RET_OK:
137 0 : if (xParamCallback.is())
138 : {
139 0 : xParamCallback->setParameters(aDlg->getValues());
140 0 : xParamCallback->select();
141 : }
142 0 : break;
143 : default:
144 0 : if (-1 != nAbortPos)
145 0 : _rContinuations[nAbortPos]->select();
146 0 : break;
147 : }
148 : }
149 0 : catch( const Exception& )
150 : {
151 : DBG_UNHANDLED_EXCEPTION();
152 0 : }
153 0 : }
154 :
155 0 : void BasicInteractionHandler::implHandle(const SQLExceptionInfo& _rSqlInfo, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
156 : {
157 0 : SolarMutexGuard aGuard;
158 : // want to open a dialog...
159 :
160 0 : sal_Int32 nApprovePos = getContinuation(APPROVE, _rContinuations);
161 0 : sal_Int32 nDisapprovePos = getContinuation(DISAPPROVE, _rContinuations);
162 0 : sal_Int32 nAbortPos = getContinuation(ABORT, _rContinuations);
163 0 : sal_Int32 nRetryPos = getContinuation(RETRY, _rContinuations);
164 :
165 : // determine the style of the dialog, dependent on the present continuation types
166 0 : WinBits nDialogStyle = 0;
167 0 : bool bHaveCancel = nAbortPos != -1;
168 : // "approve" means "Yes", "disapprove" means "No"
169 : // VCL only supports having both (which makes sense ...)
170 0 : if ( ( nApprovePos != -1 ) || ( nDisapprovePos != -1 ) )
171 0 : nDialogStyle = ( bHaveCancel ? WB_YES_NO_CANCEL : WB_YES_NO ) | WB_DEF_YES;
172 : else
173 : {
174 : // if there's no yes/no, then use a default OK button
175 0 : nDialogStyle = ( bHaveCancel ? WB_OK_CANCEL : WB_OK ) | WB_DEF_OK;
176 : }
177 :
178 : // If there's a "Retry" continuation, have a "Retry" button
179 0 : if ( nRetryPos != -1 )
180 : {
181 0 : nDialogStyle = WB_RETRY_CANCEL | WB_DEF_RETRY;
182 : }
183 :
184 : // execute the dialog
185 0 : ScopedVclPtrInstance< OSQLMessageBox > aDialog(nullptr, _rSqlInfo, nDialogStyle);
186 : // TODO: need a way to specify the parent window
187 0 : sal_Int16 nResult = aDialog->Execute();
188 : try
189 : {
190 0 : switch (nResult)
191 : {
192 : case RET_YES:
193 : case RET_OK:
194 0 : if ( nApprovePos != -1 )
195 0 : _rContinuations[ nApprovePos ]->select();
196 : else
197 : OSL_ENSURE( nResult != RET_YES, "BasicInteractionHandler::implHandle: no handler for YES!" );
198 0 : break;
199 :
200 : case RET_NO:
201 0 : if ( nDisapprovePos != -1 )
202 0 : _rContinuations[ nDisapprovePos ]->select();
203 : else
204 : OSL_FAIL( "BasicInteractionHandler::implHandle: no handler for NO!" );
205 0 : break;
206 :
207 : case RET_CANCEL:
208 0 : if ( nAbortPos != -1 )
209 0 : _rContinuations[ nAbortPos ]->select();
210 0 : else if ( nDisapprovePos != -1 )
211 0 : _rContinuations[ nDisapprovePos ]->select();
212 : else
213 : OSL_FAIL( "BasicInteractionHandler::implHandle: no handler for CANCEL!" );
214 0 : break;
215 : case RET_RETRY:
216 0 : if ( nRetryPos != -1 )
217 0 : _rContinuations[ nRetryPos ]->select();
218 : else
219 : OSL_FAIL( "BasicInteractionHandler::implHandle: where does the RETRY come from?" );
220 0 : break;
221 : }
222 : }
223 0 : catch( const Exception& )
224 : {
225 : DBG_UNHANDLED_EXCEPTION();
226 0 : }
227 0 : }
228 0 : void BasicInteractionHandler::implHandle(const DocumentSaveRequest& _rDocuRequest, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
229 : {
230 0 : SolarMutexGuard aGuard;
231 : // want to open a dialog ....
232 :
233 0 : sal_Int32 nApprovePos = getContinuation(APPROVE, _rContinuations);
234 0 : sal_Int32 nDisApprovePos = getContinuation(DISAPPROVE, _rContinuations);
235 0 : sal_Int32 nAbortPos = getContinuation(ABORT, _rContinuations);
236 :
237 0 : short nRet = RET_YES;
238 0 : if ( -1 != nApprovePos )
239 : {
240 : // ask whether it should be saved
241 0 : nRet = ExecuteQuerySaveDocument(NULL,_rDocuRequest.Name);
242 : }
243 :
244 0 : if ( RET_CANCEL == nRet )
245 : {
246 0 : if (-1 != nAbortPos)
247 0 : _rContinuations[nAbortPos]->select();
248 0 : return;
249 : }
250 0 : else if ( RET_YES == nRet )
251 : {
252 0 : sal_Int32 nDocuPos = getContinuation(SUPPLY_DOCUMENTSAVE, _rContinuations);
253 :
254 0 : if (-1 != nDocuPos)
255 : {
256 0 : Reference< XInteractionDocumentSave > xCallback(_rContinuations[nDocuPos], UNO_QUERY);
257 : OSL_ENSURE(xCallback.is(), "BasicInteractionHandler::implHandle(DocumentSaveRequest): can't save document without an appropriate interaction handler!s");
258 :
259 0 : ScopedVclPtrInstance< OCollectionView > aDlg(nullptr, _rDocuRequest.Content, _rDocuRequest.Name, m_xContext);
260 0 : sal_Int16 nResult = aDlg->Execute();
261 : try
262 : {
263 0 : switch (nResult)
264 : {
265 : case RET_OK:
266 0 : if (xCallback.is())
267 : {
268 0 : xCallback->setName(aDlg->getName(), aDlg->getSelectedFolder());
269 0 : xCallback->select();
270 : }
271 0 : break;
272 : default:
273 0 : if (-1 != nAbortPos)
274 0 : _rContinuations[nAbortPos]->select();
275 0 : break;
276 : }
277 : }
278 0 : catch( const Exception& )
279 : {
280 : DBG_UNHANDLED_EXCEPTION();
281 0 : }
282 : }
283 0 : else if ( -1 != nApprovePos )
284 0 : _rContinuations[nApprovePos]->select();
285 : }
286 0 : else if ( -1 != nDisApprovePos )
287 0 : _rContinuations[nDisApprovePos]->select();
288 : }
289 :
290 0 : bool BasicInteractionHandler::implHandleUnknown( const Reference< XInteractionRequest >& _rxRequest )
291 : {
292 0 : if ( m_xContext.is() )
293 : {
294 : Reference< XInteractionHandler2 > xFallbackHandler(
295 0 : InteractionHandler::createWithParent(m_xContext, 0) );
296 0 : xFallbackHandler->handle( _rxRequest );
297 0 : return true;
298 : }
299 0 : return false;
300 : }
301 :
302 0 : sal_Int32 BasicInteractionHandler::getContinuation(Continuation _eCont, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
303 : {
304 0 : const Reference< XInteractionContinuation >* pContinuations = _rContinuations.getConstArray();
305 0 : for (sal_Int32 i=0; i<_rContinuations.getLength(); ++i, ++pContinuations)
306 : {
307 0 : switch (_eCont)
308 : {
309 : case APPROVE:
310 0 : if (Reference< XInteractionApprove >(*pContinuations, UNO_QUERY).is())
311 0 : return i;
312 0 : break;
313 : case DISAPPROVE:
314 0 : if (Reference< XInteractionDisapprove >(*pContinuations, UNO_QUERY).is())
315 0 : return i;
316 0 : break;
317 : case RETRY:
318 0 : if (Reference< XInteractionRetry >(*pContinuations, UNO_QUERY).is())
319 0 : return i;
320 0 : break;
321 : case ABORT:
322 0 : if (Reference< XInteractionAbort >(*pContinuations, UNO_QUERY).is())
323 0 : return i;
324 0 : break;
325 : case SUPPLY_PARAMETERS:
326 0 : if (Reference< XInteractionSupplyParameters >(*pContinuations, UNO_QUERY).is())
327 0 : return i;
328 0 : break;
329 : case SUPPLY_DOCUMENTSAVE:
330 0 : if (Reference< XInteractionDocumentSave >(*pContinuations, UNO_QUERY).is())
331 0 : return i;
332 0 : break;
333 : }
334 : }
335 :
336 0 : return -1;
337 : }
338 :
339 : // SQLExceptionInteractionHandler
340 26 : IMPLEMENT_SERVICE_INFO_IMPLNAME_STATIC(SQLExceptionInteractionHandler, "com.sun.star.comp.dbaccess.DatabaseInteractionHandler")
341 0 : IMPLEMENT_SERVICE_INFO_SUPPORTS(SQLExceptionInteractionHandler)
342 14 : IMPLEMENT_SERVICE_INFO_GETSUPPORTED1_STATIC(SQLExceptionInteractionHandler, "com.sun.star.sdb.DatabaseInteractionHandler")
343 :
344 : Reference< XInterface >
345 1 : SAL_CALL SQLExceptionInteractionHandler::Create(const Reference< XMultiServiceFactory >& _rxORB)
346 : {
347 1 : return static_cast< XServiceInfo* >(new SQLExceptionInteractionHandler(comphelper::getComponentContext(_rxORB)));
348 : }
349 :
350 : // LegacyInteractionHandler
351 26 : IMPLEMENT_SERVICE_INFO_IMPLNAME_STATIC(LegacyInteractionHandler, "com.sun.star.comp.dbaccess.LegacyInteractionHandler")
352 0 : IMPLEMENT_SERVICE_INFO_SUPPORTS(LegacyInteractionHandler)
353 14 : IMPLEMENT_SERVICE_INFO_GETSUPPORTED1_STATIC(LegacyInteractionHandler, "com.sun.star.sdb.InteractionHandler")
354 :
355 : Reference< XInterface >
356 3 : SAL_CALL LegacyInteractionHandler::Create(const Reference< XMultiServiceFactory >& _rxORB)
357 : {
358 3 : return static_cast< XServiceInfo* >(new LegacyInteractionHandler(comphelper::getComponentContext(_rxORB)));
359 : }
360 :
361 36 : } // namespace dbaui
362 :
363 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|