LCOV - code coverage report
Current view: top level - sw/source/core/doc - DocumentListsManager.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 85 103 82.5 %
Date: 2015-06-13 12:38:46 Functions: 14 15 93.3 %
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             : #include <DocumentListsManager.hxx>
      20             : #include <doc.hxx>
      21             : #include <list.hxx>
      22             : #include <numrule.hxx>
      23             : #include <rtl/random.h>
      24             : #include <vector>
      25             : 
      26             : 
      27             : namespace sw
      28             : {
      29             : 
      30        2958 : DocumentListsManager::DocumentListsManager( SwDoc& i_rSwdoc ) : m_rDoc( i_rSwdoc ), maLists(), maListStyleLists()
      31             : {
      32        2958 : }
      33             : 
      34       22589 : SwList* DocumentListsManager::createList( const OUString& rListId,
      35             :                            const OUString& sDefaultListStyleName )
      36             : {
      37       22589 :     OUString sListId = rListId;
      38       22589 :     if ( sListId.isEmpty() )
      39             :     {
      40       22578 :         sListId = CreateUniqueListId();
      41             :     }
      42             : 
      43       22589 :     if ( getListByName( sListId ) )
      44             :     {
      45             :         OSL_FAIL( "<DocumentListsManager::createList(..)> - provided list id already used. Serious defect -> please inform OD." );
      46           0 :         return 0;
      47             :     }
      48             : 
      49       22589 :     SwNumRule* pDefaultNumRuleForNewList = m_rDoc.FindNumRulePtr( sDefaultListStyleName );
      50       22589 :     if ( !pDefaultNumRuleForNewList )
      51             :     {
      52             :         OSL_FAIL( "<DocumentListsManager::createList(..)> - for provided default list style name no list style is found. Serious defect -> please inform OD." );
      53           0 :         return 0;
      54             :     }
      55             : 
      56       22589 :     SwList* pNewList = new SwList( sListId, *pDefaultNumRuleForNewList, m_rDoc.GetNodes() );
      57       22589 :     maLists[sListId] = pNewList;
      58             : 
      59       22589 :     return pNewList;
      60             : }
      61             : 
      62          26 : void DocumentListsManager::deleteList( const OUString& sListId )
      63             : {
      64          26 :     SwList* pList = getListByName( sListId );
      65          26 :     if ( pList )
      66             :     {
      67          26 :         maLists.erase( sListId );
      68          26 :         delete pList;
      69             :     }
      70          26 : }
      71             : 
      72       79913 : SwList* DocumentListsManager::getListByName( const OUString& sListId ) const
      73             : {
      74       79913 :     SwList* pList = 0;
      75             : 
      76             :     std::unordered_map< OUString, SwList*, OUStringHash >::const_iterator
      77       79913 :                                             aListIter = maLists.find( sListId );
      78       79913 :     if ( aListIter != maLists.end() )
      79             :     {
      80       12172 :         pList = (*aListIter).second;
      81             :     }
      82             : 
      83       79913 :     return pList;
      84             : }
      85             : 
      86       22610 : SwList* DocumentListsManager::createListForListStyle( const OUString& sListStyleName )
      87             : {
      88       22610 :     if ( sListStyleName.isEmpty() )
      89             :     {
      90             :         OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - no list style name provided. Serious defect -> please inform OD." );
      91           0 :         return 0;
      92             :     }
      93             : 
      94       22610 :     if ( getListForListStyle( sListStyleName ) )
      95             :     {
      96             :         OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - a list for the provided list style name already exists. Serious defect -> please inform OD." );
      97          21 :         return 0;
      98             :     }
      99             : 
     100       22589 :     SwNumRule* pNumRule = m_rDoc.FindNumRulePtr( sListStyleName );
     101       22589 :     if ( !pNumRule )
     102             :     {
     103             :         OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - for provided list style name no list style is found. Serious defect -> please inform OD." );
     104           0 :         return 0;
     105             :     }
     106             : 
     107       22589 :     OUString sListId( pNumRule->GetDefaultListId() ); // can be empty String
     108       22589 :     if ( getListByName( sListId ) )
     109             :     {
     110          15 :         sListId.clear();
     111             :     }
     112       22589 :     SwList* pNewList = createList( sListId, sListStyleName );
     113       22589 :     maListStyleLists[sListStyleName] = pNewList;
     114       22589 :     pNumRule->SetDefaultListId( pNewList->GetListId() );
     115             : 
     116       22589 :     return pNewList;
     117             : }
     118             : 
     119       22636 : SwList* DocumentListsManager::getListForListStyle( const OUString& sListStyleName ) const
     120             : {
     121       22636 :     SwList* pList = 0;
     122             : 
     123             :     std::unordered_map< OUString, SwList*, OUStringHash >::const_iterator
     124       22636 :                             aListIter = maListStyleLists.find( sListStyleName );
     125       22636 :     if ( aListIter != maListStyleLists.end() )
     126             :     {
     127          47 :         pList = (*aListIter).second;
     128             :     }
     129             : 
     130       22636 :     return pList;
     131             : }
     132             : 
     133          26 : void DocumentListsManager::deleteListForListStyle( const OUString& sListStyleName )
     134             : {
     135          26 :     OUString sListId;
     136             :     {
     137          26 :         SwList* pList = getListForListStyle( sListStyleName );
     138             :         OSL_ENSURE( pList,
     139             :                 "<DocumentListsManager::deleteListForListStyle(..)> - misusage of method: no list found for given list style name" );
     140          26 :         if ( pList )
     141             :         {
     142          26 :             sListId = pList->GetListId();
     143             :         }
     144             :     }
     145          26 :     if ( !sListId.isEmpty() )
     146             :     {
     147          26 :         maListStyleLists.erase( sListStyleName );
     148          26 :         deleteList( sListId );
     149          26 :     }
     150          26 : }
     151             : 
     152          26 : void DocumentListsManager::deleteListsByDefaultListStyle( const OUString& rListStyleName )
     153             : {
     154          26 :     std::vector< SwList* > aListsForDeletion;
     155          26 :     tHashMapForLists::iterator aListIter = maLists.begin();
     156        2966 :     while ( aListIter != maLists.end() )
     157             :     {
     158        2914 :         SwList* pList = (*aListIter).second;
     159        2914 :         if ( pList->GetDefaultListStyleName() == rListStyleName )
     160             :         {
     161           0 :             aListsForDeletion.push_back( pList );
     162             :         }
     163        2914 :         ++aListIter;
     164             :     }
     165          52 :     while ( !aListsForDeletion.empty() )
     166             :     {
     167           0 :         SwList* pList = aListsForDeletion.back();
     168           0 :         aListsForDeletion.pop_back();
     169           0 :         deleteList( pList->GetListId() );
     170          26 :     }
     171          26 : }
     172             : 
     173           0 : void DocumentListsManager::trackChangeOfListStyleName( const OUString& sListStyleName,
     174             :                                         const OUString& sNewListStyleName )
     175             : {
     176           0 :     SwList* pList = getListForListStyle( sListStyleName );
     177             :     OSL_ENSURE( pList,
     178             :             "<DocumentListsManager::changeOfListStyleName(..)> - misusage of method: no list found for given list style name" );
     179             : 
     180           0 :     if ( pList != 0 )
     181             :     {
     182           0 :         maListStyleLists.erase( sListStyleName );
     183           0 :         maListStyleLists[sNewListStyleName] = pList;
     184             :     }
     185           0 : }
     186             : 
     187             : 
     188             : 
     189             : 
     190        8847 : DocumentListsManager::~DocumentListsManager()
     191             : {
     192       76509 :     for ( std::unordered_map< OUString, SwList*, OUStringHash >::iterator
     193        2949 :                                            aListIter = maLists.begin();
     194       51006 :         aListIter != maLists.end();
     195             :         ++aListIter )
     196             :     {
     197       22554 :          delete (*aListIter).second;
     198             :     }
     199        2949 :     maLists.clear();
     200             : 
     201        2949 :     maListStyleLists.clear();
     202        5898 : }
     203             : 
     204             : 
     205       22578 : const OUString DocumentListsManager::MakeListIdUnique( const OUString& aSuggestedUniqueListId )
     206             : {
     207       22578 :     long nHitCount = 0;
     208       22578 :     OUString aTmpStr = aSuggestedUniqueListId;
     209       45156 :     while ( getListByName( aTmpStr ) )
     210             :     {
     211           0 :         ++nHitCount;
     212           0 :         aTmpStr = aSuggestedUniqueListId;
     213           0 :         aTmpStr += OUString::number( nHitCount );
     214             :     }
     215             : 
     216       22578 :     return aTmpStr;
     217             : }
     218             : 
     219       22578 : const OUString DocumentListsManager::CreateUniqueListId()
     220             : {
     221       22578 :     static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != NULL);
     222       22578 :     if (bHack)
     223             :     {
     224             :         static sal_Int64 nIdCounter = SAL_CONST_INT64(7000000000);
     225           0 :         return MakeListIdUnique( OUString( "list" + OUString::number(nIdCounter++) ) );
     226             :     }
     227             :     else
     228             :     {
     229             :         // #i92478#
     230       22578 :         OUString aNewListId( "list" );
     231             :         // #o12311627#
     232       22578 :         static rtlRandomPool s_RandomPool( rtl_random_createPool() );
     233             :         sal_Int64 n;
     234       22578 :         rtl_random_getBytes( s_RandomPool, &n, sizeof(n) );
     235       22578 :         aNewListId += OUString::number( (n < 0 ? -n : n) );
     236             : 
     237       22578 :         return MakeListIdUnique( aNewListId );
     238             :     }
     239             : }
     240             : 
     241         177 : }
     242             : 
     243             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11