Merged revisions 4705-4906 via svnmerge from
[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_Ref 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_Ref 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_Ref 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_Ref 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_Ref 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_Ref 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<ResPool::Item>         ItemC;
73     struct SelC
74     {
75       void add( ResPool::Item 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()( ResPool::Item 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 PoolItem & installedItem_r,
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                                                                  installedItem_r,
105                                                                  available.begin(),
106                                                                  available.end() ) )
107                                                       ) );
108     }
109
110     /** Build Selectable::Ptr and feed them to some container.
111      * \todo Cleanup typedefs
112     */
113     typedef std::set<ui::Selectable::Ptr>             SelectableIndex;
114     typedef std::map<ResObject::Kind,SelectableIndex> SelectablePool;
115
116     void feed( SelectablePool & _selPool )
117     {
118       for ( KindC::const_iterator kindIt = _kinds.begin(); kindIt != _kinds.end(); ++kindIt )
119         {
120           for ( NameC::const_iterator nameIt = kindIt->second.begin(); nameIt != kindIt->second.end(); ++nameIt )
121             {
122               const ItemC & installed( nameIt->second.installed );
123               const ItemC & available( nameIt->second.available );
124
125               if ( installed.empty() )
126                 {
127                   if ( available.empty() )
128                     continue;
129                   _selPool[kindIt->first].insert( buildSelectable( kindIt->first, nameIt->first, PoolItem(), available ) );
130                 }
131               else
132                 {
133                   // ui want's one Selectable per installed item
134                   for ( ItemC::const_iterator instIt = installed.begin(); instIt != installed.end(); ++instIt )
135                     {
136                       _selPool[kindIt->first].insert( buildSelectable( kindIt->first, nameIt->first, *instIt, available ) );
137                     }
138                 }
139             }
140         }
141     }
142   };
143
144   ///////////////////////////////////////////////////////////////////
145   //
146   //    CLASS NAME : ResPoolProxy::Impl
147   //
148   /** ResPoolProxy implementation. */
149   struct ResPoolProxy::Impl
150   {
151   public:
152     Impl()
153     {}
154
155     Impl( ResPool_Ref pool_r )
156     : _pool( pool_r )
157     {
158
159       SelPoolHelper collect;
160       std::for_each( _pool.begin(), _pool.end(),
161                      functor::functorRef<void,ResPool::Item>( collect ) );
162       collect.feed( _selPool );
163     }
164
165   public:
166
167     bool empty( const ResObject::Kind & kind_r ) const
168     { return _selPool[kind_r].empty(); }
169
170     size_type size( const ResObject::Kind & kind_r ) const
171     { return _selPool[kind_r].size(); }
172
173     const_iterator byKindBegin( const ResObject::Kind & kind_r ) const
174     { return _selPool[kind_r].begin(); }
175
176     const_iterator byKindEnd( const ResObject::Kind & kind_r ) const
177     { return _selPool[kind_r].end(); }
178
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_Ref _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_Ref 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   void ResPoolProxy::saveState() const
271   { _pimpl->saveState(); }
272
273   void ResPoolProxy::saveState( const ResObject::Kind & kind_r ) const
274   { _pimpl->saveState( kind_r ); }
275
276   void ResPoolProxy::restoreState() const
277   { _pimpl->restoreState(); }
278
279   void ResPoolProxy::restoreState( const ResObject::Kind & kind_r ) const
280   { _pimpl->restoreState( kind_r ); }
281
282   bool ResPoolProxy::diffState() const
283   { return _pimpl->diffState(); }
284
285   bool ResPoolProxy::diffState( const ResObject::Kind & kind_r ) const
286   { return _pimpl->diffState( kind_r ); }
287
288   /******************************************************************
289    **
290    **   FUNCTION NAME : operator<<
291    **   FUNCTION TYPE : std::ostream &
292   */
293   std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj )
294   {
295     return str << *obj._pimpl;
296   }
297
298   /////////////////////////////////////////////////////////////////
299 } // namespace zypp
300 ///////////////////////////////////////////////////////////////////