fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / xmloff / source / text / XMLTextListAutoStylePool.cxx
blob73385016f6151d15e2ac18a0f247d0a31ae1adcf
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 .
20 #include <tools/debug.hxx>
21 #include <o3tl/sorted_vector.hxx>
22 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
23 #include <com/sun/star/container/XNamed.hpp>
24 #include <com/sun/star/container/XIndexReplace.hpp>
25 #include <rtl/ustrbuf.hxx>
26 #include <xmloff/xmlnume.hxx>
27 #include "xmloff/XMLTextListAutoStylePool.hxx"
28 #include <xmloff/xmlexp.hxx>
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::beans;
34 using namespace ::com::sun::star::container;
35 using namespace ::com::sun::star::style;
38 class XMLTextListAutoStylePoolEntry_Impl
40 OUString sName;
41 OUString sInternalName;
42 Reference < XIndexReplace > xNumRules;
43 sal_uInt32 nPos;
44 sal_Bool bIsNamed;
47 public:
49 XMLTextListAutoStylePoolEntry_Impl(
50 sal_uInt32 nPos,
51 const Reference < XIndexReplace > & rNumRules,
52 XMLTextListAutoStylePoolNames_Impl& rNames,
53 const OUString& rPrefix,
54 sal_uInt32& rName );
56 XMLTextListAutoStylePoolEntry_Impl(
57 const Reference < XIndexReplace > & rNumRules ) :
58 xNumRules( rNumRules ),
59 nPos( 0 ),
60 bIsNamed( sal_False )
62 Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
63 if( xNamed.is() )
65 sInternalName = xNamed->getName();
66 bIsNamed = sal_True;
70 XMLTextListAutoStylePoolEntry_Impl(
71 const OUString& rInternalName ) :
72 sInternalName( rInternalName ),
73 nPos( 0 ),
74 bIsNamed( sal_True )
78 const OUString& GetName() const { return sName; }
79 const OUString& GetInternalName() const { return sInternalName; }
80 const Reference < XIndexReplace > & GetNumRules() const { return xNumRules; }
81 sal_uInt32 GetPos() const { return nPos; }
82 sal_Bool IsNamed() const { return bIsNamed; }
85 XMLTextListAutoStylePoolEntry_Impl::XMLTextListAutoStylePoolEntry_Impl(
86 sal_uInt32 nP,
87 const Reference < XIndexReplace > & rNumRules,
88 XMLTextListAutoStylePoolNames_Impl& rNames,
89 const OUString& rPrefix,
90 sal_uInt32& rName ) :
91 xNumRules( rNumRules ),
92 nPos( nP ),
93 bIsNamed( sal_False )
95 Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
96 if( xNamed.is() )
98 sInternalName = xNamed->getName();
99 bIsNamed = sal_True;
102 // create a name that hasn't been used before. The created name has not
103 // to be added to the array, because it will never tried again
104 OUStringBuffer sBuffer( 7 );
107 rName++;
108 sBuffer.append( rPrefix );
109 sBuffer.append( (sal_Int32)rName );
110 sName = sBuffer.makeStringAndClear();
112 while (rNames.find(sName) != rNames.end());
115 struct XMLTextListAutoStylePoolEntryCmp_Impl
117 bool operator()(
118 XMLTextListAutoStylePoolEntry_Impl* const& r1,
119 XMLTextListAutoStylePoolEntry_Impl* const& r2 ) const
121 if( r1->IsNamed() )
123 if( r2->IsNamed() )
124 return r1->GetInternalName().compareTo( r2->GetInternalName() ) < 0;
125 else
126 return true;
128 else
130 if( r2->IsNamed() )
131 return false;
132 else
133 return r1->GetNumRules().get() < r2->GetNumRules().get();
137 class XMLTextListAutoStylePool_Impl : public o3tl::sorted_vector<XMLTextListAutoStylePoolEntry_Impl*, XMLTextListAutoStylePoolEntryCmp_Impl> {};
139 XMLTextListAutoStylePool::XMLTextListAutoStylePool( SvXMLExport& rExp ) :
140 rExport( rExp ),
141 sPrefix( "L" ),
142 pPool( new XMLTextListAutoStylePool_Impl ),
143 nName( 0 )
145 Reference<ucb::XAnyCompareFactory> xCompareFac( rExp.GetModel(), uno::UNO_QUERY );
146 if( xCompareFac.is() )
147 mxNumRuleCompare = xCompareFac->createAnyCompareByName( OUString( "NumberingRules" ) );
148 sal_uInt16 nExportFlags = rExport.getExportFlags();
149 sal_Bool bStylesOnly = (nExportFlags & EXPORT_STYLES) != 0 && (nExportFlags & EXPORT_CONTENT) == 0;
150 if( bStylesOnly )
151 sPrefix = OUString( "ML" );
155 XMLTextListAutoStylePool::~XMLTextListAutoStylePool()
157 // The XMLTextListAutoStylePoolEntry_Impl object in the pool need delete explicitly in dtor.
158 pPool->DeleteAndDestroyAll();
159 delete pPool;
162 void XMLTextListAutoStylePool::RegisterName( const OUString& rName )
164 m_aNames.insert(rName);
167 sal_uInt32 XMLTextListAutoStylePool::Find( XMLTextListAutoStylePoolEntry_Impl* pEntry ) const
169 if( !pEntry->IsNamed() && mxNumRuleCompare.is() )
171 const sal_uInt32 nCount = pPool->size();
173 uno::Any aAny1, aAny2;
174 aAny1 <<= pEntry->GetNumRules();
176 for( sal_uLong nPos = 0; nPos < nCount; nPos++ )
178 aAny2 <<= (*pPool)[nPos]->GetNumRules();
180 if( mxNumRuleCompare->compare( aAny1, aAny2 ) == 0 )
181 return nPos;
184 else
186 XMLTextListAutoStylePool_Impl::const_iterator it = pPool->find( pEntry );
187 if( it != pPool->end() )
188 return it - pPool->begin();
191 return (sal_uInt32)-1;
194 OUString XMLTextListAutoStylePool::Add(
195 const Reference < XIndexReplace > & rNumRules )
197 OUString sName;
198 XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
200 sal_uInt32 nPos = Find( &aTmp );
201 if( nPos != (sal_uInt32)-1 )
203 sName = (*pPool)[ nPos ]->GetName();
205 else
207 XMLTextListAutoStylePoolEntry_Impl *pEntry =
208 new XMLTextListAutoStylePoolEntry_Impl( pPool->size(),
209 rNumRules, m_aNames, sPrefix,
210 nName );
211 pPool->insert( pEntry );
212 sName = pEntry->GetName();
215 return sName;
218 OUString XMLTextListAutoStylePool::Find(
219 const Reference < XIndexReplace > & rNumRules ) const
221 OUString sName;
222 XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
224 sal_uInt32 nPos = Find( &aTmp );
225 if( nPos != (sal_uInt32)-1 )
226 sName = (*pPool)[ nPos ]->GetName();
228 return sName;
231 OUString XMLTextListAutoStylePool::Find(
232 const OUString& rInternalName ) const
234 OUString sName;
235 XMLTextListAutoStylePoolEntry_Impl aTmp( rInternalName );
236 sal_uInt32 nPos = Find( &aTmp );
237 if( nPos != (sal_uInt32)-1 )
238 sName = (*pPool)[ nPos ]->GetName();
240 return sName;
243 void XMLTextListAutoStylePool::exportXML() const
245 sal_uInt32 nCount = pPool->size();
246 if( !nCount )
247 return;
249 XMLTextListAutoStylePoolEntry_Impl **aExpEntries =
250 new XMLTextListAutoStylePoolEntry_Impl*[nCount];
252 sal_uInt32 i;
253 for( i=0; i < nCount; i++ )
255 aExpEntries[i] = 0;
257 for( i=0; i < nCount; i++ )
259 XMLTextListAutoStylePoolEntry_Impl *pEntry = (*pPool)[i];
260 DBG_ASSERT( pEntry->GetPos() < nCount, "Illegal pos" );
261 aExpEntries[pEntry->GetPos()] = pEntry;
264 SvxXMLNumRuleExport aNumRuleExp( rExport );
266 for( i=0; i < nCount; i++ )
268 XMLTextListAutoStylePoolEntry_Impl *pEntry = aExpEntries[i];
269 aNumRuleExp.exportNumberingRule( pEntry->GetName(), sal_False,
270 pEntry->GetNumRules() );
272 delete [] aExpEntries;
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */