1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
37 const OUString
&rErr
, // error text
38 const OUString
&rAction
) // action text
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
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
) )
59 else if ( ERRCODE_BUTTON_OK
== (nFlags
& ERRCODE_BUTTON_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
) )
66 switch(nFlags
& 0x0f00)
68 case ERRCODE_BUTTON_DEF_OK
:
72 case ERRCODE_BUTTON_DEF_CANCEL
:
73 eBits
|= WB_DEF_CANCEL
;
76 case ERRCODE_BUTTON_DEF_YES
:
80 case ERRCODE_BUTTON_DEF_NO
:
85 OUString
aErr(SvtResId(STR_ERR_HDLMESS
).toString());
86 OUString
aAction(rAction
);
87 if ( !aAction
.isEmpty() )
89 aErr
= aErr
.replaceAll("$(ACTION)", aAction
);
90 aErr
= aErr
.replaceAll("$(ERROR)", rErr
);
93 switch ( nFlags
& 0xf000 )
95 case ERRCODE_MSG_ERROR
:
96 pBox
.reset(VclPtr
<ErrorBox
>::Create(pWin
, eBits
, aErr
));
99 case ERRCODE_MSG_WARNING
:
100 pBox
.reset(VclPtr
<WarningBox
>::Create(pWin
, eBits
, aErr
));
103 case ERRCODE_MSG_INFO
:
104 pBox
.reset(VclPtr
<InfoBox
>::Create(pWin
, aErr
));
107 case ERRCODE_MSG_QUERY
:
108 pBox
.reset(VclPtr
<QueryBox
>::Create(pWin
, eBits
, aErr
));
113 SAL_WARN( "svtools.misc", "no MessBox type");
114 return ERRCODE_BUTTON_OK
;
118 sal_uInt16 nRet
= RET_CANCEL
;
119 switch ( pBox
->Execute() )
122 nRet
= ERRCODE_BUTTON_OK
;
125 nRet
= ERRCODE_BUTTON_CANCEL
;
128 nRet
= ERRCODE_BUTTON_RETRY
;
131 nRet
= ERRCODE_BUTTON_YES
;
134 nRet
= ERRCODE_BUTTON_NO
;
137 SAL_WARN( "svtools.misc", "Unknown MessBox return value" );
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
);
153 pFreeMgr
= pMgr
= ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() );
159 SfxErrorHandler::~SfxErrorHandler()
166 bool SfxErrorHandler::CreateString(
167 const ErrorInfo
*pErr
, OUString
&rStr
, sal_uInt16
& nFlags
) const
171 Assemble error string for the ErrorInfo pErr.
176 sal_uLong nErrCode
= pErr
->GetErrorCode() & ERRCODE_ERROR_MASK
;
177 if( nErrCode
>=lEnd
|| nErrCode
<=lStart
)
179 const MessageInfo
*pMsgInfo
= PTR_CAST(MessageInfo
,pErr
);
182 if(GetMessageString(nErrCode
, rStr
, nFlags
))
184 rStr
= rStr
.replaceAll("$(ARG1)", pMsgInfo
->GetMessageArg());
188 else if(GetErrorString(nErrCode
, rStr
, nFlags
))
190 const StringErrorInfo
*pStringInfo
= PTR_CAST(StringErrorInfo
,pErr
);
193 rStr
= rStr
.replaceAll(OUString("$(ARG1)"),
194 pStringInfo
->GetErrorString());
198 const TwoStringErrorInfo
* pTwoStringInfo
= PTR_CAST(TwoStringErrorInfo
,
202 rStr
= rStr
.replaceAll("$(ARG1)", pTwoStringInfo
->GetArg1());
203 rStr
= rStr
.replaceAll("$(ARG2)", pTwoStringInfo
->GetArg2());
213 class ResString
: public OUString
217 Helpclass to read a string and optional ExtraData from
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()),
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
250 Helpclass for access to string sub-resources of a resource
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
272 Creates the string for the class of the error. Will always
273 be read from the resource of the Sfx.
279 boost::scoped_ptr
<ResMgr
> pResMgr(ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() ));
282 ResId
aId(RID_ERRHDL
, *pResMgr
);
283 ErrorResource_Impl
aEr(aId
, (sal_uInt16
)lClassId
);
286 rStr
= static_cast<ResString
>(aEr
).GetString();
295 bool SfxErrorHandler::GetMessageString(
296 sal_uLong lErrId
, OUString
&rStr
, sal_uInt16
&nFlags
) const
300 Creates the string to output a message box
306 boost::scoped_ptr
<ResId
> pResId(new ResId(nId
, *pMgr
));
308 ErrorResource_Impl
aEr(*pResId
, (sal_uInt16
)lErrId
);
311 ResString
aErrorString(aEr
);
312 sal_uInt16 nResFlags
= aErrorString
.GetFlags();
314 nFlags
=aErrorString
.GetFlags();
315 rStr
= aErrorString
.GetString();
324 bool SfxErrorHandler::GetErrorString(
325 sal_uLong lErrId
, OUString
&rStr
, sal_uInt16
&nFlags
) const
329 Creates the error string for the actual error
335 SolarMutexGuard aGuard
;
338 rStr
= SvtResId(RID_ERRHDL_CLASS
).toString();
339 ResId
aResId(nId
, *pMgr
);
342 ErrorResource_Impl
aEr(aResId
, (sal_uInt16
)lErrId
);
345 ResString
aErrorString(aEr
);
347 sal_uInt16 nResFlags
= aErrorString
.GetFlags();
350 rStr
= rStr
.replaceAll(OUString("$(ERROR)"), aErrorString
.GetString());
360 GetClassString(lErrId
& ERRCODE_CLASS_MASK
,
362 if(!aErrStr
.isEmpty())
364 rStr
= rStr
.replaceAll("$(CLASS)",aErrStr
);
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
)
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
),
388 if( nResId
==USHRT_MAX
)
394 bool SfxErrorContext::GetString(sal_uLong nErrId
, OUString
&rStr
)
398 Constructs the description of a error context
403 ResMgr
* pFreeMgr
= NULL
;
406 pFreeMgr
= pMgr
= ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() );
410 SolarMutexGuard aGuard
;
412 ResId
aResId( nResId
, *pMgr
);
414 ErrorResource_Impl
aTestEr( aResId
, nCtxId
);
417 rStr
= static_cast<ResString
>(aTestEr
).GetString();
418 rStr
= rStr
.replaceAll(OUString("$(ARG1)"), aArg1
);
423 SAL_WARN( "svtools.misc", "ErrorContext cannot find the resource" );
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() );
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */