Add Selectable::locked (short for TABOO||PROTECED)
[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, unless
124        * it violated any solver policy (see \ref updateCandidateObj).
125        */
126       PoolItem candidateObj() const;
127
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.
132        */
133       PoolItem candidateObjFrom( Repository repo_r ) const;
134
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
139        * update policy.
140        */
141       PoolItem updateCandidateObj() const;
142
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
146        */
147       bool identicalInstalled( const PoolItem & rhs ) const;
148
149       /** \c True if the \ref candidateObj is installed (same content).
150        * \see \ref identicalInstalled.
151        */
152       bool identicalInstalledCandidate() const
153       { return identicalInstalled( candidateObj() ); }
154
155       /** \c True if the \ref updateCandidateObj is installed (same content).
156        * \see \ref identicalInstalled.
157        */
158       bool identicalInstalledUpdateCandidate() const
159       { return identicalInstalled( updateCandidateObj() ); }
160
161
162       /** Return the \ref installedObj resolvable casted to a specific kind.
163        * \code
164        *   Selectable mySelectable;
165        *   Package::constPtr p( mySelectable.installedAsKind<Package>() );
166        * \endcode
167       */
168       template<class _Res>
169       typename ResTraits<_Res>::constPtrType installedAsKind() const
170       { return asKind<_Res>( candidateObj() ); }
171
172       /** Return the \ref candidateObj resolvable casted to a specific kind.
173        * \code
174        *   Selectable mySelectable;
175        *   Package::constPtr p( mySelectable.candidateAsKind<Package>() );
176        * \endcode
177       */
178       template<class _Res>
179       typename ResTraits<_Res>::constPtrType candidateAsKind() const
180       { return asKind<_Res>( candidateObj() ); }
181
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.
189        */
190       PoolItem setCandidate( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
191       /** \overload */
192       PoolItem setCandidate( ResObject::constPtr newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
193
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.
199        */
200       bool setOnSystem( const PoolItem & newCandidate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
201
202       /** An object you could use as pars pro toto.
203        *
204        * \return the \ref candidateObj, or ,if no available objects
205        * exist, the \ref installedObj.
206        */
207       PoolItem theObj() const;
208
209       ////////////////////////////////////////////////////////////////////////
210
211       /** \name Available objects iterators.
212        * Oredered according to solver policy. 'Best' first.
213       */
214       //@{
215       bool availableEmpty() const;
216       available_size_type availableSize() const;
217       available_iterator availableBegin() const;
218       available_iterator availableEnd() const;
219       //@}
220
221       ////////////////////////////////////////////////////////////////////////
222
223       /** \name Insatlled objects iterators.
224        * Ordered by install time. Latest first.
225       */
226       //@{
227       bool installedEmpty() const;
228       installed_size_type installedSize() const;
229       installed_iterator installedBegin() const;
230       installed_iterator installedEnd() const;
231       //}
232
233       ////////////////////////////////////////////////////////////////////////
234
235     public:
236       /** \name Query for objects within this Selectable.
237       */
238       //@{
239       /** True if either installed or candidate object is present */
240       bool hasObject() const
241       { return (! installedEmpty()) || candidateObj(); }
242
243       /** True if installed object is present. */
244       bool hasInstalledObj() const
245       { return ! installedEmpty(); }
246
247       /** True if candidate object is present. */
248       bool hasCandidateObj() const
249       { return candidateObj(); }
250
251       /** True if installed and candidate object is present */
252       bool hasBothObjects() const
253       { return (! installedEmpty()) && candidateObj(); }
254
255       /** True if installed object is present but no candidate. */
256       bool hasInstalledObjOnly() const
257       { return (! installedEmpty()) && ! candidateObj(); }
258
259       /** True if candidate object is present but no installed. */
260       bool hasCandidateObjOnly() const
261       { return ( installedEmpty() ) && candidateObj(); }
262       //@}
263
264       /**
265        * True if this package has no replacement from
266        * the available repositories
267        */
268       bool isUnmaintained() const;
269
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
274        */
275       //@{
276       /** Returns true for packages, because packages are not
277        * classified by the solver.
278       */
279       bool isUndetermined() const;
280
281       /** Returns true if the patch is relevant which means that at least
282        *  one package of the patch is installed.
283        */
284       bool isRelevant() const;
285
286       /** Whether a relevant patchs requirements are met. */
287       bool isSatisfied() const;
288
289       /** Whether a relevant patchs requirements are broken. */
290       bool isBroken() const;
291
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
295        * \c satisfied.
296        */
297       bool isNeeded() const;
298       //@}
299
300      public:
301       /** \name Query and maip objects fate in case of commit.
302       */
303       //@{
304       enum Fate {
305         TO_DELETE  = -1,
306         UNMODIFIED = 0,
307         TO_INSTALL = 1
308       };
309       /**  */
310       Fate fate() const;
311
312       /** True if neither to delete or to install */
313       bool unmodified() const
314       { return fate() == UNMODIFIED; }
315
316       /** True if locked (subclass of unmodified). */
317       bool locked() const
318       { Status st( status() ); return( st == S_Protected || st == S_Taboo ); }
319
320       /** True if either to delete or to install */
321       bool toModify() const
322       { return fate() != UNMODIFIED; }
323
324       /** True if to delete */
325       bool toDelete() const
326       { return fate() == TO_DELETE; }
327
328       /** True if to install */
329       bool toInstall() const
330       { return fate() == TO_INSTALL; }
331
332       /** True if would be on system after commit. */
333       bool onSystem() const
334       { return( ( hasInstalledObj() && !toDelete() )
335               ||( hasCandidateObj() && toInstall() ) ); }
336
337       /** True if would be off system after commit. */
338       bool offSystem() const
339       { return ! onSystem(); }
340
341       /** */
342       bool setFate( Fate fate_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
343
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 ); }
347
348       /** Take care the item gets installed if it is not. */
349       bool setInstalled( ResStatus::TransactByValue causer_r = ResStatus::USER );
350
351       /** Take care the item gets installed if it is not, or is older. */
352       bool setUpToDate( ResStatus::TransactByValue causer_r = ResStatus::USER );
353
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 ); }
357
358       /** Take care the item gets deleted if it is installed. */
359       bool setDeleted( ResStatus::TransactByValue causer_r = ResStatus::USER );
360
361       /** Set the item to stay unmodified. */
362       bool unset( ResStatus::TransactByValue causer_r = ResStatus::USER )
363       { return setFate( UNMODIFIED, causer_r ); }
364       //@}
365
366     public:
367       /**
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.
372       */
373       //@{
374       /** Return the current Status */
375       Status status() const;
376
377       /**
378        * Try to set a new Status.
379        * Returns \c false if the transitions is not allowed.
380        */
381       bool setStatus( const Status state_r, ResStatus::TransactByValue causer_r = ResStatus::USER );
382
383       /** Return who caused the modification. */
384       ResStatus::TransactByValue modifiedBy() const;
385
386       /** Return value of LicenceConfirmed bit. */
387       bool hasLicenceConfirmed() const;
388
389       /** Set LicenceConfirmed bit. */
390       void setLicenceConfirmed( bool val_r = true );
391       //@}
392
393     public:
394       /** Implementation  */
395       class Impl;
396       typedef shared_ptr<Impl> Impl_Ptr;
397       /** Default ctor */
398       Selectable( Impl_Ptr pimpl_r );
399     private:
400       /** Dtor */
401       ~Selectable();
402     private:
403       /** Pointer to implementation */
404       RW_pointer<Impl> _pimpl;
405     };
406     ///////////////////////////////////////////////////////////////////
407
408     /** \relates Selectable Stream output */
409     std::ostream & operator<<( std::ostream & str, const Selectable & obj );
410
411     /** \relates Selectable More verbose stream output */
412     std::ostream & dumpOn( std::ostream & str, const Selectable & obj );
413
414     /** Solvable to Selectable transform functor.
415      * \relates Selectable
416      * \relates sat::SolvIterMixin
417      */
418     struct asSelectable
419     {
420       typedef Selectable_Ptr result_type;
421
422       Selectable_Ptr operator()( const sat::Solvable & solv_r ) const;
423
424       Selectable_Ptr operator()( const PoolItem & pi_r ) const
425       { return operator()( pi_r.satSolvable() ); }
426     };
427
428     /////////////////////////////////////////////////////////////////
429   } // namespace ui
430   ///////////////////////////////////////////////////////////////////
431   /////////////////////////////////////////////////////////////////
432 } // namespace zypp
433 ///////////////////////////////////////////////////////////////////
434 #endif // ZYPP_UI_SELECTABLE_H