1/*
2 configuration.h - wraps gpgme configuration components
3 Copyright (C) 2010 Klarälvdalens Datakonsult AB
4
5 This file is part of GPGME++.
6
7 GPGME++ is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 GPGME++ is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with GPGME++; see the file COPYING.LIB. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23// -*- c++ -*-
24#ifndef __GPGMEPP_CONFIGURATION_H__
25#define __GPGMEPP_CONFIGURATION_H__
26
27#include <gpgme++/global.h>
28
29#include <gpgme++/gpgmefw.h>
30
31#include <boost/shared_ptr.hpp>
32#include <boost/weak_ptr.hpp>
33#include <boost/type_traits/remove_pointer.hpp>
34#if 0
35#include <boost/variant.hpp>
36#include <boost/optional.hpp>
37#endif
38
39#include <iosfwd>
40#include <vector>
41#include <string>
42#include <algorithm>
43
44namespace GpgME {
45namespace Configuration {
46
47 typedef boost::shared_ptr< boost::remove_pointer<gpgme_conf_comp_t>::type > shared_gpgme_conf_comp_t;
48 typedef boost::weak_ptr< boost::remove_pointer<gpgme_conf_comp_t>::type > weak_gpgme_conf_comp_t;
49
50 class Argument;
51 class Option;
52 class Component;
53
54 enum Level {
55 Basic,
56 Advanced,
57 Expert,
58 Invisible,
59 Internal,
60
61 NumLevels
62 };
63
64 enum Type {
65 NoType,
66 StringType,
67 IntegerType,
68 UnsignedIntegerType,
69
70 FilenameType = 32,
71 LdapServerType,
72 KeyFingerprintType,
73 PublicKeyType,
74 SecretKeyType,
75 AliasListType,
76
77 MaxType
78 };
79
80 enum Flag {
81 Group = ( 1 << 0 ),
82 Optional = ( 1 << 1 ),
83 List = ( 1 << 2 ),
84 Runtime = ( 1 << 3 ),
85 Default = ( 1 << 4 ),
86 DefaultDescription = ( 1 << 5 ),
87 NoArgumentDescription = ( 1 << 6 ),
88 NoChange = ( 1 << 7 ),
89
90 LastFlag = NoChange
91 };
92
93 //
94 // class Component
95 //
96
97 class GPGMEPP_EXPORT Component {
98 public:
99 Component() : comp() {}
100 explicit Component( const shared_gpgme_conf_comp_t & comp )
101 : comp( comp ) {}
102
103 // copy ctor is ok
104
105 const Component & operator=( const Component & other ) {
106 if ( this != &other ) {
107 Component( other ).swap( *this );
108 }
109 return *this;
110 }
111
112 void swap( Component & other ) {
113 using std::swap;
114 swap( this->comp, other.comp );
115 }
116
117 bool isNull() const {
118 return !comp;
119 }
120
121 static std::vector<Component> load( Error & err );
122 Error save() const;
123
124 const char * name() const;
125 const char * description() const;
126 const char * programName() const;
127
128 Option option( unsigned int index ) const;
129 Option option( const char * name ) const;
130
131 unsigned int numOptions() const;
132
133 std::vector<Option> options() const;
134
135 GPGMEPP_MAKE_SAFE_BOOL_OPERATOR( !isNull() )
136 private:
137 shared_gpgme_conf_comp_t comp;
138 };
139
140 //
141 // class Option
142 //
143
144 class GPGMEPP_EXPORT Option {
145 public:
146 Option() : comp(), opt( 0 ) {}
147 Option( const shared_gpgme_conf_comp_t & comp, gpgme_conf_opt_t opt )
148 : comp( comp ), opt( opt ) {}
149
150 const Option & operator=( const Option & other ) {
151 if ( this != &other ) {
152 Option( other ).swap( *this );
153 }
154 return *this;
155 }
156
157 void swap( Option & other ) {
158 using std::swap;
159 swap( this->comp, other.comp );
160 swap( this->opt, other.opt );
161 }
162
163 bool isNull() const {
164 return comp.expired() || !opt;
165 }
166
167 Component parent() const;
168
169 unsigned int flags() const;
170
171 Level level() const;
172
173 const char * name() const;
174 const char * description() const;
175 const char * argumentName() const;
176
177 Type type() const;
178 Type alternateType() const;
179
180 Argument defaultValue() const;
181 const char * defaultDescription() const;
182
183 Argument noArgumentValue() const;
184 const char * noArgumentDescription() const;
185
186 /*! The value that is in the config file (or null, if it's not set). */
187 Argument activeValue() const;
188 /*! The value that is in this object, ie. either activeValue(), newValue(), or defaultValue() */
189 Argument currentValue() const;
190
191 Argument newValue() const;
192 bool set() const;
193 bool dirty() const;
194
195 Error setNewValue( const Argument & argument );
196 Error resetToDefaultValue();
197 Error resetToActiveValue();
198
199 Argument createNoneArgument( bool set ) const;
200 Argument createStringArgument( const char * value ) const;
201 Argument createStringArgument( const std::string & value ) const;
202 Argument createIntArgument( int value ) const;
203 Argument createUIntArgument( unsigned int value ) const;
204
205 Argument createNoneListArgument( unsigned int count ) const;
206 Argument createStringListArgument( const std::vector<const char *> & value ) const;
207 Argument createStringListArgument( const std::vector<std::string> & value ) const;
208 Argument createIntListArgument( const std::vector<int> & values ) const;
209 Argument createUIntListArgument( const std::vector<unsigned int> & values ) const;
210
211 GPGMEPP_MAKE_SAFE_BOOL_OPERATOR( !isNull() )
212 private:
213 weak_gpgme_conf_comp_t comp;
214 gpgme_conf_opt_t opt;
215 };
216
217 //
218 // class Argument
219 //
220
221 class GPGMEPP_EXPORT Argument {
222 friend class ::GpgME::Configuration::Option;
223 Argument( const shared_gpgme_conf_comp_t & comp, gpgme_conf_opt_t opt, gpgme_conf_arg_t arg, bool owns );
224 public:
225 Argument() : comp(), opt( 0 ), arg( 0 ) {}
226 //Argument( const shared_gpgme_conf_comp_t & comp, gpgme_conf_opt_t opt, gpgme_conf_arg_t arg );
227 Argument( const Argument & other );
228 ~Argument();
229
230 const Argument & operator=( const Argument & other ) {
231 if ( this != &other ) {
232 Argument( other ).swap( *this );
233 }
234 return *this;
235 }
236
237 void swap( Argument & other ) {
238 using std::swap;
239 swap( this->comp, other.comp );
240 swap( this->opt, other.opt );
241 swap( this->arg, other.arg );
242 }
243
244 bool isNull() const {
245 return comp.expired() || !opt || !arg;
246 }
247
248 Option parent() const;
249
250 unsigned int numElements() const;
251
252 bool boolValue() const;
253 const char * stringValue( unsigned int index=0 ) const;
254 int intValue( unsigned int index=0 ) const;
255 unsigned int uintValue( unsigned int index=0 ) const;
256
257 unsigned int numberOfTimesSet() const;
258 std::vector<const char *> stringValues() const;
259 std::vector<int> intValues() const;
260 std::vector<unsigned int> uintValues() const;
261
262 GPGMEPP_MAKE_SAFE_BOOL_OPERATOR( !isNull() )
263 private:
264 weak_gpgme_conf_comp_t comp;
265 gpgme_conf_opt_t opt;
266 gpgme_conf_arg_t arg;
267 };
268
269 GPGMEPP_EXPORT std::ostream & operator<<( std::ostream & os, Level level );
270 GPGMEPP_EXPORT std::ostream & operator<<( std::ostream & os, Type type );
271 GPGMEPP_EXPORT std::ostream & operator<<( std::ostream & os, Flag flag );
272 GPGMEPP_EXPORT std::ostream & operator<<( std::ostream & os, const Component & component );
273 GPGMEPP_EXPORT std::ostream & operator<<( std::ostream & os, const Option & option );
274 GPGMEPP_EXPORT std::ostream & operator<<( std::ostream & os, const Argument & argument );
275
276} // namespace Configuration
277} // namespace GpgME
278
279GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION( Configuration::Component )
280GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION( Configuration::Option )
281GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION( Configuration::Argument )
282
283#endif // __GPGMEPP_CONFIGURATION_H__
284