1 /*
2  * Copyright (C) 2010 Stefan Walter
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation; either version 2.1 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include "gcr/gcr-certificate.h"
21 
22 #include "gcr-certificate-renderer.h"
23 #include "gcr-certificate-widget.h"
24 #include "gcr-deprecated.h"
25 #include "gcr-renderer.h"
26 #include "gcr-viewer.h"
27 
28 #include "gck/gck.h"
29 
30 #include <gdk/gdk.h>
31 #include <glib/gi18n-lib.h>
32 
33 /**
34  * SECTION:gcr-certificate-widget
35  * @title: GcrCertificateWidget
36  * @short_description: Certificate widget and renderer
37  *
38  * A #GcrCertificateWidget can be used to display a certificate. The widget
39  * is normally in a collapsed state showing only details, but can be expanded
40  * by the user.
41  *
42  * Use gcr_certificate_widget_new() to create a new certificate widget. Only
43  * one certificate can be displayed.  A #GcrCertificateWidget contains a
44  * #GcrViewer internally and #GcrCertificateRenderer is used to render the
45  * certificate to the viewer. To show more than one certificate in a view,
46  * create the viewer and add renderers to it.
47  */
48 
49 /**
50  * GcrCertificateWidget:
51  *
52  * A widget that displays a certificate.
53  */
54 
55 /**
56  * GcrCertificateWidgetClass:
57  *
58  * The class for #GcrCertificateWidget
59  */
60 
61 enum {
62 	PROP_0,
63 	PROP_CERTIFICATE,
64 	PROP_ATTRIBUTES
65 };
66 
67 struct _GcrCertificateWidgetPrivate {
68 	GcrViewer *viewer;
69 	GcrCertificateRenderer *renderer;
70 };
71 
72 G_DEFINE_TYPE_WITH_PRIVATE (GcrCertificateWidget, gcr_certificate_widget, GTK_TYPE_BIN);
73 
74 /* -----------------------------------------------------------------------------
75  * OBJECT
76  */
77 
78 static GObject*
gcr_certificate_widget_constructor(GType type,guint n_props,GObjectConstructParam * props)79 gcr_certificate_widget_constructor (GType type, guint n_props, GObjectConstructParam *props)
80 {
81 	GObject *obj = G_OBJECT_CLASS (gcr_certificate_widget_parent_class)->constructor (type, n_props, props);
82 	GcrCertificateWidget *self = NULL;
83 
84 	g_return_val_if_fail (obj, NULL);
85 
86 	self = GCR_CERTIFICATE_WIDGET (obj);
87 
88 	self->pv->viewer = gcr_viewer_new_scrolled ();
89 	gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->pv->viewer));
90 	gtk_widget_show (GTK_WIDGET (self->pv->viewer));
91 
92 	gcr_viewer_add_renderer (self->pv->viewer, GCR_RENDERER (self->pv->renderer));
93 	return obj;
94 }
95 
96 static void
gcr_certificate_widget_init(GcrCertificateWidget * self)97 gcr_certificate_widget_init (GcrCertificateWidget *self)
98 {
99 	self->pv = gcr_certificate_widget_get_instance_private (self);
100 	self->pv->renderer = gcr_certificate_renderer_new (NULL);
101 }
102 
103 static void
gcr_certificate_widget_finalize(GObject * obj)104 gcr_certificate_widget_finalize (GObject *obj)
105 {
106 	GcrCertificateWidget *self = GCR_CERTIFICATE_WIDGET (obj);
107 
108 	g_assert (self->pv->renderer);
109 	g_object_unref (self->pv->renderer);
110 	self->pv->renderer = NULL;
111 
112 	g_assert (self->pv->viewer);
113 	self->pv->viewer = NULL;
114 
115 	G_OBJECT_CLASS (gcr_certificate_widget_parent_class)->finalize (obj);
116 }
117 
118 static void
gcr_certificate_widget_set_property(GObject * obj,guint prop_id,const GValue * value,GParamSpec * pspec)119 gcr_certificate_widget_set_property (GObject *obj, guint prop_id, const GValue *value,
120                                      GParamSpec *pspec)
121 {
122 	GcrCertificateWidget *self = GCR_CERTIFICATE_WIDGET (obj);
123 
124 	switch (prop_id) {
125 	case PROP_CERTIFICATE:
126 		gcr_certificate_widget_set_certificate (self, g_value_get_object (value));
127 		break;
128 	case PROP_ATTRIBUTES:
129 		gcr_certificate_widget_set_attributes (self, g_value_get_boxed (value));
130 		break;
131 	default:
132 		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
133 		break;
134 	}
135 }
136 
137 static void
gcr_certificate_widget_get_property(GObject * obj,guint prop_id,GValue * value,GParamSpec * pspec)138 gcr_certificate_widget_get_property (GObject *obj, guint prop_id, GValue *value,
139                                      GParamSpec *pspec)
140 {
141 	GcrCertificateWidget *self = GCR_CERTIFICATE_WIDGET (obj);
142 
143 	switch (prop_id) {
144 	case PROP_CERTIFICATE:
145 		g_value_set_object (value, gcr_certificate_widget_get_certificate (self));
146 		break;
147 	case PROP_ATTRIBUTES:
148 		g_value_set_boxed (value, gcr_certificate_widget_get_attributes (self));
149 		break;
150 	default:
151 		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
152 		break;
153 	}
154 }
155 
156 static void
gcr_certificate_widget_class_init(GcrCertificateWidgetClass * klass)157 gcr_certificate_widget_class_init (GcrCertificateWidgetClass *klass)
158 {
159 	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
160 
161 	gcr_certificate_widget_parent_class = g_type_class_peek_parent (klass);
162 
163 	gobject_class->constructor = gcr_certificate_widget_constructor;
164 	gobject_class->finalize = gcr_certificate_widget_finalize;
165 	gobject_class->set_property = gcr_certificate_widget_set_property;
166 	gobject_class->get_property = gcr_certificate_widget_get_property;
167 
168 	g_object_class_install_property (gobject_class, PROP_CERTIFICATE,
169 	           g_param_spec_object("certificate", "Certificate", "Certificate to display.",
170 	                               GCR_TYPE_CERTIFICATE,
171 	                               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172 
173 	g_object_class_install_property (gobject_class, PROP_ATTRIBUTES,
174 	         g_param_spec_boxed ("attributes", "Attributes", "Attributes which contain the certificate",
175 	                             GCK_TYPE_ATTRIBUTES,
176 	                             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 }
178 
179 /* -----------------------------------------------------------------------------
180  * PUBLIC
181  */
182 
183 /**
184  * gcr_certificate_widget_new:
185  * @certificate: (nullable): certificate to display, or %NULL
186  *
187  * Create a new certificate widget which displays a given certificate.
188  *
189  * Returns: (transfer full): a newly allocated #GcrCertificateWidget, which
190  *          should be freed with g_object_unref()
191  */
192 GcrCertificateWidget *
gcr_certificate_widget_new(GcrCertificate * certificate)193 gcr_certificate_widget_new (GcrCertificate *certificate)
194 {
195 	return g_object_new (GCR_TYPE_CERTIFICATE_WIDGET, "certificate", certificate, NULL);
196 }
197 
198 /**
199  * gcr_certificate_widget_get_certificate:
200  * @self: The certificate widget
201  *
202  * Get the certificate displayed in the widget.
203  *
204  * Returns: (nullable) (transfer none): the certificate
205  */
206 GcrCertificate *
gcr_certificate_widget_get_certificate(GcrCertificateWidget * self)207 gcr_certificate_widget_get_certificate (GcrCertificateWidget *self)
208 {
209 	g_return_val_if_fail (GCR_IS_CERTIFICATE_WIDGET (self), NULL);
210 	return gcr_certificate_renderer_get_certificate (self->pv->renderer);
211 }
212 
213 /**
214  * gcr_certificate_widget_set_certificate:
215  * @self: The certificate widget
216  * @certificate: (nullable): the certificate to display
217  *
218  * Set the certificate displayed in the widget
219  */
220 void
gcr_certificate_widget_set_certificate(GcrCertificateWidget * self,GcrCertificate * certificate)221 gcr_certificate_widget_set_certificate (GcrCertificateWidget *self, GcrCertificate *certificate)
222 {
223 	g_return_if_fail (GCR_IS_CERTIFICATE_WIDGET (self));
224 	gcr_certificate_renderer_set_certificate (self->pv->renderer, certificate);
225 }
226 
227 /**
228  * gcr_certificate_widget_get_attributes:
229  * @self: The certificate widget
230  *
231  * Get the attributes displayed in the widget. The attributes should contain
232  * a certificate.
233  *
234  * Returns: (nullable) (transfer none): the attributes, owned by the widget
235  */
236 GckAttributes *
gcr_certificate_widget_get_attributes(GcrCertificateWidget * self)237 gcr_certificate_widget_get_attributes (GcrCertificateWidget *self)
238 {
239 	g_return_val_if_fail (GCR_IS_CERTIFICATE_WIDGET (self), NULL);
240 	return gcr_renderer_get_attributes (GCR_RENDERER (self->pv->renderer));
241 }
242 
243 /**
244  * gcr_certificate_widget_set_attributes:
245  * @self: The certificate widget
246  * @attrs: (nullable): the attributes to display
247  *
248  * Set the attributes displayed in the widget. The attributes should contain
249  * a certificate.
250  */
251 void
gcr_certificate_widget_set_attributes(GcrCertificateWidget * self,GckAttributes * attrs)252 gcr_certificate_widget_set_attributes (GcrCertificateWidget *self,
253                                        GckAttributes *attrs)
254 {
255 	g_return_if_fail (GCR_IS_CERTIFICATE_WIDGET (self));
256 	gcr_renderer_set_attributes (GCR_RENDERER (self->pv->renderer), attrs);
257 }
258