Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / Patch.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Patch.h
10  *
11 */
12 #ifndef ZYPP_PATCH_H
13 #define ZYPP_PATCH_H
14
15 #include "zypp/base/Flags.h"
16 #include "zypp/sat/SolvAttr.h"
17 #include "zypp/ResObject.h"
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23
24   DEFINE_PTR_TYPE(Patch);
25
26
27   /**
28    * Class representing a patch.
29    *
30    * A patch represents a specific problem that
31    * can be fixed by pulling in the patch dependencies.
32    *
33    * Patches can be marked for installation but their
34    * installation is a no-op.
35    */
36   class Patch : public ResObject
37   {
38     public:
39       typedef Patch                    Self;
40       typedef ResTraits<Self>          TraitsType;
41       typedef TraitsType::PtrType      Ptr;
42       typedef TraitsType::constPtrType constPtr;
43
44     public:
45       typedef sat::SolvableSet Contents;
46
47       enum Category {
48         CAT_OTHER       = 1,    //!< unknown value specified
49         CAT_YAST        = 1<<1, //!<
50         CAT_SECURITY    = 1<<2, //!<
51         CAT_RECOMMENDED = 1<<3, //!<
52         CAT_OPTIONAL    = 1<<4, //!<
53         CAT_DOCUMENT    = 1<<5  //!<
54       };
55       ZYPP_DECLARE_FLAGS(Categories, Category);
56
57       /**
58        * Flags defining if and why this
59        * patch is interactive.
60        */
61       enum InteractiveFlag {
62         NoFlags = 0x0000,
63         Reboot  = 0x0001,
64         Message = 0x0002,
65         License = 0x0004
66       };
67       ZYPP_DECLARE_FLAGS(InteractiveFlags, InteractiveFlag);
68
69       /**
70        * \brief Possible severity levels for (security) patches.
71        * Metadata string values are mapped to this enum to ease
72        * computations. For a string representation call
73        * \ref asSring( const Patch::SeverityFlag & ).
74        */
75       enum SeverityFlag {
76         SEV_OTHER       = 1,    //!< unknown value specified
77         SEV_NONE        = 1<<1, //!< no value specified
78         SEV_LOW         = 1<<2, //!< Low
79         SEV_MODERATE    = 1<<3, //!< Moderate
80         SEV_IMPORTANT   = 1<<4, //!< Important
81         SEV_CRITICAL    = 1<<5  //!< Critical
82       };
83       ZYPP_DECLARE_FLAGS(SeverityFlags, SeverityFlag);
84
85     public:
86       /**
87        * Issue date time. For now it is the same as
88        * \ref buildtime().
89        */
90       Date timestamp() const
91       { return buildtime(); }
92
93       /** \name Patch Category */
94       //@{
95       /**
96        * Patch category (recommended, security,...)
97        */
98       std::string category() const;
99
100       /** This patch's category as enum of wellknown categories.
101        * Unknown values are mapped to \ref CAT_OTHER.
102        */
103       Category categoryEnum() const;
104
105       /** Whether this patch's category matches \a category_r */
106       bool isCategory( const std::string & category_r ) const;
107       /** \overload taking OR'ed \ref Categories */
108       bool isCategory( Categories category_r ) const;
109 #ifndef SWIG // Swig treats it as syntax error
110       /** \overload taking container of category strings
111        * 2nd template arg just to prevent instantiation for Category
112        */
113       template <class TContainer, typename = typename TContainer::value_type>
114       bool isCategory( const TContainer & categories_r ) const
115       {
116         for ( const std::string & el : categories_r )
117         { if ( isCategory( el ) ) return true; }
118         return false;
119       }
120 #endif
121       /** Patch category as enum of wellknown categories.
122        * Unknown values are mapped to \ref CAT_OTHER.
123        */
124       static Category categoryEnum( const std::string & category_r );
125       //@}
126
127       /** \name Patch Severity */
128       //@{
129       /**
130        * Severity string as specified in metadata.
131        * For use in computaions see \ref severityFlag.
132        */
133       std::string severity() const;
134
135       /**
136        * Severity string mapped to an enum.
137        * Unknown string values are mapped to \ref SEV_OTHER
138        */
139       SeverityFlag severityFlag() const;
140
141       /** Whether this patch's severity matches \a severity_r */
142       bool isSeverity( const std::string & severity_r ) const;
143       /** \overload taking OR'ed \ref SeverityFlags */
144       bool isSeverity( SeverityFlags severity_r ) const;
145 #ifndef SWIG // Swig treats it as syntax error
146       /** \overload taking container of severity strings
147        * 2nd template arg just to prevent instantiation for SeverityFlag
148        */
149       template <class TContainer, typename = typename TContainer::value_type>
150       bool isSeverity( const TContainer & severities_r ) const
151       {
152         for ( const std::string & el : severities_r )
153         { if ( isSeverity( el ) ) return true; }
154         return false;
155       }
156 #endif
157       /** Severity string mapped to an enum.
158        * Unknown string values are mapped to \ref SEV_OTHER
159        */
160       static SeverityFlag severityFlag( const std::string & category_r );
161       //@}
162
163       /**
164        * Does the system need to reboot to finish the update process?
165        */
166       bool rebootSuggested() const;
167
168       /**
169        * Does the patch affect the package manager itself?
170        * restart is suggested then
171        */
172       bool restartSuggested() const;
173
174       /**
175        * Does the patch needs the user to relogin to take effect?
176        * relogin is suggested then
177        */
178       bool reloginSuggested() const;
179
180       /**
181        * \short Information or warning to be displayed to the user
182        */
183       std::string message( const Locale & lang_r = Locale() ) const;
184
185       /**
186        * Get the InteractiveFlags of this Patch
187        */
188       InteractiveFlags interactiveFlags() const;
189
190       /**
191        * Is the patch still interactive when ignoring this flags?
192        */
193       bool interactiveWhenIgnoring( InteractiveFlags flags_r = NoFlags ) const;
194
195       /**
196        * Is the patch installation interactive? (does it need user input?)
197        *
198        * For security reasons patches requiring a reboot are not
199        * installed in an unattended mode. They are considered to be
200        * \c interactive so the user gets informed about the need for
201        * reboot. \a ignoreRebootFlag_r may be used to explicitly turn
202        * off this behavior and include those patches (unless they actually
203        * contain interactive components as well, like messages or licenses).
204        */
205       bool interactive() const;
206
207     public:
208       /**
209        * The collection of packages associated with this patch.
210        */
211       Contents contents() const;
212
213     public:
214
215       /** Query class for Patch issue references */
216       class ReferenceIterator;
217       /**
218        * Get an iterator to the beginning of the patch
219        * references. \see Patch::ReferenceIterator
220        */
221       ReferenceIterator referencesBegin() const;
222       /**
223        * Get an iterator to the end of the patch
224        * references. \see Patch::ReferenceIterator
225        */
226       ReferenceIterator referencesEnd() const;
227
228     protected:
229       friend Ptr make<Self>( const sat::Solvable & solvable_r );
230       /** Ctor */
231       Patch( const sat::Solvable & solvable_r );
232       /** Dtor */
233       virtual ~Patch();
234   };
235   ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Patch::Categories);
236   ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Patch::InteractiveFlags);
237   ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Patch::SeverityFlags);
238
239   /** \relates Patch::Category string representation.*/
240   std::string asString( const Patch::Category & obj );
241
242   /** \relates Patch::InteractiveFlag string representation.*/
243   std::string asString( const Patch::InteractiveFlag & obj );
244
245   /** \relates Patch::SeverityFlag string representation.*/
246   std::string asString( const Patch::SeverityFlag & obj );
247
248   /**
249    * Query class for Patch issue references
250    * like bugzilla and security issues the
251    * patch is supposed to fix.
252    *
253    * The iterator does not provide a dereference
254    * operator so you can do * on it, but you can
255    * access the attributes of each patch issue reference
256    * directly from the iterator.
257    *
258    * \code
259    * for ( Patch::ReferenceIterator it = patch->referencesBegin();
260    *       it != patch->referencesEnd();
261    *       ++it )
262    * {
263    *   cout << it.href() << endl;
264    * }
265    * \endcode
266    *
267    */
268   class Patch::ReferenceIterator : public boost::iterator_adaptor<
269       Patch::ReferenceIterator           // Derived
270       , sat::LookupAttr::iterator        // Base
271       , int                              // Value
272       , boost::forward_traversal_tag     // CategoryOrTraversal
273       , int                              // Reference
274   >
275   {
276     public:
277       ReferenceIterator() {}
278       explicit ReferenceIterator( const sat::Solvable & val_r );
279
280       /**
281        * The id of the reference. For bugzilla entries
282        * this is the bug number as a string.
283        */
284       std::string id() const;
285       /**
286        * Url or pointer where to find more information
287        */
288       std::string href() const;
289       /**
290        * Title describing the issue
291        */
292       std::string title() const;
293       /**
294        * Type of the reference. For example
295        * "bugzilla"
296        */
297       std::string type() const;
298
299     private:
300       friend class boost::iterator_core_access;
301       int dereference() const { return 0; }
302   };
303
304   inline Patch::ReferenceIterator Patch::referencesBegin() const
305   { return ReferenceIterator(satSolvable()); }
306
307   inline Patch::ReferenceIterator Patch::referencesEnd() const
308   { return ReferenceIterator(); }
309
310   /////////////////////////////////////////////////////////////////
311
312 } // namespace zypp
313 ///////////////////////////////////////////////////////////////////
314 #endif // ZYPP_PATCH_H