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