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/ZConfig.h"
21 #include "zypp/ui/Selectable.h"
22 #include "zypp/ui/SelectableTraits.h"
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////
35 // CLASS NAME : Selectable::Impl
37 /** Selectable implementation.
38 * \note Implementation is based in PoolItem, just the Selectable
39 * inteface restricts them to ResObject::constPtr.
41 struct Selectable::Impl
45 typedef SelectableTraits::AvailableItemSet AvailableItemSet;
46 typedef SelectableTraits::available_iterator available_iterator;
47 typedef SelectableTraits::available_const_iterator available_const_iterator;
48 typedef SelectableTraits::available_size_type available_size_type;
50 typedef SelectableTraits::InstalledItemSet InstalledItemSet;
51 typedef SelectableTraits::installed_iterator installed_iterator;
52 typedef SelectableTraits::installed_const_iterator installed_const_iterator;
53 typedef SelectableTraits::installed_size_type installed_size_type;
56 template <class _Iterator>
57 Impl( const ResObject::Kind & kind_r,
58 const std::string & name_r,
61 : _ident( sat::Solvable::SplitIdent( kind_r, name_r ).ident() )
65 for_( it, begin_r, end_r )
67 if ( it->status().isInstalled() )
68 _installedItems.insert( *it );
70 _availableItems.insert( *it );
72 _defaultCandidate = defaultCandidate();
77 IdString ident() const
81 ResObject::Kind kind() const
85 const std::string & name() const
89 Status status() const;
92 bool setStatus( const Status state_r, ResStatus::TransactByValue causer_r );
94 /** Installed object (transacting ot highest version). */
95 PoolItem installedObj() const
97 if ( installedEmpty() )
99 PoolItem ret( transactingInstalled() );
100 return ret ? ret : *_installedItems.begin();
103 /** Best among available objects.
104 * The transacting candidate or the one scheduled to receive
107 PoolItem candidateObj() const
109 PoolItem ret( transactingCandidate() );
112 return _candidate ? _candidate : _defaultCandidate;
115 /** Set a userCandidate (out of available objects).
116 * \return The new userCandidate or NULL if choice was invalid
117 * (not among availableObjs).
119 PoolItem setCandidate( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r );
121 /** The best candidate provided by a specific \ref Repository, if there is one.
122 * In contrary to \ref candidateObj, this may return no item even if
123 * there are available objects. This simply means the \ref Repository
124 * does not provide this object.
126 PoolItem candidateObjFrom( Repository repo_r ) const
128 for_( it, availableBegin(), availableEnd() )
130 if ( (*it)->repository() == repo_r )
136 /** The best candidate for update, if there is one.
137 * In contrary to \ref candidateObj, this may return no item even if
138 * there are available objects. This simply means the best object is
139 * already installed, and all available objects violate at least one
142 PoolItem updateCandidateObj() const
144 if ( multiversionInstall() || installedEmpty() || ! _defaultCandidate )
145 return _defaultCandidate;
146 // Here: installed and _defaultCandidate are non NULL and it's not a
147 // multiversion install.
149 // update candidate must come from the highest priority repo
150 if ( _defaultCandidate->repoInfo().priority() != (*availableBegin())->repoInfo().priority() )
153 PoolItem installed( installedObj() );
154 // check vendor change
155 if ( ! ( ZConfig::instance().solver_allowVendorChange()
156 || VendorAttr::instance().equivalent( _defaultCandidate->vendor(), installed->vendor() ) ) )
159 // check arch change (arch noarch changes are allowed)
160 if ( _defaultCandidate->arch() != installed->arch()
161 && ! ( _defaultCandidate->arch() == Arch_noarch || installed->arch() == Arch_noarch ) )
164 // check greater edition
165 if ( _defaultCandidate->edition() <= installed->edition() )
168 return _defaultCandidate;
171 /** \copydoc Selectable::highestAvailableVersionObj() */
172 PoolItem highestAvailableVersionObj() const
175 for_( it, availableBegin(), availableEnd() )
177 if ( !ret || (*it).satSolvable().edition() > ret.satSolvable().edition() )
183 /** \c True if \a rhs has the same content as an installed one.
184 * \see \ref sat::Solvable::identical
186 bool identicalInstalled( const PoolItem & rhs ) const
188 if ( !installedEmpty() && rhs )
190 for_( it, _installedItems.begin(), _installedItems.end() )
192 if ( identical( *it, rhs ) )
199 /** Best among all objects. */
200 PoolItem theObj() const
202 PoolItem ret( candidateObj() );
205 return installedObj();
208 ////////////////////////////////////////////////////////////////////////
210 bool availableEmpty() const
211 { return _availableItems.empty(); }
213 available_size_type availableSize() const
214 { return _availableItems.size(); }
216 available_const_iterator availableBegin() const
217 { return _availableItems.begin(); }
219 available_const_iterator availableEnd() const
220 { return _availableItems.end(); }
222 ////////////////////////////////////////////////////////////////////////
224 bool installedEmpty() const
225 { return _installedItems.empty(); }
227 installed_size_type installedSize() const
228 { return _installedItems.size(); }
230 installed_iterator installedBegin() const
231 { return _installedItems.begin(); }
233 installed_iterator installedEnd() const
234 { return _installedItems.end(); }
236 ////////////////////////////////////////////////////////////////////////
238 bool isUnmaintained() const
239 { return availableEmpty(); }
241 bool multiversionInstall() const
242 { return theObj().satSolvable().multiversionInstall(); }
244 bool pickInstall( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
246 bool pickDelete( const PoolItem & pi_r, ResStatus::TransactByValue causer_r, bool yesno_r );
248 ////////////////////////////////////////////////////////////////////////
250 bool isUndetermined() const
252 PoolItem cand( candidateObj() );
253 return ! cand || cand.isUndetermined();
255 bool isRelevant() const
257 PoolItem cand( candidateObj() );
258 return cand && cand.isRelevant();
260 bool isSatisfied() const
262 PoolItem cand( candidateObj() );
263 return cand && cand.isSatisfied();
265 bool isBroken() const
267 PoolItem cand( candidateObj() );
268 return cand && cand.isBroken();
271 /** Return who caused the modification. */
272 ResStatus::TransactByValue modifiedBy() const;
274 /** Return value of LicenceConfirmed bit. */
275 bool hasLicenceConfirmed() const
276 { return candidateObj() && candidateObj().status().isLicenceConfirmed(); }
278 /** Set LicenceConfirmed bit. */
279 void setLicenceConfirmed( bool val_r )
280 { if ( candidateObj() ) candidateObj().status().setLicenceConfirmed( val_r ); }
283 PoolItem transactingInstalled() const
285 for_( it, installedBegin(), installedEnd() )
287 if ( (*it).status().transacts() )
293 PoolItem transactingCandidate() const
295 for_( it, availableBegin(), availableEnd() )
297 if ( (*it).status().transacts() )
303 PoolItem defaultCandidate() const
305 if ( ! ( multiversionInstall() || installedEmpty() ) )
307 // prefer the installed objects arch and vendor
308 bool solver_allowVendorChange( ZConfig::instance().solver_allowVendorChange() );
309 for ( installed_const_iterator iit = installedBegin();
310 iit != installedEnd(); ++iit )
312 PoolItem sameArch; // in case there's no same vendor at least stay with same arch.
313 for ( available_const_iterator it = availableBegin();
314 it != availableEnd(); ++it )
316 // 'same arch' includes allowed changes to/from noarch.
317 if ( (*iit)->arch() == (*it)->arch() || (*iit)->arch() == Arch_noarch || (*it)->arch() == Arch_noarch )
319 if ( ! solver_allowVendorChange )
321 if ( VendorAttr::instance().equivalent( (*iit), (*it) ) )
323 else if ( ! sameArch ) // remember best same arch in case no same vendor found
326 else // same arch is sufficient
334 if ( _availableItems.empty() )
337 return *_availableItems.begin();
340 bool allCandidatesLocked() const
342 for ( available_const_iterator it = availableBegin();
343 it != availableEnd(); ++it )
345 if ( ! (*it).status().isLocked() )
348 return( ! _availableItems.empty() );
352 const IdString _ident;
353 const ResObject::Kind _kind;
354 const std::string _name;
355 InstalledItemSet _installedItems;
356 AvailableItemSet _availableItems;
357 //! Best among availabe with restpect to installed.
358 PoolItem _defaultCandidate;
360 //! The object selected by setCandidateObj() method.
363 ///////////////////////////////////////////////////////////////////
365 /** \relates Selectable::Impl Stream output */
366 inline std::ostream & operator<<( std::ostream & str, const Selectable::Impl & obj )
368 return str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
369 << " (I " << obj.installedSize() << ")"
370 << " (A " << obj.availableSize() << ")"
371 << obj.candidateObj();
374 /** \relates Selectable::Impl Stream output */
375 inline std::ostream & dumpOn( std::ostream & str, const Selectable::Impl & obj )
377 str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
378 << ( obj.multiversionInstall() ? " (multiversion)" : "") << endl;
380 if ( obj.installedEmpty() )
381 str << " (I 0) {}" << endl << " ";
384 PoolItem icand( obj.installedObj() );
385 str << " (I " << obj.installedSize() << ") {" << endl;
386 for_( it, obj.installedBegin(), obj.installedEnd() )
393 str << " " << t << " " << *it << endl;
398 if ( obj.availableEmpty() )
404 PoolItem cand( obj.candidateObj() );
405 PoolItem up( obj.updateCandidateObj() );
406 str << "(A " << obj.availableSize() << ") {" << endl;
407 for_( it, obj.availableBegin(), obj.availableEnd() )
412 t = *it == up ? 'C' : 'c';
414 else if ( *it == up )
418 str << " " << t << " " << *it << endl;
425 /////////////////////////////////////////////////////////////////
427 ///////////////////////////////////////////////////////////////////
428 /////////////////////////////////////////////////////////////////
430 ///////////////////////////////////////////////////////////////////
431 #endif // ZYPP_UI_SELECTABLEIMPL_H