Release 0.2.1
[gpgme.git] / gpgme / delete.c
1 /* delete.c -  delete a key 
2  *      Copyright (C) 2001 g10 Code GmbH
3  *
4  * This file is part of GPGME.
5  *
6  * GPGME is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GPGME 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <assert.h>
27
28 #include "util.h"
29 #include "context.h"
30 #include "ops.h"
31 #include "key.h"
32
33 static void
34 delete_status_handler ( GpgmeCtx ctx, GpgStatusCode code, char *args )
35 {
36     if ( ctx->out_of_core )
37         return;
38
39     switch (code) {
40       case STATUS_EOF:
41         break;
42
43       default:
44         /* ignore all other codes */
45         break;
46     }
47 }
48
49
50 GpgmeError
51 gpgme_op_delete_start ( GpgmeCtx c, const GpgmeKey key, int allow_secret )
52 {
53     GpgmeError rc = 0;
54     int i;
55     const char *s;
56
57     fail_on_pending_request( c );
58     c->pending = 1;
59
60     if (!key) {
61         rc = mk_error (Invalid_Value);
62         goto leave;
63     }
64
65     if ( c->gpg ) {
66         _gpgme_gpg_release ( c->gpg ); 
67         c->gpg = NULL;
68     }
69     
70     rc = _gpgme_gpg_new ( &c->gpg );
71     if (rc)
72         goto leave;
73
74     _gpgme_gpg_set_status_handler ( c->gpg, delete_status_handler, c );
75
76     /* build the commandline */
77     for ( i=0; i < c->verbosity; i++ )
78         _gpgme_gpg_add_arg ( c->gpg, "--verbose" );
79     _gpgme_gpg_add_arg ( c->gpg, allow_secret?
80                          "--delete-secret-and-public-key":"--delete-key" );
81     
82     _gpgme_gpg_add_arg ( c->gpg, "--" );
83     s = gpgme_key_get_string_attr ( key, GPGME_ATTR_FPR, NULL, 0 );
84     if (!s) {
85         rc = mk_error (Invalid_Key);
86         goto leave;
87     }
88     _gpgme_gpg_add_arg ( c->gpg, s );
89
90     /* do it */
91     rc = _gpgme_gpg_spawn ( c->gpg, c );
92
93  leave:
94     if (rc) {
95         c->pending = 0; 
96         _gpgme_gpg_release ( c->gpg ); c->gpg = NULL;
97     }
98     return rc;
99 }
100
101
102 GpgmeError
103 gpgme_op_delete ( GpgmeCtx c, const GpgmeKey key, int allow_secret )
104 {
105     int rc = gpgme_op_delete_start ( c, key, allow_secret );
106     if ( !rc ) {
107         gpgme_wait (c, 1);
108         c->pending = 0;
109         /* FIXME: check for success */
110     }
111     return rc;
112 }
113
114
115
116