fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / svtools / source / misc / ehdl.cxx
blobe6a9eff4184a4a63fb07e97f1b10fc64d99ad978
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>
26 #include <svtools/ehdl.hxx>
27 #include <svtools/svtresid.hxx>
28 #include <svtools/svtools.hrc>
29 #include <svtools/sfxecode.hxx>
31 //=========================================================================
33 static sal_uInt16 aWndFunc(
34 Window *pWin, // Parent des Dialoges
35 sal_uInt16 nFlags,
36 const OUString &rErr, // Fehlertext
37 const OUString &rAction) // Actiontext
39 /* [Beschreibung]
41 Bringt eine Fehlerbox auf den Schirm. Je nach nFlags werden
42 Error/ Info usw. Boxen mit den gewuenschten Buttons angezeigt
44 Rueckgabewert ist der gedrueckte Button
50 SolarMutexGuard aGuard;
52 // aus den Flags die benoetigten WinBits ermitteln
53 WinBits eBits=0;
54 if ( (ERRCODE_BUTTON_CANCEL|ERRCODE_BUTTON_RETRY) == (nFlags & (ERRCODE_BUTTON_CANCEL|ERRCODE_BUTTON_RETRY)) )
55 eBits = WB_RETRY_CANCEL;
56 else if ( ERRCODE_BUTTON_OK_CANCEL == (nFlags & ERRCODE_BUTTON_OK_CANCEL) )
57 eBits = WB_OK_CANCEL;
58 else if ( ERRCODE_BUTTON_OK == (nFlags & ERRCODE_BUTTON_OK) )
59 eBits = WB_OK;
60 else if ( ERRCODE_BUTTON_YES_NO_CANCEL == (nFlags & ERRCODE_BUTTON_YES_NO_CANCEL) )
61 eBits = WB_YES_NO_CANCEL;
62 else if ( ERRCODE_BUTTON_YES_NO == (nFlags & ERRCODE_BUTTON_YES_NO) )
63 eBits = WB_YES_NO;
65 switch(nFlags & 0x0f00)
67 case ERRCODE_BUTTON_DEF_OK:
68 eBits |= WB_DEF_OK;
69 break;
71 case ERRCODE_BUTTON_DEF_CANCEL:
72 eBits |= WB_DEF_CANCEL;
73 break;
75 case ERRCODE_BUTTON_DEF_YES:
76 eBits |= WB_DEF_YES;
77 break;
79 case ERRCODE_BUTTON_DEF_NO:
80 eBits |= WB_DEF_NO;
81 break;
84 String aErr(SvtResId(STR_ERR_HDLMESS).toString());
85 String aAction(rAction);
86 if ( aAction.Len() )
87 aAction += OUString(":\n");
88 aErr.SearchAndReplace(OUString("$(ACTION)"), aAction);
89 aErr.SearchAndReplace(OUString("$(ERROR)"), rErr);
91 MessBox* pBox;
92 switch ( nFlags & 0xf000 )
94 case ERRCODE_MSG_ERROR:
95 pBox = new ErrorBox(pWin, eBits, aErr);
96 break;
98 case ERRCODE_MSG_WARNING:
99 pBox = new WarningBox(pWin, eBits, aErr);
100 break;
102 case ERRCODE_MSG_INFO:
103 pBox = new InfoBox(pWin, aErr);
104 break;
106 case ERRCODE_MSG_QUERY:
107 pBox = new QueryBox(pWin, eBits, aErr);
108 break;
110 default:
112 SAL_WARN( "svtools.misc", "no MessBox type");
113 pBox = NULL;
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 delete pBox;
141 return nRet;
144 //-------------------------------------------------------------------------
146 SfxErrorHandler::SfxErrorHandler(sal_uInt16 nIdP, sal_uLong lStartP, sal_uLong lEndP, ResMgr *pMgrP) :
148 lStart(lStartP), lEnd(lEndP), nId(nIdP), pMgr(pMgrP), pFreeMgr( NULL )
151 RegisterDisplay(&aWndFunc);
152 if( ! pMgr )
154 pFreeMgr = pMgr = ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() );
158 //-------------------------------------------------------------------------
160 SfxErrorHandler::~SfxErrorHandler()
162 delete pFreeMgr;
165 //-------------------------------------------------------------------------
167 sal_Bool SfxErrorHandler::CreateString(
168 const ErrorInfo *pErr, OUString &rStr, sal_uInt16& nFlags) const
170 /* [Beschreibung]
172 Der Fehlerstring fuer die ErrorInfo pErr wird zusammengesetzt.
177 sal_uLong nErrCode = pErr->GetErrorCode() & ERRCODE_ERROR_MASK;
178 if( nErrCode>=lEnd || nErrCode<=lStart )
179 return sal_False;
180 MessageInfo *pMsgInfo=PTR_CAST(MessageInfo,pErr);
181 if(pMsgInfo)
183 if(GetMessageString(nErrCode, rStr, nFlags))
185 rStr = rStr.replaceAll("$(ARG1)", pMsgInfo->GetMessageArg());
186 return sal_True;
189 else if(GetErrorString(nErrCode, rStr, nFlags))
191 StringErrorInfo *pStringInfo=PTR_CAST(StringErrorInfo,pErr);
192 if(pStringInfo)
194 rStr = rStr.replaceAll(OUString("$(ARG1)"),
195 pStringInfo->GetErrorString());
197 else
199 TwoStringErrorInfo * pTwoStringInfo = PTR_CAST(TwoStringErrorInfo,
200 pErr);
201 if (pTwoStringInfo)
203 rStr = rStr.replaceAll("$(ARG1)", pTwoStringInfo->GetArg1());
204 rStr = rStr.replaceAll("$(ARG2)", pTwoStringInfo->GetArg2());
207 return sal_True;
209 return sal_False;
212 //-------------------------------------------------------------------------
214 class ResString: public String
216 /* [Beschreibung]
218 Hilfsklasse zum Auslesen eines Strings und optionaler ExtraData aus
219 einer String Resource.
224 sal_uInt16 nFlags;
225 public:
226 sal_uInt16 GetFlags() const {return nFlags;}
227 const String & GetString() const {return *this;}
228 ResString( ResId &rId);
231 //-------------------------------------------------------------------------
233 ResString::ResString(ResId & rId):
234 String(rId.SetAutoRelease(sal_False).toString()),
235 nFlags(0)
237 ResMgr * pResMgr = rId.GetResMgr();
238 // String ctor temporarily sets global ResManager
239 if (pResMgr->GetRemainSize())
240 nFlags = sal_uInt16(pResMgr->ReadShort());
241 rId.SetAutoRelease(sal_True);
242 pResMgr->PopContext();
245 //-------------------------------------------------------------------------
247 struct ErrorResource_Impl : private Resource
249 /* [Beschreibung]
251 Hilfsklasse zum Zugriff auf String SubResourcen einer Resource
256 ResId aResId;
258 ErrorResource_Impl(ResId& rErrIdP, sal_uInt16 nId)
259 : Resource(rErrIdP),aResId(nId,*rErrIdP.GetResMgr()){}
261 ~ErrorResource_Impl() { FreeResource(); }
263 operator ResString(){ return ResString( aResId ); }
264 operator sal_Bool(){return IsAvailableRes(aResId.SetRT(RSC_STRING));}
269 sal_Bool SfxErrorHandler::GetClassString(sal_uLong lClassId, String &rStr) const
271 /* [Beschreibung]
273 Erzeugt den String fuer die Klasse des Fehlers. Wird immer aus der
274 Resource des Sfx gelesen
279 sal_Bool bRet = sal_False;
280 ResMgr* pResMgr = ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() );
281 if( pResMgr )
283 ResId aId(RID_ERRHDL, *pResMgr );
284 ErrorResource_Impl aEr(aId, (sal_uInt16)lClassId);
285 if(aEr)
287 rStr=((ResString)aEr).GetString();
288 bRet = sal_True;
291 delete pResMgr;
292 return bRet;
295 //-------------------------------------------------------------------------
297 sal_Bool SfxErrorHandler::GetMessageString(
298 sal_uLong lErrId, OUString &rStr, sal_uInt16 &nFlags) const
300 /* [Beschreibung]
302 Erzeugt den String fuer die Ausgabe in einer MessageBox
307 sal_Bool bRet = sal_False;
308 ResId *pResId= new ResId(nId, *pMgr);
310 ErrorResource_Impl aEr(*pResId, (sal_uInt16)lErrId);
311 if(aEr)
313 ResString aErrorString(aEr);
314 sal_uInt16 nResFlags = aErrorString.GetFlags();
315 if( nResFlags )
316 nFlags=aErrorString.GetFlags();
317 rStr = aErrorString.GetString();
318 bRet = sal_True;
321 delete pResId;
322 return bRet;
325 //-------------------------------------------------------------------------
327 sal_Bool SfxErrorHandler::GetErrorString(
328 sal_uLong lErrId, OUString &rStr, sal_uInt16 &nFlags) const
330 /* [Beschreibung]
331 Erzeugt den Fehlerstring fuer den eigentlichen Fehler ohne
332 dessen Klasse
337 SolarMutexGuard aGuard;
339 sal_Bool bRet = sal_False;
340 rStr=SvtResId(RID_ERRHDL_CLASS).toString();
341 ResId aResId(nId, *pMgr);
344 ErrorResource_Impl aEr(aResId, (sal_uInt16)lErrId);
345 if(aEr)
347 ResString aErrorString(aEr);
349 sal_uInt16 nResFlags = aErrorString.GetFlags();
350 if ( nResFlags )
351 nFlags = nResFlags;
352 rStr = rStr.replaceAll(OUString("$(ERROR)"), aErrorString.GetString());
353 bRet = sal_True;
355 else
356 bRet = sal_False;
359 if( bRet )
361 String aErrStr;
362 GetClassString(lErrId & ERRCODE_CLASS_MASK,
363 aErrStr);
364 if(aErrStr.Len())
365 aErrStr += OUString(".\n");
366 rStr = rStr.replaceAll(OUString("$(CLASS)"),aErrStr);
369 return bRet;
372 //-------------------------------------------------------------------------
374 SfxErrorContext::SfxErrorContext(
375 sal_uInt16 nCtxIdP, Window *pWindow, sal_uInt16 nResIdP, ResMgr *pMgrP)
376 : ErrorContext(pWindow), nCtxId(nCtxIdP), nResId(nResIdP), pMgr(pMgrP)
378 if( nResId==USHRT_MAX )
379 nResId=RID_ERRCTX;
382 //-------------------------------------------------------------------------
384 SfxErrorContext::SfxErrorContext(
385 sal_uInt16 nCtxIdP, const String &aArg1P, Window *pWindow,
386 sal_uInt16 nResIdP, ResMgr *pMgrP)
387 : ErrorContext(pWindow), nCtxId(nCtxIdP), nResId(nResIdP), pMgr(pMgrP),
388 aArg1(aArg1P)
390 if( nResId==USHRT_MAX )
391 nResId=RID_ERRCTX;
394 //-------------------------------------------------------------------------
396 sal_Bool SfxErrorContext::GetString(sal_uLong nErrId, OUString &rStr)
398 /* [Beschreibung]
400 Baut die Beschreibung eines ErrorContextes auf
404 bool bRet = false;
405 ResMgr* pFreeMgr = NULL;
406 if( ! pMgr )
408 pFreeMgr = pMgr = ResMgr::CreateResMgr("ofa", Application::GetSettings().GetUILanguageTag() );
410 if( pMgr )
412 SolarMutexGuard aGuard;
414 ResId aResId( nResId, *pMgr );
416 ErrorResource_Impl aTestEr( aResId, nCtxId );
417 if ( aTestEr )
419 rStr = ( (ResString)aTestEr ).GetString();
420 rStr = rStr.replaceAll(OUString("$(ARG1)"), aArg1 );
421 bRet = true;
423 else
425 SAL_WARN( "svtools.misc", "ErrorContext cannot find the resource" );
426 bRet = false;
429 if ( bRet )
431 sal_uInt16 nId = ( nErrId & ERRCODE_WARNING_MASK ) ? ERRCTX_WARNING : ERRCTX_ERROR;
432 ResId aSfxResId( RID_ERRCTX, *pMgr );
433 ErrorResource_Impl aEr( aSfxResId, nId );
434 rStr = rStr.replaceAll( OUString("$(ERR)"), ( (ResString)aEr ).GetString() );
438 if( pFreeMgr )
440 delete pFreeMgr;
441 pMgr = NULL;
443 return bRet;
446 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */