LibreOffice Module svtools (master) 1
ehdl.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 <unotools/resmgr.hxx>
21#include <utility>
22#include <vcl/stdtext.hxx>
23#include <vcl/svapp.hxx>
24#include <vcl/weld.hxx>
25#include <sal/log.hxx>
26
27#include <svtools/ehdl.hxx>
28#include <svtools/svtresid.hxx>
29#include <svtools/sfxecode.hxx>
30#include <memory>
31#include <errtxt.hrc>
32
34 weld::Window *pWin, // Parent of the dialog
35 DialogMask nFlags,
36 const OUString &rErr, // error text
37 const OUString &rAction) // action text
38
39/* [Description]
40
41 Draw an errorbox on the screen. Depending on nFlags
42 Error/Info etc. boxes with the requested buttons are shown.
43
44 Returnvalue is the button pressed
45
46 */
47
48
49{
50 SolarMutexGuard aGuard;
51
52 // determine necessary WinBits from the flags
53 VclButtonsType eButtonsType = VclButtonsType::NONE;
54 bool bAddRetry = false;
55 if ( (nFlags & (DialogMask::ButtonsCancel | DialogMask::ButtonsRetry)) == (DialogMask::ButtonsCancel | DialogMask::ButtonsRetry))
56 {
57 bAddRetry = true;
58 eButtonsType = VclButtonsType::Cancel;
59 }
60 else if ( (nFlags & DialogMask::ButtonsOk) == DialogMask::ButtonsOk )
61 eButtonsType = VclButtonsType::Ok;
62 else if ( (nFlags & DialogMask::ButtonsYesNo) == DialogMask::ButtonsYesNo )
63 eButtonsType = VclButtonsType::YesNo;
64
65 OUString aErr("$(ACTION)$(ERROR)");
66 OUString aAction(rAction);
67 if ( !aAction.isEmpty() )
68 aAction += ":\n";
69 aErr = aErr.replaceAll("$(ACTION)", aAction);
70 aErr = aErr.replaceAll("$(ERROR)", rErr);
71
72 VclMessageType eMessageType;
73 switch (nFlags & DialogMask(0xf000))
74 {
75 case DialogMask::MessageError:
76 eMessageType = VclMessageType::Error;
77 break;
78
79 case DialogMask::MessageWarning:
80 eMessageType = VclMessageType::Warning;
81 break;
82
83 case DialogMask::MessageInfo:
84 eMessageType = VclMessageType::Info;
85 break;
86
87 default:
88 {
89 SAL_WARN( "svtools.misc", "no MessBox type");
90 return DialogMask::ButtonsOk;
91 }
92 }
93
94 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pWin,
95 eMessageType, eButtonsType, aErr));
96
97 if (bAddRetry)
98 xBox->add_button(GetStandardText(StandardButtonType::Retry), RET_RETRY);
99
100 switch(nFlags & DialogMask(0x0f00))
101 {
102 case DialogMask::ButtonDefaultsOk:
103 xBox->set_default_response(RET_OK);
104 break;
105 case DialogMask::ButtonDefaultsCancel:
106 xBox->set_default_response(RET_CANCEL);
107 break;
108 case DialogMask::ButtonDefaultsYes:
109 xBox->set_default_response(RET_YES);
110 break;
111 case DialogMask::ButtonDefaultsNo:
112 xBox->set_default_response(RET_NO);
113 break;
114 default:
115 break;
116 }
117
118 DialogMask nRet = DialogMask::NONE;
119 switch (xBox->run())
120 {
121 case RET_OK:
122 nRet = DialogMask::ButtonsOk;
123 break;
124 case RET_CANCEL:
125 nRet = DialogMask::ButtonsCancel;
126 break;
127 case RET_RETRY:
128 nRet = DialogMask::ButtonsRetry;
129 break;
130 case RET_YES:
131 nRet = DialogMask::ButtonsYes;
132 break;
133 case RET_NO:
134 nRet = DialogMask::ButtonsNo;
135 break;
136 default:
137 SAL_WARN( "svtools.misc", "Unknown MessBox return value" );
138 break;
139 }
140
141 return nRet;
142}
143
144SfxErrorHandler::SfxErrorHandler(const ErrMsgCode* pIdPs, ErrCodeArea lStartP, ErrCodeArea lEndP, const std::locale& rLocale)
145 : lStart(lStartP), lEnd(lEndP), pIds(pIdPs), aResLocale(rLocale)
146{
148}
149
151{
152}
153
154bool SfxErrorHandler::CreateString(const ErrorInfo *pErr, OUString &rStr) const
155
156/* [Description]
157
158 Assemble error string for the ErrorInfo pErr.
159
160 */
161
162{
163 ErrCode nErrCode(sal_uInt32(pErr->GetErrorCode()) & ERRCODE_ERROR_MASK);
164 if (pErr->GetErrorCode().GetArea() < lStart || lEnd < pErr->GetErrorCode().GetArea())
165 return false;
166 if(GetErrorString(nErrCode, rStr))
167 {
168 const StringErrorInfo *pStringInfo = dynamic_cast<const StringErrorInfo *>(pErr);
169 if(pStringInfo)
170 {
171 rStr = rStr.replaceAll("$(ARG1)", pStringInfo->GetErrorString());
172 }
173 else
174 {
175 const TwoStringErrorInfo * pTwoStringInfo = dynamic_cast<const TwoStringErrorInfo* >(pErr);
176 if (pTwoStringInfo)
177 {
178 rStr = rStr.replaceAll("$(ARG1)", pTwoStringInfo->GetArg1());
179 rStr = rStr.replaceAll("$(ARG2)", pTwoStringInfo->GetArg2());
180 }
181 }
182 return true;
183 }
184 return false;
185}
186
187void SfxErrorHandler::GetClassString(ErrCodeClass lClassId, OUString &rStr)
188
189/* [Description]
190
191 Creates the string for the class of the error. Will always
192 be read from the resource of the Sfx.
193
194 */
195
196{
197 for (const std::pair<TranslateId, ErrCodeClass>* pItem = RID_ERRHDL_CLASS; pItem->first; ++pItem)
198 {
199 if (pItem->second == lClassId)
200 {
201 rStr = SvtResId(pItem->first);
202 break;
203 }
204 }
205}
206
207bool SfxErrorHandler::GetErrorString(ErrCode lErrId, OUString &rStr) const
208
209/* [Description]
210
211 Creates the error string for the actual error
212 without its class
213
214 */
215
216{
217 bool bRet = false;
218 rStr = "$(CLASS)$(ERROR)";
219
220 for (const ErrMsgCode* pItem = pIds; pItem->second; ++pItem)
221 {
222 if (pItem->second.StripWarningAndDynamic() == lErrId.StripWarningAndDynamic())
223 {
224 rStr = rStr.replaceAll("$(ERROR)", Translate::get(pItem->first, aResLocale));
225 bRet = true;
226 break;
227 }
228 }
229
230 if( bRet )
231 {
232 OUString aErrStr;
233 GetClassString(lErrId.GetClass(), aErrStr);
234 if(!aErrStr.isEmpty())
235 aErrStr += ".\n";
236 rStr = rStr.replaceAll("$(CLASS)",aErrStr);
237 }
238
239 return bRet;
240}
241
243 sal_uInt16 nCtxIdP, weld::Window *pWindow, const ErrMsgCode* pIdsP, const std::locale& rResLocaleP)
244: ErrorContext(pWindow), nCtxId(nCtxIdP), pIds(pIdsP), aResLocale(rResLocaleP)
245{
246 if (!pIds)
248}
249
250
252 sal_uInt16 nCtxIdP, OUString aArg1P, weld::Window *pWindow,
253 const ErrMsgCode* pIdsP, const std::locale& rResLocaleP)
254: ErrorContext(pWindow), nCtxId(nCtxIdP), pIds(pIdsP), aResLocale(rResLocaleP),
255 aArg1(std::move(aArg1P))
256{
257 if (!pIds)
259}
260
261bool SfxErrorContext::GetString(ErrCode nErrId, OUString &rStr)
262
263/* [Description]
264
265 Constructs the description of an error context
266 */
267
268{
269 bool bRet = false;
270 for (const ErrMsgCode* pItem = pIds; pItem->second; ++pItem)
271 {
272 if (sal_uInt32(pItem->second) == nCtxId)
273 {
274 rStr = Translate::get(pItem->first, aResLocale);
275 rStr = rStr.replaceAll("$(ARG1)", aArg1);
276 bRet = true;
277 break;
278 }
279 }
280
281 SAL_WARN_IF(!bRet, "svtools.misc", "ErrorContext cannot find the resource");
282
283 if ( bRet )
284 {
285 sal_uInt16 nId = nErrId.IsWarning() ? ERRCTX_WARNING : ERRCTX_ERROR;
286 for (const ErrMsgCode* pItem = RID_ERRCTX; pItem->second; ++pItem)
287 {
288 if (sal_uInt32(pItem->second) == nId)
289 {
290 rStr = rStr.replaceAll("$(ERR)", Translate::get(pItem->first, aResLocale));
291 break;
292 }
293 }
294 }
295
296 return bRet;
297}
298
299/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
bool IsWarning() const
static void RegisterDisplay(BasicDisplayErrorFunc *)
bool GetString(ErrCode nErrId, OUString &rStr) override
Definition: ehdl.cxx:261
sal_uInt16 nCtxId
Definition: ehdl.hxx:44
SfxErrorContext(sal_uInt16 nCtxIdP, weld::Window *pWin=nullptr, const ErrMsgCode *pIds=nullptr, const std::locale &rResLocaleP=SvtResLocale())
Definition: ehdl.cxx:242
const ErrMsgCode * pIds
Definition: ehdl.hxx:45
std::locale aResLocale
Definition: ehdl.hxx:46
OUString aArg1
Definition: ehdl.hxx:47
bool GetErrorString(ErrCode lErrId, OUString &) const
Definition: ehdl.cxx:207
static SVT_DLLPRIVATE void GetClassString(ErrCodeClass lErrId, OUString &)
Definition: ehdl.cxx:187
SfxErrorHandler(const ErrMsgCode *pIds, ErrCodeArea lStart, ErrCodeArea lEnd, const std::locale &rResLocale=SvtResLocale())
Definition: ehdl.cxx:144
virtual ~SfxErrorHandler() override
Definition: ehdl.cxx:150
virtual bool CreateString(const ErrorInfo *, OUString &) const override
Definition: ehdl.cxx:154
const OUString & GetErrorString() const
const OUString & GetArg1() const
const OUString & GetArg2() const
static DialogMask aWndFunc(weld::Window *pWin, DialogMask nFlags, const OUString &rErr, const OUString &rAction)
Definition: ehdl.cxx:33
SVT_DLLPUBLIC const ErrMsgCode RID_ERRCTX[]
std::pair< TranslateId, ErrCode > ErrMsgCode
Definition: ehdl.hxx:26
ErrCodeClass
#define ERRCODE_ERROR_MASK
ErrCodeArea
DialogMask
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
OUString get(TranslateId sContextAndId, const std::locale &loc)
sal_Int16 nId
#define ERRCTX_ERROR
Definition: sfxecode.hxx:43
#define ERRCTX_WARNING
Definition: sfxecode.hxx:44
OUString VCL_DLLPUBLIC GetStandardText(StandardButtonType eButton)
OUString SvtResId(TranslateId aId)
Definition: svtresid.cxx:24
RET_OK
RET_CANCEL
RET_NO
RET_RETRY
RET_YES
VclMessageType
VclButtonsType