Bump version to 5.0-14
[LibreOffice.git] / svtools / source / misc / ehdl.cxx
blobfa8cf14fed01c727f656971bb89f6c4e3878ccf1
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 <tools/rcid.h>
22 #include <tools/wintypes.hxx>
23 #include <vcl/msgbox.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/settings.hxx>
27 #include <svtools/ehdl.hxx>
28 #include <svtools/svtresid.hxx>
29 #include <svtools/svtools.hrc>
30 #include <svtools/sfxecode.hxx>
31 #include <boost/scoped_ptr.hpp>
34 static sal_uInt16 aWndFunc(
35 vcl::Window *pWin, // Parent of the dialog
36 sal_uInt16 nFlags,
37 const OUString &rErr, // error text
38 const OUString &rAction) // action text
40 /* [Description]
42 Draw an errorbox on the screen. Depending on nFlags
43 Error/Info etc. boxes with the requested buttosn are shown.
45 Returnvalue is the button pressed
51 SolarMutexGuard aGuard;
53 // determine necessary WinBits from the flags
54 WinBits eBits=0;
55 if ( (ERRCODE_BUTTON_CANCEL|ERRCODE_BUTTON_RETRY) == (nFlags & (ERRCODE_BUTTON_CANCEL|ERRCODE_BUTTON_RETRY)) )
56 eBits = WB_RETRY_CANCEL;
57 else if ( ERRCODE_BUTTON_OK_CANCEL == (nFlags & ERRCODE_BUTTON_OK_CANCEL) )
58 eBits = WB_OK_CANCEL;
59 else if ( ERRCODE_BUTTON_OK == (nFlags & ERRCODE_BUTTON_OK) )
60 eBits = WB_OK;
61 else if ( ERRCODE_BUTTON_YES_NO_CANCEL == (nFlags & ERRCODE_BUTTON_YES_NO_CANCEL) )
62 eBits = WB_YES_NO_CANCEL;
63 else if ( ERRCODE_BUTTON_YES_NO == (nFlags & ERRCODE_BUTTON_YES_NO) )
64 eBits = WB_YES_NO;
66 switch(nFlags & 0x0f00)
68 case ERRCODE_BUTTON_DEF_OK:
69 eBits |= WB_DEF_OK;
70 break;
72 case ERRCODE_BUTTON_DEF_CANCEL:
73 eBits |= WB_DEF_CANCEL;
74 break;
76 case ERRCODE_BUTTON_DEF_YES:
77 eBits |= WB_DEF_YES;
78 break;
80 case ERRCODE_BUTTON_DEF_NO:
81 eBits |= WB_DEF_NO;
82 break;
85 OUString aErr(SvtResId(STR_ERR_HDLMESS).toString());
86 OUString aAction(rAction);
87 if ( !aAction.isEmpty() )
88 aAction += ":\n";
89 aErr = aErr.replaceAll("$(ACTION)", aAction);
90 aErr = aErr.replaceAll("$(ERROR)", rErr);
92 VclPtr<MessBox> pBox;
93 switch ( nFlags & 0xf000 )
95 case ERRCODE_MSG_ERROR:
96 pBox.reset(VclPtr<ErrorBox>::Create(pWin, eBits, aErr));
97 break;
99 case ERRCODE_MSG_WARNING:
100 pBox.reset(VclPtr<WarningBox>::Create(pWin, eBits, aErr));
101 break;
103 case ERRCODE_MSG_INFO:
104 pBox.reset(VclPtr<InfoBox>::Create(pWin, aErr));
105 break;
107 case ERRCODE_MSG_QUERY:
108 pBox.reset(VclPtr<QueryBox>::Create(pWin, eBits, aErr));
109 break;
111 default:
113 SAL_WARN( "svtools.misc", "no MessBox type");
114 return ERRCODE_BUTTON_OK;
118 sal_uInt16 nRet = RET_CANCEL;
119 switch ( pBox->Execute() )
121 case RET_OK:
122 nRet = ERRCODE_BUTTON_OK;
123 break;
124 case RET_CANCEL:
125 nRet = ERRCODE_BUTTON_CANCEL;
126 break;
127 case RET_RETRY:
128 nRet = ERRCODE_BUTTON_RETRY;
129 break;
130 case RET_YES:
131 nRet = ERRCODE_BUTTON_YES;
132 break;
133 case RET_NO:
134 nRet = ERRCODE_BUTTON_NO;
135 break;
136 default:
137 SAL_WARN( "svtools.misc", "Unknown MessBox return value" );
138 break;
140 return nRet;
145 SfxErrorHandler::SfxErrorHandler(sal_uInt16 nIdP, sal_uLong lStartP, sal_uLong lEndP, ResMgr *pMgrP) :
147 lStart(lStartP), lEnd(lEndP), nId(nIdP), pMgr(pMgrP), pFreeMgr( NULL )
150 RegisterDisplay(&aWndFunc);
151 if( ! pMgr )
153 pFreeMgr = pMgr = ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() );
159 SfxErrorHandler::~SfxErrorHandler()
161 delete pFreeMgr;
166 bool SfxErrorHandler::CreateString(
167 const ErrorInfo *pErr, OUString &rStr, sal_uInt16& nFlags) const
169 /* [Description]
171 Assemble error string for the ErrorInfo pErr.
176 sal_uLong nErrCode = pErr->GetErrorCode() & ERRCODE_ERROR_MASK;
177 if( nErrCode>=lEnd || nErrCode<=lStart )
178 return false;
179 const MessageInfo *pMsgInfo = PTR_CAST(MessageInfo,pErr);
180 if(pMsgInfo)
182 if(GetMessageString(nErrCode, rStr, nFlags))
184 rStr = rStr.replaceAll("$(ARG1)", pMsgInfo->GetMessageArg());
185 return true;
188 else if(GetErrorString(nErrCode, rStr, nFlags))
190 const StringErrorInfo *pStringInfo = PTR_CAST(StringErrorInfo,pErr);
191 if(pStringInfo)
193 rStr = rStr.replaceAll(OUString("$(ARG1)"),
194 pStringInfo->GetErrorString());
196 else
198 const TwoStringErrorInfo * pTwoStringInfo = PTR_CAST(TwoStringErrorInfo,
199 pErr);
200 if (pTwoStringInfo)
202 rStr = rStr.replaceAll("$(ARG1)", pTwoStringInfo->GetArg1());
203 rStr = rStr.replaceAll("$(ARG2)", pTwoStringInfo->GetArg2());
206 return true;
208 return false;
213 class ResString: public OUString
215 /* [Description]
217 Helpclass to read a string and optional ExtraData from
218 a string Resource.
223 sal_uInt16 nFlags;
224 public:
225 sal_uInt16 GetFlags() const {return nFlags;}
226 const OUString & GetString() const {return *this;}
227 ResString( ResId &rId);
232 ResString::ResString(ResId & rId):
233 OUString(rId.SetAutoRelease(false).toString()),
234 nFlags(0)
236 ResMgr * pResMgr = rId.GetResMgr();
237 // String ctor temporarily sets global ResManager
238 if (pResMgr->GetRemainSize())
239 nFlags = sal_uInt16(pResMgr->ReadShort());
240 rId.SetAutoRelease(true);
241 pResMgr->PopContext();
246 struct ErrorResource_Impl : private Resource
248 /* [Description]
250 Helpclass for access to string sub-resources of a resource
255 ResId aResId;
257 ErrorResource_Impl(ResId& rErrIdP, sal_uInt16 nId)
258 : Resource(rErrIdP),aResId(nId,*rErrIdP.GetResMgr()){}
260 ~ErrorResource_Impl() { FreeResource(); }
262 operator ResString() { return ResString( aResId ); }
263 operator bool() { return IsAvailableRes(aResId.SetRT(RSC_STRING)); }
268 bool SfxErrorHandler::GetClassString(sal_uLong lClassId, OUString &rStr) const
270 /* [Description]
272 Creates the string for the class of the error. Will always
273 be read from the resource of the Sfx.
278 bool bRet = false;
279 boost::scoped_ptr<ResMgr> pResMgr(ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() ));
280 if( pResMgr )
282 ResId aId(RID_ERRHDL, *pResMgr );
283 ErrorResource_Impl aEr(aId, (sal_uInt16)lClassId);
284 if(aEr)
286 rStr = static_cast<ResString>(aEr).GetString();
287 bRet = true;
290 return bRet;
295 bool SfxErrorHandler::GetMessageString(
296 sal_uLong lErrId, OUString &rStr, sal_uInt16 &nFlags) const
298 /* [Description]
300 Creates the string to output a message box
305 bool bRet = false;
306 boost::scoped_ptr<ResId> pResId(new ResId(nId, *pMgr));
308 ErrorResource_Impl aEr(*pResId, (sal_uInt16)lErrId);
309 if(aEr)
311 ResString aErrorString(aEr);
312 sal_uInt16 nResFlags = aErrorString.GetFlags();
313 if( nResFlags )
314 nFlags=aErrorString.GetFlags();
315 rStr = aErrorString.GetString();
316 bRet = true;
319 return bRet;
324 bool SfxErrorHandler::GetErrorString(
325 sal_uLong lErrId, OUString &rStr, sal_uInt16 &nFlags) const
327 /* [Description]
329 Creates the error string for the actual error
330 without its class
335 SolarMutexGuard aGuard;
337 bool bRet = false;
338 rStr = SvtResId(RID_ERRHDL_CLASS).toString();
339 ResId aResId(nId, *pMgr);
342 ErrorResource_Impl aEr(aResId, (sal_uInt16)lErrId);
343 if(aEr)
345 ResString aErrorString(aEr);
347 sal_uInt16 nResFlags = aErrorString.GetFlags();
348 if ( nResFlags )
349 nFlags = nResFlags;
350 rStr = rStr.replaceAll(OUString("$(ERROR)"), aErrorString.GetString());
351 bRet = true;
353 else
354 bRet = false;
357 if( bRet )
359 OUString aErrStr;
360 GetClassString(lErrId & ERRCODE_CLASS_MASK,
361 aErrStr);
362 if(!aErrStr.isEmpty())
363 aErrStr += ".\n";
364 rStr = rStr.replaceAll("$(CLASS)",aErrStr);
367 return bRet;
372 SfxErrorContext::SfxErrorContext(
373 sal_uInt16 nCtxIdP, vcl::Window *pWindow, sal_uInt16 nResIdP, ResMgr *pMgrP)
374 : ErrorContext(pWindow), nCtxId(nCtxIdP), nResId(nResIdP), pMgr(pMgrP)
376 if( nResId==USHRT_MAX )
377 nResId=RID_ERRCTX;
382 SfxErrorContext::SfxErrorContext(
383 sal_uInt16 nCtxIdP, const OUString &aArg1P, vcl::Window *pWindow,
384 sal_uInt16 nResIdP, ResMgr *pMgrP)
385 : ErrorContext(pWindow), nCtxId(nCtxIdP), nResId(nResIdP), pMgr(pMgrP),
386 aArg1(aArg1P)
388 if( nResId==USHRT_MAX )
389 nResId=RID_ERRCTX;
394 bool SfxErrorContext::GetString(sal_uLong nErrId, OUString &rStr)
396 /* [Description]
398 Constructs the description of a error context
402 bool bRet = false;
403 ResMgr* pFreeMgr = NULL;
404 if( ! pMgr )
406 pFreeMgr = pMgr = ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() );
408 if( pMgr )
410 SolarMutexGuard aGuard;
412 ResId aResId( nResId, *pMgr );
414 ErrorResource_Impl aTestEr( aResId, nCtxId );
415 if ( aTestEr )
417 rStr = static_cast<ResString>(aTestEr).GetString();
418 rStr = rStr.replaceAll(OUString("$(ARG1)"), aArg1 );
419 bRet = true;
421 else
423 SAL_WARN( "svtools.misc", "ErrorContext cannot find the resource" );
424 bRet = false;
427 if ( bRet )
429 sal_uInt16 nId = ( nErrId & ERRCODE_WARNING_MASK ) ? ERRCTX_WARNING : ERRCTX_ERROR;
430 ResId aSfxResId( RID_ERRCTX, *pMgr );
431 ErrorResource_Impl aEr( aSfxResId, nId );
432 rStr = rStr.replaceAll( OUString("$(ERR)"), static_cast<ResString>(aEr).GetString() );
436 if( pFreeMgr )
438 delete pFreeMgr;
439 pMgr = NULL;
441 return bRet;
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */