- Fix permanent duplication of gpg keys in the rpm database. Also
[platform/upstream/libzypp.git] / zypp / PublicKey.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/PublicKey.h
10  *
11 */
12 #ifndef ZYPP_PUBLICKEY_H
13 #define ZYPP_PUBLICKEY_H
14
15 #include <iosfwd>
16 #include <map>
17 #include <list>
18 #include <set>
19 #include <string>
20
21 #include "zypp/base/PtrTypes.h"
22 #include "zypp/base/Exception.h"
23 #include "zypp/Pathname.h"
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28
29   /**
30    * Exception thrown when the supplied key is
31    * not a valid gpg key
32    */
33   class BadKeyException : public Exception
34   {
35     public:
36       /** Ctor taking message.
37      * Use \ref ZYPP_THROW to throw exceptions.
38        */
39       BadKeyException()
40       : Exception( "Bad Key Exception" )
41       {}
42
43       Pathname keyFile() const
44       { return _keyfile; }
45
46       /** Ctor taking message.
47        * Use \ref ZYPP_THROW to throw exceptions.
48        */
49       BadKeyException( const std::string & msg_r, const Pathname &keyfile = Pathname() )
50       : Exception( msg_r ), _keyfile(keyfile)
51       {}
52       /** Dtor. */
53       virtual ~BadKeyException() throw() {};
54     private:
55       Pathname _keyfile;
56   };
57
58
59   // forward declaration of class Date
60   class Date;
61
62   ///////////////////////////////////////////////////////////////////
63   //
64   //    CLASS NAME : PublicKey
65   //
66   /**
67    * Class that represent a GPG Public Key
68    */
69   class PublicKey
70   {
71     friend std::ostream & operator<<( std::ostream & str, const PublicKey & obj );
72
73   public:
74     /** Implementation  */
75     class Impl;
76
77   public:
78     PublicKey();
79    /** Ctor
80     * \throws when data does not make a key
81     */
82     PublicKey(const Pathname &file);
83
84     ~PublicKey();
85
86     bool isValid() const
87     { return ( ! id().empty() && ! fingerprint().empty() && !path().empty() ); }
88
89     std::string asString() const;
90     std::string armoredData() const;
91     std::string id() const;
92     std::string name() const;
93     std::string fingerprint() const;
94
95     /**
96      * Date when the key was created (time is 00:00:00)
97      */
98     Date created() const;
99
100     /**
101      * Date when the key expires (time is 00:00:00)
102      * If the key never expires the date is Date() (i.e. 0 seconds since the epoch (1.1.1970))
103      */
104     Date expires() const;
105
106     Pathname path() const;
107
108     bool operator==( PublicKey b ) const;
109     bool operator==( std::string sid ) const;
110
111   private:
112     /** Pointer to implementation */
113     RWCOW_pointer<Impl> _pimpl;
114   };
115   ///////////////////////////////////////////////////////////////////
116
117   /** \relates PublicKey Stream output */
118   inline std::ostream & operator<<( std::ostream & str, const PublicKey & obj )
119   { return str << obj.asString(); }
120
121   /** \relates PublicKey Detailed stream output */
122   std::ostream & dumpOn( std::ostream & str, const PublicKey & obj );
123
124  /////////////////////////////////////////////////////////////////
125 } // namespace zypp
126 ///////////////////////////////////////////////////////////////////
127 #endif // ZYPP_PUBLICKEY_H