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 ResObject::Kind & 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
83 ResObject::Kind kind() 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_( it, availableBegin(), availableEnd() )
132 if ( (*it)->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_( it, availableBegin(), availableEnd() )
180 if ( !ret || (*it).satSolvable().edition() > ret.satSolvable().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_const_iterator availableBegin() const
240 { return _availableItems.begin(); }
242 available_const_iterator availableEnd() const
243 { return _availableItems.end(); }
245 ////////////////////////////////////////////////////////////////////////
247 bool installedEmpty() const
248 { return _installedItems.empty(); }
250 installed_size_type installedSize() const
251 { return _installedItems.size(); }
253 installed_iterator installedBegin() const
254 { return _installedItems.begin(); }
256 installed_iterator installedEnd() const
257 { return _installedItems.end(); }
259 ////////////////////////////////////////////////////////////////////////
261 const PickList & picklist() const
263 if ( ! _picklistPtr )
265 _picklistPtr.reset( new PickList );
266 // installed without identical avaialble first:
267 for_( it, _installedItems.begin(), _installedItems.end() )
269 if ( ! identicalAvailable( *it ) )
270 _picklistPtr->push_back( *it );
272 _picklistPtr->insert( _picklistPtr->end(), availableBegin(), availableEnd() );
274 return *_picklistPtr;
277 bool picklistEmpty() const
278 { return picklist().empty(); }
280 picklist_size_type picklistSize() const
281 { return picklist().size(); }
283 picklist_iterator picklistBegin() const
284 { return picklist().begin(); }
286 picklist_iterator picklistEnd() const
287 { return picklist().end(); }
289 ////////////////////////////////////////////////////////////////////////
291 bool isUnmaintained() const
292 { return availableEmpty(); }
294 bool multiversionInstall() const
295 { return sat::Pool::instance().isMultiversion( ident() ); }
297 bool pickInstall( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
299 bool pickDelete( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
301 Status pickStatus( const PoolItem & pi_r ) const;
303 bool setPickStatus( const PoolItem & pi_r, Status state_r, ResStatus::TransactByValue causer_r );
305 ////////////////////////////////////////////////////////////////////////
307 bool isUndetermined() const
309 PoolItem cand( candidateObj() );
310 return ! cand || cand.isUndetermined();
312 bool isRelevant() const
314 PoolItem cand( candidateObj() );
315 return cand && cand.isRelevant();
317 bool isSatisfied() const
319 PoolItem cand( candidateObj() );
320 return cand && cand.isSatisfied();
322 bool isBroken() const
324 PoolItem cand( candidateObj() );
325 return cand && cand.isBroken();
328 /** Return who caused the modification. */
329 ResStatus::TransactByValue modifiedBy() const;
331 /** Return value of LicenceConfirmed bit. */
332 bool hasLicenceConfirmed() const
333 { return candidateObj() && candidateObj().status().isLicenceConfirmed(); }
335 /** Set LicenceConfirmed bit. */
336 void setLicenceConfirmed( bool val_r )
337 { if ( candidateObj() ) candidateObj().status().setLicenceConfirmed( val_r ); }
340 PoolItem transactingInstalled() const
342 for_( it, installedBegin(), installedEnd() )
344 if ( (*it).status().transacts() )
350 PoolItem transactingCandidate() const
352 for_( it, availableBegin(), availableEnd() )
354 if ( (*it).status().transacts() )
360 PoolItem defaultCandidate() const
362 if ( ! installedEmpty() )
364 // prefer the installed objects arch and vendor
365 bool solver_allowVendorChange( ResPool::instance().resolver().allowVendorChange() );
366 for ( installed_const_iterator iit = installedBegin();
367 iit != installedEnd(); ++iit )
369 PoolItem sameArch; // in case there's no same vendor at least stay with same arch.
370 for ( available_const_iterator it = availableBegin();
371 it != availableEnd(); ++it )
373 // 'same arch' includes allowed changes to/from noarch.
374 if ( (*iit)->arch() == (*it)->arch() || (*iit)->arch() == Arch_noarch || (*it)->arch() == Arch_noarch )
376 if ( ! solver_allowVendorChange )
378 if ( VendorAttr::instance().equivalent( (*iit), (*it) ) )
380 else if ( ! sameArch ) // remember best same arch in case no same vendor found
383 else // same arch is sufficient
391 if ( _availableItems.empty() )
394 return *_availableItems.begin();
397 bool allCandidatesLocked() const
399 for ( available_const_iterator it = availableBegin();
400 it != availableEnd(); ++it )
402 if ( ! (*it).status().isLocked() )
405 return( ! _availableItems.empty() );
408 bool allInstalledLocked() const
410 for ( installed_const_iterator it = installedBegin();
411 it != installedEnd(); ++it )
413 if ( ! (*it).status().isLocked() )
416 return( ! _installedItems.empty() );
421 const IdString _ident;
422 const ResObject::Kind _kind;
423 const std::string _name;
424 InstalledItemSet _installedItems;
425 AvailableItemSet _availableItems;
426 //! The object selected by setCandidateObj() method.
428 //! lazy initialized picklist
429 mutable scoped_ptr<PickList> _picklistPtr;
431 ///////////////////////////////////////////////////////////////////
433 /** \relates Selectable::Impl Stream output */
434 inline std::ostream & operator<<( std::ostream & str, const Selectable::Impl & obj )
436 return str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
437 << " (I " << obj.installedSize() << ")"
438 << " (A " << obj.availableSize() << ")"
439 << obj.candidateObj();
442 /** \relates Selectable::Impl Stream output */
443 inline std::ostream & dumpOn( std::ostream & str, const Selectable::Impl & obj )
445 str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
446 << ( obj.multiversionInstall() ? " (multiversion)" : "") << endl;
448 if ( obj.installedEmpty() )
449 str << " (I 0) {}" << endl << " ";
452 PoolItem icand( obj.installedObj() );
453 str << " (I " << obj.installedSize() << ") {" << endl;
454 for_( it, obj.installedBegin(), obj.installedEnd() )
461 str << " " << t << " " << *it << endl;
466 if ( obj.availableEmpty() )
468 str << "(A 0) {}" << endl << " ";
472 PoolItem cand( obj.candidateObj() );
473 PoolItem up( obj.updateCandidateObj() );
474 str << "(A " << obj.availableSize() << ") {" << endl;
475 for_( it, obj.availableBegin(), obj.availableEnd() )
480 t = *it == up ? 'C' : 'c';
482 else if ( *it == up )
486 str << " " << t << " " << *it << endl;
491 if ( obj.picklistEmpty() )
497 PoolItem cand( obj.candidateObj() );
498 PoolItem up( obj.updateCandidateObj() );
499 str << "(P " << obj.picklistSize() << ") {" << endl;
500 for_( it, obj.picklistBegin(), obj.picklistEnd() )
505 t = *it == up ? 'C' : 'c';
507 else if ( *it == up )
511 str << " " << t << " " << *it << "\t" << obj.pickStatus( *it ) << endl;
518 /////////////////////////////////////////////////////////////////
520 ///////////////////////////////////////////////////////////////////
521 /////////////////////////////////////////////////////////////////
523 ///////////////////////////////////////////////////////////////////
524 #endif // ZYPP_UI_SELECTABLEIMPL_H