bump product version to 5.0.4.1
[LibreOffice.git] / uui / source / iahndl-errorhandler.cxx
blob31dfe1e40370cd40cc4aa5965e11b9785f12f220
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 <osl/mutex.hxx>
21 #include <vcl/svapp.hxx>
22 #include <vcl/msgbox.hxx>
24 #include <com/sun/star/task/XInteractionAbort.hpp>
25 #include <com/sun/star/task/XInteractionApprove.hpp>
26 #include <com/sun/star/task/XInteractionDisapprove.hpp>
27 #include <com/sun/star/task/XInteractionRetry.hpp>
29 #include <tools/errinf.hxx>
30 #include <svtools/svtools.hrc>
32 #include "ids.hrc"
33 #include "getcontinuations.hxx"
35 #include "iahndl.hxx"
36 #include <boost/scoped_ptr.hpp>
38 using namespace com::sun::star;
40 namespace {
42 sal_uInt16
43 executeErrorDialog(
44 vcl::Window * pParent,
45 task::InteractionClassification eClassification,
46 OUString const & rContext,
47 OUString const & rMessage,
48 WinBits nButtonMask)
50 SolarMutexGuard aGuard;
52 OUStringBuffer aText(rContext);
53 if (!rContext.isEmpty() && !rMessage.isEmpty())
54 aText.appendAscii(":\n");
55 //TODO! must be internationalized
56 aText.append(rMessage);
58 VclPtr< MessBox > xBox;
59 try
61 switch (eClassification)
63 case task::InteractionClassification_ERROR:
64 xBox.reset(VclPtr<ErrorBox>::Create(pParent,
65 nButtonMask,
66 aText.makeStringAndClear()));
67 break;
69 case task::InteractionClassification_WARNING:
70 xBox.reset(VclPtr<WarningBox>::Create(pParent,
71 nButtonMask,
72 aText.makeStringAndClear()));
73 break;
75 case task::InteractionClassification_INFO:
76 # define WB_DEF_BUTTONS (WB_DEF_OK | WB_DEF_CANCEL | WB_DEF_RETRY)
77 //(want to ignore any default button settings)...
78 if ((nButtonMask & WB_DEF_BUTTONS) == WB_DEF_OK)
79 xBox.reset(VclPtr<InfoBox>::Create(pParent,
80 aText.makeStringAndClear()));
81 else
82 xBox.reset(VclPtr<ErrorBox>::Create(pParent,
83 nButtonMask,
84 aText.makeStringAndClear()));
85 break;
87 case task::InteractionClassification_QUERY:
88 xBox.reset(VclPtr<QueryBox>::Create(pParent,
89 nButtonMask,
90 aText.makeStringAndClear()));
91 break;
93 default:
94 OSL_ASSERT(false);
95 break;
98 catch (std::bad_alloc const &)
100 throw uno::RuntimeException("out of memory");
103 sal_uInt16 aResult = xBox->Execute();
105 xBox.disposeAndClear();
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)
142 if (bObtainErrorStringOnly)
144 bHasErrorString = isInformationalErrorMessageRequest(rContinuations);
145 if (!bHasErrorString)
146 return;
149 OUString aMessage;
151 enum Source { SOURCE_DEFAULT, SOURCE_CNT, SOURCE_SVX, SOURCE_UUI };
152 static char const * const aManager[4] = { "ofa", "cnt", "svx", "uui" };
153 static sal_uInt16 const aId[4]
154 = { RID_ERRHDL,
155 RID_CHAOS_START + 12,
156 // cf. chaos/source/inc/cntrids.hrc, where
157 // #define RID_CHAOS_ERRHDL (RID_CHAOS_START + 12)
158 RID_SVX_START + 350, // RID_SVXERRCODE
159 RID_UUI_ERRHDL };
160 ErrCode nErrorId = nErrorCode & ~ERRCODE_WARNING_MASK;
161 Source eSource = nErrorId < ERRCODE_AREA_LIB1 ?
162 SOURCE_DEFAULT :
163 nErrorId >= ERRCODE_AREA_CHAOS
164 && nErrorId < ERRCODE_AREA_CHAOS_END ?
165 SOURCE_CNT :
166 nErrorId >= ERRCODE_AREA_SVX
167 && nErrorId <= ERRCODE_AREA_SVX_END ?
168 SOURCE_SVX :
169 SOURCE_UUI;
171 SolarMutexGuard aGuard;
172 boost::scoped_ptr< ResMgr > xManager;
173 xManager.reset(ResMgr::CreateResMgr(aManager[eSource]));
174 if (!xManager.get())
175 return;
176 ResId aResId(aId[eSource], *xManager.get());
177 if (!ErrorResource(aResId).getString(nErrorCode, aMessage))
178 return;
181 aMessage = replaceMessageWithArguments( aMessage, rArguments );
183 if (bObtainErrorStringOnly)
185 rErrorString = aMessage;
186 return;
188 else
190 //TODO! It can happen that the buttons calculated below do not match
191 // the error text from the resource (e.g., some text that is not a
192 // question, but YES and NO buttons). Some error texts have
193 // ExtraData that specifies a set of buttons, but that data is not
194 // really useful, because a single error text may well make sense
195 // both with only an OK button and with RETRY and CANCEL buttons.
197 uno::Reference< task::XInteractionApprove > xApprove;
198 uno::Reference< task::XInteractionDisapprove > xDisapprove;
199 uno::Reference< task::XInteractionRetry > xRetry;
200 uno::Reference< task::XInteractionAbort > xAbort;
201 getContinuations(
202 rContinuations, &xApprove, &xDisapprove, &xRetry, &xAbort);
204 // The following mapping uses the bit mask
205 // Approve = 8,
206 // Disapprove = 4,
207 // Retry = 2,
208 // Abort = 1
210 // The mapping has five properties on which the code to select the
211 // correct continuation relies:
212 // 1 The OK button is mapped to Approve if that is available,
213 // otherwise to Abort if that is available, otherwise to none.
214 // 2 The CANCEL button is always mapped to Abort.
215 // 3 The RETRY button is always mapped to Retry.
216 // 4 The NO button is always mapped to Disapprove.
217 // 5 The YES button is always mapped to Approve.
219 // Because the WinBits button combinations are quite restricted, not
220 // every request can be served here.
222 // Finally, it seems to be better to leave default button
223 // determination to VCL (the favouring of CANCEL as default button
224 // seems to not always be what the user wants)...
225 WinBits const aButtonMask[16]
226 = { 0,
227 WB_OK /*| WB_DEF_OK*/, // Abort
229 WB_RETRY_CANCEL /*| WB_DEF_CANCEL*/, // Retry, Abort
234 WB_OK /*| WB_DEF_OK*/, // Approve
235 WB_OK_CANCEL /*| WB_DEF_CANCEL*/, // Approve, Abort
238 WB_YES_NO /*| WB_DEF_NO*/, // Approve, Disapprove
239 WB_YES_NO_CANCEL /*| WB_DEF_CANCEL*/,
240 // Approve, Disapprove, Abort
242 0 };
244 WinBits nButtonMask = aButtonMask[(xApprove.is() ? 8 : 0)
245 | (xDisapprove.is() ? 4 : 0)
246 | (xRetry.is() ? 2 : 0)
247 | (xAbort.is() ? 1 : 0)];
248 if (nButtonMask == 0)
249 return;
251 //TODO! remove this backwards compatibility?
252 OUString aContext(getContextProperty());
253 if (aContext.isEmpty() && nErrorCode != 0)
255 SolarMutexGuard aGuard;
256 ErrorContext * pContext = ErrorContext::GetContext();
257 if (pContext)
259 OUString aContextString;
260 if (pContext->GetString(nErrorCode, aContextString))
261 aContext = aContextString;
265 sal_uInt16 nResult = executeErrorDialog(
266 getParentProperty(), eClassification, aContext, aMessage, nButtonMask );
268 switch (nResult)
270 case ERRCODE_BUTTON_OK:
271 OSL_ENSURE(xApprove.is() || xAbort.is(), "unexpected situation");
272 if (xApprove.is())
273 xApprove->select();
274 else if (xAbort.is())
275 xAbort->select();
276 break;
278 case ERRCODE_BUTTON_CANCEL:
279 OSL_ENSURE(xAbort.is(), "unexpected situation");
280 if (xAbort.is())
281 xAbort->select();
282 break;
284 case ERRCODE_BUTTON_RETRY:
285 OSL_ENSURE(xRetry.is(), "unexpected situation");
286 if (xRetry.is())
287 xRetry->select();
288 break;
290 case ERRCODE_BUTTON_NO:
291 OSL_ENSURE(xDisapprove.is(), "unexpected situation");
292 if (xDisapprove.is())
293 xDisapprove->select();
294 break;
296 case ERRCODE_BUTTON_YES:
297 OSL_ENSURE(xApprove.is(), "unexpected situation");
298 if (xApprove.is())
299 xApprove->select();
300 break;
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */