- Selectable deserves a cleanup, some consistency.
[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/Logger.h"
14
15 #include "zypp/base/Iterator.h"
16 #include "zypp/base/Algorithm.h"
17 #include "zypp/base/Functional.h"
18
19 #include "zypp/ResPoolProxy.h"
20 #include "zypp/ui/SelectableImpl.h"
21
22 using std::endl;
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27
28   /** Tem. friend of PoolItem */
29   struct PoolItemSaver
30   {
31     void saveState( ResPool pool_r )
32     {
33       std::for_each( pool_r.begin(), pool_r.end(),
34                      std::mem_fun_ref(&PoolItem::saveState) );
35     }
36
37     void saveState( ResPool pool_r, const ResObject::Kind & kind_r )
38     {
39       std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
40                      std::mem_fun_ref(&PoolItem::saveState) );
41     }
42
43     void restoreState( ResPool pool_r )
44     {
45       std::for_each( pool_r.begin(), pool_r.end(),
46                      std::mem_fun_ref(&PoolItem::restoreState) );
47     }
48
49     void restoreState( ResPool pool_r, const ResObject::Kind & kind_r )
50     {
51       std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
52                      std::mem_fun_ref(&PoolItem::restoreState) );
53     }
54
55     bool diffState( ResPool pool_r ) const
56     {
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 );
60     }
61
62     bool diffState( ResPool pool_r, const ResObject::Kind & kind_r ) const
63     {
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 );
67     }
68   };
69
70   struct SelPoolHelper
71   {
72     typedef std::set<PoolItem>         ItemC;
73     struct SelC
74     {
75       void add( PoolItem it )
76       {
77         if ( it.status().isInstalled() )
78           installed.insert( it );
79         else
80           available.insert( it );
81       }
82       ItemC installed;
83       ItemC available;
84     };
85     typedef std::map<std::string,SelC>      NameC;
86     typedef std::map<ResObject::Kind,NameC> KindC;
87
88     KindC _kinds;
89
90     /** collect from a pool */
91     void operator()( PoolItem it )
92     {
93       _kinds[it->kind()][it->name()].add( it );
94     }
95
96
97     ui::Selectable::Ptr buildSelectable( const ResObject::Kind & kind_r,
98                                          const std::string & name_r,
99                                          const ItemC & installed,
100                                          const ItemC & available )
101     {
102       return ui::Selectable::Ptr( new ui::Selectable(
103              ui::Selectable::Impl_Ptr( new ui::Selectable::Impl( kind_r, name_r,
104                                                                  installed.begin(),
105                                                                  installed.end(),
106                                                                  available.begin(),
107                                                                  available.end() ) )
108                                                       ) );
109     }
110
111     /** Build Selectable::Ptr and feed them to some container.
112      * \todo Cleanup typedefs
113     */
114     typedef std::set<ui::Selectable::Ptr>             SelectableIndex;
115     typedef std::map<ResObject::Kind,SelectableIndex> SelectablePool;
116
117     void feed( SelectablePool & _selPool )
118     {
119       for ( KindC::const_iterator kindIt = _kinds.begin(); kindIt != _kinds.end(); ++kindIt )
120         {
121           for ( NameC::const_iterator nameIt = kindIt->second.begin(); nameIt != kindIt->second.end(); ++nameIt )
122             {
123               const ItemC & installed( nameIt->second.installed );
124               const ItemC & available( nameIt->second.available );
125
126               if ( installed.empty() && available.empty() )
127                     continue;
128               else
129                     _selPool[kindIt->first].insert( buildSelectable( kindIt->first, nameIt->first, installed, available ) );
130             }
131         }
132     }
133   };
134
135   ///////////////////////////////////////////////////////////////////
136   //
137   //    CLASS NAME : ResPoolProxy::Impl
138   //
139   /** ResPoolProxy implementation. */
140   struct ResPoolProxy::Impl
141   {
142   public:
143     Impl()
144     :_pool( ResPool::instance() )
145     {}
146
147     Impl( ResPool pool_r )
148     : _pool( pool_r )
149     {
150       SelPoolHelper collect;
151       std::for_each( _pool.begin(), _pool.end(),
152                      functor::functorRef<void,PoolItem>( collect ) );
153       collect.feed( _selPool );
154     }
155
156   public:
157
158     bool empty( const ResObject::Kind & kind_r ) const
159     { return _selPool[kind_r].empty(); }
160
161     size_type size( const ResObject::Kind & kind_r ) const
162     { return _selPool[kind_r].size(); }
163
164     const_iterator byKindBegin( const ResObject::Kind & kind_r ) const
165     { return _selPool[kind_r].begin(); }
166
167     const_iterator byKindEnd( const ResObject::Kind & kind_r ) const
168     { return _selPool[kind_r].end(); }
169
170   public:
171     size_type knownRepositoriesSize() const
172     { return _pool.knownRepositoriesSize(); }
173
174     repository_iterator knownRepositoriesBegin() const
175     { return _pool.knownRepositoriesBegin(); }
176
177     repository_iterator knownRepositoriesEnd() const
178     { return _pool.knownRepositoriesEnd(); }
179
180   public:
181
182     void saveState() const
183     { PoolItemSaver().saveState( _pool ); }
184
185     void saveState( const ResObject::Kind & kind_r ) const
186     { PoolItemSaver().saveState( _pool, kind_r ); }
187
188     void restoreState() const
189     { PoolItemSaver().restoreState( _pool ); }
190
191     void restoreState( const ResObject::Kind & kind_r ) const
192     { PoolItemSaver().restoreState( _pool, kind_r ); }
193
194     bool diffState() const
195     { return PoolItemSaver().diffState( _pool ); }
196
197     bool diffState( const ResObject::Kind & kind_r ) const
198     { return PoolItemSaver().diffState( _pool, kind_r ); }
199
200   private:
201     ResPool _pool;
202     mutable SelectablePool _selPool;
203
204   public:
205     /** Offer default Impl. */
206     static shared_ptr<Impl> nullimpl()
207     {
208       static shared_ptr<Impl> _nullimpl( new Impl );
209       return _nullimpl;
210     }
211   };
212   ///////////////////////////////////////////////////////////////////
213
214   /** \relates ResPoolProxy::Impl Stream output */
215   inline std::ostream & operator<<( std::ostream & str, const ResPoolProxy::Impl & obj )
216   {
217     return str << "ResPoolProxy::Impl";
218   }
219
220   ///////////////////////////////////////////////////////////////////
221   //
222   //    CLASS NAME : ResPoolProxy
223   //
224   ///////////////////////////////////////////////////////////////////
225
226   ///////////////////////////////////////////////////////////////////
227   //
228   //    METHOD NAME : ResPoolProxy::ResPoolProxy
229   //    METHOD TYPE : Ctor
230   //
231   ResPoolProxy::ResPoolProxy()
232   : _pimpl( Impl::nullimpl() )
233   {}
234
235   ///////////////////////////////////////////////////////////////////
236   //
237   //    METHOD NAME : ResPoolProxy::ResPoolProxy
238   //    METHOD TYPE : Ctor
239   //
240   ResPoolProxy::ResPoolProxy( ResPool pool_r )
241   : _pimpl( new Impl( pool_r ) )
242   {}
243
244   ///////////////////////////////////////////////////////////////////
245   //
246   //    METHOD NAME : ResPoolProxy::~ResPoolProxy
247   //    METHOD TYPE : Dtor
248   //
249   ResPoolProxy::~ResPoolProxy()
250   {}
251
252   ///////////////////////////////////////////////////////////////////
253   //
254   // forward to implementation
255   //
256   ///////////////////////////////////////////////////////////////////
257
258   bool ResPoolProxy::empty( const ResObject::Kind & kind_r ) const
259   { return _pimpl->empty( kind_r ); }
260
261   ResPoolProxy::size_type ResPoolProxy::size( const ResObject::Kind & kind_r ) const
262   { return _pimpl->size( kind_r ); }
263
264   ResPoolProxy::const_iterator ResPoolProxy::byKindBegin( const ResObject::Kind & kind_r ) const
265   { return _pimpl->byKindBegin( kind_r ); }
266
267   ResPoolProxy::const_iterator ResPoolProxy::byKindEnd( const ResObject::Kind & kind_r ) const
268   { return _pimpl->byKindEnd( kind_r ); }
269
270   ResPoolProxy::size_type ResPoolProxy::knownRepositoriesSize() const
271   { return _pimpl->knownRepositoriesSize(); }
272
273   ResPoolProxy::repository_iterator ResPoolProxy::knownRepositoriesBegin() const
274   { return _pimpl->knownRepositoriesBegin(); }
275
276   ResPoolProxy::repository_iterator ResPoolProxy::knownRepositoriesEnd() const
277   { return _pimpl->knownRepositoriesEnd(); }
278
279   void ResPoolProxy::saveState() const
280   { _pimpl->saveState(); }
281
282   void ResPoolProxy::saveState( const ResObject::Kind & kind_r ) const
283   { _pimpl->saveState( kind_r ); }
284
285   void ResPoolProxy::restoreState() const
286   { _pimpl->restoreState(); }
287
288   void ResPoolProxy::restoreState( const ResObject::Kind & kind_r ) const
289   { _pimpl->restoreState( kind_r ); }
290
291   bool ResPoolProxy::diffState() const
292   { return _pimpl->diffState(); }
293
294   bool ResPoolProxy::diffState( const ResObject::Kind & kind_r ) const
295   { return _pimpl->diffState( kind_r ); }
296
297   /******************************************************************
298    **
299    **   FUNCTION NAME : operator<<
300    **   FUNCTION TYPE : std::ostream &
301   */
302   std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj )
303   {
304     return str << *obj._pimpl;
305   }
306
307   /////////////////////////////////////////////////////////////////
308 } // namespace zypp
309 ///////////////////////////////////////////////////////////////////