xref: /trunk/main/uui/source/iahndl-errorhandler.cxx (revision 46c69c84579d1f1df536015e7d54c9965db90b69)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <memory>
25 
26 #include "vos/mutex.hxx"
27 #include "vcl/svapp.hxx"
28 #include "vcl/msgbox.hxx"
29 
30 #include "com/sun/star/task/XInteractionAbort.hpp"
31 #include "com/sun/star/task/XInteractionApprove.hpp"
32 #include "com/sun/star/task/XInteractionDisapprove.hpp"
33 #include "com/sun/star/task/XInteractionRetry.hpp"
34 
35 #include "tools/errinf.hxx" // ErrorHandler, ErrorContext, ...
36 #include "svtools/svtools.hrc" // RID_ERRHDL
37 
38 #include "ids.hrc"
39 #include "getcontinuations.hxx"
40 
41 #include "iahndl.hxx"
42 
43 using namespace com::sun::star;
44 
45 namespace {
46 
47 sal_uInt16
executeErrorDialog(Window * pParent,task::InteractionClassification eClassification,rtl::OUString const & rContext,rtl::OUString const & rMessage,WinBits nButtonMask)48 executeErrorDialog(
49     Window * pParent,
50     task::InteractionClassification eClassification,
51     rtl::OUString const & rContext,
52     rtl::OUString const & rMessage,
53     WinBits nButtonMask)
54 {
55     vos::OGuard aGuard(Application::GetSolarMutex());
56 
57     rtl::OUStringBuffer aText(rContext);
58     if (rContext.getLength() != 0 && rMessage.getLength() != 0)
59         aText.appendAscii(RTL_CONSTASCII_STRINGPARAM(":\n"));
60             //TODO! must be internationalized
61     aText.append(rMessage);
62 
63     std::unique_ptr< MessBox > xBox;
64     try
65     {
66         switch (eClassification)
67         {
68         case task::InteractionClassification_ERROR:
69             xBox.reset(new ErrorBox(pParent,
70                                     nButtonMask,
71                                     aText.makeStringAndClear()));
72             break;
73 
74         case task::InteractionClassification_WARNING:
75             xBox.reset(new WarningBox(pParent,
76                                       nButtonMask,
77                                       aText.makeStringAndClear()));
78             break;
79 
80         case task::InteractionClassification_INFO:
81             if ((nButtonMask & 0x01F00000) == WB_DEF_OK)
82                 //TODO! missing win bit button mask define (want to ignore
83                 // any default button settings)...
84                 xBox.reset(new InfoBox(pParent,
85                                        aText.makeStringAndClear()));
86             else
87                 xBox.reset(new ErrorBox(pParent,
88                                         nButtonMask,
89                                         aText.makeStringAndClear()));
90             break;
91 
92         case task::InteractionClassification_QUERY:
93             xBox.reset(new QueryBox(pParent,
94                                     nButtonMask,
95                                     aText.makeStringAndClear()));
96             break;
97 
98         default:
99             OSL_ASSERT(false);
100             break;
101         }
102     }
103     catch (std::bad_alloc const &)
104     {
105         throw uno::RuntimeException(
106             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
107             uno::Reference< uno::XInterface >());
108     }
109 
110     sal_uInt16 aResult = xBox->Execute();
111     switch( aResult )
112     {
113     case BUTTONID_OK:
114         aResult = ERRCODE_BUTTON_OK;
115         break;
116     case BUTTONID_CANCEL:
117         aResult = ERRCODE_BUTTON_CANCEL;
118         break;
119     case BUTTONID_YES:
120         aResult = ERRCODE_BUTTON_YES;
121         break;
122     case BUTTONID_NO:
123         aResult = ERRCODE_BUTTON_NO;
124         break;
125     case BUTTONID_RETRY:
126         aResult = ERRCODE_BUTTON_RETRY;
127         break;
128     }
129 
130     return aResult;
131 }
132 
133 }
134 
135 void
handleErrorHandlerRequest(task::InteractionClassification eClassification,ErrCode nErrorCode,std::vector<rtl::OUString> const & rArguments,uno::Sequence<uno::Reference<task::XInteractionContinuation>> const & rContinuations,bool bObtainErrorStringOnly,bool & bHasErrorString,rtl::OUString & rErrorString)136 UUIInteractionHelper::handleErrorHandlerRequest(
137     task::InteractionClassification eClassification,
138     ErrCode nErrorCode,
139     std::vector< rtl::OUString > const & rArguments,
140     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
141         rContinuations,
142     bool bObtainErrorStringOnly,
143     bool & bHasErrorString,
144     rtl::OUString & rErrorString)
145 {
146     if (bObtainErrorStringOnly)
147     {
148         bHasErrorString = isInformationalErrorMessageRequest(rContinuations);
149         if (!bHasErrorString)
150             return;
151     }
152 
153     rtl::OUString aMessage;
154     {
155         enum Source { SOURCE_DEFAULT, SOURCE_CNT, SOURCE_SVX, SOURCE_UUI };
156         static char const * const aManager[4]
157             = { CREATEVERSIONRESMGR_NAME(ofa),
158                 CREATEVERSIONRESMGR_NAME(cnt),
159                 CREATEVERSIONRESMGR_NAME(svx),
160                 CREATEVERSIONRESMGR_NAME(uui) };
161         static sal_uInt16 const aId[4]
162             = { RID_ERRHDL,
163                 RID_CHAOS_START + 12,
164                 // cf. chaos/source/inc/cntrids.hrc, where
165                 // #define RID_CHAOS_ERRHDL (RID_CHAOS_START + 12)
166                 RID_SVX_START + 350, // RID_SVXERRCODE
167                 RID_UUI_ERRHDL };
168         ErrCode nErrorId = nErrorCode & ~ERRCODE_WARNING_MASK;
169         Source eSource = nErrorId < ERRCODE_AREA_LIB1 ?
170             SOURCE_DEFAULT :
171             nErrorId >= ERRCODE_AREA_CHAOS
172             && nErrorId < ERRCODE_AREA_CHAOS_END ?
173             SOURCE_CNT :
174             nErrorId >= ERRCODE_AREA_SVX
175             && nErrorId <= ERRCODE_AREA_SVX_END ?
176             SOURCE_SVX :
177             SOURCE_UUI;
178 
179         vos::OGuard aGuard(Application::GetSolarMutex());
180         std::unique_ptr< ResMgr > xManager;
181         xManager.reset(ResMgr::CreateResMgr(aManager[eSource]));
182         if (!xManager.get())
183             return;
184         ResId aResId(aId[eSource], *xManager.get());
185         if (!ErrorResource(aResId).getString(nErrorCode, &aMessage))
186             return;
187     }
188 
189     aMessage = replaceMessageWithArguments( aMessage, rArguments );
190 
191     if (bObtainErrorStringOnly)
192     {
193         rErrorString = aMessage;
194         return;
195     }
196     else
197     {
198         //TODO! It can happen that the buttons calculated below do not match
199         // the error text from the resource (e.g., some text that is not a
200         // question, but YES and NO buttons).  Some error texts have
201         // ExtraData that specifies a set of buttons, but that data is not
202         // really useful, because a single error text may well make sense
203         // both with only an OK button and with RETRY and CANCEL buttons.
204 
205         uno::Reference< task::XInteractionApprove > xApprove;
206         uno::Reference< task::XInteractionDisapprove > xDisapprove;
207         uno::Reference< task::XInteractionRetry > xRetry;
208         uno::Reference< task::XInteractionAbort > xAbort;
209         getContinuations(
210             rContinuations, &xApprove, &xDisapprove, &xRetry, &xAbort);
211 
212         // The following mapping uses the bit mask
213         //     Approve = 8,
214         //     Disapprove = 4,
215         //     Retry = 2,
216         //     Abort = 1
217         //
218         // The mapping has five properties on which the code to select the
219         // correct continuation relies:
220         // 1  The OK button is mapped to Approve if that is available,
221         //    otherwise to Abort if that is available, otherwise to none.
222         // 2  The CANCEL button is always mapped to Abort.
223         // 3  The RETRY button is always mapped to Retry.
224         // 4  The NO button is always mapped to Disapprove.
225         // 5  The YES button is always mapped to Approve.
226         //
227         // Because the WinBits button combinations are quite restricted, not
228         // every request can be served here.
229         //
230         // Finally, it seems to be better to leave default button
231         // determination to VCL (the favouring of CANCEL as default button
232         // seems to not always be what the user wants)...
233         WinBits const aButtonMask[16]
234             = { 0,
235                 WB_OK /*| WB_DEF_OK*/, // Abort
236                 0,
237                 WB_RETRY_CANCEL /*| WB_DEF_CANCEL*/, // Retry, Abort
238                 0,
239                 0,
240                 0,
241                 0,
242                 WB_OK /*| WB_DEF_OK*/, // Approve
243                 WB_OK_CANCEL /*| WB_DEF_CANCEL*/, // Approve, Abort
244                 0,
245                 0,
246                 WB_YES_NO /*| WB_DEF_NO*/, // Approve, Disapprove
247                 WB_YES_NO_CANCEL /*| WB_DEF_CANCEL*/,
248                 // Approve, Disapprove, Abort
249                 0,
250                 0 };
251 
252         WinBits nButtonMask = aButtonMask[(xApprove.is() ? 8 : 0)
253                                           | (xDisapprove.is() ? 4 : 0)
254                                           | (xRetry.is() ? 2 : 0)
255                                           | (xAbort.is() ? 1 : 0)];
256         if (nButtonMask == 0)
257             return;
258 
259         //TODO! remove this backwards compatibility?
260         rtl::OUString aContext(getContextProperty());
261         if (aContext.getLength() == 0 && nErrorCode != 0)
262         {
263             vos::OGuard aGuard(Application::GetSolarMutex());
264             ErrorContext * pContext = ErrorContext::GetContext();
265             if (pContext)
266             {
267                 UniString aContextString;
268                 if (pContext->GetString(nErrorCode, aContextString))
269                     aContext = aContextString;
270             }
271         }
272 
273         sal_uInt16 nResult = executeErrorDialog(
274             getParentProperty(), eClassification, aContext, aMessage, nButtonMask );
275 
276         switch (nResult)
277         {
278         case ERRCODE_BUTTON_OK:
279             OSL_ENSURE(xApprove.is() || xAbort.is(), "unexpected situation");
280             if (xApprove.is())
281                 xApprove->select();
282             else if (xAbort.is())
283                 xAbort->select();
284             break;
285 
286         case ERRCODE_BUTTON_CANCEL:
287             OSL_ENSURE(xAbort.is(), "unexpected situation");
288             if (xAbort.is())
289                 xAbort->select();
290             break;
291 
292         case ERRCODE_BUTTON_RETRY:
293             OSL_ENSURE(xRetry.is(), "unexpected situation");
294             if (xRetry.is())
295                 xRetry->select();
296             break;
297 
298         case ERRCODE_BUTTON_NO:
299             OSL_ENSURE(xDisapprove.is(), "unexpected situation");
300             if (xDisapprove.is())
301                 xDisapprove->select();
302             break;
303 
304         case ERRCODE_BUTTON_YES:
305             OSL_ENSURE(xApprove.is(), "unexpected situation");
306             if (xApprove.is())
307                 xApprove->select();
308             break;
309         }
310 
311     }
312 }
313