1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_slideshow.hxx"
30 
31 #include <canvas/debug.hxx>
32 #include <unoviewcontainer.hxx>
33 
34 #include <boost/bind.hpp>
35 
36 #include <algorithm>
37 
38 
39 using namespace ::com::sun::star;
40 
41 // -----------------------------------------------------------------------------
42 
43 namespace slideshow
44 {
45     namespace internal
46     {
47         UnoViewContainer::UnoViewContainer() :
48             maViews()
49         {
50         }
51 
52         bool UnoViewContainer::addView( const UnoViewSharedPtr& rView )
53         {
54             // check whether same view is already added
55             const UnoViewVector::iterator aEnd( maViews.end() );
56 
57             // already added?
58             if( ::std::find_if( maViews.begin(),
59                                 aEnd,
60                                 ::boost::bind(
61                                     ::std::equal_to< uno::Reference< presentation::XSlideShowView > >(),
62                                     ::boost::cref( rView->getUnoView() ),
63                                     ::boost::bind(
64                                         &UnoView::getUnoView,
65                                         _1 ) ) ) != aEnd )
66             {
67                 // yes, nothing to do
68                 return false;
69             }
70 
71             // add locally
72             maViews.push_back( rView );
73 
74             return true;
75         }
76 
77         UnoViewSharedPtr UnoViewContainer::removeView( const uno::Reference< presentation::XSlideShowView >& xView )
78         {
79             // check whether same view is already added
80             const UnoViewVector::iterator aEnd( maViews.end() );
81             UnoViewVector::iterator aIter;
82 
83             // added in the first place?
84             if( (aIter=::std::find_if( maViews.begin(),
85                                        aEnd,
86                                        ::boost::bind(
87                                            ::std::equal_to< uno::Reference< presentation::XSlideShowView > >(),
88                                            ::boost::cref( xView ),
89                                            ::boost::bind(
90                                                &UnoView::getUnoView,
91                                                _1 ) ) ) ) == aEnd )
92             {
93                 // nope, nothing to do
94                 return UnoViewSharedPtr();
95             }
96 
97             OSL_ENSURE(
98                 ::std::count_if(
99                     maViews.begin(),
100                     aEnd,
101                     ::boost::bind(
102                         ::std::equal_to< uno::Reference< presentation::XSlideShowView > >(),
103                         ::boost::cref( xView ),
104                         ::boost::bind(
105                             &UnoView::getUnoView,
106                             _1 ))) == 1,
107                 "UnoViewContainer::removeView(): View was added multiple times" );
108 
109             UnoViewSharedPtr pView( *aIter );
110 
111             // actually erase from container
112             maViews.erase( aIter );
113 
114             return pView;
115         }
116 
117         bool UnoViewContainer::removeView( const UnoViewSharedPtr& rView )
118         {
119             // remove locally
120             const UnoViewVector::iterator aEnd( maViews.end() );
121             UnoViewVector::iterator aIter;
122             if( (aIter=::std::find( maViews.begin(),
123                                     aEnd,
124                                     rView )) == aEnd )
125             {
126                 // view seemingly was not added, failed
127                 return false;
128             }
129 
130             OSL_ENSURE( ::std::count( maViews.begin(),
131                                       aEnd,
132                                       rView ) == 1,
133                         "UnoViewContainer::removeView(): View was added multiple times" );
134 
135             // actually erase from container
136             maViews.erase( aIter );
137 
138             return true;
139         }
140 
141         void UnoViewContainer::dispose()
142         {
143             ::std::for_each( maViews.begin(),
144                              maViews.end(),
145                              ::boost::mem_fn(&UnoView::_dispose) );
146             maViews.clear();
147         }
148     }
149 }
150