1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_comphelper.hxx"
26 
27 #include <comphelper/numberedcollection.hxx>
28 #include <algorithm>
29 
30 //_______________________________________________
31 // includes
32 
33 #include <com/sun/star/frame/UntitledNumbersConst.hpp>
34 
35 //_______________________________________________
36 // namespace
37 
38 namespace comphelper{
39 
40 namespace css = ::com::sun::star;
41 
42 //_______________________________________________
43 // definitions
44 
45 static const ::rtl::OUString ERRMSG_INVALID_COMPONENT_PARAM = ::rtl::OUString::createFromAscii("NULL as component reference not allowed.");
46 static const ::rtl::OUString ERRMSG_INVALID_NUMBER_PARAM    = ::rtl::OUString::createFromAscii("Special valkud INVALID_NUMBER not allowed as input parameter.");
47 
48 //-----------------------------------------------
NumberedCollection()49 NumberedCollection::NumberedCollection()
50     : ::cppu::BaseMutex ()
51     , m_sUntitledPrefix ()
52     , m_lComponents     ()
53     , m_xOwner          ()
54 {
55 }
56 
57 //-----------------------------------------------
~NumberedCollection()58 NumberedCollection::~NumberedCollection()
59 {
60 }
61 
62 //-----------------------------------------------
setOwner(const css::uno::Reference<css::uno::XInterface> & xOwner)63 void NumberedCollection::setOwner(const css::uno::Reference< css::uno::XInterface >& xOwner)
64 {
65     // SYNCHRONIZED ->
66     ::osl::ResettableMutexGuard aLock(m_aMutex);
67 
68         m_xOwner = xOwner;
69 
70     // <- SYNCHRONIZED
71 }
72 
73 //-----------------------------------------------
setUntitledPrefix(const::rtl::OUString & sPrefix)74 void NumberedCollection::setUntitledPrefix(const ::rtl::OUString& sPrefix)
75 {
76     // SYNCHRONIZED ->
77     ::osl::ResettableMutexGuard aLock(m_aMutex);
78 
79         m_sUntitledPrefix = sPrefix;
80 
81     // <- SYNCHRONIZED
82 }
83 
84 //-----------------------------------------------
leaseNumber(const css::uno::Reference<css::uno::XInterface> & xComponent)85 ::sal_Int32 SAL_CALL NumberedCollection::leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent)
86     throw (css::lang::IllegalArgumentException,
87            css::uno::RuntimeException         )
88 {
89     // SYNCHRONIZED ->
90     ::osl::ResettableMutexGuard aLock(m_aMutex);
91 
92         if ( ! xComponent.is ())
93             throw css::lang::IllegalArgumentException (ERRMSG_INVALID_COMPONENT_PARAM, m_xOwner.get(), 1);
94 
95         long                              pComponent = (long) xComponent.get ();
96         TNumberedItemHash::const_iterator pIt        = m_lComponents.find (pComponent);
97 
98         // a) component already exists - return it's number directly
99         if (pIt != m_lComponents.end())
100             return pIt->second.nNumber;
101 
102         // b) component must be added new to this container
103 
104         // b1) collection is full - no further components possible
105         //     -> return INVALID_NUMBER
106         ::sal_Int32 nFreeNumber = impl_searchFreeNumber();
107         if (nFreeNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
108             return css::frame::UntitledNumbersConst::INVALID_NUMBER;
109 
110         // b2) add component to collection and return its number
111         TNumberedItem aItem;
112         aItem.xItem   = css::uno::WeakReference< css::uno::XInterface >(xComponent);
113         aItem.nNumber = nFreeNumber;
114         m_lComponents[pComponent] = aItem;
115 
116         return nFreeNumber;
117 
118     // <- SYNCHRONIZED
119 }
120 
121 //-----------------------------------------------
releaseNumber(::sal_Int32 nNumber)122 void SAL_CALL NumberedCollection::releaseNumber(::sal_Int32 nNumber)
123     throw (css::lang::IllegalArgumentException,
124            css::uno::RuntimeException         )
125 {
126     // SYNCHRONIZED ->
127     ::osl::ResettableMutexGuard aLock(m_aMutex);
128 
129         if (nNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
130             throw css::lang::IllegalArgumentException (ERRMSG_INVALID_NUMBER_PARAM, m_xOwner.get(), 1);
131 
132         TDeadItemList               lDeadItems;
133         TNumberedItemHash::iterator pComponent;
134 
135         for (  pComponent  = m_lComponents.begin ();
136                pComponent != m_lComponents.end   ();
137              ++pComponent                          )
138         {
139             const TNumberedItem&                              rItem = pComponent->second;
140             const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
141 
142             if ( ! xItem.is ())
143             {
144                 lDeadItems.push_back(pComponent->first);
145                 continue;
146             }
147 
148             if (rItem.nNumber == nNumber)
149             {
150                 m_lComponents.erase (pComponent);
151                 break;
152             }
153         }
154 
155         impl_cleanUpDeadItems(m_lComponents, lDeadItems);
156 
157     // <- SYNCHRONIZED
158 }
159 
160 //-----------------------------------------------
releaseNumberForComponent(const css::uno::Reference<css::uno::XInterface> & xComponent)161 void SAL_CALL NumberedCollection::releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent)
162     throw (css::lang::IllegalArgumentException,
163            css::uno::RuntimeException         )
164 {
165     // SYNCHRONIZED ->
166     ::osl::ResettableMutexGuard aLock(m_aMutex);
167 
168         if ( ! xComponent.is ())
169             throw css::lang::IllegalArgumentException (ERRMSG_INVALID_COMPONENT_PARAM, m_xOwner.get(), 1);
170 
171         long                        pComponent = (long) xComponent.get ();
172         TNumberedItemHash::iterator pIt        = m_lComponents.find (pComponent);
173 
174         // a) component exists and will be removed
175         if (pIt != m_lComponents.end())
176             m_lComponents.erase(pIt);
177 
178         // else
179         // b) component does not exists - nothing todo here (ignore request!)
180 
181     // <- SYNCHRONIZED
182 }
183 
184 //-----------------------------------------------
getUntitledPrefix()185 ::rtl::OUString SAL_CALL NumberedCollection::getUntitledPrefix()
186     throw (css::uno::RuntimeException)
187 {
188     // SYNCHRONIZED ->
189     ::osl::ResettableMutexGuard aLock(m_aMutex);
190 
191         return m_sUntitledPrefix;
192 
193     // <- SYNCHRONIZED
194 }
195 
196 //-----------------------------------------------
197 /** create an ordered list of all possible numbers ...
198     e.g. {1,2,3,...,N} Max size of these list will be
199     current size of component list + 1 .
200 
201     "+1" ... because in case all numbers in range 1..n
202     are in use we need a new number n+1 :-)
203 
204     Every item which is already used as unique number
205     will be removed. At the end a list of e.g. {3,6,...,M}
206     exists where the first item represent the lowest free
207     number (in this example 3).
208  */
impl_searchFreeNumber()209 ::sal_Int32 NumberedCollection::impl_searchFreeNumber ()
210 {
211     // create ordered list of all possible numbers.
212     ::std::vector< ::sal_Int32 > lPossibleNumbers;
213     ::sal_Int32                  c = (::sal_Int32)m_lComponents.size ();
214     ::sal_Int32                  i = 1;
215 
216     // c can't be less than 0 ... otherwise hash.size() has an error :-)
217     // But we need at least n+1 numbers here.
218 	c += 1;
219 
220     for (i=1; i<=c; ++i)
221         lPossibleNumbers.push_back (i);
222 
223     // SYNCHRONIZED ->
224     ::osl::ResettableMutexGuard aLock(m_aMutex);
225 
226         TDeadItemList                     lDeadItems;
227         TNumberedItemHash::const_iterator pComponent;
228 
229         for (  pComponent  = m_lComponents.begin ();
230                pComponent != m_lComponents.end   ();
231              ++pComponent                          )
232         {
233             const TNumberedItem&                              rItem = pComponent->second;
234             const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
235 
236             if ( ! xItem.is ())
237             {
238                 lDeadItems.push_back(pComponent->first);
239                 continue;
240             }
241 
242             ::std::vector< ::sal_Int32 >::iterator pPossible = ::std::find(lPossibleNumbers.begin (), lPossibleNumbers.end (), rItem.nNumber);
243             if (pPossible != lPossibleNumbers.end ())
244                 lPossibleNumbers.erase (pPossible);
245         }
246 
247         impl_cleanUpDeadItems(m_lComponents, lDeadItems);
248 
249         // a) non free numbers ... return INVALID_NUMBER
250         if (lPossibleNumbers.size () < 1)
251             return css::frame::UntitledNumbersConst::INVALID_NUMBER;
252 
253         // b) return first free number
254         return *(lPossibleNumbers.begin ());
255 
256     // <- SYNCHRONIZED
257 }
258 
impl_cleanUpDeadItems(TNumberedItemHash & lItems,const TDeadItemList & lDeadItems)259 void NumberedCollection::impl_cleanUpDeadItems (      TNumberedItemHash& lItems    ,
260                                                 const TDeadItemList&     lDeadItems)
261 {
262     TDeadItemList::const_iterator pIt;
263 
264     for (  pIt  = lDeadItems.begin ();
265            pIt != lDeadItems.end   ();
266          ++pIt                       )
267     {
268         const long& rDeadItem = *pIt;
269         lItems.erase(rDeadItem);
270     }
271 }
272 
273 } // namespace comphelper
274