1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
22 #include <numrule.hxx>
24 #include <comphelper/random.hxx>
25 #include <osl/diagnose.h>
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." );
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." );
57 SwList
* pNewList
= new SwList( sListId
, *pDefaultNumRuleForNewList
, m_rDoc
.GetNodes() );
58 maLists
[sListId
].reset(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();
76 void DocumentListsManager::createListForListStyle( const OUString
& sListStyleName
)
78 if ( sListStyleName
.isEmpty() )
80 OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - no list style name provided. Serious defect." );
84 if ( getListForListStyle( sListStyleName
) )
86 OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - a list for the provided list style name already exists. Serious defect." );
90 SwNumRule
* pNumRule
= m_rDoc
.FindNumRulePtr( sListStyleName
);
93 OSL_FAIL( "<DocumentListsManager::createListForListStyle(..)> - for provided list style name no list style is found. Serious defect." );
97 OUString
sListId( pNumRule
->GetDefaultListId() ); // can be empty String
98 if ( getListByName( sListId
) )
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
;
121 void DocumentListsManager::deleteListForListStyle( const OUString
& sListStyleName
)
125 SwList
* pList
= getListForListStyle( sListStyleName
);
127 "<DocumentListsManager::deleteListForListStyle(..)> - misusage of method: no list found for given list style name" );
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
);
154 void DocumentListsManager::trackChangeOfListStyleName( const OUString
& sListStyleName
,
155 const OUString
& sNewListStyleName
)
157 SwList
* pList
= getListForListStyle( sListStyleName
);
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
) )
188 aTmpStr
= aSuggestedUniqueListId
+ OUString::number( nHitCount
);
194 OUString
DocumentListsManager::CreateUniqueListId()
196 static bool bHack
= (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != nullptr);
199 static sal_Int64 nIdCounter
= SAL_CONST_INT64(7000000000);
200 return MakeListIdUnique( OUString( "list" + OUString::number(nIdCounter
++) ) );
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: */