LCOV - code coverage report
Current view: top level - sw/source/core/doc - DocumentListsManager.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 85 103 82.5 %
Date: 2014-11-03 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        5052 : DocumentListsManager::DocumentListsManager( SwDoc& i_rSwdoc ) : m_rSwdoc( i_rSwdoc ), maLists(), maListStyleLists()
      31             : {
      32        5052 : }
      33             : 
      34       42646 : SwList* DocumentListsManager::createList( const OUString& rListId,
      35             :                            const OUString& sDefaultListStyleName )
      36             : {
      37       42646 :     OUString sListId = rListId;
      38       42646 :     if ( sListId.isEmpty() )
      39             :     {
      40       42646 :         sListId = CreateUniqueListId();
      41             :     }
      42             : 
      43       42646 :     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       42646 :     SwNumRule* pDefaultNumRuleForNewList = m_rSwdoc.FindNumRulePtr( sDefaultListStyleName );
      50       42646 :     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       42646 :     SwList* pNewList = new SwList( sListId, *pDefaultNumRuleForNewList, m_rSwdoc.GetNodes() );
      57       42646 :     maLists[sListId] = pNewList;
      58             : 
      59       42646 :     return pNewList;
      60             : }
      61             : 
      62          38 : void DocumentListsManager::deleteList( const OUString& sListId )
      63             : {
      64          38 :     SwList* pList = getListByName( sListId );
      65          38 :     if ( pList )
      66             :     {
      67          38 :         maLists.erase( sListId );
      68          38 :         delete pList;
      69             :     }
      70          38 : }
      71             : 
      72      139114 : SwList* DocumentListsManager::getListByName( const OUString& sListId ) const
      73             : {
      74      139114 :     SwList* pList = 0;
      75             : 
      76             :     boost::unordered_map< OUString, SwList*, OUStringHash >::const_iterator
      77      139114 :                                             aListIter = maLists.find( sListId );
      78      139114 :     if ( aListIter != maLists.end() )
      79             :     {
      80       11206 :         pList = (*aListIter).second;
      81             :     }
      82             : 
      83      139114 :     return pList;
      84             : }
      85             : 
      86       42684 : SwList* DocumentListsManager::createListForListStyle( const OUString& sListStyleName )
      87             : {
      88       42684 :     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       42684 :     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          38 :         return 0;
      98             :     }
      99             : 
     100       42646 :     SwNumRule* pNumRule = m_rSwdoc.FindNumRulePtr( sListStyleName );
     101       42646 :     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       42646 :     OUString sListId( pNumRule->GetDefaultListId() ); // can be empty String
     108       42646 :     if ( getListByName( sListId ) )
     109             :     {
     110          30 :         sListId = OUString();
     111             :     }
     112       42646 :     SwList* pNewList = createList( sListId, sListStyleName );
     113       42646 :     maListStyleLists[sListStyleName] = pNewList;
     114       42646 :     pNumRule->SetDefaultListId( pNewList->GetListId() );
     115             : 
     116       42646 :     return pNewList;
     117             : }
     118             : 
     119       42722 : SwList* DocumentListsManager::getListForListStyle( const OUString& sListStyleName ) const
     120             : {
     121       42722 :     SwList* pList = 0;
     122             : 
     123             :     boost::unordered_map< OUString, SwList*, OUStringHash >::const_iterator
     124       42722 :                             aListIter = maListStyleLists.find( sListStyleName );
     125       42722 :     if ( aListIter != maListStyleLists.end() )
     126             :     {
     127          76 :         pList = (*aListIter).second;
     128             :     }
     129             : 
     130       42722 :     return pList;
     131             : }
     132             : 
     133          38 : void DocumentListsManager::deleteListForListStyle( const OUString& sListStyleName )
     134             : {
     135          38 :     OUString sListId;
     136             :     {
     137          38 :         SwList* pList = getListForListStyle( sListStyleName );
     138             :         OSL_ENSURE( pList,
     139             :                 "<DocumentListsManager::deleteListForListStyle(..)> - misusage of method: no list found for given list style name" );
     140          38 :         if ( pList )
     141             :         {
     142          38 :             sListId = pList->GetListId();
     143             :         }
     144             :     }
     145          38 :     if ( !sListId.isEmpty() )
     146             :     {
     147          38 :         maListStyleLists.erase( sListStyleName );
     148          38 :         deleteList( sListId );
     149          38 :     }
     150          38 : }
     151             : 
     152          38 : void DocumentListsManager::deleteListsByDefaultListStyle( const OUString& rListStyleName )
     153             : {
     154          38 :     std::vector< SwList* > aListsForDeletion;
     155          38 :     tHashMapForLists::iterator aListIter = maLists.begin();
     156        5890 :     while ( aListIter != maLists.end() )
     157             :     {
     158        5814 :         SwList* pList = (*aListIter).second;
     159        5814 :         if ( pList->GetDefaultListStyleName() == rListStyleName )
     160             :         {
     161           0 :             aListsForDeletion.push_back( pList );
     162             :         }
     163        5814 :         ++aListIter;
     164             :     }
     165          76 :     while ( !aListsForDeletion.empty() )
     166             :     {
     167           0 :         SwList* pList = aListsForDeletion.back();
     168           0 :         aListsForDeletion.pop_back();
     169           0 :         deleteList( pList->GetListId() );
     170          38 :     }
     171          38 : }
     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       15135 : DocumentListsManager::~DocumentListsManager()
     191             : {
     192      142938 :     for ( boost::unordered_map< OUString, SwList*, OUStringHash >::iterator
     193        5045 :                                            aListIter = maLists.begin();
     194       95292 :         aListIter != maLists.end();
     195             :         ++aListIter )
     196             :     {
     197       42601 :          delete (*aListIter).second;
     198             :     }
     199        5045 :     maLists.clear();
     200             : 
     201        5045 :     maListStyleLists.clear();
     202       10090 : }
     203             : 
     204             : 
     205       42646 : const OUString DocumentListsManager::MakeListIdUnique( const OUString& aSuggestedUniqueListId )
     206             : {
     207       42646 :     long nHitCount = 0;
     208       42646 :     OUString aTmpStr = aSuggestedUniqueListId;
     209       85292 :     while ( getListByName( aTmpStr ) )
     210             :     {
     211           0 :         ++nHitCount;
     212           0 :         aTmpStr = aSuggestedUniqueListId;
     213           0 :         aTmpStr += OUString::number( nHitCount );
     214             :     }
     215             : 
     216       42646 :     return aTmpStr;
     217             : }
     218             : 
     219       42646 : const OUString DocumentListsManager::CreateUniqueListId()
     220             : {
     221       42646 :     static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != NULL);
     222       42646 :     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       42646 :         OUString aNewListId( "list" );
     231             :         // #o12311627#
     232       42646 :         static rtlRandomPool s_RandomPool( rtl_random_createPool() );
     233             :         sal_Int64 n;
     234       42646 :         rtl_random_getBytes( s_RandomPool, &n, sizeof(n) );
     235       42646 :         aNewListId += OUString::number( (n < 0 ? -n : n) );
     236             : 
     237       42646 :         return MakeListIdUnique( aNewListId );
     238             :     }
     239             : }
     240             : 
     241         270 : }
     242             : 
     243             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10