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