Imported Upstream version 15.18.0
[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 ResKind & 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       ResKind 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 ( const PoolItem & pi : available() )
131         {
132           if ( pi.repository() == repo_r )
133             return pi;
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         // multiversionInstall: This returns the candidate for the last
149         // instance installed. Actually we'd need a list here.
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         PoolItem installed( installedObj() );
157         // check vendor change
158         if ( ! ( ResPool::instance().resolver().allowVendorChange()
159                  || VendorAttr::instance().equivalent( defaultCand->vendor(), installed->vendor() ) ) )
160           return PoolItem();
161
162         // check arch change (arch noarch changes are allowed)
163         if ( defaultCand->arch() != installed->arch()
164            && ! ( defaultCand->arch() == Arch_noarch || installed->arch() == Arch_noarch ) )
165           return PoolItem();
166
167         // check greater edition
168         if ( defaultCand->edition() <= installed->edition() )
169           return PoolItem();
170
171         return defaultCand;
172       }
173
174       /** \copydoc Selectable::highestAvailableVersionObj()const */
175       PoolItem highestAvailableVersionObj() const
176       {
177         PoolItem ret;
178         for ( const PoolItem & pi : available() )
179         {
180           if ( !ret || pi.edition() > ret.edition() )
181             ret = pi;
182         }
183         return ret;
184       }
185
186       /** \copydoc Selectable::identicalAvailable( const PoolItem & )const */
187       bool identicalAvailable( const PoolItem & rhs ) const
188       { return bool(identicalAvailableObj( rhs )); }
189
190       /** \copydoc Selectable::identicalInstalled( const PoolItem & )const */
191       bool identicalInstalled( const PoolItem & rhs ) const
192       { return bool(identicalInstalledObj( rhs )); }
193
194       /** \copydoc Selectable::identicalAvailableObj( const PoolItem & rhs ) const */
195       PoolItem identicalAvailableObj( const PoolItem & rhs ) const
196       {
197         if ( !availableEmpty() && rhs )
198         {
199           for_( it, _availableItems.begin(), _availableItems.end() )
200           {
201             if ( identical( *it, rhs ) )
202               return *it;
203           }
204         }
205         return PoolItem();
206       }
207
208       /** \copydoc Selectable::identicalInstalledObj( const PoolItem & rhs ) const */
209       PoolItem identicalInstalledObj( const PoolItem & rhs ) const
210       {
211         if ( !installedEmpty() && rhs )
212         {
213           for_( it, _installedItems.begin(), _installedItems.end() )
214           {
215             if ( identical( *it, rhs ) )
216               return *it;
217           }
218         }
219         return PoolItem();
220       }
221
222       /** Best among all objects. */
223       PoolItem theObj() const
224       {
225         PoolItem ret( candidateObj() );
226         if ( ret )
227           return ret;
228         return installedObj();
229       }
230
231       ////////////////////////////////////////////////////////////////////////
232
233       bool availableEmpty() const
234       { return _availableItems.empty(); }
235
236       available_size_type availableSize() const
237       { return _availableItems.size(); }
238
239       available_iterator availableBegin() const
240       { return _availableItems.begin(); }
241
242       available_iterator availableEnd() const
243       { return _availableItems.end(); }
244
245       inline Iterable<available_iterator>  available() const
246       { return makeIterable( availableBegin(), availableEnd() ); }
247
248       ////////////////////////////////////////////////////////////////////////
249
250       bool installedEmpty() const
251       { return _installedItems.empty(); }
252
253       installed_size_type installedSize() const
254       { return _installedItems.size(); }
255
256       installed_iterator installedBegin() const
257       { return _installedItems.begin(); }
258
259       installed_iterator installedEnd() const
260       { return _installedItems.end(); }
261
262       inline Iterable<installed_iterator>  installed() const
263       { return makeIterable( installedBegin(), installedEnd() ); }
264
265       ////////////////////////////////////////////////////////////////////////
266
267       const PickList & picklist() const
268       {
269         if ( ! _picklistPtr )
270         {
271           _picklistPtr.reset( new PickList );
272           // installed without identical avaialble first:
273           for ( const PoolItem & pi : installed() )
274           {
275             if ( ! identicalAvailable( pi ) )
276               _picklistPtr->push_back( pi );
277           }
278           _picklistPtr->insert( _picklistPtr->end(), availableBegin(), availableEnd() );
279         }
280         return *_picklistPtr;
281       }
282
283       bool picklistEmpty() const
284       { return picklist().empty(); }
285
286       picklist_size_type picklistSize() const
287       { return picklist().size(); }
288
289       picklist_iterator picklistBegin() const
290       { return picklist().begin(); }
291
292       picklist_iterator picklistEnd() const
293       { return picklist().end(); }
294
295       ////////////////////////////////////////////////////////////////////////
296
297       bool isUnmaintained() const
298       { return availableEmpty(); }
299
300       bool multiversionInstall() const
301       {
302         for ( const PoolItem & pi : picklist() )
303         {
304           if ( pi.multiversionInstall() )
305             return true;
306         }
307         return false;
308       }
309
310       bool pickInstall( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
311
312       bool pickDelete( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
313
314       Status pickStatus( const PoolItem & pi_r ) const;
315
316       bool setPickStatus( const PoolItem & pi_r, Status state_r, ResStatus::TransactByValue causer_r );
317
318       ////////////////////////////////////////////////////////////////////////
319
320       bool isUndetermined() const
321       {
322         PoolItem cand( candidateObj() );
323         return ! cand || cand.isUndetermined();
324       }
325       bool isRelevant() const
326       {
327         PoolItem cand( candidateObj() );
328         return cand && cand.isRelevant();
329       }
330       bool isSatisfied() const
331        {
332         PoolItem cand( candidateObj() );
333         return cand && cand.isSatisfied();
334       }
335       bool isBroken() const
336       {
337         PoolItem cand( candidateObj() );
338         return cand && cand.isBroken();
339       }
340
341       /** Return who caused the modification. */
342       ResStatus::TransactByValue modifiedBy() const;
343
344       /** Return value of LicenceConfirmed bit. */
345       bool hasLicenceConfirmed() const
346       { return candidateObj() && candidateObj().status().isLicenceConfirmed(); }
347
348       /** Set LicenceConfirmed bit. */
349       void setLicenceConfirmed( bool val_r )
350       { if ( candidateObj() ) candidateObj().status().setLicenceConfirmed( val_r ); }
351
352     private:
353       PoolItem transactingInstalled() const
354       {
355         for ( const PoolItem & pi : installed() )
356           {
357             if ( pi.status().transacts() )
358               return pi;
359           }
360         return PoolItem();
361       }
362
363       PoolItem transactingCandidate() const
364       {
365         for ( const PoolItem & pi : available() )
366           {
367             if ( pi.status().transacts() )
368               return pi;
369           }
370         return PoolItem();
371       }
372
373       PoolItem defaultCandidate() const
374       {
375         if ( ! installedEmpty() )
376         {
377           // prefer the installed objects arch and vendor
378           bool solver_allowVendorChange( ResPool::instance().resolver().allowVendorChange() );
379           for ( const PoolItem & ipi : installed() )
380           {
381             PoolItem sameArch; // in case there's no same vendor at least stay with same arch.
382             for (  const PoolItem & api : available() )
383             {
384               // 'same arch' includes allowed changes to/from noarch.
385               if ( ipi.arch() == api.arch() || ipi.arch() == Arch_noarch || api.arch() == Arch_noarch )
386               {
387                 if ( ! solver_allowVendorChange )
388                 {
389                   if ( VendorAttr::instance().equivalent( ipi, api ) )
390                     return api;
391                   else if ( ! sameArch ) // remember best same arch in case no same vendor found
392                      sameArch = api;
393                 }
394                 else // same arch is sufficient
395                   return api;
396               }
397             }
398             if ( sameArch )
399               return sameArch;
400           }
401         }
402         if ( _availableItems.empty() )
403           return PoolItem();
404
405         return *_availableItems.begin();
406       }
407
408       bool allCandidatesLocked() const
409       {
410         for ( const PoolItem & pi : available() )
411           {
412             if ( ! pi.status().isLocked() )
413               return false;
414           }
415         return( ! _availableItems.empty() );
416       }
417
418       bool allInstalledLocked() const
419       {
420         for ( const PoolItem & pi : installed() )
421           {
422             if ( ! pi.status().isLocked() )
423               return false;
424           }
425         return( ! _installedItems.empty() );
426       }
427
428
429     private:
430       const IdString         _ident;
431       const ResKind          _kind;
432       const std::string      _name;
433       InstalledItemSet       _installedItems;
434       AvailableItemSet       _availableItems;
435       //! The object selected by setCandidateObj() method.
436       PoolItem               _candidate;
437       //! lazy initialized picklist
438       mutable scoped_ptr<PickList> _picklistPtr;
439     };
440     ///////////////////////////////////////////////////////////////////
441
442     /** \relates Selectable::Impl Stream output */
443     inline std::ostream & operator<<( std::ostream & str, const Selectable::Impl & obj )
444     {
445       return str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
446                  << " (I " << obj.installedSize() << ")"
447                  << " (A " << obj.availableSize() << ")"
448                  << obj.candidateObj();
449     }
450
451     /** \relates Selectable::Impl Stream output */
452     inline std::ostream & dumpOn( std::ostream & str, const Selectable::Impl & obj )
453     {
454       str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
455           << ( obj.multiversionInstall() ? " (multiversion)" : "") << endl;
456
457       if ( obj.installedEmpty() )
458         str << "   (I 0) {}" << endl << "   ";
459       else
460       {
461         PoolItem icand( obj.installedObj() );
462         str << "   (I " << obj.installedSize() << ") {" << endl;
463         for ( const PoolItem & pi : obj.installed() )
464         {
465           char t = ' ';
466           if ( pi == icand )
467           {
468             t = 'i';
469           }
470           str << " " << t << " " << pi << endl;
471         }
472         str << "}  ";
473       }
474
475       if ( obj.availableEmpty() )
476       {
477         str << "(A 0) {}" << endl << "   ";
478       }
479       else
480       {
481         PoolItem cand( obj.candidateObj() );
482         PoolItem up( obj.updateCandidateObj() );
483         str << "(A " << obj.availableSize() << ") {" << endl;
484         for ( const PoolItem & pi : obj.available() )
485         {
486           char t = ' ';
487           if ( pi == cand )
488           {
489             t = pi == up ? 'C' : 'c';
490           }
491           else if ( pi == up )
492           {
493             t = 'u';
494           }
495           str << " " << t << " " << pi << endl;
496         }
497         str << "}  ";
498       }
499
500       if ( obj.picklistEmpty() )
501       {
502         str << "(P 0) {}";
503       }
504       else
505       {
506         PoolItem cand( obj.candidateObj() );
507         PoolItem up( obj.updateCandidateObj() );
508         str << "(P " << obj.picklistSize() << ") {" << endl;
509         for ( const PoolItem & pi : obj.picklist() )
510         {
511           char t = ' ';
512           if ( pi == cand )
513           {
514             t = pi == up ? 'C' : 'c';
515           }
516           else if ( pi == up )
517           {
518             t = 'u';
519           }
520           str << " " << t << " " << pi << "\t" << obj.pickStatus( pi ) << endl;
521         }
522         str << "}  ";
523       }
524
525       return str;
526     }
527     /////////////////////////////////////////////////////////////////
528   } // namespace ui
529   ///////////////////////////////////////////////////////////////////
530   /////////////////////////////////////////////////////////////////
531 } // namespace zypp
532 ///////////////////////////////////////////////////////////////////
533 #endif // ZYPP_UI_SELECTABLEIMPL_H