1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ResPoolProxy.cc
13 #include "zypp/base/Logger.h"
15 #include "zypp/base/Iterator.h"
16 #include "zypp/base/Algorithm.h"
17 #include "zypp/base/Functional.h"
19 #include "zypp/ResPoolProxy.h"
20 #include "zypp/ui/SelectableImpl.h"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 /** Tem. friend of PoolItem */
31 void saveState( ResPool_Ref pool_r )
33 std::for_each( pool_r.begin(), pool_r.end(),
34 std::mem_fun_ref(&PoolItem::saveState) );
37 void saveState( ResPool_Ref pool_r, const ResObject::Kind & kind_r )
39 std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
40 std::mem_fun_ref(&PoolItem::saveState) );
43 void restoreState( ResPool_Ref pool_r )
45 std::for_each( pool_r.begin(), pool_r.end(),
46 std::mem_fun_ref(&PoolItem::restoreState) );
49 void restoreState( ResPool_Ref pool_r, const ResObject::Kind & kind_r )
51 std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
52 std::mem_fun_ref(&PoolItem::restoreState) );
55 bool diffState( ResPool_Ref pool_r ) const
57 // return whether some PoolItem::sameState reported \c false.
58 return( invokeOnEach( pool_r.begin(), pool_r.end(),
59 std::mem_fun_ref(&PoolItem::sameState) ) < 0 );
62 bool diffState( ResPool_Ref pool_r, const ResObject::Kind & kind_r ) const
64 // return whether some PoolItem::sameState reported \c false.
65 return( invokeOnEach( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
66 std::mem_fun_ref(&PoolItem::sameState) ) < 0 );
72 typedef std::set<ResPool::Item> ItemC;
75 void add( ResPool::Item it )
77 if ( it.status().isInstalled() )
78 installed.insert( it );
80 available.insert( it );
85 typedef std::map<std::string,SelC> NameC;
86 typedef std::map<ResObject::Kind,NameC> KindC;
90 /** collect from a pool */
91 void operator()( ResPool::Item it )
93 _kinds[it->kind()][it->name()].add( it );
97 ui::Selectable::Ptr buildSelectable( const ResObject::Kind & kind_r,
98 const std::string & name_r,
99 const PoolItem & installedItem_r,
100 const ItemC & available )
102 return ui::Selectable::Ptr( new ui::Selectable(
103 ui::Selectable::Impl_Ptr( new ui::Selectable::Impl( kind_r, name_r,
110 /** Build Selectable::Ptr and feed them to some container.
111 * \todo Cleanup typedefs
113 typedef std::set<ui::Selectable::Ptr> SelectableIndex;
114 typedef std::map<ResObject::Kind,SelectableIndex> SelectablePool;
116 void feed( SelectablePool & _selPool )
118 for ( KindC::const_iterator kindIt = _kinds.begin(); kindIt != _kinds.end(); ++kindIt )
120 for ( NameC::const_iterator nameIt = kindIt->second.begin(); nameIt != kindIt->second.end(); ++nameIt )
122 const ItemC & installed( nameIt->second.installed );
123 const ItemC & available( nameIt->second.available );
125 if ( installed.empty() )
127 if ( available.empty() )
129 _selPool[kindIt->first].insert( buildSelectable( kindIt->first, nameIt->first, PoolItem(), available ) );
133 // ui want's one Selectable per installed item
134 for ( ItemC::const_iterator instIt = installed.begin(); instIt != installed.end(); ++instIt )
136 _selPool[kindIt->first].insert( buildSelectable( kindIt->first, nameIt->first, *instIt, available ) );
144 ///////////////////////////////////////////////////////////////////
146 // CLASS NAME : ResPoolProxy::Impl
148 /** ResPoolProxy implementation. */
149 struct ResPoolProxy::Impl
155 Impl( ResPool_Ref pool_r )
159 SelPoolHelper collect;
160 std::for_each( _pool.begin(), _pool.end(),
161 functor::functorRef<void,ResPool::Item>( collect ) );
162 collect.feed( _selPool );
167 bool empty( const ResObject::Kind & kind_r ) const
168 { return _selPool[kind_r].empty(); }
170 size_type size( const ResObject::Kind & kind_r ) const
171 { return _selPool[kind_r].size(); }
173 const_iterator byKindBegin( const ResObject::Kind & kind_r ) const
174 { return _selPool[kind_r].begin(); }
176 const_iterator byKindEnd( const ResObject::Kind & kind_r ) const
177 { return _selPool[kind_r].end(); }
182 void saveState() const
183 { PoolItemSaver().saveState( _pool ); }
185 void saveState( const ResObject::Kind & kind_r ) const
186 { PoolItemSaver().saveState( _pool, kind_r ); }
188 void restoreState() const
189 { PoolItemSaver().restoreState( _pool ); }
191 void restoreState( const ResObject::Kind & kind_r ) const
192 { PoolItemSaver().restoreState( _pool, kind_r ); }
194 bool diffState() const
195 { return PoolItemSaver().diffState( _pool ); }
197 bool diffState( const ResObject::Kind & kind_r ) const
198 { return PoolItemSaver().diffState( _pool, kind_r ); }
202 mutable SelectablePool _selPool;
205 /** Offer default Impl. */
206 static shared_ptr<Impl> nullimpl()
208 static shared_ptr<Impl> _nullimpl( new Impl );
212 ///////////////////////////////////////////////////////////////////
214 /** \relates ResPoolProxy::Impl Stream output */
215 inline std::ostream & operator<<( std::ostream & str, const ResPoolProxy::Impl & obj )
217 return str << "ResPoolProxy::Impl";
220 ///////////////////////////////////////////////////////////////////
222 // CLASS NAME : ResPoolProxy
224 ///////////////////////////////////////////////////////////////////
226 ///////////////////////////////////////////////////////////////////
228 // METHOD NAME : ResPoolProxy::ResPoolProxy
229 // METHOD TYPE : Ctor
231 ResPoolProxy::ResPoolProxy()
232 : _pimpl( Impl::nullimpl() )
235 ///////////////////////////////////////////////////////////////////
237 // METHOD NAME : ResPoolProxy::ResPoolProxy
238 // METHOD TYPE : Ctor
240 ResPoolProxy::ResPoolProxy( ResPool_Ref pool_r )
241 : _pimpl( new Impl( pool_r ) )
244 ///////////////////////////////////////////////////////////////////
246 // METHOD NAME : ResPoolProxy::~ResPoolProxy
247 // METHOD TYPE : Dtor
249 ResPoolProxy::~ResPoolProxy()
252 ///////////////////////////////////////////////////////////////////
254 // forward to implementation
256 ///////////////////////////////////////////////////////////////////
258 bool ResPoolProxy::empty( const ResObject::Kind & kind_r ) const
259 { return _pimpl->empty( kind_r ); }
261 ResPoolProxy::size_type ResPoolProxy::size( const ResObject::Kind & kind_r ) const
262 { return _pimpl->size( kind_r ); }
264 ResPoolProxy::const_iterator ResPoolProxy::byKindBegin( const ResObject::Kind & kind_r ) const
265 { return _pimpl->byKindBegin( kind_r ); }
267 ResPoolProxy::const_iterator ResPoolProxy::byKindEnd( const ResObject::Kind & kind_r ) const
268 { return _pimpl->byKindEnd( kind_r ); }
270 void ResPoolProxy::saveState() const
271 { _pimpl->saveState(); }
273 void ResPoolProxy::saveState( const ResObject::Kind & kind_r ) const
274 { _pimpl->saveState( kind_r ); }
276 void ResPoolProxy::restoreState() const
277 { _pimpl->restoreState(); }
279 void ResPoolProxy::restoreState( const ResObject::Kind & kind_r ) const
280 { _pimpl->restoreState( kind_r ); }
282 bool ResPoolProxy::diffState() const
283 { return _pimpl->diffState(); }
285 bool ResPoolProxy::diffState( const ResObject::Kind & kind_r ) const
286 { return _pimpl->diffState( kind_r ); }
288 /******************************************************************
290 ** FUNCTION NAME : operator<<
291 ** FUNCTION TYPE : std::ostream &
293 std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj )
295 return str << *obj._pimpl;
298 /////////////////////////////////////////////////////////////////
300 ///////////////////////////////////////////////////////////////////