Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / HistoryLogData.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9
10 /** \file zypp/HistoryLogData.h
11  *
12  */
13 #ifndef ZYPP_HISTORYLOGDATA_H_
14 #define ZYPP_HISTORYLOGDATA_H_
15
16 #include <iosfwd>
17
18 #include "zypp/APIConfig.h"
19 #include "zypp/Date.h"
20 #include "zypp/Edition.h"
21 #include "zypp/Arch.h"
22 #include "zypp/CheckSum.h"
23 #include "zypp/Url.h"
24
25 #define HISTORY_LOG_DATE_FORMAT "%Y-%m-%d %H:%M:%S"
26
27 ///////////////////////////////////////////////////////////////////
28 namespace zypp
29 {
30   ///////////////////////////////////////////////////////////////////
31   /// \class HistoryActionID
32   /// \brief Enumeration of known history actions.
33   /// \ingroup g_EnumerationClass
34   /// \ingroup g_ZyppHistory
35   ///////////////////////////////////////////////////////////////////
36   struct HistoryActionID
37   {
38     static const HistoryActionID NONE;
39
40     static const HistoryActionID INSTALL;
41     static const HistoryActionID REMOVE;
42     static const HistoryActionID REPO_ADD;
43     static const HistoryActionID REPO_REMOVE;
44     static const HistoryActionID REPO_CHANGE_ALIAS;
45     static const HistoryActionID REPO_CHANGE_URL;
46     static const HistoryActionID STAMP_COMMAND;
47
48     enum ID
49     {
50       NONE_e,
51
52       INSTALL_e,
53       REMOVE_e,
54       REPO_ADD_e,
55       REPO_REMOVE_e,
56       REPO_CHANGE_ALIAS_e,
57       REPO_CHANGE_URL_e,
58       STAMP_COMMAND_e
59     };
60
61     HistoryActionID() : _id(NONE_e) {}
62
63     HistoryActionID(ID id) : _id(id) {}
64
65     explicit HistoryActionID(const std::string & strval_r);
66
67     ID toEnum() const { return _id; }
68
69     static HistoryActionID::ID parse(const std::string & strval_r);
70
71     const std::string & asString(bool pad = false) const;
72
73     private:
74     ID _id;
75   };
76
77   /** \relates HistoryActionID */
78   inline bool operator==( const HistoryActionID & lhs, const HistoryActionID & rhs )
79   { return lhs.toEnum() == rhs.toEnum(); }
80
81   /** \relates HistoryActionID */
82   inline bool operator!=( const HistoryActionID & lhs, const HistoryActionID & rhs )
83   { return lhs.toEnum() != rhs.toEnum(); }
84
85   /** \relates HistoryActionID */
86   std::ostream & operator << (std::ostream & str, const HistoryActionID & id);
87   ///////////////////////////////////////////////////////////////////
88
89   ///////////////////////////////////////////////////////////////////
90   /// \class HistoryLogData
91   /// \brief A zypp history log line split into fields
92   /// \ingroup g_ZyppHistory
93   ///
94   /// Each valid history log line starts with a date and HistoryActionID
95   /// field. Subsequent fields depend on the kind of action. See derived
96   /// classes for convenient access to those flields.
97   ///
98   /// HistoryLogData itself provides mostly generic access to the fields
99   /// plain string values. Derived classes for well known entries tell
100   ///
101   ///////////////////////////////////////////////////////////////////
102   class HistoryLogData
103   {
104   public:
105     typedef shared_ptr<HistoryLogData>          Ptr;
106     typedef shared_ptr<const HistoryLogData>    constPtr;
107
108     typedef std::vector<std::string>    FieldVector;
109     typedef FieldVector::size_type      size_type;
110     typedef FieldVector::const_iterator const_iterator;
111
112   public:
113     /** Ctor \b moving \a FieldVector (via swap).
114      * \throws ParseException if \a fields_r has not at least \a expect_r entries
115      * \note 2 fields (date and action) are always required.
116      */
117     explicit HistoryLogData( FieldVector & fields_r, size_type expect_r = 2 );
118
119     /** Ctor \b moving \a FieldVector (via swap).
120      * \throws ParseException if \a fields_r has the wrong \ref HistoryActionID or not at least \a expect_r entries.
121      * \note 2 fields (date and action) are always required.
122      */
123     HistoryLogData( FieldVector & fields_r, HistoryActionID action_r, size_type expect_r = 2 );
124
125     /** Dtor */
126     virtual ~HistoryLogData();
127
128     /** Factory method creating HistoryLogData classes.
129      *
130      * Moves \a fields_r into a HistoryLogData or derived object, depending on the
131      * HistoryActionID. For known action ids a coresponing HistoryLogData class
132      * is created, to allow convenient access to the field values. For unknown
133      * action ids a plain HistoryLogData object is created. \ref HistoryActionID
134      * \ref NONE_e id used in this case.
135      *
136      * \throws ParseException if \a fields_r does not contain the required format.
137      */
138     static Ptr create( FieldVector & fields_r );
139
140   public:
141     /** Whether FieldVector is empty. */
142     bool empty() const;
143
144     /** Number of fields in vector. */
145     size_type size() const;
146
147     /** Iterator pointing to 1st element in vector (or end()). */
148     const_iterator begin() const;
149
150     /** Iterator pointing behind the last element in vector. */
151     const_iterator end() const;
152
153     /** Access (optional) field by number.
154      * \returns an empty string if \a idx_r is out of range.
155      * \see \ref at
156      */
157     const std::string & optionalAt( size_type idx_r ) const;
158     /** \overload */
159     const std::string & operator[]( size_type idx_r ) const
160     { return optionalAt( idx_r ); }
161
162     /** Access (required) field by number.
163      * \throws std::out_of_range if \a idx_r is out of range.
164      * \see \ref optionalAt
165      */
166     const std::string & at( size_type idx_r ) const;
167
168   public:
169     enum Index                  ///< indices of known fields
170     {
171       DATE_INDEX        = 0,    ///< date
172       ACTION_INDEX      = 1,    ///< HistoryActionID
173     };
174
175   public:
176     Date        date()          const;  ///< date
177     HistoryActionID action()    const;  ///< HistoryActionID (or \c NONE_e if unknown)
178
179   public:
180     class Impl;                 ///< Implementation class
181   private:
182     RWCOW_pointer<Impl> _pimpl; ///< Pointer to implementation
183   protected:
184     HistoryLogData & operator=( const HistoryLogData & ); ///< no base class assign
185   };
186
187   /** \relates HistoryLogData Stream output */
188   std::ostream & operator<<( std::ostream & str, const HistoryLogData & obj );
189   ///////////////////////////////////////////////////////////////////
190
191   ///////////////////////////////////////////////////////////////////
192   /// \class HistoryLogDataInstall
193   /// \brief  A zypp history log line for an installed packaged.
194   /// \ingroup g_ZyppHistory
195   ///////////////////////////////////////////////////////////////////
196   class HistoryLogDataInstall : public HistoryLogData
197   {
198   public:
199     typedef shared_ptr<HistoryLogDataInstall>           Ptr;
200     typedef shared_ptr<const HistoryLogDataInstall>     constPtr;
201     /** Ctor \b moving \a FieldVector (via swap).
202      * \throws ParseException if \a fields_r has the wrong \ref HistoryActionID or number of fields.
203      */
204     HistoryLogDataInstall( FieldVector & fields_r );
205
206   public:
207     enum Index                  ///< indices of known fields
208     {
209       DATE_INDEX        = HistoryLogData::DATE_INDEX,
210       ACTION_INDEX      = HistoryLogData::ACTION_INDEX,
211       NAME_INDEX,               ///< package name
212       EDITION_INDEX,            ///< package edition
213       ARCH_INDEX,               ///< package architecture
214       REQBY_INDEX,              ///< requested by (user@hostname, pid:appname, or empty (solver))
215       REPOALIAS_INDEX,          ///< repository providing the package
216       CHEKSUM_INDEX,            ///< package checksum
217       USERDATA_INDEX,           ///< userdata/transactionID
218     };
219
220    public:
221     std::string name()          const;  ///< package name
222     Edition     edition()       const;  ///< package edition
223     Arch        arch()          const;  ///< package architecture
224     std::string reqby()         const;  ///< requested by (user@hostname, pid:appname, or empty (solver))
225     std::string repoAlias()     const;  ///< repository providing the package
226     CheckSum    checksum()      const;  ///< package checksum
227     std::string userdata()      const;  ///< userdata/transactionID
228   };
229
230   ///////////////////////////////////////////////////////////////////
231   /// \class HistoryLogDataRemove
232   /// \brief A zypp history log line for a removed packge.
233   /// \ingroup g_ZyppHistory
234   ///////////////////////////////////////////////////////////////////
235   class HistoryLogDataRemove : public HistoryLogData
236   {
237   public:
238     typedef shared_ptr<HistoryLogDataRemove>            Ptr;
239     typedef shared_ptr<const HistoryLogDataRemove>      constPtr;
240     /** Ctor \b moving \a FieldVector (via swap).
241      * \throws ParseException if \a fields_r has the wrong \ref HistoryActionID or number of fields.
242      */
243     HistoryLogDataRemove( FieldVector & fields_r );
244
245   public:
246     enum Index                  ///< indices of known fields
247     {
248       DATE_INDEX        = HistoryLogData::DATE_INDEX,
249       ACTION_INDEX      = HistoryLogData::ACTION_INDEX,
250       NAME_INDEX,               ///< package name
251       EDITION_INDEX,            ///< package edition
252       ARCH_INDEX,               ///< package architecture
253       REQBY_INDEX,              ///< requested by (user@hostname, pid:appname, or empty (solver))
254       USERDATA_INDEX,           ///< userdata/transactionID
255     };
256
257   public:
258     std::string name()          const;  ///< package name
259     Edition     edition()       const;  ///< package edition
260     Arch        arch()          const;  ///< package architecture
261     std::string reqby()         const;  ///< requested by (user@hostname, pid:appname, or empty (solver))
262     std::string userdata()      const;  ///< userdata/transactionID
263   };
264
265   ///////////////////////////////////////////////////////////////////
266   /// \class HistoryLogDataRepoAdd
267   /// \brief A zypp history log line for an added repository.
268   /// \ingroup g_ZyppHistory
269   ///////////////////////////////////////////////////////////////////
270   class HistoryLogDataRepoAdd : public HistoryLogData
271   {
272   public:
273     typedef shared_ptr<HistoryLogDataRepoAdd>           Ptr;
274     typedef shared_ptr<const HistoryLogDataRepoAdd>     constPtr;
275     /** Ctor \b moving \a FieldVector (via swap).
276      * \throws ParseException if \a fields_r has the wrong \ref HistoryActionID or number of fields.
277      */
278     HistoryLogDataRepoAdd( FieldVector & fields_r );
279
280   public:
281     enum Index                  ///< indices of known fields
282     {
283       DATE_INDEX        = HistoryLogData::DATE_INDEX,
284       ACTION_INDEX      = HistoryLogData::ACTION_INDEX,
285       ALIAS_INDEX,              ///< repository alias
286       URL_INDEX,                ///< repository url
287       USERDATA_INDEX,           ///< userdata/transactionID
288     };
289
290   public:
291     std::string alias()         const;  ///< repository alias
292     Url         url()           const;  ///< repository url
293     std::string userdata()      const;  ///< userdata/transactionID
294   };
295
296   ///////////////////////////////////////////////////////////////////
297   /// \class HistoryLogDataRepoRemove
298   /// \brief A zypp history log line for a removed repository.
299   /// \ingroup g_ZyppHistory
300   ///////////////////////////////////////////////////////////////////
301   class HistoryLogDataRepoRemove : public HistoryLogData
302   {
303   public:
304     typedef shared_ptr<HistoryLogDataRepoRemove>        Ptr;
305     typedef shared_ptr<const HistoryLogDataRepoRemove>  constPtr;
306     /** Ctor \b moving \a FieldVector (via swap).
307      * \throws ParseException if \a fields_r has the wrong \ref HistoryActionID or number of fields.
308      */
309     HistoryLogDataRepoRemove( FieldVector & fields_r );
310
311   public:
312     enum Index                  ///< indices of known fields
313     {
314       DATE_INDEX        = HistoryLogData::DATE_INDEX,
315       ACTION_INDEX      = HistoryLogData::ACTION_INDEX,
316       ALIAS_INDEX,              ///< repository alias
317       USERDATA_INDEX,           ///< userdata/transactionID
318     };
319
320   public:
321     std::string alias()         const;  ///< repository alias
322     std::string userdata()      const;  ///< userdata/transactionID
323   };
324
325   ///////////////////////////////////////////////////////////////////
326   /// \class HistoryLogDataRepoAliasChange
327   /// \brief A zypp history log line for a repo alias change.
328   /// \ingroup g_ZyppHistory
329   ///////////////////////////////////////////////////////////////////
330   class HistoryLogDataRepoAliasChange : public HistoryLogData
331   {
332   public:
333     typedef shared_ptr<HistoryLogDataRepoAliasChange>           Ptr;
334     typedef shared_ptr<const HistoryLogDataRepoAliasChange>     constPtr;
335     /** Ctor \b moving \a FieldVector (via swap).
336      * \throws ParseException if \a fields_r has the wrong \ref HistoryActionID or number of fields.
337      */
338     HistoryLogDataRepoAliasChange( FieldVector & fields_r );
339
340   public:
341     enum Index                  ///< indices of known fields
342     {
343       DATE_INDEX        = HistoryLogData::DATE_INDEX,
344       ACTION_INDEX      = HistoryLogData::ACTION_INDEX,
345       OLDALIAS_INDEX,           ///< repositories old alias
346       NEWALIAS_INDEX,           ///< repositories new alias
347       USERDATA_INDEX,           ///< userdata/transactionID
348    };
349
350   public:
351     std::string oldAlias()      const;  ///< repositories old alias
352     std::string newAlias()      const;  ///< repositories new alias
353     std::string userdata()      const;  ///< userdata/transactionID
354   };
355
356   ///////////////////////////////////////////////////////////////////
357   /// \class HistoryLogDataRepoUrlChange
358   /// \brief A zypp history log line for a repo url change.
359   /// \ingroup g_ZyppHistory
360   ///////////////////////////////////////////////////////////////////
361   class HistoryLogDataRepoUrlChange : public HistoryLogData
362   {
363   public:
364     typedef shared_ptr<HistoryLogDataRepoUrlChange>             Ptr;
365     typedef shared_ptr<const HistoryLogDataRepoUrlChange>       constPtr;
366     /** Ctor \b moving \a FieldVector (via swap).
367      * \throws ParseException if \a fields_r has the wrong \ref HistoryActionID or number of fields.
368      */
369     HistoryLogDataRepoUrlChange( FieldVector & fields_r );
370
371   public:
372     enum Index                  ///< indices of known fields
373     {
374       DATE_INDEX        = HistoryLogData::DATE_INDEX,
375       ACTION_INDEX      = HistoryLogData::ACTION_INDEX,
376       ALIAS_INDEX,              ///< repository alias
377       NEWURL_INDEX,             ///< repositories new url
378       USERDATA_INDEX,           ///< userdata/transactionID
379    };
380
381   public:
382     std::string alias()         const;  ///< repository alias
383     Url         newUrl()        const;  ///< repositories new url
384     std::string userdata()      const;  ///< userdata/transactionID
385   };
386
387   ///////////////////////////////////////////////////////////////////
388   /// \class HistoryLogDataStampCommand
389   /// \brief A zypp history log line identifying the program that
390   /// triggered the following commit.
391   /// \ingroup g_ZyppHistory
392   ///////////////////////////////////////////////////////////////////
393   class HistoryLogDataStampCommand : public HistoryLogData
394   {
395   public:
396     typedef shared_ptr<HistoryLogDataStampCommand>              Ptr;
397     typedef shared_ptr<const HistoryLogDataStampCommand>        constPtr;
398     /** Ctor \b moving \a FieldVector (via swap).
399      * \throws ParseException if \a fields_r has the wrong \ref HistoryActionID or number of fields.
400      */
401     HistoryLogDataStampCommand( FieldVector & fields_r );
402
403   public:
404     enum Index                  ///< indices of known fields
405     {
406       DATE_INDEX        = HistoryLogData::DATE_INDEX,
407       ACTION_INDEX      = HistoryLogData::ACTION_INDEX,
408       USER_INDEX,               ///< executed by user@hostname
409       COMMAND_INDEX,            ///< the commandline executed
410       USERDATA_INDEX,           ///< userdata/transactionID
411    };
412
413   public:
414     std::string executedBy()            const;  ///< executed by user@hostname
415     std::string command()               const;  ///< the commandline executed
416     std::string userdata()              const;  ///< userdata/transactionID
417   };
418
419 } // namespace zypp
420 ///////////////////////////////////////////////////////////////////
421 #endif /* ZYPP_HISTORYLOGDATA_H_ */