LibreOffice Module sw (master) 1
DocumentListsManager.cxx
Go to the documentation of this file.
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 */
20#include <doc.hxx>
21#include <list.hxx>
22#include <numrule.hxx>
23
24#include <comphelper/random.hxx>
25#include <osl/diagnose.h>
26
27
28namespace sw
29{
30
32{
33}
34
35SwList* DocumentListsManager::createList( const OUString& rListId,
36 const OUString& sDefaultListStyleName )
37{
38 OUString sListId = rListId;
39 if ( sListId.isEmpty() )
40 {
41 sListId = CreateUniqueListId();
42 }
43
44 if ( getListByName( sListId ) )
45 {
46 OSL_FAIL( "<DocumentListsManager::createList(..)> - provided list id already used. Serious defect." );
47 return nullptr;
48 }
49
50 SwNumRule* pDefaultNumRuleForNewList = m_rDoc.FindNumRulePtr( sDefaultListStyleName );
51 if ( !pDefaultNumRuleForNewList )
52 {
53 OSL_FAIL( "<DocumentListsManager::createList(..)> - for provided default list style name no list style is found. Serious defect." );
54 return nullptr;
55 }
56
57 SwList* pNewList = new SwList( sListId, *pDefaultNumRuleForNewList, m_rDoc.GetNodes() );
58 maLists[sListId].reset(pNewList);
59
60 return pNewList;
61}
62
63SwList* DocumentListsManager::getListByName( const OUString& sListId ) const
64{
65 SwList* pList = nullptr;
66
67 auto aListIter = maLists.find( sListId );
68 if ( aListIter != maLists.end() )
69 {
70 pList = (*aListIter).second.get();
71 }
72
73 return pList;
74}
75
76void DocumentListsManager::createListForListStyle( const OUString& sListStyleName )
77{
78 if ( sListStyleName.isEmpty() )
79 {
80 OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - no list style name provided. Serious defect." );
81 return;
82 }
83
84 if ( getListForListStyle( sListStyleName ) )
85 {
86 OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - a list for the provided list style name already exists. Serious defect." );
87 return;
88 }
89
90 SwNumRule* pNumRule = m_rDoc.FindNumRulePtr( sListStyleName );
91 if ( !pNumRule )
92 {
93 OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - for provided list style name no list style is found. Serious defect." );
94 return;
95 }
96
97 OUString sListId( pNumRule->GetDefaultListId() ); // can be empty String
98 if ( getListByName( sListId ) )
99 {
100 sListId.clear();
101 }
102 SwList* pNewList = createList( sListId, sListStyleName );
103 maListStyleLists[sListStyleName] = pNewList;
104 pNumRule->SetDefaultListId( pNewList->GetListId() );
105}
106
107SwList* DocumentListsManager::getListForListStyle( const OUString& sListStyleName ) const
108{
109 SwList* pList = nullptr;
110
111 std::unordered_map< OUString, SwList* >::const_iterator
112 aListIter = maListStyleLists.find( sListStyleName );
113 if ( aListIter != maListStyleLists.end() )
114 {
115 pList = (*aListIter).second;
116 }
117
118 return pList;
119}
120
121void DocumentListsManager::deleteListForListStyle( const OUString& sListStyleName )
122{
123 OUString sListId;
124 {
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 )
129 {
130 sListId = pList->GetListId();
131 }
132 }
133 if ( !sListId.isEmpty() )
134 {
135 maListStyleLists.erase( sListStyleName );
136 maLists.erase( sListId );
137 }
138}
139
140void DocumentListsManager::deleteListsByDefaultListStyle( const OUString& rListStyleName )
141{
142 auto aListIter = maLists.begin();
143 while ( aListIter != maLists.end() )
144 {
145 if ( (*aListIter).second->GetDefaultListStyleName() == rListStyleName )
146 {
147 aListIter = maLists.erase(aListIter);
148 }
149 else
150 ++aListIter;
151 }
152}
153
154void DocumentListsManager::trackChangeOfListStyleName( const OUString& sListStyleName,
155 const OUString& sNewListStyleName )
156{
157 SwList* pList = getListForListStyle( sListStyleName );
158 OSL_ENSURE( pList,
159 "<DocumentListsManager::changeOfListStyleName(..)> - misusage of method: no list found for given list style name" );
160
161 if ( pList != nullptr )
162 {
163 maListStyleLists.erase( sListStyleName );
164 maListStyleLists[sNewListStyleName] = pList;
165 }
166 for (auto & it : maLists) // tdf#91131 update these references too
167 {
168 if (it.second->GetDefaultListStyleName() == sListStyleName)
169 {
170 it.second->SetDefaultListStyleName(sNewListStyleName);
171 }
172 }
173}
174
175
177{
178}
179
180
181OUString DocumentListsManager::MakeListIdUnique( const OUString& aSuggestedUniqueListId )
182{
183 tools::Long nHitCount = 0;
184 OUString aTmpStr = aSuggestedUniqueListId;
185 while ( getListByName( aTmpStr ) )
186 {
187 ++nHitCount;
188 aTmpStr = aSuggestedUniqueListId + OUString::number( nHitCount );
189 }
190
191 return aTmpStr;
192}
193
195{
196 static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != nullptr);
197 if (bHack)
198 {
199 static sal_Int64 nIdCounter = SAL_CONST_INT64(7000000000);
200 return MakeListIdUnique( OUString( "list" + OUString::number(nIdCounter++) ) );
201 }
202 else
203 {
204 // #i92478#
206 std::numeric_limits<unsigned int>::max()));
207 OUString const aNewListId = "list" + OUString::number(n);
208 return MakeListIdUnique( aNewListId );
209 }
210}
211
212}
213
214/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: doc.hxx:197
SwNumRule * FindNumRulePtr(const OUString &rName) const
Definition: docnum.cxx:2454
SwNodes & GetNodes()
Definition: doc.hxx:422
Definition: list.hxx:40
const OUString & GetListId() const
Definition: list.hxx:47
void SetDefaultListStyleName(OUString const &)
Definition: list.cxx:185
const OUString & GetDefaultListId() const
Definition: numrule.hxx:194
void SetDefaultListId(const OUString &sDefaultListId)
Definition: numrule.hxx:190
std::unordered_map< OUString, SwListPtr > maLists
std::unordered_map< OUString, SwList * > maListStyleLists
SwList * createList(const OUString &rListId, const OUString &rDefaultListStyleName) override
void deleteListsByDefaultListStyle(const OUString &rListStyleName) override
OUString MakeListIdUnique(const OUString &aSuggestedUniqueListId)
SwList * getListByName(const OUString &rListId) const override
SwList * getListForListStyle(const OUString &rListStyleName) const override
DocumentListsManager(SwDoc &i_rSwdoc)
virtual ~DocumentListsManager() override
void trackChangeOfListStyleName(const OUString &rListStyleName, const OUString &rNewListStyleName) override
void deleteListForListStyle(const OUString &rListStyleName) override
void createListForListStyle(const OUString &rListStyleName) override
SwDoc & m_rDoc
Definition: docbm.cxx:1228
sal_Int64 n
unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
Dialog to specify the properties of date form field.
long Long