LibreOffice Module dbaccess (master) 1
dbinteraction.cxx
Go to the documentation of this file.
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"
22#include <osl/diagnose.h>
24#include <sqlmessage.hxx>
25#include <com/sun/star/task/InteractionHandler.hpp>
26#include <com/sun/star/task/XInteractionApprove.hpp>
27#include <com/sun/star/task/XInteractionDisapprove.hpp>
28#include <com/sun/star/task/XInteractionRetry.hpp>
29#include <com/sun/star/task/XInteractionAbort.hpp>
30#include <com/sun/star/sdb/XInteractionSupplyParameters.hpp>
31#include <com/sun/star/sdb/XInteractionDocumentSave.hpp>
33#include <paramdialog.hxx>
34#include <vcl/svapp.hxx>
35#include <CollectionView.hxx>
37
38extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
40 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
41{
42 return cppu::acquire(new ::dbaui::SQLExceptionInteractionHandler(context));
43}
44
45extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
47 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
48{
49 return cppu::acquire(new ::dbaui::LegacyInteractionHandler(context));
50}
51
52namespace dbaui
53{
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::ucb;
56 using namespace ::com::sun::star::sdb;
57 using namespace ::com::sun::star::lang;
58 using namespace ::com::sun::star::task;
59 using namespace ::com::sun::star::beans;
60 using namespace ::dbtools;
61
62 // BasicInteractionHandler
63 BasicInteractionHandler::BasicInteractionHandler( const Reference< XComponentContext >& rxContext, const bool i_bFallbackToGeneric )
64 :m_xContext( rxContext )
65 ,m_bFallbackToGeneric( i_bFallbackToGeneric )
66 {
67 OSL_ENSURE( !m_bFallbackToGeneric,
68 "BasicInteractionHandler::BasicInteractionHandler: enabling legacy behavior, there should be no clients of this anymore!" );
69 }
70
72 {
74 m_xParentWindow.set(aMap.getValue("Parent"), UNO_QUERY);
75 }
76
78 {
79 return impl_handle_throw( i_rRequest );
80 }
81
83 {
84 impl_handle_throw( i_rRequest );
85 }
86
88 {
89 Any aRequest( i_Request->getRequest() );
90 OSL_ENSURE(aRequest.hasValue(), "BasicInteractionHandler::handle: invalid request!");
91 if ( !aRequest.hasValue() )
92 // no request -> no handling
93 return false;
94
95 Sequence< Reference< XInteractionContinuation > > aContinuations( i_Request->getContinuations() );
96
97 // try to extract an SQLException (or one of its derived members
98 SQLExceptionInfo aInfo( aRequest );
99 if ( aInfo.isValid() )
100 {
101 implHandle( aInfo, aContinuations );
102 return true;
103 }
104
105 ParametersRequest aParamRequest;
106 if ( aRequest >>= aParamRequest )
107 {
108 implHandle( aParamRequest, aContinuations );
109 return true;
110 }
111
112 DocumentSaveRequest aDocuRequest;
113 if ( aRequest >>= aDocuRequest )
114 {
115 implHandle( aDocuRequest, aContinuations );
116 return true;
117 }
118
120 return implHandleUnknown( i_Request );
121
122 return false;
123 }
124
125 void BasicInteractionHandler::implHandle(const ParametersRequest& _rParamRequest, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
126 {
127 SolarMutexGuard aGuard;
128 // want to open a dialog...
129
130 sal_Int32 nAbortPos = getContinuation(ABORT, _rContinuations);
131 sal_Int32 nParamPos = getContinuation(SUPPLY_PARAMETERS, _rContinuations);
132
134 if (-1 != nParamPos)
135 xParamCallback.set(_rContinuations[nParamPos], UNO_QUERY);
136 OSL_ENSURE(xParamCallback.is(), "BasicInteractionHandler::implHandle(ParametersRequest): can't set the parameters without an appropriate interaction handler!s");
137
138 OParameterDialog aDlg(Application::GetFrameWeld(m_xParentWindow), _rParamRequest.Parameters, _rParamRequest.Connection, m_xContext);
139 sal_Int16 nResult = aDlg.run();
140 try
141 {
142 switch (nResult)
143 {
144 case RET_OK:
145 if (xParamCallback.is())
146 {
147 xParamCallback->setParameters(aDlg.getValues());
148 xParamCallback->select();
149 }
150 break;
151 default:
152 if (-1 != nAbortPos)
153 _rContinuations[nAbortPos]->select();
154 break;
155 }
156 }
157 catch( const Exception& )
158 {
159 DBG_UNHANDLED_EXCEPTION("dbaccess");
160 }
161 }
162
163 void BasicInteractionHandler::implHandle(const SQLExceptionInfo& _rSqlInfo, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
164 {
165 SolarMutexGuard aGuard;
166 // want to open a dialog...
167
168 sal_Int32 nApprovePos = getContinuation(APPROVE, _rContinuations);
169 sal_Int32 nDisapprovePos = getContinuation(DISAPPROVE, _rContinuations);
170 sal_Int32 nAbortPos = getContinuation(ABORT, _rContinuations);
171 sal_Int32 nRetryPos = getContinuation(RETRY, _rContinuations);
172
173 // determine the style of the dialog, dependent on the present continuation types
174 MessBoxStyle nDialogStyle = MessBoxStyle::NONE;
175 bool bHaveCancel = nAbortPos != -1;
176 // "approve" means "Yes", "disapprove" means "No"
177 // VCL only supports having both (which makes sense ...)
178 if ( ( nApprovePos != -1 ) || ( nDisapprovePos != -1 ) )
180 else
181 {
182 // if there's no yes/no, then use a default OK button
183 nDialogStyle = ( bHaveCancel ? MessBoxStyle::OkCancel : MessBoxStyle::Ok ) | MessBoxStyle::DefaultOk;
184 }
185
186 // If there's a "Retry" continuation, have a "Retry" button
187 if ( nRetryPos != -1 )
188 {
190 }
191
192 // execute the dialog
193 OSQLMessageBox aDialog(nullptr, _rSqlInfo, nDialogStyle);
194 // TODO: need a way to specify the parent window
195 sal_Int16 nResult = aDialog.run();
196 try
197 {
198 switch (nResult)
199 {
200 case RET_YES:
201 case RET_OK:
202 if ( nApprovePos != -1 )
203 _rContinuations[ nApprovePos ]->select();
204 else
205 OSL_ENSURE( nResult != RET_YES, "BasicInteractionHandler::implHandle: no handler for YES!" );
206 break;
207
208 case RET_NO:
209 if ( nDisapprovePos != -1 )
210 _rContinuations[ nDisapprovePos ]->select();
211 else
212 OSL_FAIL( "BasicInteractionHandler::implHandle: no handler for NO!" );
213 break;
214
215 case RET_CANCEL:
216 if ( nAbortPos != -1 )
217 _rContinuations[ nAbortPos ]->select();
218 else if ( nDisapprovePos != -1 )
219 _rContinuations[ nDisapprovePos ]->select();
220 else
221 OSL_FAIL( "BasicInteractionHandler::implHandle: no handler for CANCEL!" );
222 break;
223 case RET_RETRY:
224 if ( nRetryPos != -1 )
225 _rContinuations[ nRetryPos ]->select();
226 else
227 OSL_FAIL( "BasicInteractionHandler::implHandle: where does the RETRY come from?" );
228 break;
229 }
230 }
231 catch( const Exception& )
232 {
233 DBG_UNHANDLED_EXCEPTION("dbaccess");
234 }
235 }
236 void BasicInteractionHandler::implHandle(const DocumentSaveRequest& _rDocuRequest, const Sequence< Reference< XInteractionContinuation > >& _rContinuations)
237 {
238 SolarMutexGuard aGuard;
239 // want to open a dialog...
240
241 sal_Int32 nApprovePos = getContinuation(APPROVE, _rContinuations);
242 sal_Int32 nDisApprovePos = getContinuation(DISAPPROVE, _rContinuations);
243 sal_Int32 nAbortPos = getContinuation(ABORT, _rContinuations);
244
245 short nRet = RET_YES;
246 if ( -1 != nApprovePos )
247 {
248 // ask whether it should be saved
250 }
251
252 if ( RET_CANCEL == nRet )
253 {
254 if (-1 != nAbortPos)
255 _rContinuations[nAbortPos]->select();
256 return;
257 }
258 else if ( RET_YES == nRet )
259 {
260 sal_Int32 nDocuPos = getContinuation(SUPPLY_DOCUMENTSAVE, _rContinuations);
261
262 if (-1 != nDocuPos)
263 {
264 Reference< XInteractionDocumentSave > xCallback(_rContinuations[nDocuPos], UNO_QUERY);
265 OSL_ENSURE(xCallback.is(), "BasicInteractionHandler::implHandle(DocumentSaveRequest): can't save document without an appropriate interaction handler!s");
266
267 OCollectionView aDlg(Application::GetFrameWeld(m_xParentWindow), _rDocuRequest.Content, _rDocuRequest.Name, m_xContext);
268 sal_Int16 nResult = aDlg.run();
269 try
270 {
271 switch (nResult)
272 {
273 case RET_OK:
274 if (xCallback.is())
275 {
276 xCallback->setName(aDlg.getName(), aDlg.getSelectedFolder());
277 xCallback->select();
278 }
279 break;
280 default:
281 if (-1 != nAbortPos)
282 _rContinuations[nAbortPos]->select();
283 break;
284 }
285 }
286 catch( const Exception& )
287 {
288 DBG_UNHANDLED_EXCEPTION("dbaccess");
289 }
290 }
291 else if ( -1 != nApprovePos )
292 _rContinuations[nApprovePos]->select();
293 }
294 else if ( -1 != nDisApprovePos )
295 _rContinuations[nDisApprovePos]->select();
296 }
297
299 {
300 if ( m_xContext.is() )
301 {
302 Reference< XInteractionHandler2 > xFallbackHandler(
303 InteractionHandler::createWithParent(m_xContext, nullptr) );
304 xFallbackHandler->handle( _rxRequest );
305 return true;
306 }
307 return false;
308 }
309
311 {
312 const Reference< XInteractionContinuation >* pContinuations = _rContinuations.getConstArray();
313 for (sal_Int32 i=0; i<_rContinuations.getLength(); ++i, ++pContinuations)
314 {
315 switch (_eCont)
316 {
317 case APPROVE:
318 if (Reference< XInteractionApprove >(*pContinuations, UNO_QUERY).is())
319 return i;
320 break;
321 case DISAPPROVE:
322 if (Reference< XInteractionDisapprove >(*pContinuations, UNO_QUERY).is())
323 return i;
324 break;
325 case RETRY:
326 if (Reference< XInteractionRetry >(*pContinuations, UNO_QUERY).is())
327 return i;
328 break;
329 case ABORT:
330 if (Reference< XInteractionAbort >(*pContinuations, UNO_QUERY).is())
331 return i;
332 break;
334 if (Reference< XInteractionSupplyParameters >(*pContinuations, UNO_QUERY).is())
335 return i;
336 break;
338 if (Reference< XInteractionDocumentSave >(*pContinuations, UNO_QUERY).is())
339 return i;
340 break;
341 }
342 }
343
344 return -1;
345 }
346
347 // SQLExceptionInteractionHandler
348 OUString SAL_CALL SQLExceptionInteractionHandler::getImplementationName()
349 {
350 return "com.sun.star.comp.dbaccess.DatabaseInteractionHandler";
351 }
352 sal_Bool SAL_CALL SQLExceptionInteractionHandler::supportsService(const OUString& _rServiceName)
353 {
354 const css::uno::Sequence< OUString > aSupported(getSupportedServiceNames());
355 for (const OUString& s : aSupported)
356 if (s == _rServiceName)
357 return true;
358
359 return false;
360 }
361 css::uno::Sequence< OUString > SAL_CALL SQLExceptionInteractionHandler::getSupportedServiceNames()
362 {
363 return { "com.sun.star.sdb.DatabaseInteractionHandler" };
364 }
365
366 // LegacyInteractionHandler
367 OUString SAL_CALL LegacyInteractionHandler::getImplementationName()
368 {
369 return "com.sun.star.comp.dbaccess.LegacyInteractionHandler";
370 }
371 sal_Bool SAL_CALL LegacyInteractionHandler::supportsService(const OUString& _rServiceName)
372 {
373 const css::uno::Sequence< OUString > aSupported(getSupportedServiceNames());
374 for (const OUString& s : aSupported)
375 if (s == _rServiceName)
376 return true;
377
378 return false;
379 }
380 css::uno::Sequence< OUString > SAL_CALL LegacyInteractionHandler::getSupportedServiceNames()
381 {
382 return { "com.sun.star.sdb.InteractionHandler" };
383 }
384
385} // namespace dbaui
386
387/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SFX2_DLLPUBLIC short ExecuteQuerySaveDocument(weld::Widget *_pParent, std::u16string_view _rTitle)
static weld::Window * GetFrameWeld(const css::uno::Reference< css::awt::XWindow > &rWindow)
const css::uno::Reference< css::uno::XComponentContext > m_xContext
static void implHandle(const ::dbtools::SQLExceptionInfo &_rSqlInfo, const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > &_rContinuations)
handle SQLExceptions (and derived classes)
bool impl_handle_throw(const css::uno::Reference< css::task::XInteractionRequest > &i_Request)
css::uno::Reference< css::awt::XWindow > m_xParentWindow
virtual void SAL_CALL handle(const css::uno::Reference< css::task::XInteractionRequest > &Request) override
static sal_Int32 getContinuation(Continuation _eCont, const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > &_rContinuations)
check if a given continuation sequence contains a given continuation type
bool implHandleUnknown(const css::uno::Reference< css::task::XInteractionRequest > &_rxRequest)
handles requests which are not SDB-specific
Continuation
known continuation types
virtual sal_Bool SAL_CALL handleInteractionRequest(const css::uno::Reference< css::task::XInteractionRequest > &Request) override
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &rArgs) override
BasicInteractionHandler(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const bool i_bFallbackToGeneric)
Reference< XComponentContext > m_xContext
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_dbaccess_DatabaseInteractionHandler_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_dbaccess_LegacyInteractionHandler_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
#define DBG_UNHANDLED_EXCEPTION(...)
MessBoxStyle
css::uno::Sequence< OUString > getSupportedServiceNames()
int i
HashMap_OWString_Interface aMap
unsigned char sal_Bool
RET_OK
RET_CANCEL
RET_NO
RET_RETRY
RET_YES