LCOV - code coverage report
Current view: top level - tools/source/ref - errinf.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 50 167 29.9 %
Date: 2014-04-11 Functions: 8 51 15.7 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <limits.h>
      21             : #include <tools/shl.hxx>
      22             : #include <tools/debug.hxx>
      23             : #include <tools/errinf.hxx>
      24             : #include <rtl/strbuf.hxx>
      25             : 
      26             : class ErrorHandler;
      27             : 
      28             : namespace {
      29             : typedef void (* DisplayFnPtr)();
      30             : }
      31             : 
      32             : struct EDcrData
      33             : {
      34             : public:
      35             :     ErrorHandler               *pFirstHdl;
      36             :     ErrorContext               *pFirstCtx;
      37             :     DisplayFnPtr               pDsp;
      38             :     bool                   bIsWindowDsp;
      39             : 
      40             :     DynamicErrorInfo            *ppDcr[ERRCODE_DYNAMIC_COUNT];
      41             :     sal_uInt16                  nNextDcr;
      42             :                                 EDcrData();
      43             : 
      44             : static  EDcrData                *GetData();
      45             : };
      46             : 
      47             : class EDcr_Impl
      48             : {
      49             :     sal_uIntPtr                 lErrId;
      50             :     sal_uInt16                  nMask;
      51             : 
      52             :     void                        RegisterEDcr(DynamicErrorInfo *);
      53             :     void                        UnRegisterEDcr(DynamicErrorInfo *);
      54             :     static ErrorInfo           *GetDynamicErrorInfo(sal_uIntPtr lId);
      55             : 
      56             : friend class DynamicErrorInfo;
      57             : friend class ErrorInfo;
      58             : };
      59             : 
      60         163 : EDcrData::EDcrData()
      61             :     : pFirstHdl(0)
      62             :     , pFirstCtx(0)
      63             :     , pDsp(0)
      64             :     , bIsWindowDsp(false)
      65         163 :     , nNextDcr(0)
      66             : {
      67        5216 :     for(sal_uInt16 n=0;n<ERRCODE_DYNAMIC_COUNT;n++)
      68        5053 :         ppDcr[n]=0;
      69         163 : }
      70             : 
      71        5979 : EDcrData *EDcrData::GetData()
      72             : {
      73             : #ifdef BOOTSTRAP
      74             :     return 0x0;
      75             : #else
      76        5979 :     EDcrData **ppDat=(EDcrData **)GetAppData(SHL_ERR);
      77        5979 :     if(!*ppDat)
      78             :     {
      79         163 :         return (*ppDat=new EDcrData);
      80             :     }
      81             :     else
      82        5816 :         return *ppDat;
      83             : #endif
      84             : }
      85             : 
      86           0 : void EDcr_Impl::RegisterEDcr(DynamicErrorInfo *pDcr)
      87             : {
      88             :     // Register dynamic identifier
      89           0 :     EDcrData* pData=EDcrData::GetData();
      90           0 :     lErrId= (((sal_uIntPtr)pData->nNextDcr + 1) << ERRCODE_DYNAMIC_SHIFT) +
      91           0 :         pDcr->GetErrorCode();
      92           0 :     DynamicErrorInfo **ppDcr=pData->ppDcr;
      93           0 :     sal_uInt16 nNext=pData->nNextDcr;
      94             : 
      95           0 :     if(ppDcr[nNext])
      96             :     {
      97           0 :         delete ppDcr[nNext];
      98             :     }
      99           0 :     ppDcr[nNext]=pDcr;
     100           0 :     if(++pData->nNextDcr>=ERRCODE_DYNAMIC_COUNT)
     101           0 :         pData->nNextDcr=0;
     102           0 : }
     103             : 
     104           0 : void EDcr_Impl::UnRegisterEDcr(DynamicErrorInfo *pDcr)
     105             : {
     106             : 
     107           0 :     EDcrData* pData=EDcrData::GetData();
     108           0 :     DynamicErrorInfo **ppDcr=pData->ppDcr;
     109             :     sal_uIntPtr lIdx=(
     110           0 :         ((sal_uIntPtr)(*pDcr) & ERRCODE_DYNAMIC_MASK)>>ERRCODE_DYNAMIC_SHIFT)-1;
     111             :     DBG_ASSERT(ppDcr[lIdx]==pDcr,"ErrHdl: Error nicht gefunden");
     112           0 :     if(ppDcr[lIdx]==pDcr)
     113           0 :         ppDcr[lIdx]=0;
     114           0 : }
     115             : 
     116           0 : TYPEINIT0(ErrorInfo);
     117           0 : TYPEINIT1(DynamicErrorInfo, ErrorInfo);
     118           0 : TYPEINIT1(StringErrorInfo, DynamicErrorInfo);
     119           0 : TYPEINIT1(TwoStringErrorInfo, DynamicErrorInfo);
     120           0 : TYPEINIT1(MessageInfo, DynamicErrorInfo);
     121             : 
     122           0 : ErrorInfo *ErrorInfo::GetErrorInfo(sal_uIntPtr lId)
     123             : {
     124           0 :     if(lId & ERRCODE_DYNAMIC_MASK)
     125           0 :         return EDcr_Impl::GetDynamicErrorInfo(lId);
     126             :     else
     127           0 :         return new ErrorInfo(lId);
     128             : }
     129             : 
     130           0 : DynamicErrorInfo::operator sal_uIntPtr() const
     131             : {
     132           0 :     return pImpl->lErrId;
     133             : }
     134             : 
     135           0 : DynamicErrorInfo::DynamicErrorInfo(sal_uIntPtr lArgUserId, sal_uInt16 nMask)
     136           0 : : ErrorInfo(lArgUserId)
     137             : {
     138           0 :     pImpl=new EDcr_Impl;
     139           0 :     pImpl->RegisterEDcr(this);
     140           0 :     pImpl->nMask=nMask;
     141           0 : }
     142             : 
     143           0 : DynamicErrorInfo::~DynamicErrorInfo()
     144             : {
     145           0 :     pImpl->UnRegisterEDcr(this);
     146           0 :     delete pImpl;
     147           0 : }
     148             : 
     149           0 : ErrorInfo* EDcr_Impl::GetDynamicErrorInfo(sal_uIntPtr lId)
     150             : {
     151           0 :     sal_uIntPtr lIdx=((lId & ERRCODE_DYNAMIC_MASK)>>ERRCODE_DYNAMIC_SHIFT)-1;
     152           0 :     DynamicErrorInfo* pDcr=EDcrData::GetData()->ppDcr[lIdx];
     153           0 :     if(pDcr && (sal_uIntPtr)(*pDcr)==lId)
     154           0 :         return pDcr;
     155             :     else
     156           0 :         return new ErrorInfo(lId & ~ERRCODE_DYNAMIC_MASK);
     157             : }
     158             : 
     159           0 : sal_uInt16 DynamicErrorInfo::GetDialogMask() const
     160             : {
     161           0 :     return pImpl->nMask;
     162             : }
     163             : 
     164           0 : StringErrorInfo::StringErrorInfo(
     165             :     sal_uIntPtr UserId, const OUString& aStringP,  sal_uInt16 nFlags)
     166           0 : : DynamicErrorInfo(UserId, nFlags), aString(aStringP)
     167             : {
     168           0 : }
     169             : 
     170             : class ErrHdl_Impl
     171             : {
     172             : public:
     173             :     ErrorHandler        *pNext;
     174             :     static bool         CreateString(const ErrorHandler *pStart,
     175             :                                      const ErrorInfo*, OUString&, sal_uInt16&);
     176             : };
     177             : 
     178           0 : static void aDspFunc(const OUString &rErr, const OUString &rAction)
     179             : {
     180           0 :     OStringBuffer aErr("Aktion: ");
     181           0 :     aErr.append(OUStringToOString(rAction, RTL_TEXTENCODING_ASCII_US));
     182           0 :     aErr.append(" Fehler: ");
     183           0 :     aErr.append(OUStringToOString(rErr, RTL_TEXTENCODING_ASCII_US));
     184           0 :     OSL_FAIL(aErr.getStr());
     185           0 : }
     186             : 
     187        1315 : ErrorContext::ErrorContext(Window *pWinP)
     188             : {
     189        1315 :     EDcrData *pData=EDcrData::GetData();
     190        1315 :     ErrorContext *&pHdl=pData->pFirstCtx;
     191        1315 :     pWin=pWinP;
     192        1315 :     pNext=pHdl;
     193        1315 :     pHdl=this;
     194        1315 : }
     195             : 
     196        1315 : ErrorContext::~ErrorContext()
     197             : {
     198        1315 :     ErrorContext **ppCtx=&(EDcrData::GetData()->pFirstCtx);
     199        2630 :     while(*ppCtx && *ppCtx!=this)
     200           0 :         ppCtx=&((*ppCtx)->pNext);
     201        1315 :     if(*ppCtx)
     202        1315 :         *ppCtx=(*ppCtx)->pNext;
     203        1315 : }
     204             : 
     205           0 : ErrorContext *ErrorContext::GetContext()
     206             : {
     207           0 :     return EDcrData::GetData()->pFirstCtx;
     208             : }
     209             : 
     210         590 : ErrorHandler::ErrorHandler()
     211             : {
     212         590 :     pImpl=new ErrHdl_Impl;
     213         590 :     EDcrData *pData=EDcrData::GetData();
     214         590 :     ErrorHandler *&pHdl=pData->pFirstHdl;
     215         590 :     pImpl->pNext=pHdl;
     216         590 :     pHdl=this;
     217         590 :     if(!pData->pDsp)
     218          93 :         RegisterDisplay(&aDspFunc);
     219         590 : }
     220             : 
     221         361 : ErrorHandler::~ErrorHandler()
     222             : {
     223         361 :     ErrorHandler **ppHdl=&(EDcrData::GetData()->pFirstHdl);
     224         861 :     while(*ppHdl && *ppHdl!=this)
     225         139 :         ppHdl=&((*ppHdl)->pImpl->pNext);
     226         361 :     if(*ppHdl)
     227         361 :         *ppHdl=(*ppHdl)->pImpl->pNext;
     228         361 :     delete pImpl;
     229         361 : }
     230             : 
     231         590 : void ErrorHandler::RegisterDisplay(WindowDisplayErrorFunc *aDsp)
     232             : {
     233         590 :     EDcrData *pData=EDcrData::GetData();
     234         590 :     pData->bIsWindowDsp=true;
     235         590 :     pData->pDsp = reinterpret_cast< DisplayFnPtr >(aDsp);
     236         590 : }
     237             : 
     238        1808 : void ErrorHandler::RegisterDisplay(BasicDisplayErrorFunc *aDsp)
     239             : {
     240        1808 :     EDcrData *pData=EDcrData::GetData();
     241        1808 :     pData->bIsWindowDsp=false;
     242        1808 :     pData->pDsp = reinterpret_cast< DisplayFnPtr >(aDsp);
     243        1808 : }
     244             : 
     245             : /** Handles an error.
     246             : 
     247             :     If nFlags is not set, the DynamicErrorInfo flags or the
     248             :     resource flags will be used.
     249             :     Thus:
     250             : 
     251             :     1. nFlags,
     252             :     2. Resource Flags
     253             :     3. Dynamic Flags
     254             :     4. Default ERRCODE_BUTTON_OK, ERRCODE_MSG_ERROR
     255             : 
     256             :     @param lId               error id
     257             :     @param nFlags            error flags.
     258             :     @param bJustCreateString ???
     259             :     @param rError            ???
     260             : 
     261             :     @return ???
     262             : */
     263           0 : sal_uInt16 ErrorHandler::HandleError_Impl(
     264             :     sal_uIntPtr lId, sal_uInt16 nFlags, bool bJustCreateString, OUString & rError)
     265             : {
     266           0 :     OUString aErr;
     267           0 :     OUString aAction;
     268           0 :     if(!lId || lId == ERRCODE_ABORT)
     269           0 :         return 0;
     270           0 :     EDcrData *pData=EDcrData::GetData();
     271           0 :     ErrorInfo *pInfo=ErrorInfo::GetErrorInfo(lId);
     272           0 :     ErrorContext *pCtx=ErrorContext::GetContext();
     273           0 :     if(pCtx)
     274           0 :         pCtx->GetString(pInfo->GetErrorCode(), aAction);
     275           0 :     Window *pParent=0;
     276             :     // Remove parent from context
     277           0 :     for(;pCtx;pCtx=pCtx->pNext)
     278           0 :         if(pCtx->GetParent())
     279             :         {
     280           0 :             pParent=pCtx->GetParent();
     281           0 :             break;
     282             :         }
     283             : 
     284           0 :     bool bWarning = ((lId & ERRCODE_WARNING_MASK) == ERRCODE_WARNING_MASK);
     285           0 :     sal_uInt16 nErrFlags = ERRCODE_BUTTON_DEF_OK | ERRCODE_BUTTON_OK;
     286           0 :     if (bWarning)
     287           0 :         nErrFlags |= ERRCODE_MSG_WARNING;
     288             :     else
     289           0 :         nErrFlags |= ERRCODE_MSG_ERROR;
     290             : 
     291           0 :     DynamicErrorInfo* pDynPtr=PTR_CAST(DynamicErrorInfo,pInfo);
     292           0 :     if(pDynPtr)
     293             :     {
     294           0 :         sal_uInt16 nDynFlags = pDynPtr->GetDialogMask();
     295           0 :         if( nDynFlags )
     296           0 :             nErrFlags = nDynFlags;
     297             :     }
     298             : 
     299           0 :     if(ErrHdl_Impl::CreateString(pData->pFirstHdl,pInfo,aErr,nErrFlags))
     300             :     {
     301           0 :         if (bJustCreateString)
     302             :         {
     303           0 :             rError = aErr;
     304           0 :             return 1;
     305             :         }
     306             :         else
     307             :         {
     308           0 :             if(!pData->pDsp)
     309             :             {
     310           0 :                 OStringBuffer aStr("Action: ");
     311           0 :                 aStr.append(OUStringToOString(aAction, RTL_TEXTENCODING_ASCII_US));
     312           0 :                 aStr.append("\nFehler: ");
     313           0 :                 aStr.append(OUStringToOString(aErr, RTL_TEXTENCODING_ASCII_US));
     314           0 :                 OSL_FAIL(aStr.getStr());
     315             :             }
     316             :             else
     317             :             {
     318           0 :                 delete pInfo;
     319           0 :                 if(!pData->bIsWindowDsp)
     320             :                 {
     321           0 :                     (*(BasicDisplayErrorFunc*)pData->pDsp)(aErr,aAction);
     322           0 :                     return 0;
     323             :                 }
     324             :                 else
     325             :                 {
     326           0 :                     if (nFlags != USHRT_MAX)
     327           0 :                         nErrFlags = nFlags;
     328             :                     return (*(WindowDisplayErrorFunc*)pData->pDsp)(
     329           0 :                         pParent, nErrFlags, aErr, aAction);
     330             :                 }
     331             :             }
     332             :         }
     333             :     }
     334             :     OSL_FAIL("Error nicht behandelt");
     335             :     // Error 1 is General Error in the Sfx
     336           0 :     if(pInfo->GetErrorCode()!=1)
     337             :     {
     338           0 :         HandleError_Impl(1, USHRT_MAX, bJustCreateString, rError);
     339             :     }
     340             :     else
     341             :     {
     342             :         OSL_FAIL("Error 1 nicht gehandeled");
     343             :     }
     344           0 :     delete pInfo;
     345           0 :     return 0;
     346             : }
     347             : 
     348             : // static
     349           0 : bool ErrorHandler::GetErrorString(sal_uIntPtr lId, OUString& rStr)
     350             : {
     351           0 :     return (bool)HandleError_Impl( lId, USHRT_MAX, true, rStr );
     352             : }
     353             : 
     354             : /** Handles an error.
     355             : 
     356             :     @see ErrorHandler::HandleError_Impl
     357             : */
     358           0 : sal_uInt16 ErrorHandler::HandleError(sal_uIntPtr lId, sal_uInt16 nFlags)
     359             : {
     360           0 :     OUString aDummy;
     361           0 :     return HandleError_Impl( lId, nFlags, false, aDummy );
     362             : }
     363             : 
     364           0 : bool ErrHdl_Impl::CreateString( const ErrorHandler *pStart,
     365             :                                     const ErrorInfo* pInfo, OUString& pStr,
     366             :                                     sal_uInt16 &rFlags)
     367             : {
     368           0 :     for(const ErrorHandler *pHdl=pStart;pHdl;pHdl=pHdl->pImpl->pNext)
     369             :     {
     370           0 :         if(pHdl->CreateString( pInfo, pStr, rFlags))
     371           0 :             return true;
     372             :     }
     373           0 :     return false;
     374             : }
     375             : 
     376             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10