Discourage using SafeBool in favor of explicit operator bool
[platform/upstream/libzypp.git] / zypp / ui / SelectableImpl.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ui/SelectableImpl.h
10  *
11 */
12 #ifndef ZYPP_UI_SELECTABLEIMPL_H
13 #define ZYPP_UI_SELECTABLEIMPL_H
14
15 #include <iostream>
16 #include "zypp/base/LogTools.h"
17
18 #include "zypp/base/PtrTypes.h"
19
20 #include "zypp/ResPool.h"
21 #include "zypp/Resolver.h"
22 #include "zypp/ui/Selectable.h"
23 #include "zypp/ui/SelectableTraits.h"
24
25 using std::endl;
26
27 ///////////////////////////////////////////////////////////////////
28 namespace zypp
29 { /////////////////////////////////////////////////////////////////
30   ///////////////////////////////////////////////////////////////////
31   namespace ui
32   { /////////////////////////////////////////////////////////////////
33
34     ///////////////////////////////////////////////////////////////////
35     //
36     //  CLASS NAME : Selectable::Impl
37     //
38     /** Selectable implementation.
39      * \note Implementation is based in PoolItem, just the Selectable
40      * inteface restricts them to ResObject::constPtr.
41     */
42     struct Selectable::Impl
43     {
44     public:
45
46       typedef SelectableTraits::AvailableItemSet         AvailableItemSet;
47       typedef SelectableTraits::available_iterator       available_iterator;
48       typedef SelectableTraits::available_const_iterator available_const_iterator;
49       typedef SelectableTraits::available_size_type      available_size_type;
50
51       typedef SelectableTraits::InstalledItemSet         InstalledItemSet;
52       typedef SelectableTraits::installed_iterator       installed_iterator;
53       typedef SelectableTraits::installed_const_iterator installed_const_iterator;
54       typedef SelectableTraits::installed_size_type      installed_size_type;
55
56       typedef SelectableTraits::PickList                PickList;
57
58     public:
59       template <class _Iterator>
60       Impl( const ResObject::Kind & kind_r,
61             const std::string & name_r,
62             _Iterator begin_r,
63             _Iterator end_r )
64       : _ident( sat::Solvable::SplitIdent( kind_r, name_r ).ident() )
65       , _kind( kind_r )
66       , _name( name_r )
67       {
68         for_( it, begin_r, end_r )
69         {
70           if ( it->status().isInstalled() )
71             _installedItems.insert( *it );
72           else
73             _availableItems.insert( *it );
74         }
75       }
76
77     public:
78       /**  */
79       IdString ident() const
80       { return _ident; }
81
82       /**  */
83       ResObject::Kind kind() const
84       { return _kind; }
85
86       /**  */
87       const std::string & name() const
88       { return _name; }
89
90       /**  */
91       Status status() const;
92
93       /**  */
94       bool setStatus( Status state_r, ResStatus::TransactByValue causer_r );
95
96       /** Installed object (transacting ot highest version). */
97       PoolItem installedObj() const
98       {
99         if ( installedEmpty() )
100           return PoolItem();
101         PoolItem ret( transactingInstalled() );
102         return ret ? ret : *_installedItems.begin();
103       }
104
105       /** Best among available objects.
106        * The transacting candidate or the one scheduled to receive
107        * the transact bit.
108       */
109       PoolItem candidateObj() const
110       {
111         PoolItem ret( transactingCandidate() );
112         if ( ret )
113           return ret;
114         return _candidate ? _candidate : defaultCandidate();
115       }
116
117       /** Set a userCandidate (out of available objects).
118        * \return The new userCandidate or NULL if choice was invalid
119        * (not among availableObjs).
120       */
121       PoolItem setCandidate( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r );
122
123       /** The best candidate provided by a specific \ref Repository, if there is one.
124        * In contrary to \ref candidateObj, this may return no item even if
125        * there are available objects. This simply means the \ref Repository
126        * does not provide this object.
127        */
128       PoolItem candidateObjFrom( Repository repo_r ) const
129       {
130         for_( it, availableBegin(), availableEnd() )
131         {
132           if ( (*it)->repository() == repo_r )
133             return *it;
134         }
135         return PoolItem();
136       }
137
138       /** The best candidate for update, if there is one.
139        * In contrary to \ref candidateObj, this may return no item even if
140        * there are available objects. This simply means the best object is
141        * already installed, and all available objects violate at least one
142        * update policy.
143        */
144       PoolItem updateCandidateObj() const
145       {
146         PoolItem defaultCand( defaultCandidate() );
147
148         if ( multiversionInstall() )
149           return identicalInstalled( defaultCand ) ? PoolItem() : defaultCand;
150
151         if ( installedEmpty() || ! defaultCand )
152           return defaultCand;
153         // Here: installed and defaultCand are non NULL and it's not a
154         //       multiversion install.
155
156         // update candidate must come from the highest priority repo
157         if ( defaultCand->repoInfo().priority() != (*availableBegin())->repoInfo().priority() )
158           return PoolItem();
159
160         PoolItem installed( installedObj() );
161         // check vendor change
162         if ( ! ( ResPool::instance().resolver().allowVendorChange()
163                  || VendorAttr::instance().equivalent( defaultCand->vendor(), installed->vendor() ) ) )
164           return PoolItem();
165
166         // check arch change (arch noarch changes are allowed)
167         if ( defaultCand->arch() != installed->arch()
168            && ! ( defaultCand->arch() == Arch_noarch || installed->arch() == Arch_noarch ) )
169           return PoolItem();
170
171         // check greater edition
172         if ( defaultCand->edition() <= installed->edition() )
173           return PoolItem();
174
175         return defaultCand;
176       }
177
178       /** \copydoc Selectable::highestAvailableVersionObj()const */
179       PoolItem highestAvailableVersionObj() const
180       {
181         PoolItem ret;
182         for_( it, availableBegin(), availableEnd() )
183         {
184           if ( !ret || (*it).satSolvable().edition() > ret.satSolvable().edition() )
185             ret = *it;
186         }
187         return ret;
188       }
189
190       /** \copydoc Selectable::identicalAvailable( const PoolItem & )const */
191       bool identicalAvailable( const PoolItem & rhs ) const
192       { return bool(identicalAvailableObj( rhs )); }
193
194       /** \copydoc Selectable::identicalInstalled( const PoolItem & )const */
195       bool identicalInstalled( const PoolItem & rhs ) const
196       { return bool(identicalInstalledObj( rhs )); }
197
198       /** \copydoc Selectable::identicalAvailableObj( const PoolItem & rhs ) const */
199       PoolItem identicalAvailableObj( const PoolItem & rhs ) const
200       {
201         if ( !availableEmpty() && rhs )
202         {
203           for_( it, _availableItems.begin(), _availableItems.end() )
204           {
205             if ( identical( *it, rhs ) )
206               return *it;
207           }
208         }
209         return PoolItem();
210       }
211
212       /** \copydoc Selectable::identicalInstalledObj( const PoolItem & rhs ) const */
213       PoolItem identicalInstalledObj( const PoolItem & rhs ) const
214       {
215         if ( !installedEmpty() && rhs )
216         {
217           for_( it, _installedItems.begin(), _installedItems.end() )
218           {
219             if ( identical( *it, rhs ) )
220               return *it;
221           }
222         }
223         return PoolItem();
224       }
225
226       /** Best among all objects. */
227       PoolItem theObj() const
228       {
229         PoolItem ret( candidateObj() );
230         if ( ret )
231           return ret;
232         return installedObj();
233       }
234
235       ////////////////////////////////////////////////////////////////////////
236
237       bool availableEmpty() const
238       { return _availableItems.empty(); }
239
240       available_size_type availableSize() const
241       { return _availableItems.size(); }
242
243       available_const_iterator availableBegin() const
244       { return _availableItems.begin(); }
245
246       available_const_iterator availableEnd() const
247       { return _availableItems.end(); }
248
249       ////////////////////////////////////////////////////////////////////////
250
251       bool installedEmpty() const
252       { return _installedItems.empty(); }
253
254       installed_size_type installedSize() const
255       { return _installedItems.size(); }
256
257       installed_iterator installedBegin() const
258       { return _installedItems.begin(); }
259
260       installed_iterator installedEnd() const
261       { return _installedItems.end(); }
262
263       ////////////////////////////////////////////////////////////////////////
264
265       const PickList & picklist() const
266       {
267         if ( ! _picklistPtr )
268         {
269           _picklistPtr.reset( new PickList );
270           // installed without identical avaialble first:
271           for_( it, _installedItems.begin(), _installedItems.end() )
272           {
273             if ( ! identicalAvailable( *it ) )
274               _picklistPtr->push_back( *it );
275           }
276           _picklistPtr->insert( _picklistPtr->end(), availableBegin(), availableEnd() );
277         }
278         return *_picklistPtr;
279       }
280
281       bool picklistEmpty() const
282       { return picklist().empty(); }
283
284       picklist_size_type picklistSize() const
285       { return picklist().size(); }
286
287       picklist_iterator picklistBegin() const
288       { return picklist().begin(); }
289
290       picklist_iterator picklistEnd() const
291       { return picklist().end(); }
292
293       ////////////////////////////////////////////////////////////////////////
294
295       bool isUnmaintained() const
296       { return availableEmpty(); }
297
298       bool multiversionInstall() const
299       { return sat::Pool::instance().isMultiversion( ident() ); }
300
301       bool pickInstall( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
302
303       bool pickDelete( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
304
305       Status pickStatus( const PoolItem & pi_r ) const;
306
307       bool setPickStatus( const PoolItem & pi_r, Status state_r, ResStatus::TransactByValue causer_r );
308
309       ////////////////////////////////////////////////////////////////////////
310
311       bool isUndetermined() const
312       {
313         PoolItem cand( candidateObj() );
314         return ! cand || cand.isUndetermined();
315       }
316       bool isRelevant() const
317       {
318         PoolItem cand( candidateObj() );
319         return cand && cand.isRelevant();
320       }
321       bool isSatisfied() const
322        {
323         PoolItem cand( candidateObj() );
324         return cand && cand.isSatisfied();
325       }
326       bool isBroken() const
327       {
328         PoolItem cand( candidateObj() );
329         return cand && cand.isBroken();
330       }
331
332       /** Return who caused the modification. */
333       ResStatus::TransactByValue modifiedBy() const;
334
335       /** Return value of LicenceConfirmed bit. */
336       bool hasLicenceConfirmed() const
337       { return candidateObj() && candidateObj().status().isLicenceConfirmed(); }
338
339       /** Set LicenceConfirmed bit. */
340       void setLicenceConfirmed( bool val_r )
341       { if ( candidateObj() ) candidateObj().status().setLicenceConfirmed( val_r ); }
342
343     private:
344       PoolItem transactingInstalled() const
345       {
346         for_( it, installedBegin(), installedEnd() )
347           {
348             if ( (*it).status().transacts() )
349               return (*it);
350           }
351         return PoolItem();
352       }
353
354       PoolItem transactingCandidate() const
355       {
356         for_( it, availableBegin(), availableEnd() )
357           {
358             if ( (*it).status().transacts() )
359               return (*it);
360           }
361         return PoolItem();
362       }
363
364       PoolItem defaultCandidate() const
365       {
366         if ( ! ( multiversionInstall() || installedEmpty() ) )
367         {
368           // prefer the installed objects arch and vendor
369           bool solver_allowVendorChange( ResPool::instance().resolver().allowVendorChange() );
370           for ( installed_const_iterator iit = installedBegin();
371                 iit != installedEnd(); ++iit )
372           {
373             PoolItem sameArch; // in case there's no same vendor at least stay with same arch.
374             for ( available_const_iterator it = availableBegin();
375                   it != availableEnd(); ++it )
376             {
377               // 'same arch' includes allowed changes to/from noarch.
378               if ( (*iit)->arch() == (*it)->arch() || (*iit)->arch() == Arch_noarch || (*it)->arch() == Arch_noarch )
379               {
380                 if ( ! solver_allowVendorChange )
381                 {
382                   if ( VendorAttr::instance().equivalent( (*iit), (*it) ) )
383                     return *it;
384                   else if ( ! sameArch ) // remember best same arch in case no same vendor found
385                      sameArch = *it;
386                 }
387                 else // same arch is sufficient
388                   return *it;
389               }
390             }
391             if ( sameArch )
392               return sameArch;
393           }
394         }
395         if ( _availableItems.empty() )
396           return PoolItem();
397
398         return *_availableItems.begin();
399       }
400
401       bool allCandidatesLocked() const
402       {
403         for ( available_const_iterator it = availableBegin();
404               it != availableEnd(); ++it )
405           {
406             if ( ! (*it).status().isLocked() )
407               return false;
408           }
409         return( ! _availableItems.empty() );
410       }
411
412       bool allInstalledLocked() const
413       {
414         for ( installed_const_iterator it = installedBegin();
415               it != installedEnd(); ++it )
416           {
417             if ( ! (*it).status().isLocked() )
418               return false;
419           }
420         return( ! _installedItems.empty() );
421       }
422
423
424     private:
425       const IdString         _ident;
426       const ResObject::Kind  _kind;
427       const std::string      _name;
428       InstalledItemSet       _installedItems;
429       AvailableItemSet       _availableItems;
430       //! The object selected by setCandidateObj() method.
431       PoolItem               _candidate;
432       //! lazy initialized picklist
433       mutable scoped_ptr<PickList> _picklistPtr;
434     };
435     ///////////////////////////////////////////////////////////////////
436
437     /** \relates Selectable::Impl Stream output */
438     inline std::ostream & operator<<( std::ostream & str, const Selectable::Impl & obj )
439     {
440       return str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
441                  << " (I " << obj.installedSize() << ")"
442                  << " (A " << obj.availableSize() << ")"
443                  << obj.candidateObj();
444     }
445
446     /** \relates Selectable::Impl Stream output */
447     inline std::ostream & dumpOn( std::ostream & str, const Selectable::Impl & obj )
448     {
449       str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
450           << ( obj.multiversionInstall() ? " (multiversion)" : "") << endl;
451
452       if ( obj.installedEmpty() )
453         str << "   (I 0) {}" << endl << "   ";
454       else
455       {
456         PoolItem icand( obj.installedObj() );
457         str << "   (I " << obj.installedSize() << ") {" << endl;
458         for_( it, obj.installedBegin(), obj.installedEnd() )
459         {
460           char t = ' ';
461           if ( *it == icand )
462           {
463             t = 'i';
464           }
465           str << " " << t << " " << *it << endl;
466         }
467         str << "}  ";
468       }
469
470       if ( obj.availableEmpty() )
471       {
472         str << "(A 0) {}" << endl << "   ";
473       }
474       else
475       {
476         PoolItem cand( obj.candidateObj() );
477         PoolItem up( obj.updateCandidateObj() );
478         str << "(A " << obj.availableSize() << ") {" << endl;
479         for_( it, obj.availableBegin(), obj.availableEnd() )
480         {
481           char t = ' ';
482           if ( *it == cand )
483           {
484             t = *it == up ? 'C' : 'c';
485           }
486           else if ( *it == up )
487           {
488             t = 'u';
489           }
490           str << " " << t << " " << *it << endl;
491         }
492         str << "}  ";
493       }
494
495       if ( obj.picklistEmpty() )
496       {
497         str << "(P 0) {}";
498       }
499       else
500       {
501         PoolItem cand( obj.candidateObj() );
502         PoolItem up( obj.updateCandidateObj() );
503         str << "(P " << obj.picklistSize() << ") {" << endl;
504         for_( it, obj.picklistBegin(), obj.picklistEnd() )
505         {
506           char t = ' ';
507           if ( *it == cand )
508           {
509             t = *it == up ? 'C' : 'c';
510           }
511           else if ( *it == up )
512           {
513             t = 'u';
514           }
515           str << " " << t << " " << *it << "\t" << obj.pickStatus( *it ) << endl;
516         }
517         str << "}  ";
518       }
519
520       return str;
521     }
522     /////////////////////////////////////////////////////////////////
523   } // namespace ui
524   ///////////////////////////////////////////////////////////////////
525   /////////////////////////////////////////////////////////////////
526 } // namespace zypp
527 ///////////////////////////////////////////////////////////////////
528 #endif // ZYPP_UI_SELECTABLEIMPL_H