LibreOffice Module svx (master)  1
UnoNameItemTable.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  */
19 
20 
21 #include <set>
22 
23 #include <svl/itempool.hxx>
24 #include <svl/itemset.hxx>
26 #include <comphelper/sequence.hxx>
28 
29 #include <svx/svdmodel.hxx>
30 #include "UnoNameItemTable.hxx"
31 #include <vcl/svapp.hxx>
32 
33 #include <svx/unoapi.hxx>
34 #include <memory>
35 
36 using namespace ::com::sun::star;
37 using namespace ::cppu;
38 
39 SvxUnoNameItemTable::SvxUnoNameItemTable( SdrModel* pModel, sal_uInt16 nWhich, sal_uInt8 nMemberId ) throw()
40 : mpModel( pModel ),
41  mpModelPool( pModel ? &pModel->GetItemPool() : nullptr ),
42  mnWhich( nWhich ), mnMemberId( nMemberId )
43 {
44  if( pModel )
45  StartListening( *pModel );
46 }
47 
49 {
50  if( mpModel )
52  dispose();
53 }
54 
55 bool SvxUnoNameItemTable::isValid( const NameOrIndex* pItem ) const
56 {
57  return pItem && !pItem->GetName().isEmpty();
58 }
59 
61 {
62  maItemSetVector.clear();
63 }
64 
65 void SvxUnoNameItemTable::Notify( SfxBroadcaster&, const SfxHint& rHint ) throw()
66 {
67  if (rHint.GetId() != SfxHintId::ThisIsAnSdrHint)
68  return;
69  const SdrHint* pSdrHint = static_cast<const SdrHint*>(&rHint);
70  if( SdrHintKind::ModelCleared == pSdrHint->GetKind() )
71  dispose();
72 }
73 
74 sal_Bool SAL_CALL SvxUnoNameItemTable::supportsService( const OUString& ServiceName )
75 {
76  return cppu::supportsService(this, ServiceName);
77 }
78 
79 void SvxUnoNameItemTable::ImplInsertByName( const OUString& aName, const uno::Any& aElement )
80 {
81  maItemSetVector.push_back( std::make_unique< SfxItemSet >( *mpModelPool, std::initializer_list<SfxItemSet::Pair>{{mnWhich, mnWhich}} ) );
82 
83  std::unique_ptr<NameOrIndex> xNewItem(createItem());
84  xNewItem->SetName(aName);
85  xNewItem->PutValue(aElement, mnMemberId);
86  xNewItem->SetWhich(mnWhich);
87  maItemSetVector.back()->Put(*xNewItem);
88 }
89 
90 // XNameContainer
91 void SAL_CALL SvxUnoNameItemTable::insertByName( const OUString& aApiName, const uno::Any& aElement )
92 {
93  SolarMutexGuard aGuard;
94  comphelper::ProfileZone aZone("SvxUnoNameItemTable::insertByName");
95 
96  if( hasByName( aApiName ) )
97  throw container::ElementExistException();
98 
99  OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
100 
101  ImplInsertByName( aName, aElement );
102 }
103 
104 
105 void SAL_CALL SvxUnoNameItemTable::removeByName( const OUString& aApiName )
106 {
107  SolarMutexGuard aGuard;
108  comphelper::ProfileZone aZone("SvxUnoNameItemTable::removeByName");
109 
110  // a little quickfix for 2.0 to let applications clear api
111  // created items that are not used
112  if ( aApiName == "~clear~" )
113  {
114  dispose();
115  return;
116  }
117 
118  OUString sName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
119 
120  auto aIter = std::find_if(maItemSetVector.begin(), maItemSetVector.end(),
121  [&](const std::unique_ptr<SfxItemSet>& rpItem) {
122  const NameOrIndex *pItem = static_cast<const NameOrIndex *>(&(rpItem->Get( mnWhich ) ));
123  return sName == pItem->GetName();
124  });
125  if (aIter != maItemSetVector.end())
126  {
127  maItemSetVector.erase( aIter );
128  return;
129  }
130 
131  if (!hasByName(sName))
132  throw container::NoSuchElementException();
133 }
134 
135 // XNameReplace
136 void SAL_CALL SvxUnoNameItemTable::replaceByName( const OUString& aApiName, const uno::Any& aElement )
137 {
138  SolarMutexGuard aGuard;
139 
140  OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
141 
142  auto aIter = std::find_if(maItemSetVector.begin(), maItemSetVector.end(),
143  [&](const std::unique_ptr<SfxItemSet>& rpItem) {
144  const NameOrIndex *pItem = static_cast<const NameOrIndex *>(&(rpItem->Get( mnWhich ) ));
145  return aName == pItem->GetName();
146  });
147  if (aIter != maItemSetVector.end())
148  {
149  std::unique_ptr<NameOrIndex> xNewItem(createItem());
150  xNewItem->SetName(aName);
151  if (!xNewItem->PutValue(aElement, mnMemberId) || !isValid(xNewItem.get()))
152  throw lang::IllegalArgumentException();
153  (*aIter)->Put(*xNewItem);
154  return;
155  }
156 
157  // if it is not in our own sets, modify the pool!
158  bool bFound = false;
159 
160  if (mpModelPool)
161  {
162  NameOrIndex aSample(mnWhich, aName);
163  for (const SfxPoolItem* pNameOrIndex : mpModelPool->FindItemSurrogate(mnWhich, aSample))
164  if (isValid(static_cast<const NameOrIndex*>(pNameOrIndex)))
165  {
166  const_cast<SfxPoolItem*>(pNameOrIndex)->PutValue( aElement, mnMemberId );
167  bFound = true;
168  }
169  }
170 
171  if( !bFound )
172  throw container::NoSuchElementException();
173 
174  ImplInsertByName( aName, aElement );
175 
176  if( !hasByName( aName ) )
177  throw container::NoSuchElementException();
178 }
179 
180 // XNameAccess
181 uno::Any SAL_CALL SvxUnoNameItemTable::getByName( const OUString& aApiName )
182 {
183  SolarMutexGuard aGuard;
184  comphelper::ProfileZone aZone("SvxUnoNameItemTable::getByName");
185 
186  OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
187 
188  if (mpModelPool && !aName.isEmpty())
189  {
190  NameOrIndex aSample(mnWhich, aName);
191  for (const SfxPoolItem* pFindItem : mpModelPool->FindItemSurrogate(mnWhich, aSample))
192  if (isValid(static_cast<const NameOrIndex*>(pFindItem)))
193  {
194  uno::Any aAny;
195  pFindItem->QueryValue( aAny, mnMemberId );
196  return aAny;
197  }
198  }
199 
200  throw container::NoSuchElementException();
201 }
202 
203 uno::Sequence< OUString > SAL_CALL SvxUnoNameItemTable::getElementNames( )
204 {
205  SolarMutexGuard aGuard;
206 
207  std::set< OUString > aNameSet;
208 
209 
210  if (mpModelPool)
211  for (const SfxPoolItem* pItem : mpModelPool->GetItemSurrogates(mnWhich))
212  {
213  const NameOrIndex *pNameOrIndex = static_cast<const NameOrIndex*>(pItem);
214 
215  if( !isValid( pNameOrIndex ) )
216  continue;
217 
218  OUString aApiName = SvxUnogetApiNameForItem(mnWhich, pNameOrIndex->GetName());
219  aNameSet.insert(aApiName);
220  }
221 
222  return comphelper::containerToSequence(aNameSet);
223 }
224 
225 sal_Bool SAL_CALL SvxUnoNameItemTable::hasByName( const OUString& aApiName )
226 {
227  SolarMutexGuard aGuard;
228 
229  OUString aName = SvxUnogetInternalNameForItem(mnWhich, aApiName);
230 
231  if (aName.isEmpty())
232  return false;
233 
234  if (!mpModelPool)
235  return false;
236 
237  NameOrIndex aSample(mnWhich, aName);
238  for (const SfxPoolItem* pFindItem : mpModelPool->FindItemSurrogate(mnWhich, aSample))
239  if (isValid(static_cast<const NameOrIndex*>(pFindItem)))
240  return true;
241  return false;
242 }
243 
245 {
246  SolarMutexGuard aGuard;
247 
248  if (mpModelPool)
249  for (const SfxPoolItem* pItem : mpModelPool->GetItemSurrogates(mnWhich))
250  {
251  const NameOrIndex *pNameOrIndex = static_cast<const NameOrIndex*>(pItem);
252 
253  if( isValid( pNameOrIndex ) )
254  return true;
255  }
256 
257  return false;
258 }
259 
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxItemPool * mpModelPool
SdrHintKind GetKind() const
Definition: svdmodel.hxx:123
std::vector< const SfxPoolItem * > FindItemSurrogate(sal_uInt16 nWhich, SfxPoolItem const &rNeedle) const
virtual NameOrIndex * createItem() const =0
virtual void SAL_CALL insertByName(const OUString &aName, const css::uno::Any &aElement) override
ItemPoolVector maItemSetVector
virtual bool isValid(const NameOrIndex *pItem) const
OUString SvxUnogetApiNameForItem(const sal_uInt16 nWhich, const OUString &rInternalName)
if the given name is a predefined name for the current language it is replaced by the corresponding a...
Definition: unoprov.cxx:1951
virtual void SAL_CALL removeByName(const OUString &Name) override
void ImplInsertByName(const OUString &aName, const css::uno::Any &aElement)
OUString SvxUnogetInternalNameForItem(const sal_uInt16 nWhich, const OUString &rApiName)
if the given name is a predefined api name it is replaced by the predefined name for the current lang...
Definition: unoprov.cxx:1984
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
OUString sName
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
SvxUnoNameItemTable(SdrModel *pModel, sal_uInt16 nWhich, sal_uInt8 nMemberId)
unsigned char sal_Bool
virtual ~SvxUnoNameItemTable() override
virtual sal_Bool SAL_CALL hasElements() override
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
unsigned char sal_uInt8
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
OUString aName
Item2Range GetItemSurrogates(sal_uInt16 nWhich) const
void EndListening(SfxBroadcaster &rBroadcaster, bool bRemoveAllDuplicates=false)
OUString const & GetName() const
Definition: xit.hxx:52
void dispose()
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override