1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ui/Selectable.h
12 #ifndef ZYPP_UI_SELECTABLE_H
13 #define ZYPP_UI_SELECTABLE_H
17 #include "zypp/base/ReferenceCounted.h"
18 #include "zypp/base/NonCopyable.h"
19 #include "zypp/base/PtrTypes.h"
20 #include "zypp/base/Iterator.h"
22 #include "zypp/ui/SelectableTraits.h"
23 #include "zypp/ui/Status.h"
25 ///////////////////////////////////////////////////////////////////
27 { /////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
33 DEFINE_PTR_TYPE(Selectable);
35 ///////////////////////////////////////////////////////////////////
37 // CLASS NAME : Selectable
39 /** Collects PoolItems of same kind and name.
41 * Selectable is a status wrapper. The ui::Status is calculated
42 * from (and transated to) \ref PoolItems individual \ref ResStatus
45 * Available objects are sorted according the solver policies, 'best'
46 * packages first (e.g. by repository priority, then Arch, then Edition).
48 * Installed objects are sorted according the installation date, newer install
51 class Selectable : public base::ReferenceCounted, private base::NonCopyable
53 friend std::ostream & operator<<( std::ostream & str, const Selectable & obj );
54 friend std::ostream & dumpOn( std::ostream & str, const Selectable & obj );
57 typedef intrusive_ptr<Selectable> Ptr;
58 typedef intrusive_ptr<const Selectable> constPtr;
60 /** Iterates over ResObject::constPtr */
61 typedef SelectableTraits::available_iterator available_iterator;
62 typedef SelectableTraits::available_size_type available_size_type;
64 typedef SelectableTraits::installed_iterator installed_iterator;
65 typedef SelectableTraits::installed_size_type installed_size_type;
67 typedef SelectableTraits::picklist_iterator picklist_iterator;
68 typedef SelectableTraits::picklist_size_type picklist_size_type;
71 /** \name Static ctor substitues picking the item from the pool.
73 * Selectable::Ptr item;
74 * item = Selectable::get( "amarok ); // package amamrok
75 * item = Selectable::get( ResKind::patch, "amarok ); // patch amamrok
76 * item = Selectable::get( IdString( "patch:amarok" ); // patch amamrok
80 /** Get the \ref Selctable */
81 static Ptr get( const pool::ByIdent & ident_r );
83 /** Get the \ref Selctable by it's \c sat-identifyer. */
84 static Ptr get( IdString ident_r )
85 { return get( pool::ByIdent( ident_r ) ); }
87 /** Get the \ref Selctable by \c kind and \c name. */
88 static Ptr get( ResKind kind_r, const std::string & name_r )
89 { return get( pool::ByIdent( kind_r, name_r ) ); }
91 /** Get the \c Package \ref Selctable by \c name. */
92 static Ptr get( const std::string & name_r )
93 { return get( pool::ByIdent( ResKind::package, name_r ) ); }
95 /** Get the \ref Selctable containing a specific \ref sat::Solvable. */
96 static Ptr get( const sat::Solvable & solv_r )
97 { return get( pool::ByIdent( solv_r ) ); }
99 /** Get the \ref Selctable containing a specific \ref ResObject. */
100 static Ptr get( const ResObject::constPtr & resolvable_r )
101 { return resolvable_r ? get( resolvable_r->satSolvable() ) : Ptr(); }
103 /** Get the \ref Selctable containing a specific \ref PoolItem. */
104 static Ptr get( const PoolItem & pi_r )
105 { return get( pi_r.satSolvable() ); }
110 * This is the solvables \ref name, \b except for packages and
111 * source packes, prefixed by it's \ref kind.
112 * \see \ref sat::Solvable.
114 IdString ident() const;
116 /** The ResObjects kind. */
117 ResObject::Kind kind() const;
119 /** The ResObjects name. */
120 const std::string & name() const;
122 /** The last Installed object. */
123 PoolItem installedObj() const;
125 /** The 'best' or 'most interesting' among all available objects.
126 * One that is, or is likely to be, chosen for installation, unless
127 * it violated any solver policy (see \ref updateCandidateObj).
129 PoolItem candidateObj() const;
131 /** The best candidate provided by a specific \ref Repository, if there is one.
132 * In contrary to \ref candidateObj, this may return no item even if
133 * there are available objects. This simply means the \ref Repository
134 * does not provide this object.
136 PoolItem candidateObjFrom( Repository repo_r ) const;
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 /** Simply the highest available version, ignoring priorities and policies.
147 * It's doubtful whether solely looking at the version makes a good
148 * candidate, but apps ask for it. Beware that different vendors may
149 * use different (uncomparable) version schemata.
151 PoolItem highestAvailableVersionObj() const;
153 /** \c True if \a rhs is installed and one with the same content is available.
154 * Basically the same name, edition, arch, vendor and buildtime.
155 * \see \ref sat::Solvable::identical
157 bool identicalAvailable( const PoolItem & rhs ) const;
159 /** \c True if \a rhs has the same content as an installed one.
160 * Basically the same name, edition, arch, vendor and buildtime.
161 * \see \ref sat::Solvable::identical
163 bool identicalInstalled( const PoolItem & rhs ) const;
165 /** \c True if the \ref candidateObj is installed (same content).
166 * \see \ref identicalInstalled.
168 bool identicalInstalledCandidate() const
169 { return identicalInstalled( candidateObj() ); }
171 /** \c True if the \ref updateCandidateObj is installed (same content).
172 * \see \ref identicalInstalled.
174 bool identicalInstalledUpdateCandidate() const
175 { return identicalInstalled( updateCandidateObj() ); }
177 /** Return an available Object with the same content as \c rhs.
178 * Basically the same name, edition, arch, vendor and buildtime.
179 * \see \ref sat::Solvable::identical
181 PoolItem identicalAvailableObj( const PoolItem & rhs ) const;
183 /** \Return an installed Object with the same content as \c rhs.
184 * Basically the same name, edition, arch, vendor and buildtime.
185 * \see \ref sat::Solvable::identical
187 PoolItem identicalInstalledObj( const PoolItem & rhs ) const;
189 /** Return the \ref installedObj resolvable casted to a specific kind.
191 * Selectable mySelectable;
192 * Package::constPtr p( mySelectable.installedAsKind<Package>() );
196 typename ResTraits<_Res>::constPtrType installedAsKind() const
197 { return asKind<_Res>( candidateObj() ); }
199 /** Return the \ref candidateObj resolvable casted to a specific kind.
201 * Selectable mySelectable;
202 * Package::constPtr p( mySelectable.candidateAsKind<Package>() );
206 typename ResTraits<_Res>::constPtrType candidateAsKind() const
207 { return asKind<_Res>( candidateObj() ); }
209 /** Set a candidate (out of available objects).
210 * \return The new candidate, or NULL if choice was invalid
211 * (NULL or not among availableObjs). An invalid choice
212 * selects the default candidate.
213 * In case the causer is not \c ResStatus::USER the operation
214 * may also fail if there are insufficient permissions to change
215 * a transacting candidate.
217 PoolItem setCandidate( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
219 PoolItem setCandidate( ResObject::constPtr newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
221 /** Arrange the specified candidate (out of available objects) to be on system after commit.
222 * If the specified candidate is not already installed (\ref identicalInstalled),
223 * and the \a causer_r has sufficient permisssion, then \a newCandidate_r is set as the new
224 * candidate (\ref setCandidate) and selected for installation.
225 * \returns \c True if \a newCandidate_r is already installed or successfully selected for installation.
227 bool setOnSystem( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
229 /** An object you could use as pars pro toto.
231 * \return the \ref candidateObj, or ,if no available objects
232 * exist, the \ref installedObj.
234 PoolItem theObj() const;
236 ////////////////////////////////////////////////////////////////////////
238 /** \name Available objects iterators.
239 * Oredered according to solver policy. 'Best' first.
242 bool availableEmpty() const;
243 available_size_type availableSize() const;
244 available_iterator availableBegin() const;
245 available_iterator availableEnd() const;
248 ////////////////////////////////////////////////////////////////////////
250 /** \name Installed objects iterators.
251 * Ordered by install time. Latest first.
254 bool installedEmpty() const;
255 installed_size_type installedSize() const;
256 installed_iterator installedBegin() const;
257 installed_iterator installedEnd() const;
260 ////////////////////////////////////////////////////////////////////////
262 /** \name picklist iterators.
263 * This is basically the available items list prepended by those
264 * installed items, that are nolonger \ref identicalAvailable.
267 bool picklistEmpty() const;
268 picklist_size_type picklistSize() const;
269 picklist_iterator picklistBegin() const;
270 picklist_iterator picklistEnd() const;
273 ////////////////////////////////////////////////////////////////////////
276 /** \name Query for objects within this Selectable.
279 /** True if either installed or candidate object is present */
280 bool hasObject() const
281 { return (! installedEmpty()) || candidateObj(); }
283 /** True if installed object is present. */
284 bool hasInstalledObj() const
285 { return ! installedEmpty(); }
287 /** True if candidate object is present. */
288 bool hasCandidateObj() const
289 { return bool(candidateObj()); }
291 /** True if installed and candidate object is present */
292 bool hasBothObjects() const
293 { return (! installedEmpty()) && candidateObj(); }
295 /** True if installed object is present but no candidate. */
296 bool hasInstalledObjOnly() const
297 { return (! installedEmpty()) && ! candidateObj(); }
299 /** True if candidate object is present but no installed. */
300 bool hasCandidateObjOnly() const
301 { return ( installedEmpty() ) && candidateObj(); }
305 * True if this package has no replacement from
306 * the available repositories
308 bool isUnmaintained() const;
310 /** \name Multiversion install.
312 * Using \ref pickInstall or \ref pickDelete with non-multiversionInstall items
313 * is possible, but additional constraints will apply. E.g. selecting one item for
314 * install will deselect any other.
317 /** Whether different versions of this package can be installed at the same time.
318 * Per default \c false. \see also \ref ZConfig::multiversion.
320 bool multiversionInstall() const;
322 /** Select a specific available item for installation.
324 bool pickInstall( const PoolItem & pi_r, ResStatus::TransactByValue causer_r = ResStatus::USER, bool yesno_r = true );
326 /** Deselect a specific available item from installation.
328 bool pickNoInstall( const PoolItem & pi_r, ResStatus::TransactByValue causer_r = ResStatus::USER )
329 { return pickInstall( pi_r, causer_r, false ); }
331 /** Select a specific installed item for deletion.
333 bool pickDelete( const PoolItem & pi_r, ResStatus::TransactByValue causer_r = ResStatus::USER, bool yesno_r = true );
335 /** Deselect a specific installed item from deletion.
337 bool pickNoDelete( const PoolItem & pi_r, ResStatus::TransactByValue causer_r = ResStatus::USER )
338 { return pickDelete( pi_r, causer_r, false ); }
340 /** Compute the \ref ui::Status for an individual PoolItem.
341 * This just takes into account the item and any identical
342 * installed (or available) one.
344 * Assume there are 3 identical 'foo-1.1 (vendor A)' items,
345 * one 'foo-2.1 (vendor A)' and one ''foo-1.1 (vendor B)':
347 * installed: . foo-1.1 (vendor A) -> S_KeepInstalled
348 * available: foo-2.1 (vendor A) (repo 1) -> S_NoInst
349 * . foo-1.1 (vendor A) (repo 1) -> S_KeepInstalled
350 * . foo-1.1 (vendor A) (repo 2) -> S_KeepInstalled
351 * foo-1.1 (vendor B) (repo 3) -> S_NoInst
353 * After 'foo-1.1 (vendor A) (repo 1)' was selected to be installed:
355 * installed: . foo-1.1 (vendor A) -> S_Update
356 * available: foo-2.1 (vendor A) (repo 1) -> S_NoInst
357 * I foo-1.1 (vendor A) (repo 1) -> S_Update
358 * . foo-1.1 (vendor A) (repo 2) -> S_KeepInstalled
359 * foo-1.1 (vendor B) (repo 3) -> S_NoInst
361 * \see \ref sat::Solvable::identical
363 Status pickStatus( const PoolItem & pi_r ) const;
365 /** Assign a new status to a specific item. */
366 bool setPickStatus( const PoolItem & pi_r, Status state_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
369 /** \name Classification of available patches (pseudo installed items).
370 * A patch is either \c not \c relevant, \c satisfied or \c broken.
371 * The same applies to other pseudo installed kinds.
372 * \see \ref traits::isPseudoInstalled
375 /** Returns true for packages, because packages are not
376 * classified by the solver.
378 bool isUndetermined() const;
380 /** Returns true if the patch is relevant which means that at least
381 * one package of the patch is installed.
383 bool isRelevant() const;
385 /** Whether a relevant patchs requirements are met. */
386 bool isSatisfied() const;
388 /** Whether a relevant patchs requirements are broken. */
389 bool isBroken() const;
391 /** This includes \c unlocked broken patches, as well as those already
392 * selected to be installed. This is because already selected
393 * patches will be classified as \c satisfied. \c Locked but broken
394 * patches will be classified as \ref isUnwanted.
396 bool isNeeded() const;
398 /** Broken (needed) but locked patches. */
399 bool isUnwanted() const;
403 /** \name Query and maip objects fate in case of commit.
414 /** True if neither to delete or to install */
415 bool unmodified() const
416 { return fate() == UNMODIFIED; }
418 /** True if locked (subclass of unmodified). */
420 { Status st( status() ); return( st == S_Protected || st == S_Taboo ); }
422 /** True if either to delete or to install */
423 bool toModify() const
424 { return fate() != UNMODIFIED; }
426 /** True if to delete */
427 bool toDelete() const
428 { return fate() == TO_DELETE; }
430 /** True if to install */
431 bool toInstall() const
432 { return fate() == TO_INSTALL; }
434 /** True if would be on system after commit. */
435 bool onSystem() const
436 { return( ( hasInstalledObj() && !toDelete() )
437 ||( hasCandidateObj() && toInstall() ) ); }
439 /** True if would be off system after commit. */
440 bool offSystem() const
441 { return ! onSystem(); }
444 bool setFate( Fate fate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
446 /** Set the item to be installed (new- or re-install). */
447 bool setToInstall( ResStatus::TransactByValue causer_r = ResStatus::USER )
448 { return setFate( TO_INSTALL, causer_r ); }
450 /** Take care the item gets installed if it is not. */
451 bool setInstalled( ResStatus::TransactByValue causer_r = ResStatus::USER );
453 /** Take care the item gets installed if it is not, or is older. */
454 bool setUpToDate( ResStatus::TransactByValue causer_r = ResStatus::USER );
456 /** Set the item to be deleted (must be installed). */
457 bool setToDelete( ResStatus::TransactByValue causer_r = ResStatus::USER )
458 { return setFate( TO_DELETE, causer_r ); }
460 /** Take care the item gets deleted if it is installed. */
461 bool setDeleted( ResStatus::TransactByValue causer_r = ResStatus::USER );
463 /** Set the item to stay unmodified. */
464 bool unset( ResStatus::TransactByValue causer_r = ResStatus::USER )
465 { return setFate( UNMODIFIED, causer_r ); }
470 * \name Special inteface for Y2UI.
471 * \note This interface acts on \ref ResStatus::USER level.
472 * The \ref Status enum, and allowed state transitions are
473 * tightly related to the Y2UI.
476 /** Return the current Status */
477 Status status() const;
480 * Try to set a new Status.
481 * Returns \c false if the transitions is not allowed.
483 bool setStatus( Status state_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
485 /** Return who caused the modification. */
486 ResStatus::TransactByValue modifiedBy() const;
488 /** Return value of LicenceConfirmed bit. */
489 bool hasLicenceConfirmed() const;
491 /** Set LicenceConfirmed bit. */
492 void setLicenceConfirmed( bool val_r = true );
496 /** Implementation */
498 typedef shared_ptr<Impl> Impl_Ptr;
500 Selectable( Impl_Ptr pimpl_r );
505 /** Pointer to implementation */
506 RW_pointer<Impl> _pimpl;
508 ///////////////////////////////////////////////////////////////////
510 /** \relates Selectable Stream output */
511 std::ostream & operator<<( std::ostream & str, const Selectable & obj );
513 /** \relates Selectable More verbose stream output */
514 std::ostream & dumpOn( std::ostream & str, const Selectable & obj );
516 /** Solvable to Selectable transform functor.
517 * \relates Selectable
518 * \relates sat::SolvIterMixin
522 typedef Selectable_Ptr result_type;
524 Selectable_Ptr operator()( const sat::Solvable & solv_r ) const;
526 Selectable_Ptr operator()( const PoolItem & pi_r ) const
527 { return operator()( pi_r.satSolvable() ); }
530 /////////////////////////////////////////////////////////////////
532 ///////////////////////////////////////////////////////////////////
533 /////////////////////////////////////////////////////////////////
535 ///////////////////////////////////////////////////////////////////
536 #endif // ZYPP_UI_SELECTABLE_H