try to clarify doc
[platform/upstream/libzypp.git] / zypp / ui / Selectable.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ui/Selectable.h
10  *
11 */
12 #ifndef ZYPP_UI_SELECTABLE_H
13 #define ZYPP_UI_SELECTABLE_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/ReferenceCounted.h"
18 #include "zypp/base/NonCopyable.h"
19 #include "zypp/base/PtrTypes.h"
20 #include "zypp/base/Iterator.h"
21
22 #include "zypp/ui/SelectableTraits.h"
23 #include "zypp/ui/Status.h"
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28
29   ///////////////////////////////////////////////////////////////////
30   namespace ui
31   { /////////////////////////////////////////////////////////////////
32
33     DEFINE_PTR_TYPE(Selectable);
34
35     ///////////////////////////////////////////////////////////////////
36     //
37     //  CLASS NAME : Selectable
38     //
39     /** Collects PoolItems of same kind and name.
40      *
41      * Selectable is a status wrapper. The ui::Status is calculated
42      * from (and transated to) \ref PoolItems individual \ref ResStatus
43      * values.
44      *
45      * Available objects are sorted according the solver policies, 'best'
46      * packages first (e.g. by repository priority, then Arch, then Edition).
47      *
48      * Installed objects are sorted according the installation date, newer install
49      * time first.
50     */
51     class Selectable : public base::ReferenceCounted, private base::NonCopyable
52     {
53       friend std::ostream & operator<<( std::ostream & str, const Selectable & obj );
54       friend std::ostream & dumpOn( std::ostream & str, const Selectable & obj );
55
56     public:
57       typedef intrusive_ptr<Selectable>        Ptr;
58       typedef intrusive_ptr<const Selectable>  constPtr;
59
60       /** Iterates over ResObject::constPtr */
61       typedef SelectableTraits::available_iterator      available_iterator;
62       typedef SelectableTraits::available_size_type     available_size_type;
63
64       typedef SelectableTraits::installed_iterator      installed_iterator;
65       typedef SelectableTraits::installed_size_type     installed_size_type;
66
67     public:
68       /** \name Static ctor substitues picking the item from the pool.
69        * \code
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
74        * \endcode
75       */
76       //@{
77       /** Get the \ref Selctable */
78       static Ptr get( const pool::ByIdent & ident_r );
79
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 ) ); }
83
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 ) ); }
87
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 ) ); }
91
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 ) ); }
95
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(); }
99
100       /** Get the \ref Selctable containing a specific \ref PoolItem. */
101       static Ptr get( const PoolItem & pi_r )
102       { return get( pi_r.satSolvable() ); }
103       //@}
104
105     public:
106       /** The identifier.
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.
110        */
111       IdString ident() const;
112
113       /** The ResObjects kind. */
114       ResObject::Kind kind() const;
115
116       /** The ResObjects name.  */
117       const std::string & name() const;
118
119       /** The last Installed object. */
120       PoolItem installedObj() const;
121
122       /** The 'best' or 'most interesting' among all available objects.
123        * One that is, or is likely to be, chosen for installation.
124        */
125       PoolItem candidateObj() const;
126
127       /** Return the \ref installedObj resolvable casted to a specific kind.
128        * \code
129        *   Selectable mySelectable;
130        *   Package::constPtr p( mySelectable.installedAsKind<Package>() );
131        * \endcode
132       */
133       template<class _Res>
134       typename ResTraits<_Res>::constPtrType installedAsKind() const
135       { return asKind<_Res>( candidateObj() ); }
136
137       /** Return the \ref candidateObj resolvable casted to a specific kind.
138        * \code
139        *   Selectable mySelectable;
140        *   Package::constPtr p( mySelectable.candidateAsKind<Package>() );
141        * \endcode
142       */
143       template<class _Res>
144       typename ResTraits<_Res>::constPtrType candidateAsKind() const
145       { return asKind<_Res>( candidateObj() ); }
146
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.
154        */
155       PoolItem setCandidate( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
156       /** \overload */
157       PoolItem setCandidate( ResObject::constPtr newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
158
159       /** An object you could use as pars pro toto.
160        *
161        * \return the \ref candidateObj, or ,if no available objects
162        * exist, the \ref installedObj.
163        */
164       PoolItem theObj() const;
165
166       ////////////////////////////////////////////////////////////////////////
167
168       /** \name Available objects iterators.
169        * Oredered according to solver policy. 'Best' first.
170       */
171       //@{
172       bool availableEmpty() const;
173       available_size_type availableSize() const;
174       available_iterator availableBegin() const;
175       available_iterator availableEnd() const;
176       //@}
177
178       ////////////////////////////////////////////////////////////////////////
179
180       /** \name Insatlled objects iterators.
181        * Ordered by install time. Latest first.
182       */
183       //@{
184       bool installedEmpty() const;
185       installed_size_type installedSize() const;
186       installed_iterator installedBegin() const;
187       installed_iterator installedEnd() const;
188       //}
189
190       ////////////////////////////////////////////////////////////////////////
191
192     public:
193       /** \name Query for objects within this Selectable.
194       */
195       //@{
196       /** True if either installed or candidate object is present */
197       bool hasObject() const
198       { return (! installedEmpty()) || candidateObj(); }
199
200       /** True if installed object is present. */
201       bool hasInstalledObj() const
202       { return ! installedEmpty(); }
203
204       /** True if candidate object is present. */
205       bool hasCandidateObj() const
206       { return candidateObj(); }
207
208       /** True if installed and candidate object is present */
209       bool hasBothObjects() const
210       { return (! installedEmpty()) && candidateObj(); }
211
212       /** True if installed object is present but no candidate. */
213       bool hasInstalledObjOnly() const
214       { return (! installedEmpty()) && ! candidateObj(); }
215
216       /** True if candidate object is present but no installed. */
217       bool hasCandidateObjOnly() const
218       { return ( installedEmpty() ) && candidateObj(); }
219       //@}
220
221       /**
222        * True if this package has no replacement from
223        * the available repositories
224        */
225       bool isUnmaintained() const;
226
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
231        */
232       //@{
233       /** Returns true for packages, because packages are not
234        * classified by the solver.
235       */
236       bool isUndetermined() const;
237
238       /** Returns true if the patch is relevant which means that at least
239        *  one package of the patch is installed.
240        */
241       bool isRelevant() const;
242
243       /** Whether a relevant patchs requirements are met. */
244       bool isSatisfied() const;
245
246       /** Whether a relevant patchs requirements are broken. */
247       bool isBroken() const;
248
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
252        * \c satisfied.
253        */
254       bool isNeeded() const;
255       //@}
256
257      public:
258       /** \name Query and maip objects fate in case of commit.
259       */
260       //@{
261       enum Fate {
262         TO_DELETE  = -1,
263         UNMODIFIED = 0,
264         TO_INSTALL = 1
265       };
266       /**  */
267       Fate fate() const;
268
269       /** True if either to delete or to install */
270       bool unmodified() const
271       { return fate() == UNMODIFIED; }
272
273       /** True if either to delete or to install */
274       bool toModify() const
275       { return fate() != UNMODIFIED; }
276
277       /** True if to delete */
278       bool toDelete() const
279       { return fate() == TO_DELETE; }
280
281       /** True if to install */
282       bool toInstall() const
283       { return fate() == TO_INSTALL; }
284
285       /** */
286       bool setFate( Fate fate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
287
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 ); }
291
292       /** Take care the item gets installed if it is not. */
293       bool setInstalled( ResStatus::TransactByValue causer_r = ResStatus::USER );
294
295       /** Take care the item gets installed if it is not, or is older. */
296       bool setUpToDate( ResStatus::TransactByValue causer_r = ResStatus::USER );
297
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 ); }
301
302       /** Take care the item gets deleted if it is installed. */
303       bool setDeleted( ResStatus::TransactByValue causer_r = ResStatus::USER );
304
305       /** Set the item to stay unmodified. */
306       bool unset( ResStatus::TransactByValue causer_r = ResStatus::USER )
307       { return setFate( UNMODIFIED, causer_r ); }
308       //@}
309
310     public:
311       /**
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.
316       */
317       //@{
318       /** Return the current Status */
319       Status status() const;
320
321       /**
322        * Try to set a new Status.
323        * Returns \c false if the transitions is not allowed.
324        */
325       bool setStatus( const Status state_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
326
327       /** Return who caused the modification. */
328       ResStatus::TransactByValue modifiedBy() const;
329
330       /** Return value of LicenceConfirmed bit. */
331       bool hasLicenceConfirmed() const;
332
333       /** Set LicenceConfirmed bit. */
334       void setLicenceConfirmed( bool val_r = true );
335       //@}
336
337     public:
338       /** Implementation  */
339       class Impl;
340       typedef shared_ptr<Impl> Impl_Ptr;
341       /** Default ctor */
342       Selectable( Impl_Ptr pimpl_r );
343     private:
344       /** Dtor */
345       ~Selectable();
346     private:
347       /** Pointer to implementation */
348       RW_pointer<Impl> _pimpl;
349     };
350     ///////////////////////////////////////////////////////////////////
351
352     /** \relates Selectable Stream output */
353     std::ostream & operator<<( std::ostream & str, const Selectable & obj );
354
355     /** \relates Selectable More verbose stream output */
356     std::ostream & dumpOn( std::ostream & str, const Selectable & obj );
357
358     /** Solvable to Selectable transform functor.
359      * \relates Selectable
360      * \relates sat::SolvIterMixin
361      */
362     struct asSelectable
363     {
364       typedef Selectable_Ptr result_type;
365
366       Selectable_Ptr operator()( const sat::Solvable & solv_r ) const;
367
368       Selectable_Ptr operator()( const PoolItem & pi_r ) const
369       { return operator()( pi_r.satSolvable() ); }
370     };
371
372     /////////////////////////////////////////////////////////////////
373   } // namespace ui
374   ///////////////////////////////////////////////////////////////////
375   /////////////////////////////////////////////////////////////////
376 } // namespace zypp
377 ///////////////////////////////////////////////////////////////////
378 #endif // ZYPP_UI_SELECTABLE_H