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. */
95 PoolItem installedObj() const
97 if (!installedEmpty())
98 return *_installedItems.begin();
102 /** Best among available objects.
103 * The transacting candidate or the one scheduled to receive
106 PoolItem candidateObj() const
108 PoolItem ret( transactingCandidate() );
111 return _candidate ? _candidate : _defaultCandidate;
114 /** Set a userCandidate (out of available objects).
115 * \return The new userCandidate or NULL if choice was invalid
116 * (not among availableObjs).
118 PoolItem setCandidate( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r );
120 /** The best candidate provided by a specific \ref Repository, if there is one.
121 * In contrary to \ref candidateObj, this may return no item even if
122 * there are available objects. This simply means the \ref Repository
123 * does not provide this object.
125 PoolItem candidateObjFrom( Repository repo_r ) const
127 for_( it, availableBegin(), availableEnd() )
129 if ( (*it)->repository() == repo_r )
135 /** The best candidate for update, if there is one.
136 * In contrary to \ref candidateObj, this may return no item even if
137 * there are available objects. This simply means the best object is
138 * already installed, and all available objects violate at least one
141 PoolItem updateCandidateObj() const
143 if ( installedEmpty() || ! _defaultCandidate )
144 return _defaultCandidate;
145 // Here: installed and _defaultCandidate are non NULL.
147 // update candidate must come from the highest priority repo
148 if ( _defaultCandidate->repoInfo().priority() != (*availableBegin())->repoInfo().priority() )
151 PoolItem installed( installedObj() );
152 // check vendor change
153 if ( ! ( ZConfig::instance().solver_allowVendorChange()
154 || VendorAttr::instance().equivalent( _defaultCandidate->vendor(), installed->vendor() ) ) )
158 if ( _defaultCandidate->arch() != installed->arch() )
161 // check greater edition
162 if ( _defaultCandidate->edition() <= installed->edition() )
165 return _defaultCandidate;
168 /** \c True if \a rhs has the same content as an installed one.
169 * \see \ref sat::Solvable::identical
171 bool identicalInstalled( const PoolItem & rhs ) const
173 if ( !installedEmpty() && rhs )
175 for_( it, _installedItems.begin(), _installedItems.end() )
177 if ( identical( *it, rhs ) )
184 /** Best among all objects. */
185 PoolItem theObj() const
187 PoolItem ret( candidateObj() );
191 if ( ! _installedItems.empty() )
192 return (*_installedItems.begin());
197 ////////////////////////////////////////////////////////////////////////
199 bool availableEmpty() const
200 { return _availableItems.empty(); }
202 available_size_type availableSize() const
203 { return _availableItems.size(); }
205 available_const_iterator availableBegin() const
206 { return _availableItems.begin(); }
208 available_const_iterator availableEnd() const
209 { return _availableItems.end(); }
211 ////////////////////////////////////////////////////////////////////////
213 bool installedEmpty() const
214 { return _installedItems.empty(); }
216 installed_size_type installedSize() const
217 { return _installedItems.size(); }
219 installed_iterator installedBegin() const
220 { return _installedItems.begin(); }
222 installed_iterator installedEnd() const
223 { return _installedItems.end(); }
225 ////////////////////////////////////////////////////////////////////////
227 bool isUnmaintained() const
228 { return availableEmpty(); }
230 bool isUndetermined() const
232 PoolItem cand( candidateObj() );
233 return ! cand || cand.isUndetermined();
235 bool isRelevant() const
237 PoolItem cand( candidateObj() );
238 return cand && cand.isRelevant();
240 bool isSatisfied() const
242 PoolItem cand( candidateObj() );
243 return cand && cand.isSatisfied();
245 bool isBroken() const
247 PoolItem cand( candidateObj() );
248 return cand && cand.isBroken();
251 /** Return who caused the modification. */
252 ResStatus::TransactByValue modifiedBy() const;
254 /** Return value of LicenceConfirmed bit. */
255 bool hasLicenceConfirmed() const
256 { return candidateObj() && candidateObj().status().isLicenceConfirmed(); }
258 /** Set LicenceConfirmed bit. */
259 void setLicenceConfirmed( bool val_r )
260 { if ( candidateObj() ) candidateObj().status().setLicenceConfirmed( val_r ); }
263 PoolItem transactingCandidate() const
265 for ( available_const_iterator it = availableBegin();
266 it != availableEnd(); ++it )
268 if ( (*it).status().transacts() )
274 PoolItem defaultCandidate() const
276 if ( !installedEmpty() )
278 // prefer the installed objects arch and vendor
279 bool solver_allowVendorChange( ZConfig::instance().solver_allowVendorChange() );
280 for ( installed_const_iterator iit = installedBegin();
281 iit != installedEnd(); ++iit )
283 PoolItem sameArch; // in case there's no same vendor at least stay with same arch
284 for ( available_const_iterator it = availableBegin();
285 it != availableEnd(); ++it )
287 if ( (*iit)->arch() == (*it)->arch() )
289 if ( ! solver_allowVendorChange )
291 if ( VendorAttr::instance().equivalent( (*iit), (*it) ) )
293 else if ( ! sameArch ) // remember best same arch in case no same vendor found
296 else // same arch is sufficient
304 if ( _availableItems.empty() )
307 return *_availableItems.begin();
310 bool allCandidatesLocked() const
312 for ( available_const_iterator it = availableBegin();
313 it != availableEnd(); ++it )
315 if ( ! (*it).status().isLocked() )
318 return( ! _availableItems.empty() );
322 const IdString _ident;
323 const ResObject::Kind _kind;
324 const std::string _name;
325 InstalledItemSet _installedItems;
326 AvailableItemSet _availableItems;
327 //! Best among availabe with restpect to installed.
328 PoolItem _defaultCandidate;
330 //! The object selected by setCandidateObj() method.
333 ///////////////////////////////////////////////////////////////////
335 /** \relates Selectable::Impl Stream output */
336 inline std::ostream & operator<<( std::ostream & str, const Selectable::Impl & obj )
338 return str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status()
339 << " (I " << obj.installedSize() << ")"
340 << " (A " << obj.availableSize() << ")"
341 << obj.candidateObj();
344 /** \relates Selectable::Impl Stream output */
345 inline std::ostream & dumpOn( std::ostream & str, const Selectable::Impl & obj )
347 str << '[' << obj.kind() << ']' << obj.name() << ": " << obj.status() << endl;
348 if ( obj.candidateObj() )
349 str << "(C " << obj.candidateObj() << ")" << endl;
351 str << "(C NONE )" << endl;
352 dumpRange( str << " (I " << obj.installedSize() << ") ", obj.installedBegin(), obj.installedEnd() );
353 if ( obj.installedEmpty() )
355 dumpRange( str << " (A " << obj.availableSize() << ") ", obj.availableBegin(), obj.availableEnd() ) << endl;
359 /////////////////////////////////////////////////////////////////
361 ///////////////////////////////////////////////////////////////////
362 /////////////////////////////////////////////////////////////////
364 ///////////////////////////////////////////////////////////////////
365 #endif // ZYPP_UI_SELECTABLEIMPL_H