[gcr] Create new view interface and implement for certificates.
[platform/upstream/gcr.git] / gcr / gcr-simple-certificate.c
1 /*
2  * gnome-keyring
3  *
4  * Copyright (C) 2008 Stefan Walter
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19  * 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include "gcr-certificate.h"
25 #include "gcr-internal.h"
26 #include "gcr-simple-certificate.h"
27
28 #include "egg/egg-hex.h"
29
30 #include <string.h>
31
32 struct _GcrSimpleCertificatePrivate {
33         const guchar *data;
34         gsize n_data;
35         guchar *owned;
36 };
37
38 static void gcr_certificate_iface (GcrCertificateIface *iface);
39 G_DEFINE_TYPE_WITH_CODE (GcrSimpleCertificate, gcr_simple_certificate, G_TYPE_OBJECT,
40                          G_IMPLEMENT_INTERFACE (GCR_TYPE_CERTIFICATE, gcr_certificate_iface));
41
42 /* -----------------------------------------------------------------------------
43  * OBJECT
44  */
45
46 static void
47 gcr_simple_certificate_init (GcrSimpleCertificate *self)
48 {
49         self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_SIMPLE_CERTIFICATE, GcrSimpleCertificatePrivate);
50 }
51
52 static void
53 gcr_simple_certificate_finalize (GObject *obj)
54 {
55         GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (obj);
56
57         g_free (self->pv->owned);
58         self->pv->owned = NULL;
59         self->pv->data = NULL;
60         self->pv->n_data = 0;
61
62         G_OBJECT_CLASS (gcr_simple_certificate_parent_class)->finalize (obj);
63 }
64
65 static void
66 gcr_simple_certificate_class_init (GcrSimpleCertificateClass *klass)
67 {
68         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
69         gobject_class->finalize = gcr_simple_certificate_finalize;
70         g_type_class_add_private (gobject_class, sizeof (GcrSimpleCertificatePrivate));
71         _gcr_initialize ();
72 }
73
74 static const guchar*
75 gcr_simple_certificate_real_get_der_data (GcrCertificate *base, gsize *n_data)
76 {
77         GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (base);
78
79         g_return_val_if_fail (GCR_IS_CERTIFICATE (self), NULL);
80         g_return_val_if_fail (n_data, NULL);
81         g_return_val_if_fail (self->pv->data, NULL);
82
83         /* This is called when we're not a base class */
84         *n_data = self->pv->n_data;
85         return self->pv->data;
86 }
87
88 static void
89 gcr_certificate_iface (GcrCertificateIface *iface)
90 {
91         iface->get_der_data = (gpointer)gcr_simple_certificate_real_get_der_data;
92 }
93
94 /* -----------------------------------------------------------------------------
95  * PUBLIC
96  */
97
98 GcrCertificate*
99 gcr_simple_certificate_new (const guchar *data, gsize n_data)
100 {
101         GcrSimpleCertificate *cert;
102
103         g_return_val_if_fail (data, NULL);
104         g_return_val_if_fail (n_data, NULL);
105
106         cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
107
108         cert->pv->data = cert->pv->owned = g_memdup (data, n_data);
109         cert->pv->n_data = n_data;
110         return GCR_CERTIFICATE (cert);
111 }
112
113 GcrCertificate*
114 gcr_simple_certificate_new_static (const guchar *data, gsize n_data)
115 {
116         GcrSimpleCertificate *cert;
117
118         g_return_val_if_fail (data, NULL);
119         g_return_val_if_fail (n_data, NULL);
120
121         cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
122
123         cert->pv->owned = NULL;
124         cert->pv->data = data;
125         cert->pv->n_data = n_data;
126         return GCR_CERTIFICATE (cert);
127 }