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, unless
124 * it violated any solver policy (see \ref updateCandidateObj).
126 PoolItem candidateObj() const;
128 /** The best candidate provided by a specific \ref Repository, if there is one.
129 * In contrary to \ref candidateObj, this may return no item even if
130 * there are available objects. This simply means the \ref Repository
131 * does not provide this object.
133 PoolItem candidateObjFrom( Repository repo_r ) const;
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 /** \c True if \a rhs has the same content as an installed one.
144 * Basically the same name, edition, arch, vendor and buildtime.
145 * \see \ref sat::Solvable::identical
147 bool identicalInstalled( const PoolItem & rhs ) const;
149 /** \c True if the \ref candidateObj is installed (same content).
150 * \see \ref identicalInstalled.
152 bool identicalInstalledCandidate() const
153 { return identicalInstalled( candidateObj() ); }
155 /** \c True if the \ref updateCandidateObj is installed (same content).
156 * \see \ref identicalInstalled.
158 bool identicalInstalledUpdateCandidate() const
159 { return identicalInstalled( updateCandidateObj() ); }
162 /** Return the \ref installedObj resolvable casted to a specific kind.
164 * Selectable mySelectable;
165 * Package::constPtr p( mySelectable.installedAsKind<Package>() );
169 typename ResTraits<_Res>::constPtrType installedAsKind() const
170 { return asKind<_Res>( candidateObj() ); }
172 /** Return the \ref candidateObj resolvable casted to a specific kind.
174 * Selectable mySelectable;
175 * Package::constPtr p( mySelectable.candidateAsKind<Package>() );
179 typename ResTraits<_Res>::constPtrType candidateAsKind() const
180 { return asKind<_Res>( candidateObj() ); }
182 /** Set a candidate (out of available objects).
183 * \return The new candidate, or NULL if choice was invalid
184 * (NULL or not among availableObjs). An invalid choice
185 * selects the default candidate.
186 * In case the causer is not \c ResStatus::USER the operation
187 * may also fail if there are insufficient permissions to change
188 * a transacting candidate.
190 PoolItem setCandidate( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
192 PoolItem setCandidate( ResObject::constPtr newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
194 /** Arrange the specified candidate (out of available objects) to be on system after commit.
195 * If the specified candidate is not already installed (\ref identicalInstalled),
196 * and the \a causer_r has sufficient permisssion, then \a newCandidate_r is set as the new
197 * candidate (\ref setCandidate) and selected for installation.
198 * \returns \c True if \a newCandidate_r is already installed or sucessfully selected for installation.
200 bool setOnSystem( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
202 /** An object you could use as pars pro toto.
204 * \return the \ref candidateObj, or ,if no available objects
205 * exist, the \ref installedObj.
207 PoolItem theObj() const;
209 ////////////////////////////////////////////////////////////////////////
211 /** \name Available objects iterators.
212 * Oredered according to solver policy. 'Best' first.
215 bool availableEmpty() const;
216 available_size_type availableSize() const;
217 available_iterator availableBegin() const;
218 available_iterator availableEnd() const;
221 ////////////////////////////////////////////////////////////////////////
223 /** \name Insatlled objects iterators.
224 * Ordered by install time. Latest first.
227 bool installedEmpty() const;
228 installed_size_type installedSize() const;
229 installed_iterator installedBegin() const;
230 installed_iterator installedEnd() const;
233 ////////////////////////////////////////////////////////////////////////
236 /** \name Query for objects within this Selectable.
239 /** True if either installed or candidate object is present */
240 bool hasObject() const
241 { return (! installedEmpty()) || candidateObj(); }
243 /** True if installed object is present. */
244 bool hasInstalledObj() const
245 { return ! installedEmpty(); }
247 /** True if candidate object is present. */
248 bool hasCandidateObj() const
249 { return candidateObj(); }
251 /** True if installed and candidate object is present */
252 bool hasBothObjects() const
253 { return (! installedEmpty()) && candidateObj(); }
255 /** True if installed object is present but no candidate. */
256 bool hasInstalledObjOnly() const
257 { return (! installedEmpty()) && ! candidateObj(); }
259 /** True if candidate object is present but no installed. */
260 bool hasCandidateObjOnly() const
261 { return ( installedEmpty() ) && candidateObj(); }
265 * True if this package has no replacement from
266 * the available repositories
268 bool isUnmaintained() const;
270 /** \name Classification of available patches (pseudo installed items).
271 * A patch is either \c not \c relevant, \c satisfied or \c broken.
272 * The same applies to other pseudo installed kinds.
273 * \see \ref traits::isPseudoInstalled
276 /** Returns true for packages, because packages are not
277 * classified by the solver.
279 bool isUndetermined() const;
281 /** Returns true if the patch is relevant which means that at least
282 * one package of the patch is installed.
284 bool isRelevant() const;
286 /** Whether a relevant patchs requirements are met. */
287 bool isSatisfied() const;
289 /** Whether a relevant patchs requirements are broken. */
290 bool isBroken() const;
292 /** This includes still broken patches, as well as those already
293 * selected to be installed.
294 * This is because already selected patches will be classified as
297 bool isNeeded() const;
301 /** \name Query and maip objects fate in case of commit.
312 /** True if neither to delete or to install */
313 bool unmodified() const
314 { return fate() == UNMODIFIED; }
316 /** True if locked (subclass of unmodified). */
318 { Status st( status() ); return( st == S_Protected || st == S_Taboo ); }
320 /** True if either to delete or to install */
321 bool toModify() const
322 { return fate() != UNMODIFIED; }
324 /** True if to delete */
325 bool toDelete() const
326 { return fate() == TO_DELETE; }
328 /** True if to install */
329 bool toInstall() const
330 { return fate() == TO_INSTALL; }
332 /** True if would be on system after commit. */
333 bool onSystem() const
334 { return( ( hasInstalledObj() && !toDelete() )
335 ||( hasCandidateObj() && toInstall() ) ); }
337 /** True if would be off system after commit. */
338 bool offSystem() const
339 { return ! onSystem(); }
342 bool setFate( Fate fate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
344 /** Set the item to be installed (new- or re-install). */
345 bool setToInstall( ResStatus::TransactByValue causer_r = ResStatus::USER )
346 { return setFate( TO_INSTALL, causer_r ); }
348 /** Take care the item gets installed if it is not. */
349 bool setInstalled( ResStatus::TransactByValue causer_r = ResStatus::USER );
351 /** Take care the item gets installed if it is not, or is older. */
352 bool setUpToDate( ResStatus::TransactByValue causer_r = ResStatus::USER );
354 /** Set the item to be deleted (must be installed). */
355 bool setToDelete( ResStatus::TransactByValue causer_r = ResStatus::USER )
356 { return setFate( TO_DELETE, causer_r ); }
358 /** Take care the item gets deleted if it is installed. */
359 bool setDeleted( ResStatus::TransactByValue causer_r = ResStatus::USER );
361 /** Set the item to stay unmodified. */
362 bool unset( ResStatus::TransactByValue causer_r = ResStatus::USER )
363 { return setFate( UNMODIFIED, causer_r ); }
368 * \name Special inteface for Y2UI.
369 * \note This interface acts on \ref ResStatus::USER level.
370 * The \ref Status enum, and allowed state transitions are
371 * tightly related to the Y2UI.
374 /** Return the current Status */
375 Status status() const;
378 * Try to set a new Status.
379 * Returns \c false if the transitions is not allowed.
381 bool setStatus( const Status state_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
383 /** Return who caused the modification. */
384 ResStatus::TransactByValue modifiedBy() const;
386 /** Return value of LicenceConfirmed bit. */
387 bool hasLicenceConfirmed() const;
389 /** Set LicenceConfirmed bit. */
390 void setLicenceConfirmed( bool val_r = true );
394 /** Implementation */
396 typedef shared_ptr<Impl> Impl_Ptr;
398 Selectable( Impl_Ptr pimpl_r );
403 /** Pointer to implementation */
404 RW_pointer<Impl> _pimpl;
406 ///////////////////////////////////////////////////////////////////
408 /** \relates Selectable Stream output */
409 std::ostream & operator<<( std::ostream & str, const Selectable & obj );
411 /** \relates Selectable More verbose stream output */
412 std::ostream & dumpOn( std::ostream & str, const Selectable & obj );
414 /** Solvable to Selectable transform functor.
415 * \relates Selectable
416 * \relates sat::SolvIterMixin
420 typedef Selectable_Ptr result_type;
422 Selectable_Ptr operator()( const sat::Solvable & solv_r ) const;
424 Selectable_Ptr operator()( const PoolItem & pi_r ) const
425 { return operator()( pi_r.satSolvable() ); }
428 /////////////////////////////////////////////////////////////////
430 ///////////////////////////////////////////////////////////////////
431 /////////////////////////////////////////////////////////////////
433 ///////////////////////////////////////////////////////////////////
434 #endif // ZYPP_UI_SELECTABLE_H