1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ui/SelectableImpl.h
12 #ifndef ZYPP_UI_SELECTABLEIMPL_H
13 #define ZYPP_UI_SELECTABLEIMPL_H
16 #include "zypp/base/LogTools.h"
18 #include "zypp/base/PtrTypes.h"
20 #include "zypp/ResPool.h"
21 #include "zypp/Resolver.h"
22 #include "zypp/ui/Selectable.h"
23 #include "zypp/ui/SelectableTraits.h"
27 ///////////////////////////////////////////////////////////////////
29 { /////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////
32 { /////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////
36 // CLASS NAME : Selectable::Impl
38 /** Selectable implementation.
39 * \note Implementation is based in PoolItem, just the Selectable
40 * inteface restricts them to ResObject::constPtr.
42 struct Selectable::Impl
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;
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;
56 typedef SelectableTraits::PickList PickList;
59 template <class _Iterator>
60 Impl( const ResKind & kind_r,
61 const std::string & name_r,
64 : _ident( sat::Solvable::SplitIdent( kind_r, name_r ).ident() )
68 for_( it, begin_r, end_r )
70 if ( it->status().isInstalled() )
71 _installedItems.insert( *it );
73 _availableItems.insert( *it );
79 IdString ident() const
87 const std::string & name() const
91 Status status() const;
94 bool setStatus( Status state_r, ResStatus::TransactByValue causer_r );
96 /** Installed object (transacting ot highest version). */
97 PoolItem installedObj() const
99 if ( installedEmpty() )
101 PoolItem ret( transactingInstalled() );
102 return ret ? ret : *_installedItems.begin();
105 /** Best among available objects.
106 * The transacting candidate or the one scheduled to receive
109 PoolItem candidateObj() const
111 PoolItem ret( transactingCandidate() );
114 return _candidate ? _candidate : defaultCandidate();
117 /** Set a userCandidate (out of available objects).
118 * \return The new userCandidate or NULL if choice was invalid
119 * (not among availableObjs).
121 PoolItem setCandidate( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r );
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.
128 PoolItem candidateObjFrom( Repository repo_r ) const
130 for ( const PoolItem & pi : available() )
132 if ( pi.repository() == repo_r )
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
144 PoolItem updateCandidateObj() const
146 PoolItem defaultCand( defaultCandidate() );
148 // multiversionInstall: This returns the candidate for the last
149 // instance installed. Actually we'd need a list here.
151 if ( installedEmpty() || ! defaultCand )
153 // Here: installed and defaultCand are non NULL and it's not a
154 // multiversion install.
156 PoolItem installed( installedObj() );
157 // check vendor change
158 if ( ! ( ResPool::instance().resolver().allowVendorChange()
159 || VendorAttr::instance().equivalent( defaultCand->vendor(), installed->vendor() ) ) )
162 // check arch change (arch noarch changes are allowed)
163 if ( defaultCand->arch() != installed->arch()
164 && ! ( defaultCand->arch() == Arch_noarch || installed->arch() == Arch_noarch ) )
167 // check greater edition
168 if ( defaultCand->edition() <= installed->edition() )
174 /** \copydoc Selectable::highestAvailableVersionObj()const */
175 PoolItem highestAvailableVersionObj() const
178 for ( const PoolItem & pi : available() )
180 if ( !ret || pi.edition() > ret.edition() )
186 /** \copydoc Selectable::identicalAvailable( const PoolItem & )const */
187 bool identicalAvailable( const PoolItem & rhs ) const
188 { return bool(identicalAvailableObj( rhs )); }
190 /** \copydoc Selectable::identicalInstalled( const PoolItem & )const */
191 bool identicalInstalled( const PoolItem & rhs ) const
192 { return bool(identicalInstalledObj( rhs )); }
194 /** \copydoc Selectable::identicalAvailableObj( const PoolItem & rhs ) const */
195 PoolItem identicalAvailableObj( const PoolItem & rhs ) const
197 if ( !availableEmpty() && rhs )
199 for_( it, _availableItems.begin(), _availableItems.end() )
201 if ( identical( *it, rhs ) )
208 /** \copydoc Selectable::identicalInstalledObj( const PoolItem & rhs ) const */
209 PoolItem identicalInstalledObj( const PoolItem & rhs ) const
211 if ( !installedEmpty() && rhs )
213 for_( it, _installedItems.begin(), _installedItems.end() )
215 if ( identical( *it, rhs ) )
222 /** Best among all objects. */
223 PoolItem theObj() const
225 PoolItem ret( candidateObj() );
228 return installedObj();
231 ////////////////////////////////////////////////////////////////////////
233 bool availableEmpty() const
234 { return _availableItems.empty(); }
236 available_size_type availableSize() const
237 { return _availableItems.size(); }
239 available_iterator availableBegin() const
240 { return _availableItems.begin(); }
242 available_iterator availableEnd() const
243 { return _availableItems.end(); }
245 inline Iterable<available_iterator> available() const
246 { return makeIterable( availableBegin(), availableEnd() ); }
248 ////////////////////////////////////////////////////////////////////////
250 bool installedEmpty() const
251 { return _installedItems.empty(); }
253 installed_size_type installedSize() const
254 { return _installedItems.size(); }
256 installed_iterator installedBegin() const
257 { return _installedItems.begin(); }
259 installed_iterator installedEnd() const
260 { return _installedItems.end(); }
262 inline Iterable<installed_iterator> installed() const
263 { return makeIterable( installedBegin(), installedEnd() ); }
265 ////////////////////////////////////////////////////////////////////////
267 const PickList & picklist() const
269 if ( ! _picklistPtr )
271 _picklistPtr.reset( new PickList );
272 // installed without identical avaialble first:
273 for ( const PoolItem & pi : installed() )
275 if ( ! identicalAvailable( pi ) )
276 _picklistPtr->push_back( pi );
278 _picklistPtr->insert( _picklistPtr->end(), availableBegin(), availableEnd() );
280 return *_picklistPtr;
283 bool picklistEmpty() const
284 { return picklist().empty(); }
286 picklist_size_type picklistSize() const
287 { return picklist().size(); }
289 picklist_iterator picklistBegin() const
290 { return picklist().begin(); }
292 picklist_iterator picklistEnd() const
293 { return picklist().end(); }
295 ////////////////////////////////////////////////////////////////////////
297 bool isUnmaintained() const
298 { return availableEmpty(); }
300 bool multiversionInstall() const
302 for ( const PoolItem & pi : picklist() )
304 if ( pi.multiversionInstall() )
310 bool pickInstall( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
312 bool pickDelete( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
314 Status pickStatus( const PoolItem & pi_r ) const;
316 bool setPickStatus( const PoolItem & pi_r, Status state_r, ResStatus::TransactByValue causer_r );
318 ////////////////////////////////////////////////////////////////////////
320 bool isUndetermined() const
322 PoolItem cand( candidateObj() );
323 return ! cand || cand.isUndetermined();
325 bool isRelevant() const
327 PoolItem cand( candidateObj() );
328 return cand && cand.isRelevant();
330 bool isSatisfied() const
332 PoolItem cand( candidateObj() );
333 return cand && cand.isSatisfied();
335 bool isBroken() const
337 PoolItem cand( candidateObj() );
338 return cand && cand.isBroken();
341 /** Return who caused the modification. */
342 ResStatus::TransactByValue modifiedBy() const;
344 /** Return value of LicenceConfirmed bit. */
345 bool hasLicenceConfirmed() const
346 { return candidateObj() && candidateObj().status().isLicenceConfirmed(); }
348 /** Set LicenceConfirmed bit. */
349 void setLicenceConfirmed( bool val_r )
350 { if ( candidateObj() ) candidateObj().status().setLicenceConfirmed( val_r ); }
353 PoolItem transactingInstalled() const
355 for ( const PoolItem & pi : installed() )
357 if ( pi.status().transacts() )
363 PoolItem transactingCandidate() const
365 for ( const PoolItem & pi : available() )
367 if ( pi.status().transacts() )
373 PoolItem defaultCandidate() const
375 if ( ! installedEmpty() )
377 // prefer the installed objects arch and vendor
378 bool solver_allowVendorChange( ResPool::instance().resolver().allowVendorChange() );
379 for ( const PoolItem & ipi : installed() )
381 PoolItem sameArch; // in case there's no same vendor at least stay with same arch.
382 for ( const PoolItem & api : available() )
384 // 'same arch' includes allowed changes to/from noarch.
385 if ( ipi.arch() == api.arch() || ipi.arch() == Arch_noarch || api.arch() == Arch_noarch )
387 if ( ! solver_allowVendorChange )
389 if ( VendorAttr::instance().equivalent( ipi, api ) )
391 else if ( ! sameArch ) // remember best same arch in case no same vendor found
394 else // same arch is sufficient
402 if ( _availableItems.empty() )
405 return *_availableItems.begin();
408 bool allCandidatesLocked() const
410 for ( const PoolItem & pi : available() )
412 if ( ! pi.status().isLocked() )
415 return( ! _availableItems.empty() );
418 bool allInstalledLocked() const
420 for ( const PoolItem & pi : installed() )
422 if ( ! pi.status().isLocked() )
425 return( ! _installedItems.empty() );
430 const IdString _ident;
432 const std::string _name;
433 InstalledItemSet _installedItems;
434 AvailableItemSet _availableItems;
435 //! The object selected by setCandidateObj() method.
437 //! lazy initialized picklist
438 mutable scoped_ptr<PickList> _picklistPtr;
440 ///////////////////////////////////////////////////////////////////
442 /** \relates Selectable::Impl Stream output */
443 inline std::ostream & operator<<( std::ostream & str, const Selectable::Impl & obj )
445 return str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
446 << " (I " << obj.installedSize() << ")"
447 << " (A " << obj.availableSize() << ")"
448 << obj.candidateObj();
451 /** \relates Selectable::Impl Stream output */
452 inline std::ostream & dumpOn( std::ostream & str, const Selectable::Impl & obj )
454 str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
455 << ( obj.multiversionInstall() ? " (multiversion)" : "") << endl;
457 if ( obj.installedEmpty() )
458 str << " (I 0) {}" << endl << " ";
461 PoolItem icand( obj.installedObj() );
462 str << " (I " << obj.installedSize() << ") {" << endl;
463 for ( const PoolItem & pi : obj.installed() )
470 str << " " << t << " " << pi << endl;
475 if ( obj.availableEmpty() )
477 str << "(A 0) {}" << endl << " ";
481 PoolItem cand( obj.candidateObj() );
482 PoolItem up( obj.updateCandidateObj() );
483 str << "(A " << obj.availableSize() << ") {" << endl;
484 for ( const PoolItem & pi : obj.available() )
489 t = pi == up ? 'C' : 'c';
495 str << " " << t << " " << pi << endl;
500 if ( obj.picklistEmpty() )
506 PoolItem cand( obj.candidateObj() );
507 PoolItem up( obj.updateCandidateObj() );
508 str << "(P " << obj.picklistSize() << ") {" << endl;
509 for ( const PoolItem & pi : obj.picklist() )
514 t = pi == up ? 'C' : 'c';
520 str << " " << t << " " << pi << "\t" << obj.pickStatus( pi ) << endl;
527 /////////////////////////////////////////////////////////////////
529 ///////////////////////////////////////////////////////////////////
530 /////////////////////////////////////////////////////////////////
532 ///////////////////////////////////////////////////////////////////
533 #endif // ZYPP_UI_SELECTABLEIMPL_H