android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / doc / DocumentListsManager.cxx
blob16cb54071037676f5e49f40fd16dae66cf602f24
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <DocumentListsManager.hxx>
20 #include <doc.hxx>
21 #include <list.hxx>
22 #include <numrule.hxx>
24 #include <comphelper/random.hxx>
25 #include <osl/diagnose.h>
28 namespace sw
31 DocumentListsManager::DocumentListsManager( SwDoc& i_rSwdoc ) : m_rDoc( i_rSwdoc )
35 SwList* DocumentListsManager::createList( const OUString& rListId,
36 const OUString& sDefaultListStyleName )
38 OUString sListId = rListId;
39 if ( sListId.isEmpty() )
41 sListId = CreateUniqueListId();
44 if ( getListByName( sListId ) )
46 OSL_FAIL( "<DocumentListsManager::createList(..)> - provided list id already used. Serious defect." );
47 return nullptr;
50 SwNumRule* pDefaultNumRuleForNewList = m_rDoc.FindNumRulePtr( sDefaultListStyleName );
51 if ( !pDefaultNumRuleForNewList )
53 OSL_FAIL( "<DocumentListsManager::createList(..)> - for provided default list style name no list style is found. Serious defect." );
54 return nullptr;
57 SwList* pNewList = new SwList( sListId, *pDefaultNumRuleForNewList, m_rDoc.GetNodes() );
58 maLists[sListId].reset(pNewList);
60 return pNewList;
63 SwList* DocumentListsManager::getListByName( const OUString& sListId ) const
65 SwList* pList = nullptr;
67 auto aListIter = maLists.find( sListId );
68 if ( aListIter != maLists.end() )
70 pList = (*aListIter).second.get();
73 return pList;
76 void DocumentListsManager::createListForListStyle( const OUString& sListStyleName )
78 if ( sListStyleName.isEmpty() )
80 OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - no list style name provided. Serious defect." );
81 return;
84 if ( getListForListStyle( sListStyleName ) )
86 OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - a list for the provided list style name already exists. Serious defect." );
87 return;
90 SwNumRule* pNumRule = m_rDoc.FindNumRulePtr( sListStyleName );
91 if ( !pNumRule )
93 OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - for provided list style name no list style is found. Serious defect." );
94 return;
97 OUString sListId( pNumRule->GetDefaultListId() ); // can be empty String
98 if ( getListByName( sListId ) )
100 sListId.clear();
102 SwList* pNewList = createList( sListId, sListStyleName );
103 maListStyleLists[sListStyleName] = pNewList;
104 pNumRule->SetDefaultListId( pNewList->GetListId() );
107 SwList* DocumentListsManager::getListForListStyle( const OUString& sListStyleName ) const
109 SwList* pList = nullptr;
111 std::unordered_map< OUString, SwList* >::const_iterator
112 aListIter = maListStyleLists.find( sListStyleName );
113 if ( aListIter != maListStyleLists.end() )
115 pList = (*aListIter).second;
118 return pList;
121 void DocumentListsManager::deleteListForListStyle( const OUString& sListStyleName )
123 OUString sListId;
125 SwList* pList = getListForListStyle( sListStyleName );
126 OSL_ENSURE( pList,
127 "<DocumentListsManager::deleteListForListStyle(..)> - misusage of method: no list found for given list style name" );
128 if ( pList )
130 sListId = pList->GetListId();
133 if ( !sListId.isEmpty() )
135 maListStyleLists.erase( sListStyleName );
136 maLists.erase( sListId );
140 void DocumentListsManager::deleteListsByDefaultListStyle( const OUString& rListStyleName )
142 auto aListIter = maLists.begin();
143 while ( aListIter != maLists.end() )
145 if ( (*aListIter).second->GetDefaultListStyleName() == rListStyleName )
147 aListIter = maLists.erase(aListIter);
149 else
150 ++aListIter;
154 void DocumentListsManager::trackChangeOfListStyleName( const OUString& sListStyleName,
155 const OUString& sNewListStyleName )
157 SwList* pList = getListForListStyle( sListStyleName );
158 OSL_ENSURE( pList,
159 "<DocumentListsManager::changeOfListStyleName(..)> - misusage of method: no list found for given list style name" );
161 if ( pList != nullptr )
163 maListStyleLists.erase( sListStyleName );
164 maListStyleLists[sNewListStyleName] = pList;
166 for (auto & it : maLists) // tdf#91131 update these references too
168 if (it.second->GetDefaultListStyleName() == sListStyleName)
170 it.second->SetDefaultListStyleName(sNewListStyleName);
176 DocumentListsManager::~DocumentListsManager()
181 OUString DocumentListsManager::MakeListIdUnique( const OUString& aSuggestedUniqueListId )
183 tools::Long nHitCount = 0;
184 OUString aTmpStr = aSuggestedUniqueListId;
185 while ( getListByName( aTmpStr ) )
187 ++nHitCount;
188 aTmpStr = aSuggestedUniqueListId + OUString::number( nHitCount );
191 return aTmpStr;
194 OUString DocumentListsManager::CreateUniqueListId()
196 static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != nullptr);
197 if (bHack)
199 static sal_Int64 nIdCounter = SAL_CONST_INT64(7000000000);
200 return MakeListIdUnique( OUString( "list" + OUString::number(nIdCounter++) ) );
202 else
204 // #i92478#
205 unsigned int const n(comphelper::rng::uniform_uint_distribution(0,
206 std::numeric_limits<unsigned int>::max()));
207 OUString const aNewListId = "list" + OUString::number(n);
208 return MakeListIdUnique( aNewListId );
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */