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;
68 /** \name Static ctor substitues picking the item from the pool.
70 * Selectable::Ptr item;
71 * item = Selectable::get( "amarok ); // package amamrok
72 * item = Selectable::get( ResKind::patch, "amarok ); // patch amamrok
73 * item = Selectable::get( IdString( "patch:amarok" ); // patch amamrok
77 /** Get the \ref Selctable */
78 static Ptr get( const pool::ByIdent & ident_r );
80 /** Get the \ref Selctable by it's \c sat-identifyer. */
81 static Ptr get( IdString ident_r )
82 { return get( pool::ByIdent( ident_r ) ); }
84 /** Get the \ref Selctable by \c kind and \c name. */
85 static Ptr get( ResKind kind_r, const std::string & name_r )
86 { return get( pool::ByIdent( kind_r, name_r ) ); }
88 /** Get the \c Package \ref Selctable by \c name. */
89 static Ptr get( const std::string & name_r )
90 { return get( pool::ByIdent( ResKind::package, name_r ) ); }
92 /** Get the \ref Selctable containing a specific \ref sat::Solvable. */
93 static Ptr get( const sat::Solvable & solv_r )
94 { return get( pool::ByIdent( solv_r ) ); }
96 /** Get the \ref Selctable containing a specific \ref ResObject. */
97 static Ptr get( const ResObject::constPtr & resolvable_r )
98 { return resolvable_r ? get( resolvable_r->satSolvable() ) : Ptr(); }
100 /** Get the \ref Selctable containing a specific \ref PoolItem. */
101 static Ptr get( const PoolItem & pi_r )
102 { return get( pi_r.satSolvable() ); }
107 * This is the solvables \ref name, \b except for packages and
108 * source packes, prefixed by it's \ref kind.
109 * \see \ref sat::Solvable.
111 IdString ident() const;
113 /** The ResObjects kind. */
114 ResObject::Kind kind() const;
116 /** The ResObjects name. */
117 const std::string & name() const;
119 /** The last Installed object. */
120 PoolItem installedObj() const;
122 /** The 'best' or 'most interesting' among all available objects.
123 * One that is, or is likely to be, chosen for installation.
125 PoolItem candidateObj() const;
127 /** Return the \ref installedObj resolvable casted to a specific kind.
129 * Selectable mySelectable;
130 * Package::constPtr p( mySelectable.installedAsKind<Package>() );
134 typename ResTraits<_Res>::constPtrType installedAsKind() const
135 { return asKind<_Res>( candidateObj() ); }
137 /** Return the \ref candidateObj resolvable casted to a specific kind.
139 * Selectable mySelectable;
140 * Package::constPtr p( mySelectable.candidateAsKind<Package>() );
144 typename ResTraits<_Res>::constPtrType candidateAsKind() const
145 { return asKind<_Res>( candidateObj() ); }
147 /** Set a candidate (out of available objects).
148 * \return The new candidate, or NULL if choice was invalid
149 * (NULL or not among availableObjs). An invalid choice
150 * selects the default candidate.
151 * In case the causer is not \c ResStatus::USER the operation
152 * may also fail if there are insufficient permissions to change
153 * a transacting candidate.
155 PoolItem setCandidate( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
157 PoolItem setCandidate( ResObject::constPtr newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
159 /** An object you could use as pars pro toto.
161 * \return the \ref candidateObj, or ,if no available objects
162 * exist, the \ref installedObj.
164 PoolItem theObj() const;
166 ////////////////////////////////////////////////////////////////////////
168 /** \name Available objects iterators.
169 * Oredered according to solver policy. 'Best' first.
172 bool availableEmpty() const;
173 available_size_type availableSize() const;
174 available_iterator availableBegin() const;
175 available_iterator availableEnd() const;
178 ////////////////////////////////////////////////////////////////////////
180 /** \name Insatlled objects iterators.
181 * Ordered by install time. Latest first.
184 bool installedEmpty() const;
185 installed_size_type installedSize() const;
186 installed_iterator installedBegin() const;
187 installed_iterator installedEnd() const;
190 ////////////////////////////////////////////////////////////////////////
193 /** \name Query for objects within this Selectable.
196 /** True if either installed or candidate object is present */
197 bool hasObject() const
198 { return (! installedEmpty()) || candidateObj(); }
200 /** True if installed object is present. */
201 bool hasInstalledObj() const
202 { return ! installedEmpty(); }
204 /** True if candidate object is present. */
205 bool hasCandidateObj() const
206 { return candidateObj(); }
208 /** True if installed and candidate object is present */
209 bool hasBothObjects() const
210 { return (! installedEmpty()) && candidateObj(); }
212 /** True if installed object is present but no candidate. */
213 bool hasInstalledObjOnly() const
214 { return (! installedEmpty()) && ! candidateObj(); }
216 /** True if candidate object is present but no installed. */
217 bool hasCandidateObjOnly() const
218 { return ( installedEmpty() ) && candidateObj(); }
222 * True if this package has no replacement from
223 * the available repositories
225 bool isUnmaintained() const;
227 /** \name Classification of available patches (pseudo installed items).
228 * A patch is either \c not \c relevant, \c satisfied or \c broken.
229 * The same applies to other pseudo installed kinds.
230 * \see \ref traits::isPseudoInstalled
233 /** Returns true for packages, because packages are not
234 * classified by the solver.
236 bool isUndetermined() const;
238 /** Returns true if the patch is relevant which means that at least
239 * one package of the patch is installed.
241 bool isRelevant() const;
243 /** Whether a relevant patchs requirements are met. */
244 bool isSatisfied() const;
246 /** Whether a relevant patchs requirements are broken. */
247 bool isBroken() const;
249 /** This includes still broken patches, as well as those already
250 * selected to be installed.
251 * This is because already selected patches will be classified as
254 bool isNeeded() const;
258 /** \name Query and maip objects fate in case of commit.
269 /** True if either to delete or to install */
270 bool unmodified() const
271 { return fate() == UNMODIFIED; }
273 /** True if either to delete or to install */
274 bool toModify() const
275 { return fate() != UNMODIFIED; }
277 /** True if to delete */
278 bool toDelete() const
279 { return fate() == TO_DELETE; }
281 /** True if to install */
282 bool toInstall() const
283 { return fate() == TO_INSTALL; }
286 bool setFate( Fate fate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
288 /** Set the item to be installed (new- or re-install). */
289 bool setToInstall( ResStatus::TransactByValue causer_r = ResStatus::USER )
290 { return setFate( TO_INSTALL, causer_r ); }
292 /** Take care the item gets installed if it is not. */
293 bool setInstalled( ResStatus::TransactByValue causer_r = ResStatus::USER );
295 /** Take care the item gets installed if it is not, or is older. */
296 bool setUpToDate( ResStatus::TransactByValue causer_r = ResStatus::USER );
298 /** Set the item to be deleted (must be installed). */
299 bool setToDelete( ResStatus::TransactByValue causer_r = ResStatus::USER )
300 { return setFate( TO_DELETE, causer_r ); }
302 /** Take care the item gets deleted if it is installed. */
303 bool setDeleted( ResStatus::TransactByValue causer_r = ResStatus::USER );
305 /** Set the item to stay unmodified. */
306 bool unset( ResStatus::TransactByValue causer_r = ResStatus::USER )
307 { return setFate( UNMODIFIED, causer_r ); }
312 * \name Special inteface for Y2UI.
313 * \note This interface acts on \ref ResStatus::USER level.
314 * The \ref Status enum, and allowed state transitions are
315 * tightly related to the Y2UI.
318 /** Return the current Status */
319 Status status() const;
322 * Try to set a new Status.
323 * Returns \c false if the transitions is not allowed.
325 bool setStatus( const Status state_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
327 /** Return who caused the modification. */
328 ResStatus::TransactByValue modifiedBy() const;
330 /** Return value of LicenceConfirmed bit. */
331 bool hasLicenceConfirmed() const;
333 /** Set LicenceConfirmed bit. */
334 void setLicenceConfirmed( bool val_r = true );
338 /** Implementation */
340 typedef shared_ptr<Impl> Impl_Ptr;
342 Selectable( Impl_Ptr pimpl_r );
347 /** Pointer to implementation */
348 RW_pointer<Impl> _pimpl;
350 ///////////////////////////////////////////////////////////////////
352 /** \relates Selectable Stream output */
353 std::ostream & operator<<( std::ostream & str, const Selectable & obj );
355 /** \relates Selectable More verbose stream output */
356 std::ostream & dumpOn( std::ostream & str, const Selectable & obj );
358 /** Solvable to Selectable transform functor.
359 * \relates Selectable
360 * \relates sat::SolvIterMixin
364 typedef Selectable_Ptr result_type;
366 Selectable_Ptr operator()( const sat::Solvable & solv_r ) const;
368 Selectable_Ptr operator()( const PoolItem & pi_r ) const
369 { return operator()( pi_r.satSolvable() ); }
372 /////////////////////////////////////////////////////////////////
374 ///////////////////////////////////////////////////////////////////
375 /////////////////////////////////////////////////////////////////
377 ///////////////////////////////////////////////////////////////////
378 #endif // ZYPP_UI_SELECTABLE_H