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