- Add static ui::Selectable::get methods as convenient ctor substitute.
[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. That's why it offers the
42      * PoolItems ResObjects but hides their individual ResStatus.
43      * The ui::Status is calculated from (and transated to)
44      * PoolItems individual ResStatus values.
45      *
46      * \note There's one Selectable per installed item, in case more
47      * than one item is intalled.
48     */
49     class Selectable : public base::ReferenceCounted, private base::NonCopyable
50     {
51       friend std::ostream & operator<<( std::ostream & str, const Selectable & obj );
52       friend std::ostream & dumpOn( std::ostream & str, const Selectable & obj );
53
54     public:
55       typedef intrusive_ptr<Selectable>        Ptr;
56       typedef intrusive_ptr<const Selectable>  constPtr;
57
58       /** Iterates over ResObject::constPtr */
59       typedef SelectableTraits::available_iterator      available_iterator;
60       typedef SelectableTraits::available_size_type     available_size_type;
61
62       typedef SelectableTraits::installed_iterator      installed_iterator;
63       typedef SelectableTraits::installed_size_type     installed_size_type;
64
65     public:
66       /** \name Static ctor substitues picking the item from the pool.
67        * \code
68        * Selectable::Ptr item;
69        * item = Selectable::get( "amarok );                  // package amamrok
70        * item = Selectable::get( ResKind::patch, "amarok );  // patch amamrok
71        * item = Selectable::get( IdString( "patch:amarok" ); // patch amamrok
72        * \endcode
73       */
74       //@{
75       /** Get the \ref Selctable */
76       static Ptr get( const pool::ByIdent & ident_r );
77
78       /** Get the \ref Selctable by it's \c sat-identifyer. */
79       static Ptr get( IdString ident_r )
80       { return get( pool::ByIdent( ident_r ) ); }
81
82       /** Get the \ref Selctable by \c kind and \c name. */
83       static Ptr get( ResKind kind_r, const std::string & name_r )
84       { return get( pool::ByIdent( kind_r, name_r ) ); }
85
86       /** Get the \c Package \ref Selctable by \c name. */
87       static Ptr get( const std::string & name_r )
88       { return get( pool::ByIdent( ResKind::package, name_r ) ); }
89
90       /** Get the \ref Selctable containing a specific \ref sat::Solvable. */
91       static Ptr get( const sat::Solvable & solv_r )
92       { return get( pool::ByIdent( solv_r ) ); }
93
94       /** Get the \ref Selctable containing a specific \ref ResObject. */
95       static Ptr get( const ResObject::constPtr & resolvable_r )
96       { return resolvable_r ? get( resolvable_r->satSolvable() ) : Ptr(); }
97
98       /** Get the \ref Selctable containing a specific \ref PoolItem. */
99       static Ptr get( const PoolItem & pi_r )
100       { return get( pi_r.satSolvable() ); }
101       //@}
102
103     public:
104       /** The identifier.
105        * This is the solvables \ref name, \b except for packages and
106        * source packes, prefixed by it's \ref kind.
107        * \see \ref sat::Solvable.
108        */
109       IdString ident() const;
110
111       /** The ResObjects kind. */
112       ResObject::Kind kind() const;
113
114       /** The ResObjects name.  */
115       const std::string & name() const;
116
117       /** The last Installed object. */
118       PoolItem installedObj() const;
119
120       /** Best among available objects.
121        * The user selected candiate, or a default.
122        */
123       PoolItem candidateObj() const;
124
125       /** Set a candidate (out of available objects).
126        * \return The new candidate, or NULL if choice was invalid
127        * (NULL or not among availableObjs). An invalid choice
128        * selects the default candidate.
129        */
130       PoolItem setCandidate( ResObject::constPtr byUser_r );
131
132       /** Best among all objects. */
133       PoolItem theObj() const;
134
135       ////////////////////////////////////////////////////////////////////////
136
137       /** \name Available objects iterators.
138        * Oredered according to solver policy. 'Best' first.
139       */
140       //@{
141       bool availableEmpty() const;
142       available_size_type availableSize() const;
143       available_iterator availableBegin() const;
144       available_iterator availableEnd() const;
145       //@}
146
147       ////////////////////////////////////////////////////////////////////////
148
149       /** \name Insatlled objects iterators.
150        * Ordered by install time. Latest first.
151       */
152       //@{
153       bool installedEmpty() const;
154       installed_size_type installedSize() const;
155       installed_iterator installedBegin() const;
156       installed_iterator installedEnd() const;
157       //}
158
159       ////////////////////////////////////////////////////////////////////////
160
161     public:
162       /** \name Query for objects within this Selectable.
163       */
164       //@{
165       /** True if either installed or candidate object is present */
166       bool hasObject() const
167       { return (! installedEmpty()) || candidateObj(); }
168
169       /** True if installed object is present. */
170       bool hasInstalledObj() const
171       { return ! installedEmpty(); }
172
173       /** True if candidate object is present. */
174       bool hasCandidateObj() const
175       { return candidateObj(); }
176
177       /** True if installed and candidate object is present */
178       bool hasBothObjects() const
179       { return (! installedEmpty()) && candidateObj(); }
180
181       /** True if installed object is present but no candidate. */
182       bool hasInstalledObjOnly() const
183       { return (! installedEmpty()) && ! candidateObj(); }
184
185       /** True if candidate object is present but no installed. */
186       bool hasCandidateObjOnly() const
187       { return ( installedEmpty() ) && candidateObj(); }
188       //@}
189
190       /**
191        * True if this package has no replacement from
192        * the available repositories
193        */
194       bool isUnmaintained() const;
195
196       /** \name Classification of available patches (pseudo installed items).
197        * A patch is either \c not \c relevant, \c satisfied or \c broken.
198        * The same applies to other pseudo installed kinds.
199        * \see \ref traits::isPseudoInstalled
200        */
201       //@{
202       /** Returns true for packages, because packages are not
203        * classified by the solver.
204       */
205       bool isUndetermined() const;
206
207       /** Returns true if the patch is relevant which means that at least
208        *  one package of the patch is installed.
209        */
210       bool isRelevant() const;
211
212       /** Whether a relevant patchs requirements are met. */
213       bool isSatisfied() const;
214
215       /** Whether a relevant patchs requirements are broken. */
216       bool isBroken() const;
217
218       /** This includes still broken patches, as well as those already
219        *  selected to be installed.
220        * This is because already selected patches will be classified as
221        * \c satisfied.
222        */
223       bool isNeeded() const;
224       //@}
225
226      public:
227       /** \name Query objects fate in case of commit.
228       */
229       //@{
230       enum Fate {
231         TO_DELETE  = -1,
232         UNMODIFIED = 0,
233         TO_INSTALL = 1
234       };
235       /**  */
236       Fate fate() const;
237
238       /** True if either to delete or to install */
239       bool unmodified() const
240       { return fate() == UNMODIFIED; }
241
242       /** True if either to delete or to install */
243       bool toModify() const
244       { return fate() != UNMODIFIED; }
245
246       /** True if to delete */
247       bool toDelete() const
248       { return fate() == TO_DELETE; }
249
250       /** True if to install */
251       bool toInstall() const
252       { return fate() == TO_INSTALL; }
253       //@}
254
255     public:
256       /**
257        * \name Special inteface for Y2UI.
258        * \note This interface acts on \ref ResStatus::USER level.
259        * The \ref Status enum, and allowed state transitions are
260        * tightly related to the Y2UI. It might be not verry usefull
261        * outside the Y2UI.
262       */
263       //@{
264       /** Return the current Status */
265       Status status() const;
266
267       /**
268        * Try to set a new Status.
269        * Returns \c false if the transitions is not allowed.
270        */
271       bool setStatus( const Status state_r );
272
273       /** Return who caused the modification. */
274       ResStatus::TransactByValue modifiedBy() const;
275
276       /** Return value of LicenceConfirmed bit. */
277       bool hasLicenceConfirmed() const;
278
279       /** Set LicenceConfirmed bit. */
280       void setLicenceConfirmed( bool val_r = true );
281       //@}
282
283     public:
284       /** Implementation  */
285       class Impl;
286       typedef shared_ptr<Impl> Impl_Ptr;
287       /** Default ctor */
288       Selectable( Impl_Ptr pimpl_r );
289     private:
290       /** Dtor */
291       ~Selectable();
292     private:
293       /** Pointer to implementation */
294       RW_pointer<Impl> _pimpl;
295     };
296     ///////////////////////////////////////////////////////////////////
297
298     /** \relates Selectable Stream output */
299     std::ostream & operator<<( std::ostream & str, const Selectable & obj );
300
301     /** \relates Selectable More verbose stream output */
302     std::ostream & dumpOn( std::ostream & str, const Selectable & obj );
303
304     /** Solvable to Selectable transform functor.
305      * \relates Selectable
306      * \relates sat::SolvIterMixin
307      */
308     struct asSelectable
309     {
310       typedef Selectable_Ptr result_type;
311
312       Selectable_Ptr operator()( const sat::Solvable & solv_r ) const;
313
314       Selectable_Ptr operator()( const PoolItem & pi_r ) const
315       { return operator()( pi_r.satSolvable() ); }
316     };
317
318     /////////////////////////////////////////////////////////////////
319   } // namespace ui
320   ///////////////////////////////////////////////////////////////////
321   /////////////////////////////////////////////////////////////////
322 } // namespace zypp
323 ///////////////////////////////////////////////////////////////////
324 #endif // ZYPP_UI_SELECTABLE_H