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