Deprecate MediaAccess::downloads (accidentally deleted)
[platform/upstream/libzypp.git] / zypp / ResPoolProxy.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ResPoolProxy.cc
10  *
11 */
12 #include <iostream>
13 #include "zypp/base/LogTools.h"
14
15 #include "zypp/base/Measure.h"
16 using zypp::debug::Measure;
17
18 #include "zypp/base/Iterator.h"
19 #include "zypp/base/Algorithm.h"
20 #include "zypp/base/Functional.h"
21
22 #include "zypp/ResPoolProxy.h"
23 #include "zypp/pool/PoolImpl.h"
24 #include "zypp/ui/SelectableImpl.h"
25
26 using std::endl;
27
28 ///////////////////////////////////////////////////////////////////
29 namespace zypp
30 { /////////////////////////////////////////////////////////////////
31
32   /** Tem. friend of PoolItem */
33   struct PoolItemSaver
34   {
35     void saveState( ResPool pool_r )
36     {
37       std::for_each( pool_r.begin(), pool_r.end(),
38                      std::mem_fun_ref(&PoolItem::saveState) );
39     }
40
41     void saveState( ResPool pool_r, const ResKind & kind_r )
42     {
43       std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
44                      std::mem_fun_ref(&PoolItem::saveState) );
45     }
46
47     void restoreState( ResPool pool_r )
48     {
49       std::for_each( pool_r.begin(), pool_r.end(),
50                      std::mem_fun_ref(&PoolItem::restoreState) );
51     }
52
53     void restoreState( ResPool pool_r, const ResKind & kind_r )
54     {
55       std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
56                      std::mem_fun_ref(&PoolItem::restoreState) );
57     }
58
59     bool diffState( ResPool pool_r ) const
60     {
61       // return whether some PoolItem::sameState reported \c false.
62       return( invokeOnEach( pool_r.begin(), pool_r.end(),
63                             std::mem_fun_ref(&PoolItem::sameState) ) < 0 );
64     }
65
66     bool diffState( ResPool pool_r, const ResKind & kind_r ) const
67     {
68       // return whether some PoolItem::sameState reported \c false.
69       return( invokeOnEach( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
70                             std::mem_fun_ref(&PoolItem::sameState) ) < 0 );
71     }
72   };
73
74   namespace
75   {
76     ui::Selectable::Ptr makeSelectablePtr( pool::PoolImpl::Id2ItemT::const_iterator begin_r,
77                                            pool::PoolImpl::Id2ItemT::const_iterator end_r )
78     {
79       pool::PoolTraits::byIdent_iterator begin( begin_r, pool::PoolTraits::Id2ItemValueSelector() );
80       pool::PoolTraits::byIdent_iterator end( end_r, pool::PoolTraits::Id2ItemValueSelector() );
81       sat::Solvable solv( begin->satSolvable() );
82
83       return new ui::Selectable( ui::Selectable::Impl_Ptr( new ui::Selectable::Impl( solv.kind(), solv.name(), begin, end ) ) );
84     }
85   } // namespace
86
87   ///////////////////////////////////////////////////////////////////
88   //
89   //    CLASS NAME : ResPoolProxy::Impl
90   //
91   /** ResPoolProxy implementation.
92    * \todo Seedup as it is still using old index
93   */
94   struct ResPoolProxy::Impl
95   {
96     typedef std::tr1::unordered_map<sat::detail::IdType,ui::Selectable::Ptr>
97             SelectableIndex;
98
99   public:
100     Impl()
101     :_pool( ResPool::instance() )
102     {}
103
104     Impl( ResPool pool_r, const pool::PoolImpl & poolImpl_r )
105     : _pool( pool_r )
106     {
107       Measure( "id2item" );
108       const pool::PoolImpl::Id2ItemT & id2item( poolImpl_r.id2item() );
109       if ( ! id2item.empty() )
110       {
111         // set startpoint
112         pool::PoolImpl::Id2ItemT::const_iterator cbegin = id2item.begin();
113
114         for_( it, id2item.begin(), id2item.end() )
115         {
116           if ( it->first != cbegin->first )
117           {
118             // starting a new Selectable, create the previous one
119             ui::Selectable::Ptr p( makeSelectablePtr( cbegin, it ) );
120             _selPool[p->kind()].push_back( p );
121             _selIndex[cbegin->first] = p;
122             // remember new startpoint
123             cbegin = it;
124           }
125         }
126         // create the final one
127         ui::Selectable::Ptr p( makeSelectablePtr( cbegin, id2item.end() ) );
128         _selPool[p->kind()].push_back( p );
129         _selIndex[cbegin->first] = p;
130       }
131     }
132
133   public:
134     ui::Selectable::Ptr lookup( const pool::ByIdent & ident_r ) const
135     {
136       SelectableIndex::const_iterator it( _selIndex.find( ident_r.get() ) );
137       if ( it != _selIndex.end() )
138         return it->second;
139       return ui::Selectable::Ptr();
140     }
141
142   public:
143     bool empty( const ResKind & kind_r ) const
144     { return _selPool[kind_r].empty(); }
145
146     size_type size( const ResKind & kind_r ) const
147     { return _selPool[kind_r].size(); }
148
149     const_iterator byKindBegin( const ResKind & kind_r ) const
150     { return _selPool[kind_r].begin(); }
151
152     const_iterator byKindEnd( const ResKind & kind_r ) const
153     { return _selPool[kind_r].end(); }
154
155   public:
156     size_type knownRepositoriesSize() const
157     { return _pool.knownRepositoriesSize(); }
158
159     repository_iterator knownRepositoriesBegin() const
160     { return _pool.knownRepositoriesBegin(); }
161
162     repository_iterator knownRepositoriesEnd() const
163     { return _pool.knownRepositoriesEnd(); }
164
165   public:
166
167     void saveState() const
168     { PoolItemSaver().saveState( _pool ); }
169
170     void saveState( const ResKind & kind_r ) const
171     { PoolItemSaver().saveState( _pool, kind_r ); }
172
173     void restoreState() const
174     { PoolItemSaver().restoreState( _pool ); }
175
176     void restoreState( const ResKind & kind_r ) const
177     { PoolItemSaver().restoreState( _pool, kind_r ); }
178
179     bool diffState() const
180     { return PoolItemSaver().diffState( _pool ); }
181
182     bool diffState( const ResKind & kind_r ) const
183     { return PoolItemSaver().diffState( _pool, kind_r ); }
184
185   private:
186     ResPool _pool;
187     mutable SelectablePool _selPool;
188     mutable SelectableIndex _selIndex;
189
190   public:
191     /** Offer default Impl. */
192     static shared_ptr<Impl> nullimpl()
193     {
194       static shared_ptr<Impl> _nullimpl( new Impl );
195       return _nullimpl;
196     }
197   };
198   ///////////////////////////////////////////////////////////////////
199
200   /** \relates ResPoolProxy::Impl Stream output */
201   inline std::ostream & operator<<( std::ostream & str, const ResPoolProxy::Impl & obj )
202   {
203     return str << "ResPoolProxy::Impl";
204   }
205
206   ///////////////////////////////////////////////////////////////////
207   //
208   //    CLASS NAME : ResPoolProxy
209   //
210   ///////////////////////////////////////////////////////////////////
211
212   ///////////////////////////////////////////////////////////////////
213   //
214   //    METHOD NAME : ResPoolProxy::ResPoolProxy
215   //    METHOD TYPE : Ctor
216   //
217   ResPoolProxy::ResPoolProxy()
218   : _pimpl( Impl::nullimpl() )
219   {}
220
221   ///////////////////////////////////////////////////////////////////
222   //
223   //    METHOD NAME : ResPoolProxy::ResPoolProxy
224   //    METHOD TYPE : Ctor
225   //
226   ResPoolProxy::ResPoolProxy( ResPool pool_r, const pool::PoolImpl & poolImpl_r )
227   : _pimpl( new Impl( pool_r, poolImpl_r ) )
228   {}
229
230   ///////////////////////////////////////////////////////////////////
231   //
232   //    METHOD NAME : ResPoolProxy::~ResPoolProxy
233   //    METHOD TYPE : Dtor
234   //
235   ResPoolProxy::~ResPoolProxy()
236   {}
237
238   ///////////////////////////////////////////////////////////////////
239   //
240   // forward to implementation
241   //
242   ///////////////////////////////////////////////////////////////////
243
244   ui::Selectable::Ptr ResPoolProxy::lookup( const pool::ByIdent & ident_r ) const
245   { return _pimpl->lookup( ident_r ); }
246
247   bool ResPoolProxy::empty( const ResKind & kind_r ) const
248   { return _pimpl->empty( kind_r ); }
249
250   ResPoolProxy::size_type ResPoolProxy::size( const ResKind & kind_r ) const
251   { return _pimpl->size( kind_r ); }
252
253   ResPoolProxy::const_iterator ResPoolProxy::byKindBegin( const ResKind & kind_r ) const
254   { return _pimpl->byKindBegin( kind_r ); }
255
256   ResPoolProxy::const_iterator ResPoolProxy::byKindEnd( const ResKind & kind_r ) const
257   { return _pimpl->byKindEnd( kind_r ); }
258
259   ResPoolProxy::size_type ResPoolProxy::knownRepositoriesSize() const
260   { return _pimpl->knownRepositoriesSize(); }
261
262   ResPoolProxy::repository_iterator ResPoolProxy::knownRepositoriesBegin() const
263   { return _pimpl->knownRepositoriesBegin(); }
264
265   ResPoolProxy::repository_iterator ResPoolProxy::knownRepositoriesEnd() const
266   { return _pimpl->knownRepositoriesEnd(); }
267
268   void ResPoolProxy::saveState() const
269   { _pimpl->saveState(); }
270
271   void ResPoolProxy::saveState( const ResKind & kind_r ) const
272   { _pimpl->saveState( kind_r ); }
273
274   void ResPoolProxy::restoreState() const
275   { _pimpl->restoreState(); }
276
277   void ResPoolProxy::restoreState( const ResKind & kind_r ) const
278   { _pimpl->restoreState( kind_r ); }
279
280   bool ResPoolProxy::diffState() const
281   { return _pimpl->diffState(); }
282
283   bool ResPoolProxy::diffState( const ResKind & kind_r ) const
284   { return _pimpl->diffState( kind_r ); }
285
286   /******************************************************************
287    **
288    **   FUNCTION NAME : operator<<
289    **   FUNCTION TYPE : std::ostream &
290   */
291   std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj )
292   {
293     return str << *obj._pimpl;
294   }
295
296   /////////////////////////////////////////////////////////////////
297 } // namespace zypp
298 ///////////////////////////////////////////////////////////////////