1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ehdl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #include <vos/mutex.hxx>
34 #include <tools/debug.hxx>
35 #include <tools/rcid.h>
36 #include <vcl/wintypes.hxx>
37 #include <vcl/msgbox.hxx>
38 #include <vcl/svapp.hxx>
40 #include <vcl/sound.hxx>
46 #include <svtools/ehdl.hxx>
47 #include <svtools/svtdata.hxx>
48 #include <svtools/svtools.hrc>
49 #include "sfxecode.hxx"
51 //=========================================================================
53 static USHORT
aWndFunc(
54 Window
*pWin
, // Parent des Dialoges
56 const String
&rErr
, // Fehlertext
57 const String
&rAction
) // Actiontext
61 Bringt eine Fehlerbox auf den Schirm. Je nach nFlags werden
62 Error/ Info usw. Boxen mit den gewuenschten Buttons angezeigt
64 Rueckgabewert ist der gedrueckte Button
70 NAMESPACE_VOS( OGuard
) aGuard( Application::GetSolarMutex() );
72 // aus den Flags die benoetigten WinBits ermitteln
74 if ( (ERRCODE_BUTTON_CANCEL
|ERRCODE_BUTTON_RETRY
) == (nFlags
& (ERRCODE_BUTTON_CANCEL
|ERRCODE_BUTTON_RETRY
)) )
75 eBits
= WB_RETRY_CANCEL
;
76 else if ( ERRCODE_BUTTON_OK_CANCEL
== (nFlags
& ERRCODE_BUTTON_OK_CANCEL
) )
78 else if ( ERRCODE_BUTTON_OK
== (nFlags
& ERRCODE_BUTTON_OK
) )
80 else if ( ERRCODE_BUTTON_YES_NO_CANCEL
== (nFlags
& ERRCODE_BUTTON_YES_NO_CANCEL
) )
81 eBits
= WB_YES_NO_CANCEL
;
82 else if ( ERRCODE_BUTTON_YES_NO
== (nFlags
& ERRCODE_BUTTON_YES_NO
) )
85 switch(nFlags
& 0x0f00)
87 case ERRCODE_BUTTON_DEF_OK
:
91 case ERRCODE_BUTTON_DEF_CANCEL
:
92 eBits
|= WB_DEF_CANCEL
;
95 case ERRCODE_BUTTON_DEF_YES
:
99 case ERRCODE_BUTTON_DEF_NO
:
104 String
aErr(SvtResId(STR_ERR_HDLMESS
));
105 String
aAction(rAction
);
107 aAction
+= String::CreateFromAscii( ":\n" );
108 aErr
.SearchAndReplace(String::CreateFromAscii( "$(ACTION)" ), aAction
);
109 aErr
.SearchAndReplace(String::CreateFromAscii( "$(ERROR)" ), rErr
);
112 switch ( nFlags
& 0xf000 )
114 case ERRCODE_MSG_ERROR
:
115 pBox
= new ErrorBox(pWin
, eBits
, aErr
);
118 case ERRCODE_MSG_WARNING
:
119 pBox
= new WarningBox(pWin
, eBits
, aErr
);
122 case ERRCODE_MSG_INFO
:
123 pBox
= new InfoBox(pWin
, aErr
);
126 case ERRCODE_MSG_QUERY
:
127 pBox
= new QueryBox(pWin
, eBits
, aErr
);
132 DBG_ERRORFILE( "no MessBox type");
134 return ERRCODE_BUTTON_OK
;
138 USHORT nRet
= RET_CANCEL
;
139 switch ( pBox
->Execute() )
142 nRet
= ERRCODE_BUTTON_OK
;
145 nRet
= ERRCODE_BUTTON_CANCEL
;
148 nRet
= ERRCODE_BUTTON_RETRY
;
151 nRet
= ERRCODE_BUTTON_YES
;
154 nRet
= ERRCODE_BUTTON_NO
;
157 DBG_ERRORFILE( "Unknown MessBox return value" );
164 //-------------------------------------------------------------------------
166 SfxErrorHandler::SfxErrorHandler(USHORT nIdP
, ULONG lStartP
, ULONG lEndP
, ResMgr
*pMgrP
) :
168 lStart(lStartP
), lEnd(lEndP
), nId(nIdP
), pMgr(pMgrP
), pFreeMgr( NULL
)
171 RegisterDisplay(&aWndFunc
);
174 com::sun::star::lang::Locale aLocale
= Application::GetSettings().GetUILocale();
175 pFreeMgr
= pMgr
= ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(ofa
), aLocale
);
179 //-------------------------------------------------------------------------
181 SfxErrorHandler::~SfxErrorHandler()
187 //-------------------------------------------------------------------------
189 BOOL
SfxErrorHandler::CreateString(
190 const ErrorInfo
*pErr
, String
&rStr
, USHORT
& nFlags
) const
194 Der Fehlerstring fuer die ErrorInfo pErr wird zusammengesetzt.
199 ULONG nErrCode
= pErr
->GetErrorCode() & ERRCODE_ERROR_MASK
;
200 if( nErrCode
>=lEnd
|| nErrCode
<=lStart
)
202 MessageInfo
*pMsgInfo
=PTR_CAST(MessageInfo
,pErr
);
205 if(GetMessageString(nErrCode
, rStr
, nFlags
))
207 for (xub_StrLen i
= 0; i
< rStr
.Len();)
209 i
= rStr
.SearchAndReplace(String::CreateFromAscii( "$(ARG1)" ),
210 pMsgInfo
->GetMessageArg(), i
);
211 if (i
== STRING_NOTFOUND
)
213 i
= i
+ pMsgInfo
->GetMessageArg().Len();
218 else if(GetErrorString(nErrCode
, rStr
, nFlags
))
220 StringErrorInfo
*pStringInfo
=PTR_CAST(StringErrorInfo
,pErr
);
222 for (xub_StrLen i
= 0; i
< rStr
.Len();)
224 i
= rStr
.SearchAndReplace(String::CreateFromAscii( "$(ARG1)" ),
225 pStringInfo
->GetErrorString(), i
);
226 if (i
== STRING_NOTFOUND
)
228 i
= i
+ pStringInfo
->GetErrorString().Len();
232 TwoStringErrorInfo
* pTwoStringInfo
= PTR_CAST(TwoStringErrorInfo
,
235 for (USHORT i
= 0; i
< rStr
.Len();)
237 USHORT nArg1Pos
= rStr
.Search(String::CreateFromAscii( "$(ARG1)" ), i
);
238 USHORT nArg2Pos
= rStr
.Search(String::CreateFromAscii( "$(ARG2)" ), i
);
239 if (nArg1Pos
< nArg2Pos
)
241 rStr
.Replace(nArg1Pos
, 7, pTwoStringInfo
->GetArg1());
242 i
= nArg1Pos
+ pTwoStringInfo
->GetArg1().Len();
244 else if (nArg2Pos
< nArg1Pos
)
246 rStr
.Replace(nArg2Pos
, 7, pTwoStringInfo
->GetArg2());
247 i
= nArg2Pos
+ pTwoStringInfo
->GetArg2().Len();
257 //-------------------------------------------------------------------------
259 class ResString
: public String
263 Hilfsklasse zum Auslesen eines Strings und optionaler ExtraData aus
264 einer String Resource.
271 USHORT
GetFlags() const {return nFlags
;}
272 const String
& GetString() const {return *this;}
273 ResString( ResId
&rId
);
276 //-------------------------------------------------------------------------
278 ResString::ResString(ResId
& rId
):
279 String(rId
.SetAutoRelease(FALSE
)),
282 ResMgr
* pResMgr
= rId
.GetResMgr();
283 // String ctor temporarily sets global ResManager
284 if (pResMgr
->GetRemainSize())
285 nFlags
= USHORT(pResMgr
->ReadShort());
286 rId
.SetAutoRelease(TRUE
);
287 pResMgr
->PopContext();
290 //-------------------------------------------------------------------------
292 struct ErrorResource_Impl
: private Resource
296 Hilfsklasse zum Zugriff auf String SubResourcen einer Resource
303 ErrorResource_Impl(ResId
& rErrIdP
, USHORT nId
)
304 : Resource(rErrIdP
),aResId(nId
,*rErrIdP
.GetResMgr()){}
306 ~ErrorResource_Impl() { FreeResource(); }
308 operator ResString(){ return ResString( aResId
); }
309 operator BOOL(){return IsAvailableRes(aResId
.SetRT(RSC_STRING
));}
314 BOOL
SfxErrorHandler::GetClassString(ULONG lClassId
, String
&rStr
) const
318 Erzeugt den String fuer die Klasse des Fehlers. Wird immer aus der
319 Resource des Sfx gelesen
325 ResId
aId(RID_ERRHDL
, *pMgr
);
326 ErrorResource_Impl
aEr(aId
, (USHORT
)lClassId
);
329 rStr
=((ResString
)aEr
).GetString();
336 //-------------------------------------------------------------------------
338 BOOL
SfxErrorHandler::GetMessageString(
339 ULONG lErrId
, String
&rStr
, USHORT
&nFlags
) const
343 Erzeugt den String fuer die Ausgabe in einer MessageBox
349 ResId
*pResId
= new ResId(nId
, *pMgr
);
351 ErrorResource_Impl
aEr(*pResId
, (USHORT
)lErrId
);
354 ResString
aErrorString(aEr
);
355 USHORT nResFlags
= aErrorString
.GetFlags();
357 nFlags
=aErrorString
.GetFlags();
358 rStr
= aErrorString
.GetString();
366 //-------------------------------------------------------------------------
368 BOOL
SfxErrorHandler::GetErrorString(
369 ULONG lErrId
, String
&rStr
, USHORT
&nFlags
) const
372 Erzeugt den Fehlerstring fuer den eigentlichen Fehler ohne
378 NAMESPACE_VOS( OGuard
) aGuard( Application::GetSolarMutex() );
381 rStr
=String(SvtResId(RID_ERRHDL_CLASS
));
382 ResId
*pResId
= new ResId(nId
, *pMgr
);
385 ErrorResource_Impl
aEr(*pResId
, (USHORT
)lErrId
);
388 ResString
aErrorString(aEr
);
390 USHORT nResFlags
= aErrorString
.GetFlags();
393 rStr
.SearchAndReplace(
394 String::CreateFromAscii("$(ERROR)"), aErrorString
.GetString());
404 GetClassString(lErrId
& ERRCODE_CLASS_MASK
,
407 aErrStr
+=String::CreateFromAscii( ".\n" );
408 rStr
.SearchAndReplace(String::CreateFromAscii( "$(CLASS)" ),aErrStr
);
415 //-------------------------------------------------------------------------
417 SfxErrorContext::SfxErrorContext(
418 USHORT nCtxIdP
, Window
*pWindow
, USHORT nResIdP
, ResMgr
*pMgrP
)
419 : ErrorContext(pWindow
), nCtxId(nCtxIdP
), nResId(nResIdP
), pMgr(pMgrP
)
421 if( nResId
==USHRT_MAX
)
425 //-------------------------------------------------------------------------
427 SfxErrorContext::SfxErrorContext(
428 USHORT nCtxIdP
, const String
&aArg1P
, Window
*pWindow
,
429 USHORT nResIdP
, ResMgr
*pMgrP
)
430 : ErrorContext(pWindow
), nCtxId(nCtxIdP
), nResId(nResIdP
), pMgr(pMgrP
),
433 if( nResId
==USHRT_MAX
)
437 //-------------------------------------------------------------------------
439 BOOL
SfxErrorContext::GetString(ULONG nErrId
, String
&rStr
)
443 Baut die Beschreibung eines ErrorContextes auf
448 ResMgr
* pFreeMgr
= NULL
;
451 com::sun::star::lang::Locale aLocale
= Application::GetSettings().GetUILocale();
452 pFreeMgr
= pMgr
= ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(ofa
), aLocale
);
456 NAMESPACE_VOS( OGuard
) aGuard( Application::GetSolarMutex() );
458 ResId
aResId( nResId
, *pMgr
);
460 ErrorResource_Impl
aTestEr( aResId
, nCtxId
);
463 rStr
= ( (ResString
)aTestEr
).GetString();
464 rStr
.SearchAndReplace( String::CreateFromAscii( "$(ARG1)" ), aArg1
);
469 DBG_ERRORFILE( "ErrorContext cannot find the resource" );
475 USHORT nId
= ( nErrId
& ERRCODE_WARNING_MASK
) ? ERRCTX_WARNING
: ERRCTX_ERROR
;
476 ResId
aSfxResId( RID_ERRCTX
, *pMgr
);
477 ErrorResource_Impl
aEr( aSfxResId
, nId
);
478 rStr
.SearchAndReplace( String::CreateFromAscii( "$(ERR)" ), ( (ResString
)aEr
).GetString() );