LibreOffice Module vcl (master)  1
errinf.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 <osl/diagnose.h>
21 #include <rtl/instance.hxx>
22 #include <sal/log.hxx>
23 
24 #include <tools/debug.hxx>
25 #include <vcl/errinf.hxx>
26 
27 #include <algorithm>
28 #include <vector>
29 
30 class ErrorHandler;
31 class TheErrorRegistry: public rtl::Static<ErrorRegistry, TheErrorRegistry> {};
32 
34 {
35 public:
36  static bool CreateString(const ErrorInfo*, OUString&);
37 };
38 
39 bool ErrorStringFactory::CreateString(const ErrorInfo* pInfo, OUString& rStr)
40 {
41  for(const ErrorHandler *pHdlr : TheErrorRegistry::get().errorHandlers)
42  {
43  if(pHdlr->CreateString(pInfo, rStr))
44  return true;
45  }
46  return false;
47 }
48 
50  : pDsp(nullptr)
51  , bIsWindowDsp(false)
52  , nNextError(0)
53 {
54  for(DynamicErrorInfo*& rp : ppDynErrInfo)
55  rp = nullptr;
56 }
57 
59 {
60  ErrorRegistry &rData = TheErrorRegistry::get();
61  rData.bIsWindowDsp = false;
62  rData.pDsp = reinterpret_cast< DisplayFnPtr >(aDsp);
63 }
64 
66 {
67  ErrorRegistry &rData = TheErrorRegistry::get();
68  rData.bIsWindowDsp = true;
69  rData.pDsp = reinterpret_cast< DisplayFnPtr >(aDsp);
70 }
71 
72 static void aDspFunc(const OUString &rErr, const OUString &rAction)
73 {
74  SAL_WARN("vcl", "Action: " << rAction << " Error: " << rErr);
75 }
76 
77 ErrorHandler::ErrorHandler()
78 {
79  ErrorRegistry &rData = TheErrorRegistry::get();
80  rData.errorHandlers.insert(rData.errorHandlers.begin(), this);
81 
82  if(!rData.pDsp)
84 }
85 
86 ErrorHandler::~ErrorHandler()
87 {
88  auto &rErrorHandlers = TheErrorRegistry::get().errorHandlers;
89  rErrorHandlers.erase( ::std::remove(rErrorHandlers.begin(), rErrorHandlers.end(), this),
90  rErrorHandlers.end());
91 }
92 
93 bool ErrorHandler::GetErrorString(ErrCode nErrCodeId, OUString& rErrStr)
94 {
95  OUString aErr;
96 
97  if(!nErrCodeId || nErrCodeId == ERRCODE_ABORT)
98  return false;
99 
100  std::unique_ptr<ErrorInfo> pInfo = ErrorInfo::GetErrorInfo(nErrCodeId);
101 
102  if (ErrorStringFactory::CreateString(pInfo.get(),aErr))
103  {
104  rErrStr = aErr;
105  return true;
106  }
107 
108  return false;
109 }
110 
111 DialogMask ErrorHandler::HandleError(ErrCode nErrCodeId, weld::Window *pParent, DialogMask nFlags)
112 {
113  if (nErrCodeId == ERRCODE_NONE || nErrCodeId == ERRCODE_ABORT)
114  return DialogMask::NONE;
115 
116  ErrorRegistry &rData = TheErrorRegistry::get();
117  std::unique_ptr<ErrorInfo> pInfo = ErrorInfo::GetErrorInfo(nErrCodeId);
118  OUString aAction;
119 
120  if (!rData.contexts.empty())
121  {
122  rData.contexts.front()->GetString(pInfo->GetErrorCode(), aAction);
123 
124  for(ErrorContext *pCtx : rData.contexts)
125  {
126  if(pCtx->GetParent())
127  {
128  pParent = pCtx->GetParent();
129  break;
130  }
131  }
132  }
133 
134  bool bWarning = nErrCodeId.IsWarning();
136  if (bWarning)
137  nErrFlags |= DialogMask::MessageWarning;
138  else
139  nErrFlags |= DialogMask::MessageError;
140 
141  DynamicErrorInfo* pDynPtr = dynamic_cast<DynamicErrorInfo*>(pInfo.get());
142  if(pDynPtr)
143  {
144  DialogMask nDynFlags = pDynPtr->GetDialogMask();
145  if( nDynFlags != DialogMask::NONE )
146  nErrFlags = nDynFlags;
147  }
148 
149  OUString aErr;
150  if (ErrorStringFactory::CreateString(pInfo.get(), aErr))
151  {
152  if(!rData.pDsp)
153  {
154  SAL_WARN( "vcl", "Action: " << aAction << "Error: " << aErr);
155  }
156  else
157  {
158  if(!rData.bIsWindowDsp)
159  {
160  (*reinterpret_cast<BasicDisplayErrorFunc*>(rData.pDsp))(aErr,aAction);
161  return DialogMask::NONE;
162  }
163  else
164  {
165  if (nFlags != DialogMask::MAX)
166  nErrFlags = nFlags;
167 
168  return (*reinterpret_cast<WindowDisplayErrorFunc*>(rData.pDsp))(
169  pParent, nErrFlags, aErr, aAction);
170  }
171  }
172  }
173 
174  SAL_WARN( "vcl", "Error not handled " << pInfo->GetErrorCode());
175  // Error 1 (ERRCODE_ABORT) is classified as a General Error in sfx
176  if (pInfo->GetErrorCode() != ERRCODE_ABORT)
177  HandleError(ERRCODE_ABORT);
178  else
179  OSL_FAIL("ERRCODE_ABORT not handled");
180 
181  return DialogMask::NONE;
182 }
183 
185 {
187 };
188 
190  : pImpl( new ImplErrorContext )
191 {
192  pImpl->pWin = pWinP;
193  TheErrorRegistry::get().contexts.insert(TheErrorRegistry::get().contexts.begin(), this);
194 }
195 
197 {
198  auto &rContexts = TheErrorRegistry::get().contexts;
199  rContexts.erase( ::std::remove(rContexts.begin(), rContexts.end(), this), rContexts.end());
200 }
201 
203 {
204  return TheErrorRegistry::get().contexts.empty() ? nullptr : TheErrorRegistry::get().contexts.front();
205 }
206 
208 {
209  return pImpl ? pImpl->pWin : nullptr;
210 }
211 
213 {
214  friend class DynamicErrorInfo;
215  friend class ErrorInfo;
216 
217 private:
219  : nMask(nInMask)
220  {
221  }
223  static void UnRegisterError(DynamicErrorInfo const *);
224  static std::unique_ptr<ErrorInfo> GetDynamicErrorInfo(ErrCode nId);
225 
228 
229 };
230 
232 {
233  // Register dynamic identifier
234  ErrorRegistry& rData = TheErrorRegistry::get();
235  nErrId = ErrCode(((sal_uInt32(rData.nNextError) + 1) << ERRCODE_DYNAMIC_SHIFT) +
236  sal_uInt32(pDynErrInfo->GetErrorCode()));
237 
238  if(rData.ppDynErrInfo[rData.nNextError])
239  delete rData.ppDynErrInfo[rData.nNextError];
240 
241  rData.ppDynErrInfo[rData.nNextError] = pDynErrInfo;
242 
243  if(++rData.nNextError>=ERRCODE_DYNAMIC_COUNT)
244  rData.nNextError=0;
245 }
246 
248 {
249  DynamicErrorInfo **ppDynErrInfo = TheErrorRegistry::get().ppDynErrInfo;
250  sal_uInt32 nIdx = ErrCode(*pDynErrInfo).GetDynamic() - 1;
251  DBG_ASSERT(ppDynErrInfo[nIdx] == pDynErrInfo, "ErrHdl: Error not found");
252 
253  if(ppDynErrInfo[nIdx]==pDynErrInfo)
254  ppDynErrInfo[nIdx]=nullptr;
255 }
256 
257 std::unique_ptr<ErrorInfo> ImplDynamicErrorInfo::GetDynamicErrorInfo(ErrCode nId)
258 {
259  sal_uInt32 nIdx = nId.GetDynamic() - 1;
260  DynamicErrorInfo* pDynErrInfo = TheErrorRegistry::get().ppDynErrInfo[nIdx];
261 
262  if(pDynErrInfo && ErrCode(*pDynErrInfo)==nId)
263  return std::unique_ptr<ErrorInfo>(pDynErrInfo);
264  else
265  return std::make_unique<ErrorInfo>(nId.StripDynamic());
266 }
267 
268 std::unique_ptr<ErrorInfo> ErrorInfo::GetErrorInfo(ErrCode nId)
269 {
270  if(nId.IsDynamic())
272  else
273  return std::make_unique<ErrorInfo>(nId);
274 }
275 
277 {
278 }
279 
281 : ErrorInfo(nArgUserId),
282  pImpl(new ImplDynamicErrorInfo(nMask))
283 {
284  pImpl->RegisterError(this);
285 }
286 
288 {
290 }
291 
292 DynamicErrorInfo::operator ErrCode() const
293 {
294  return pImpl->nErrId;
295 }
296 
298 {
299  return pImpl->nMask;
300 }
301 
303  ErrCode nArgUserId, const OUString& aStringP, DialogMask nMask)
304 : DynamicErrorInfo(nArgUserId, nMask), aString(aStringP)
305 {
306 }
307 
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool CreateString(const ErrorInfo *, OUString &)
Definition: errinf.cxx:39
DisplayFnPtr pDsp
Definition: errinf.hxx:64
void RegisterError(DynamicErrorInfo *)
Definition: errinf.cxx:231
DialogMask const nMask
Definition: errinf.cxx:227
virtual ~ErrorContext()
Definition: errinf.cxx:196
std::vector< ErrorHandler * > errorHandlers
Definition: errinf.hxx:68
bool bIsWindowDsp
Definition: errinf.hxx:65
DialogMask GetDialogMask() const
Definition: errinf.cxx:297
DialogMask
Definition: errinf.hxx:74
DynamicErrorInfo(ErrCode nUserId, DialogMask nMask)
Definition: errinf.cxx:280
#define ERRCODE_DYNAMIC_SHIFT
Definition: errcode.hxx:51
weld::Window * GetParent()
Definition: errinf.cxx:207
virtual ~DynamicErrorInfo() override
Definition: errinf.cxx:287
ImplDynamicErrorInfo(DialogMask nInMask)
Definition: errinf.cxx:218
#define DBG_ASSERT(sCon, aError)
DialogMask WindowDisplayErrorFunc(weld::Window *, DialogMask eMask, const OUString &rErr, const OUString &rAction)
Definition: errinf.hxx:44
std::unique_ptr< ImplDynamicErrorInfo > pImpl
Definition: errinf.hxx:159
ErrorContext(weld::Window *pWin)
Definition: errinf.cxx:189
std::unique_ptr< ImplErrorContext > pImpl
Definition: errinf.hxx:209
static ErrorContext * GetContext()
Definition: errinf.cxx:202
void BasicDisplayErrorFunc(const OUString &rErr, const OUString &rAction)
Definition: errinf.hxx:47
ErrCode const & GetErrorCode() const
Definition: errinf.hxx:139
static std::unique_ptr< ErrorInfo > GetDynamicErrorInfo(ErrCode nId)
Definition: errinf.cxx:257
sal_uInt32 GetDynamic() const
Definition: errcode.hxx:116
#define ERRCODE_NONE
Definition: errcode.hxx:198
virtual ~ErrorInfo()
Definition: errinf.cxx:276
void(* DisplayFnPtr)()
Definition: errinf.hxx:42
bool IsWarning() const
Definition: errcode.hxx:100
DynamicErrorInfo * ppDynErrInfo[ERRCODE_DYNAMIC_COUNT]
Definition: errinf.hxx:71
bool IsDynamic() const
Definition: errcode.hxx:112
std::vector< ErrorContext * > contexts
Definition: errinf.hxx:69
StringErrorInfo(ErrCode nUserId, const OUString &aStringP, DialogMask nMask=DialogMask::NONE)
Definition: errinf.cxx:302
weld::Window * pWin
Definition: errinf.cxx:186
static void RegisterDisplay(BasicDisplayErrorFunc *)
Definition: errinf.cxx:58
#define ERRCODE_ABORT
Definition: errcode.hxx:270
#define ERRCODE_DYNAMIC_COUNT
Definition: errcode.hxx:55
#define SAL_WARN(area, stream)
static std::unique_ptr< ErrorInfo > GetErrorInfo(ErrCode)
Definition: errinf.cxx:268
sal_uInt16 nNextError
Definition: errinf.hxx:66
static void aDspFunc(const OUString &rErr, const OUString &rAction)
Definition: errinf.cxx:72
static void UnRegisterError(DynamicErrorInfo const *)
Definition: errinf.cxx:247
ErrCode StripDynamic() const
Definition: errcode.hxx:120