/[winpt]/trunk/Src/wptGPGMEWrapper.cpp
ViewVC logotype

Annotation of /trunk/Src/wptGPGMEWrapper.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 262 - (hide annotations)
Sat Sep 30 10:24:34 2006 UTC (18 years, 5 months ago) by twoaday
File size: 4514 byte(s)


1 twoaday 133 /* wptGPGMEWrapper.cpp - Object oriented wrapper for GPGME
2     * Copyright (C) 2006 Timo Schulz
3     *
4     * This file is part of WinPT.
5     *
6     * WinPT is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * WinPT is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with WinPT; if not, write to the Free Software Foundation,
18     * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #ifdef HAVE_CONFIG_H
22     #include <config.h>
23     #endif
24    
25     #include <windows.h>
26     #include <stdio.h>
27     #include <string.h>
28    
29     #include "gpgme.h"
30     #include "wptGPG.h"
31     #include "wptGPGME.h"
32 twoaday 195 #include "wptW32API.h"
33 twoaday 133
34    
35     /* Constructor to build gpgme context. */
36     GPGME::GPGME ()
37     {
38     gpgme_new (&ctx);
39     }
40    
41     /* Destructor to release gpgme context. */
42     GPGME::~GPGME ()
43     {
44     if (ctx != NULL)
45     gpgme_release (ctx);
46     }
47    
48 twoaday 167 /* Enable or disable the feature to add the
49     WinPT version to armored output. */
50 twoaday 133 void
51     GPGME::setChangeVersion (bool val)
52     {
53     chg_ver = val;
54     }
55    
56     /* Enable or disable armor. */
57     void
58     GPGME::setArmor (bool val)
59     {
60     gpgme_set_armor (ctx, val? 1 : 0);
61     }
62    
63     bool
64     GPGME::getArmor (void)
65     {
66     return gpgme_get_armor (ctx)? true: false;
67     }
68    
69 twoaday 260
70 twoaday 262 /* Import keys from a memory buffer.
71     Return value: 0 on success. */
72     gpgme_error_t
73     GPGME::importFromBuffer (const char *indata)
74     {
75     gpgme_data_t keydata = NULL;
76     gpgme_error_t err;
77    
78     err = gpgme_data_new_from_mem (&keydata, indata, strlen (indata), 1);
79     if (err)
80     return err;
81    
82     op_begin ();
83     err = gpgme_op_import (ctx, keydata);
84     op_end ();
85    
86     gpgme_data_release (keydata);
87     return err;
88     }
89    
90    
91 twoaday 260 /* Import the keys from the clipboard.
92     Return value: 0 on success. */
93     gpgme_error_t
94     GPGME::importFromClipboard (void)
95     {
96     gpgme_data_t keydata = NULL;
97     gpgme_error_t err;
98    
99     err = gpg_data_new_from_clipboard (&keydata, 0);
100     if (err)
101     return err;
102    
103     op_begin ();
104     err = gpgme_op_import (ctx, keydata);
105     op_end ();
106    
107     gpgme_data_release (keydata);
108     return err;
109     }
110    
111    
112     /* Import the keys from the file @file.
113     Return value: 0 on success. */
114     gpgme_error_t
115     GPGME::importFromFile (const char *file)
116     {
117     gpgme_data_t keydata;
118     gpgme_error_t err;
119    
120     err = gpgme_data_new_from_file (&keydata, file, 1);
121     if (err)
122     return err;
123    
124     op_begin ();
125     err = gpgme_op_import (ctx, keydata);
126     op_end ();
127    
128     gpgme_data_release (keydata);
129     return err;
130     }
131    
132    
133     /* Return the import result for the last GPG operation. */
134     gpgme_import_result_t
135     GPGME::importGetResult (void)
136     {
137     return gpgme_op_import_result (ctx);
138     }
139    
140    
141 twoaday 133 /* Export key pattern @patt to file @outfile.
142     Return value: 0 on success. */
143     gpgme_error_t
144     GPGME::exportToFile (const char *patt, const char *outfile)
145     {
146     gpgme_data_t dat;
147     gpgme_error_t err;
148    
149     err = gpgme_data_new (&dat);
150     if (err)
151     return err;
152 twoaday 167 op_begin ();
153 twoaday 133 err = gpgme_op_export (ctx, patt, 0, dat);
154 twoaday 167 op_end ();
155 twoaday 133 if (err) {
156     gpgme_data_release (dat);
157     return err;
158     }
159     err = gpg_data_release_and_set_file (dat, outfile);
160 twoaday 247 if (err)
161     gpgme_data_release (dat);
162 twoaday 133 return err;
163     }
164    
165    
166     /* Export key pattern @patt to the clipboard.
167     Return value: 0 on success. */
168     gpgme_error_t
169     GPGME::exportToClipboard (const char *patt)
170     {
171     gpgme_data_t dat;
172     gpgme_error_t err;
173    
174     err = gpgme_data_new (&dat);
175     if (err)
176     return err;
177 twoaday 167 op_begin ();
178 twoaday 133 err = gpgme_op_export (ctx, patt, 0, dat);
179 twoaday 167 op_end ();
180 twoaday 133 if (err) {
181     gpgme_data_release (dat);
182     return err;
183     }
184     gpg_data_release_and_set_clipboard (dat, chg_ver? 1 : 0);
185     return err;
186     }
187    
188    
189     /* Export key pattern @patt to the buffer @outdata.
190     Return value: 0 on success. */
191     gpgme_error_t
192     GPGME::exportToBuffer (const char *patt, char **outdata)
193     {
194     gpgme_error_t err;
195     gpgme_data_t dat;
196     size_t n;
197     char *p;
198    
199     err = gpgme_data_new (&dat);
200     if (err)
201     return err;
202 twoaday 167 op_begin ();
203 twoaday 133 err = gpgme_op_export (ctx, patt, 0, dat);
204 twoaday 167 op_end ();
205 twoaday 133 if (err) {
206     gpgme_data_release (dat);
207     return err;
208     }
209     p = gpgme_data_release_and_get_mem (dat, &n);
210    
211     *outdata = new char[n+1];
212     memcpy (*outdata, p, n);
213     (*outdata)[n] = 0;
214     gpgme_free (p);
215    
216     return err;
217     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26