bump product version to 4.1.6.2
[LibreOffice.git] / uui / source / iahndl-errorhandler.cxx
blobbeedaab3ed228b87196820330e6b04a9c97e78ca
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <memory>
22 #include "osl/mutex.hxx"
23 #include "vcl/svapp.hxx"
24 #include "vcl/msgbox.hxx"
26 #include "com/sun/star/task/XInteractionAbort.hpp"
27 #include "com/sun/star/task/XInteractionApprove.hpp"
28 #include "com/sun/star/task/XInteractionDisapprove.hpp"
29 #include "com/sun/star/task/XInteractionRetry.hpp"
31 #include "tools/errinf.hxx" // ErrorHandler, ErrorContext, ...
32 #include "svtools/svtools.hrc" // RID_ERRHDL
34 #include "ids.hrc"
35 #include "getcontinuations.hxx"
37 #include "iahndl.hxx"
39 using namespace com::sun::star;
41 namespace {
43 sal_uInt16
44 executeErrorDialog(
45 Window * pParent,
46 task::InteractionClassification eClassification,
47 OUString const & rContext,
48 OUString const & rMessage,
49 WinBits nButtonMask)
50 SAL_THROW((uno::RuntimeException))
52 SolarMutexGuard aGuard;
54 OUStringBuffer aText(rContext);
55 if (!rContext.isEmpty() && !rMessage.isEmpty())
56 aText.appendAscii(":\n");
57 //TODO! must be internationalized
58 aText.append(rMessage);
60 std::auto_ptr< MessBox > xBox;
61 try
63 switch (eClassification)
65 case task::InteractionClassification_ERROR:
66 xBox.reset(new ErrorBox(pParent,
67 nButtonMask,
68 aText.makeStringAndClear()));
69 break;
71 case task::InteractionClassification_WARNING:
72 xBox.reset(new WarningBox(pParent,
73 nButtonMask,
74 aText.makeStringAndClear()));
75 break;
77 case task::InteractionClassification_INFO:
78 if ((nButtonMask & 0x01F00000) == WB_DEF_OK)
79 //TODO! missing win bit button mask define (want to ignore
80 // any default button settings)...
81 xBox.reset(new InfoBox(pParent,
82 aText.makeStringAndClear()));
83 else
84 xBox.reset(new ErrorBox(pParent,
85 nButtonMask,
86 aText.makeStringAndClear()));
87 break;
89 case task::InteractionClassification_QUERY:
90 xBox.reset(new QueryBox(pParent,
91 nButtonMask,
92 aText.makeStringAndClear()));
93 break;
95 default:
96 OSL_ASSERT(false);
97 break;
100 catch (std::bad_alloc const &)
102 throw uno::RuntimeException(OUString("out of memory"),
103 uno::Reference< uno::XInterface >());
106 sal_uInt16 aResult = xBox->Execute();
107 switch( aResult )
109 case RET_OK:
110 aResult = ERRCODE_BUTTON_OK;
111 break;
112 case RET_CANCEL:
113 aResult = ERRCODE_BUTTON_CANCEL;
114 break;
115 case RET_YES:
116 aResult = ERRCODE_BUTTON_YES;
117 break;
118 case RET_NO:
119 aResult = ERRCODE_BUTTON_NO;
120 break;
121 case RET_RETRY:
122 aResult = ERRCODE_BUTTON_RETRY;
123 break;
126 return aResult;
131 void
132 UUIInteractionHelper::handleErrorHandlerRequest(
133 task::InteractionClassification eClassification,
134 ErrCode nErrorCode,
135 std::vector< OUString > const & rArguments,
136 uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
137 rContinuations,
138 bool bObtainErrorStringOnly,
139 bool & bHasErrorString,
140 OUString & rErrorString)
141 SAL_THROW((uno::RuntimeException))
143 if (bObtainErrorStringOnly)
145 bHasErrorString = isInformationalErrorMessageRequest(rContinuations);
146 if (!bHasErrorString)
147 return;
150 OUString aMessage;
152 enum Source { SOURCE_DEFAULT, SOURCE_CNT, SOURCE_SVX, SOURCE_UUI };
153 static char const * const aManager[4] = { "ofa", "cnt", "svx", "uui" };
154 static sal_uInt16 const aId[4]
155 = { RID_ERRHDL,
156 RID_CHAOS_START + 12,
157 // cf. chaos/source/inc/cntrids.hrc, where
158 // #define RID_CHAOS_ERRHDL (RID_CHAOS_START + 12)
159 RID_SVX_START + 350, // RID_SVXERRCODE
160 RID_UUI_ERRHDL };
161 ErrCode nErrorId = nErrorCode & ~ERRCODE_WARNING_MASK;
162 Source eSource = nErrorId < ERRCODE_AREA_LIB1 ?
163 SOURCE_DEFAULT :
164 nErrorId >= ERRCODE_AREA_CHAOS
165 && nErrorId < ERRCODE_AREA_CHAOS_END ?
166 SOURCE_CNT :
167 nErrorId >= ERRCODE_AREA_SVX
168 && nErrorId <= ERRCODE_AREA_SVX_END ?
169 SOURCE_SVX :
170 SOURCE_UUI;
172 SolarMutexGuard aGuard;
173 std::auto_ptr< ResMgr > xManager;
174 xManager.reset(ResMgr::CreateResMgr(aManager[eSource]));
175 if (!xManager.get())
176 return;
177 ResId aResId(aId[eSource], *xManager.get());
178 if (!ErrorResource(aResId).getString(nErrorCode, aMessage))
179 return;
182 aMessage = replaceMessageWithArguments( aMessage, rArguments );
184 if (bObtainErrorStringOnly)
186 rErrorString = aMessage;
187 return;
189 else
191 //TODO! It can happen that the buttons calculated below do not match
192 // the error text from the resource (e.g., some text that is not a
193 // question, but YES and NO buttons). Some error texts have
194 // ExtraData that specifies a set of buttons, but that data is not
195 // really useful, because a single error text may well make sense
196 // both with only an OK button and with RETRY and CANCEL buttons.
198 uno::Reference< task::XInteractionApprove > xApprove;
199 uno::Reference< task::XInteractionDisapprove > xDisapprove;
200 uno::Reference< task::XInteractionRetry > xRetry;
201 uno::Reference< task::XInteractionAbort > xAbort;
202 getContinuations(
203 rContinuations, &xApprove, &xDisapprove, &xRetry, &xAbort);
205 // The following mapping uses the bit mask
206 // Approve = 8,
207 // Disapprove = 4,
208 // Retry = 2,
209 // Abort = 1
211 // The mapping has five properties on which the code to select the
212 // correct continuation relies:
213 // 1 The OK button is mapped to Approve if that is available,
214 // otherwise to Abort if that is available, otherwise to none.
215 // 2 The CANCEL button is always mapped to Abort.
216 // 3 The RETRY button is always mapped to Retry.
217 // 4 The NO button is always mapped to Disapprove.
218 // 5 The YES button is always mapped to Approve.
220 // Because the WinBits button combinations are quite restricted, not
221 // every request can be served here.
223 // Finally, it seems to be better to leave default button
224 // determination to VCL (the favouring of CANCEL as default button
225 // seems to not always be what the user wants)...
226 WinBits const aButtonMask[16]
227 = { 0,
228 WB_OK /*| WB_DEF_OK*/, // Abort
230 WB_RETRY_CANCEL /*| WB_DEF_CANCEL*/, // Retry, Abort
235 WB_OK /*| WB_DEF_OK*/, // Approve
236 WB_OK_CANCEL /*| WB_DEF_CANCEL*/, // Approve, Abort
239 WB_YES_NO /*| WB_DEF_NO*/, // Approve, Disapprove
240 WB_YES_NO_CANCEL /*| WB_DEF_CANCEL*/,
241 // Approve, Disapprove, Abort
243 0 };
245 WinBits nButtonMask = aButtonMask[(xApprove.is() ? 8 : 0)
246 | (xDisapprove.is() ? 4 : 0)
247 | (xRetry.is() ? 2 : 0)
248 | (xAbort.is() ? 1 : 0)];
249 if (nButtonMask == 0)
250 return;
252 //TODO! remove this backwards compatibility?
253 OUString aContext(getContextProperty());
254 if (aContext.isEmpty() && nErrorCode != 0)
256 SolarMutexGuard aGuard;
257 ErrorContext * pContext = ErrorContext::GetContext();
258 if (pContext)
260 OUString aContextString;
261 if (pContext->GetString(nErrorCode, aContextString))
262 aContext = aContextString;
266 sal_uInt16 nResult = executeErrorDialog(
267 getParentProperty(), eClassification, aContext, aMessage, nButtonMask );
269 switch (nResult)
271 case ERRCODE_BUTTON_OK:
272 OSL_ENSURE(xApprove.is() || xAbort.is(), "unexpected situation");
273 if (xApprove.is())
274 xApprove->select();
275 else if (xAbort.is())
276 xAbort->select();
277 break;
279 case ERRCODE_BUTTON_CANCEL:
280 OSL_ENSURE(xAbort.is(), "unexpected situation");
281 if (xAbort.is())
282 xAbort->select();
283 break;
285 case ERRCODE_BUTTON_RETRY:
286 OSL_ENSURE(xRetry.is(), "unexpected situation");
287 if (xRetry.is())
288 xRetry->select();
289 break;
291 case ERRCODE_BUTTON_NO:
292 OSL_ENSURE(xDisapprove.is(), "unexpected situation");
293 if (xDisapprove.is())
294 xDisapprove->select();
295 break;
297 case ERRCODE_BUTTON_YES:
298 OSL_ENSURE(xApprove.is(), "unexpected situation");
299 if (xApprove.is())
300 xApprove->select();
301 break;
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */