summaryrefslogtreecommitdiff
path: root/vcl/source/window/errinf.cxx
blob: 00a99f420b01c5bc7cd94fc0cd66b5f591ad462e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <osl/diagnose.h>
#include <rtl/strbuf.hxx>

#include <vcl/errinf.hxx>
#include <vcl/window.hxx>

#include <vector>
#include <limits.h>

class ErrorHandler;
class TheErrorRegistry: public rtl::Static<ErrorRegistry, TheErrorRegistry> {};

class ErrorStringFactory
{
public:
    static bool CreateString(const ErrorInfo*, OUString&);
};

bool ErrorStringFactory::CreateString(const ErrorInfo* pInfo, OUString& rStr)
{
    for(const ErrorHandler *pHdlr : TheErrorRegistry::get().errorHandlers)
    {
        if(pHdlr->CreateString(pInfo, rStr))
            return true;
    }
    return false;
}

ErrorRegistry::ErrorRegistry()
    : pDsp(nullptr)
    , bIsWindowDsp(false)
    , nNextError(0)
{
    for(DynamicErrorInfo*& rp : ppDynErrInfo)
        rp = nullptr;
}

void ErrorRegistry::RegisterDisplay(BasicDisplayErrorFunc *aDsp)
{
    ErrorRegistry &rData = TheErrorRegistry::get();
    rData.bIsWindowDsp = false;
    rData.pDsp = reinterpret_cast< DisplayFnPtr >(aDsp);
}

void ErrorRegistry::RegisterDisplay(WindowDisplayErrorFunc *aDsp)
{
    ErrorRegistry &rData = TheErrorRegistry::get();
    rData.bIsWindowDsp = true;
    rData.pDsp = reinterpret_cast< DisplayFnPtr >(aDsp);
}

static void aDspFunc(const OUString &rErr, const OUString &rAction)
{
    SAL_WARN("vcl", "Action: " << rAction << " Error: " << rErr);
}

ErrorHandler::ErrorHandler()
{
    ErrorRegistry &rData = TheErrorRegistry::get();
    rData.errorHandlers.insert(rData.errorHandlers.begin(), this);

    if(!rData.pDsp)
        ErrorRegistry::RegisterDisplay(&aDspFunc);
}

ErrorHandler::~ErrorHandler()
{
    auto &rErrorHandlers = TheErrorRegistry::get().errorHandlers;
    rErrorHandlers.erase( ::std::remove(rErrorHandlers.begin(), rErrorHandlers.end(), this),
                          rErrorHandlers.end());
}

bool ErrorHandler::GetErrorString(ErrCode nErrCodeId, OUString& rErrStr)
{
    OUString aErr;

    if(!nErrCodeId || nErrCodeId == ERRCODE_ABORT)
        return false;

    ErrorInfo *pInfo = ErrorInfo::GetErrorInfo(nErrCodeId);

    if (ErrorStringFactory::CreateString(pInfo,aErr))
    {
        rErrStr = aErr;
        return true;
    }

    delete pInfo;
    return false;
}

DialogMask ErrorHandler::HandleError(ErrCode nErrCodeId, vcl::Window *pParent, DialogMask nFlags)
{
    if (nErrCodeId == ERRCODE_NONE || nErrCodeId == ERRCODE_ABORT)
        return DialogMask::NONE;

    ErrorRegistry &rData = TheErrorRegistry::get();
    ErrorInfo *pInfo = ErrorInfo::GetErrorInfo(nErrCodeId);
    OUString aAction;

    if (!rData.contexts.empty())
    {
        rData.contexts.front()->GetString(pInfo->GetErrorCode(), aAction);

        for(ErrorContext *pCtx : rData.contexts)
        {
            if(pCtx->GetParent())
            {
                pParent = pCtx->GetParent();
                break;
            }
        }
    }

    bool bWarning = nErrCodeId.IsWarning();
    DialogMask nErrFlags = DialogMask::ButtonDefaultsOk | DialogMask::ButtonsOk;
    if (bWarning)
        nErrFlags |= DialogMask::MessageWarning;
    else
        nErrFlags |= DialogMask::MessageError;

    DynamicErrorInfo* pDynPtr = dynamic_cast<DynamicErrorInfo*>(pInfo);
    if(pDynPtr)
    {
        DialogMask nDynFlags = pDynPtr->GetDialogMask();
        if( nDynFlags != DialogMask::NONE )
            nErrFlags = nDynFlags;
    }

    OUString aErr;
    if (ErrorStringFactory::CreateString(pInfo, aErr))
    {
        if(!rData.pDsp)
        {
            SAL_WARN( "vcl", "Action: " << aAction <<  "Error: " << aErr);
        }
        else
        {
            delete pInfo;

            if(!rData.bIsWindowDsp)
            {
                (*reinterpret_cast<BasicDisplayErrorFunc*>(rData.pDsp))(aErr,aAction);
                return DialogMask::NONE;
            }
            else
            {
                if (nFlags != DialogMask::MAX)
                    nErrFlags = nFlags;

                return (*reinterpret_cast<WindowDisplayErrorFunc*>(rData.pDsp))(
                    pParent, nErrFlags, aErr, aAction);
            }
        }
    }

    SAL_WARN( "vcl", "Error not handled " << pInfo->GetErrorCode());
    // Error 1 (ERRCODE_ABORT) is classified as a General Error in sfx
    if (pInfo->GetErrorCode() != ERRCODE_ABORT)
        HandleError(ERRCODE_ABORT);
    else
        OSL_FAIL("ERRCODE_ABORT not handled");

    delete pInfo;
    return DialogMask::NONE;
}

struct ImplErrorContext
{
    vcl::Window *pWin; // FIXME: should be VclPtr for strong lifecycle
};

ErrorContext::ErrorContext(vcl::Window *pWinP)
    : pImpl( new ImplErrorContext )
{
    pImpl->pWin = pWinP;
    TheErrorRegistry::get().contexts.insert(TheErrorRegistry::get().contexts.begin(), this);
}

ErrorContext::~ErrorContext()
{
    auto &rContexts = TheErrorRegistry::get().contexts;
    rContexts.erase( ::std::remove(rContexts.begin(), rContexts.end(), this), rContexts.end());
}

ErrorContext *ErrorContext::GetContext()
{
    return TheErrorRegistry::get().contexts.empty() ? nullptr : TheErrorRegistry::get().contexts.front();
}

vcl::Window* ErrorContext::GetParent()
{
    return pImpl ? pImpl->pWin : nullptr;
}

class ImplDynamicErrorInfo
{
    friend class DynamicErrorInfo;
    friend class ErrorInfo;

private:
    explicit ImplDynamicErrorInfo(DialogMask nInMask)
        : nMask(nInMask)
    {
    }
    void                        RegisterError(DynamicErrorInfo *);
    static void                 UnRegisterError(DynamicErrorInfo const *);
    static ErrorInfo*           GetDynamicErrorInfo(ErrCode nId);

    ErrCode                     nErrId;
    DialogMask                  nMask;

};

void ImplDynamicErrorInfo::RegisterError(DynamicErrorInfo *pDynErrInfo)
{
    // Register dynamic identifier
    ErrorRegistry& rData = TheErrorRegistry::get();
    nErrId = ErrCode(((sal_uInt32(rData.nNextError) + 1) << ERRCODE_DYNAMIC_SHIFT) +
                     sal_uInt32(pDynErrInfo->GetErrorCode()));

    if(rData.ppDynErrInfo[rData.nNextError])
        delete rData.ppDynErrInfo[rData.nNextError];

    rData.ppDynErrInfo[rData.nNextError] = pDynErrInfo;

    if(++rData.nNextError>=ERRCODE_DYNAMIC_COUNT)
        rData.nNextError=0;
}

void ImplDynamicErrorInfo::UnRegisterError(DynamicErrorInfo const *pDynErrInfo)
{
    DynamicErrorInfo **ppDynErrInfo = TheErrorRegistry::get().ppDynErrInfo;
    sal_uInt32 nIdx = ErrCode(*pDynErrInfo).GetDynamic() - 1;
    DBG_ASSERT(ppDynErrInfo[nIdx] == pDynErrInfo, "ErrHdl: Error not found");

    if(ppDynErrInfo[nIdx]==pDynErrInfo)
        ppDynErrInfo[nIdx]=nullptr;
}

ErrorInfo* ImplDynamicErrorInfo::GetDynamicErrorInfo(ErrCode nId)
{
    sal_uInt32 nIdx = nId.GetDynamic() - 1;
    DynamicErrorInfo* pDynErrInfo = TheErrorRegistry::get().ppDynErrInfo[nIdx];

    if(pDynErrInfo && ErrCode(*pDynErrInfo)==nId)
        return pDynErrInfo;
    else
        return new ErrorInfo(nId.StripDynamic());
}

ErrorInfo *ErrorInfo::GetErrorInfo(ErrCode nId)
{
    if(nId.IsDynamic())
        return ImplDynamicErrorInfo::GetDynamicErrorInfo(nId);
    else
        return new ErrorInfo(nId);
}

ErrorInfo::~ErrorInfo()
{
}

DynamicErrorInfo::DynamicErrorInfo(ErrCode nArgUserId, DialogMask nMask)
: ErrorInfo(nArgUserId),
  pImpl(new ImplDynamicErrorInfo(nMask))
{
    pImpl->RegisterError(this);
}

DynamicErrorInfo::~DynamicErrorInfo()
{
    ImplDynamicErrorInfo::UnRegisterError(this);
}

DynamicErrorInfo::operator ErrCode() const
{
    return pImpl->nErrId;
}

DialogMask DynamicErrorInfo::GetDialogMask() const
{
    return pImpl->nMask;
}

StringErrorInfo::StringErrorInfo(
    ErrCode nArgUserId, const OUString& aStringP, DialogMask nMask)
: DynamicErrorInfo(nArgUserId, nMask), aString(aStringP)
{
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */