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 TIterator>
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::identIsAutoInstalled()const */
187 bool identIsAutoInstalled() const
188 { return sat::Solvable::identIsAutoInstalled( ident() ); }
190 /** \copydoc Selectable::identicalAvailable( const PoolItem & )const */
191 bool identicalAvailable( const PoolItem & rhs ) const
192 { return bool(identicalAvailableObj( rhs )); }
194 /** \copydoc Selectable::identicalInstalled( const PoolItem & )const */
195 bool identicalInstalled( const PoolItem & rhs ) const
196 { return bool(identicalInstalledObj( rhs )); }
198 /** \copydoc Selectable::identicalAvailableObj( const PoolItem & rhs ) const */
199 PoolItem identicalAvailableObj( const PoolItem & rhs ) const
201 if ( !availableEmpty() && rhs )
203 for_( it, _availableItems.begin(), _availableItems.end() )
205 if ( identical( *it, rhs ) )
212 /** \copydoc Selectable::identicalInstalledObj( const PoolItem & rhs ) const */
213 PoolItem identicalInstalledObj( const PoolItem & rhs ) const
215 if ( !installedEmpty() && rhs )
217 for_( it, _installedItems.begin(), _installedItems.end() )
219 if ( identical( *it, rhs ) )
226 /** Best among all objects. */
227 PoolItem theObj() const
229 PoolItem ret( candidateObj() );
232 return installedObj();
235 ////////////////////////////////////////////////////////////////////////
237 bool availableEmpty() const
238 { return _availableItems.empty(); }
240 available_size_type availableSize() const
241 { return _availableItems.size(); }
243 available_iterator availableBegin() const
244 { return _availableItems.begin(); }
246 available_iterator availableEnd() const
247 { return _availableItems.end(); }
249 inline Iterable<available_iterator> available() const
250 { return makeIterable( availableBegin(), availableEnd() ); }
252 ////////////////////////////////////////////////////////////////////////
254 bool installedEmpty() const
255 { return _installedItems.empty(); }
257 installed_size_type installedSize() const
258 { return _installedItems.size(); }
260 installed_iterator installedBegin() const
261 { return _installedItems.begin(); }
263 installed_iterator installedEnd() const
264 { return _installedItems.end(); }
266 inline Iterable<installed_iterator> installed() const
267 { return makeIterable( installedBegin(), installedEnd() ); }
269 ////////////////////////////////////////////////////////////////////////
271 const PickList & picklist() const
273 if ( ! _picklistPtr )
275 _picklistPtr.reset( new PickList );
276 // installed without identical avaialble first:
277 for ( const PoolItem & pi : installed() )
279 if ( ! identicalAvailable( pi ) )
280 _picklistPtr->push_back( pi );
282 _picklistPtr->insert( _picklistPtr->end(), availableBegin(), availableEnd() );
284 return *_picklistPtr;
287 bool picklistEmpty() const
288 { return picklist().empty(); }
290 picklist_size_type picklistSize() const
291 { return picklist().size(); }
293 picklist_iterator picklistBegin() const
294 { return picklist().begin(); }
296 picklist_iterator picklistEnd() const
297 { return picklist().end(); }
299 ////////////////////////////////////////////////////////////////////////
301 bool isUnmaintained() const
302 { return availableEmpty(); }
304 bool multiversionInstall() const
306 for ( const PoolItem & pi : picklist() )
308 if ( pi.multiversionInstall() )
314 bool pickInstall( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
316 bool pickDelete( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
318 Status pickStatus( const PoolItem & pi_r ) const;
320 bool setPickStatus( const PoolItem & pi_r, Status state_r, ResStatus::TransactByValue causer_r );
322 ////////////////////////////////////////////////////////////////////////
324 bool isUndetermined() const
326 PoolItem cand( candidateObj() );
327 return ! cand || cand.isUndetermined();
329 bool isRelevant() const
331 PoolItem cand( candidateObj() );
332 return cand && cand.isRelevant();
334 bool isSatisfied() const
336 PoolItem cand( candidateObj() );
337 return cand && cand.isSatisfied();
339 bool isBroken() const
341 PoolItem cand( candidateObj() );
342 return cand && cand.isBroken();
345 /** Return who caused the modification. */
346 ResStatus::TransactByValue modifiedBy() const;
348 /** Return value of LicenceConfirmed bit. */
349 bool hasLicenceConfirmed() const
350 { return candidateObj() && candidateObj().status().isLicenceConfirmed(); }
352 /** Set LicenceConfirmed bit. */
353 void setLicenceConfirmed( bool val_r )
354 { if ( candidateObj() ) candidateObj().status().setLicenceConfirmed( val_r ); }
357 PoolItem transactingInstalled() const
359 for ( const PoolItem & pi : installed() )
361 if ( pi.status().transacts() )
367 PoolItem transactingCandidate() const
369 for ( const PoolItem & pi : available() )
371 if ( pi.status().transacts() )
377 PoolItem defaultCandidate() const
379 if ( ! installedEmpty() )
381 // prefer the installed objects arch and vendor
382 bool solver_allowVendorChange( ResPool::instance().resolver().allowVendorChange() );
383 for ( const PoolItem & ipi : installed() )
385 PoolItem sameArch; // in case there's no same vendor at least stay with same arch.
386 for ( const PoolItem & api : available() )
388 // 'same arch' includes allowed changes to/from noarch.
389 if ( ipi.arch() == api.arch() || ipi.arch() == Arch_noarch || api.arch() == Arch_noarch )
391 if ( ! solver_allowVendorChange )
393 if ( VendorAttr::instance().equivalent( ipi, api ) )
395 else if ( ! sameArch ) // remember best same arch in case no same vendor found
398 else // same arch is sufficient
406 if ( _availableItems.empty() )
409 return *_availableItems.begin();
412 bool allCandidatesLocked() const
414 for ( const PoolItem & pi : available() )
416 if ( ! pi.status().isLocked() )
419 return( ! _availableItems.empty() );
422 bool allInstalledLocked() const
424 for ( const PoolItem & pi : installed() )
426 if ( ! pi.status().isLocked() )
429 return( ! _installedItems.empty() );
434 const IdString _ident;
436 const std::string _name;
437 InstalledItemSet _installedItems;
438 AvailableItemSet _availableItems;
439 //! The object selected by setCandidateObj() method.
441 //! lazy initialized picklist
442 mutable scoped_ptr<PickList> _picklistPtr;
444 ///////////////////////////////////////////////////////////////////
446 /** \relates Selectable::Impl Stream output */
447 inline std::ostream & operator<<( std::ostream & str, const Selectable::Impl & obj )
449 return str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
450 << " (I " << obj.installedSize() << ")"
451 << " (A " << obj.availableSize() << ")"
452 << obj.candidateObj();
455 /** \relates Selectable::Impl Stream output */
456 inline std::ostream & dumpOn( std::ostream & str, const Selectable::Impl & obj )
458 str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
459 << ( obj.multiversionInstall() ? " (multiversion)" : "") << endl;
461 if ( obj.installedEmpty() )
462 str << " (I 0) {}" << endl << " ";
465 PoolItem icand( obj.installedObj() );
466 str << " (I " << obj.installedSize() << ") {" << endl;
467 for ( const PoolItem & pi : obj.installed() )
474 str << " " << t << " " << pi << endl;
479 if ( obj.availableEmpty() )
481 str << "(A 0) {}" << endl << " ";
485 PoolItem cand( obj.candidateObj() );
486 PoolItem up( obj.updateCandidateObj() );
487 str << "(A " << obj.availableSize() << ") {" << endl;
488 for ( const PoolItem & pi : obj.available() )
493 t = pi == up ? 'C' : 'c';
499 str << " " << t << " " << pi << endl;
504 if ( obj.picklistEmpty() )
510 PoolItem cand( obj.candidateObj() );
511 PoolItem up( obj.updateCandidateObj() );
512 str << "(P " << obj.picklistSize() << ") {" << endl;
513 for ( const PoolItem & pi : obj.picklist() )
518 t = pi == up ? 'C' : 'c';
524 str << " " << t << " " << pi << "\t" << obj.pickStatus( pi ) << endl;
531 /////////////////////////////////////////////////////////////////
533 ///////////////////////////////////////////////////////////////////
534 /////////////////////////////////////////////////////////////////
536 ///////////////////////////////////////////////////////////////////
537 #endif // ZYPP_UI_SELECTABLEIMPL_H