summaryrefslogtreecommitdiff
path: root/ui/gcr-renderer.c
blob: 30ce91f47c152bebec3525ce0f3745426db6d11e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * gnome-keyring
 *
 * Copyright (C) 2010 Stefan Walter
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "config.h"

#include "gcr-deprecated.h"
#include "gcr-renderer.h"

#include "gcr-certificate-renderer.h"
#include "gcr-certificate-request-renderer.h"
#include "gcr-gnupg-renderer.h"
#include "gcr-key-renderer.h"

#include "gck/gck.h"

#include <gtk/gtk.h>

/**
 * SECTION:gcr-renderer
 * @title: GcrRenderer
 * @short_description: An interface implemented by renderers.
 *
 * A #GcrRenderer is an interface that's implemented by renderers which wish
 * to render data to a #GcrViewer.
 *
 * The interaction between #GcrRenderer and #GcrViewer is not stable yet, and
 * so new renderers cannot be implemented outside the Gcr library at this time.
 *
 * To lookup a renderer for a given set of attributes, use the gcr_renderer_create()
 * function. This will create and initialize a renderer that's capable of viewing
 * the data in those attributes.
 */

/**
 * GcrRenderer:
 *
 * A renderer.
 */

/**
 * GcrRendererIface:
 * @parent: the parent interface type
 * @data_changed: signal emitted when data being rendered changes
 * @render_view: method invoked to render the data into a viewer
 * @populate_popup: method invoked to populate a popup menu with additional
 *                  renderer options
 *
 * The interface for #GcrRenderer
 */

enum {
	DATA_CHANGED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

typedef struct _GcrRegistered {
	GckAttributes *attrs;
	GType renderer_type;
} GcrRegistered;

static GArray *registered_renderers = NULL;
static gboolean registered_sorted = FALSE;

static void
gcr_renderer_default_init (GcrRendererIface *iface)
{
	static gboolean initialized = FALSE;
	if (!initialized) {

		/**
		 * GcrRenderer:label:
		 *
		 * The label to display.
		 */
		g_object_interface_install_property (iface,
		         g_param_spec_string ("label", "Label", "The label for the renderer",
		                              "", G_PARAM_READWRITE));

		/**
		 * GcrRenderer:attributes:
		 *
		 * The attributes to display.
		 */
		g_object_interface_install_property (iface,
		         g_param_spec_boxed ("attributes", "Attributes", "The data displayed in the renderer",
		                             GCK_TYPE_ATTRIBUTES, G_PARAM_READWRITE));

		/**
		 * GcrRenderer::data-changed:
		 *
		 * A signal that is emitted by the renderer when it's data
		 * changed and should be rerendered.
		 */
		signals[DATA_CHANGED] = g_signal_new ("data-changed", GCR_TYPE_RENDERER, G_SIGNAL_RUN_LAST,
		                                      G_STRUCT_OFFSET (GcrRendererIface, data_changed),
		                                      NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

		initialized = TRUE;
	}
}

typedef GcrRendererIface GcrRendererInterface;

G_DEFINE_INTERFACE (GcrRenderer, gcr_renderer, G_TYPE_OBJECT);

/**
 * gcr_renderer_render_view:
 * @self: The renderer
 * @viewer: The viewer to render to.
 *
 * Render the contents of the renderer to the given viewer.
 */
void
gcr_renderer_render_view (GcrRenderer *self, GcrViewer *viewer)
{
	g_return_if_fail (GCR_IS_RENDERER (self));
	g_return_if_fail (GCR_RENDERER_GET_INTERFACE (self)->render_view);
	GCR_RENDERER_GET_INTERFACE (self)->render_view (self, viewer);
}

/**
 * gcr_renderer_render:
 * @self: the renderer
 * @viewer: the viewer to render to
 *
 * Render a renderer to the viewer.
 *
 * Deprecated: 3.2: Use gcr_renderer_render_view() instead
 */
void
gcr_renderer_render (GcrRenderer *self,
                     GcrViewer *viewer)
{
	gcr_renderer_render_view (self, viewer);
}

/**
 * gcr_renderer_popuplate_popup:
 * @self: The renderer
 * @viewer: The viewer that is displaying a popup
 * @menu: The popup menu being displayed
 *
 * Called by #GcrViewer when about to display a popup menu for the content
 * displayed by the renderer. The renderer can add a menu item if desired.
 */
void
gcr_renderer_popuplate_popup (GcrRenderer *self, GcrViewer *viewer,
                              GtkMenu *menu)
{
	g_return_if_fail (GCR_IS_RENDERER (self));
	if (GCR_RENDERER_GET_INTERFACE (self)->populate_popup)
		GCR_RENDERER_GET_INTERFACE (self)->populate_popup (self, viewer, menu);
}

/**
 * gcr_renderer_emit_data_changed:
 * @self: The renderer
 *
 * Emit the #GcrRenderer::data-changed signal on the renderer. This is used by
 * renderer implementations.
 */
void
gcr_renderer_emit_data_changed (GcrRenderer *self)
{
	g_return_if_fail (GCR_IS_RENDERER (self));
	g_signal_emit (self, signals[DATA_CHANGED], 0);
}

static gint
sort_registered_by_n_attrs (gconstpointer a, gconstpointer b)
{
	const GcrRegistered *ra = a;
	const GcrRegistered *rb = b;
	gulong na, nb;

	g_assert (a);
	g_assert (b);

	na = gck_attributes_count (ra->attrs);
	nb = gck_attributes_count (rb->attrs);

	/* Note we're sorting in reverse order */
	if (na < nb)
		return 1;
	return (na == nb) ? 0 : -1;
}

/**
 * gcr_renderer_create:
 * @label: (allow-none): The label for the renderer
 * @attrs: The attributes to render
 *
 * Create and initialize a renderer for the given attributes and label. These
 * renderers should have been preregistered via gcr_renderer_register().
 *
 * Returns: (transfer full) (allow-none): a new renderer, or %NULL if no renderer
 *          matched the attributes; the render should be released with g_object_unref()
 */
GcrRenderer *
gcr_renderer_create (const gchar *label, GckAttributes *attrs)
{
	GcrRegistered *registered;
	gboolean matched;
	gulong n_attrs;
	gulong j;
	gsize i;

	g_return_val_if_fail (attrs, NULL);

	gcr_renderer_register_well_known ();

	if (!registered_renderers)
		return NULL;

	if (!registered_sorted) {
		g_array_sort (registered_renderers, sort_registered_by_n_attrs);
		registered_sorted = TRUE;
	}

	for (i = 0; i < registered_renderers->len; ++i) {
		registered = &(g_array_index (registered_renderers, GcrRegistered, i));
		n_attrs = gck_attributes_count (registered->attrs);

		matched = TRUE;

		for (j = 0; j < n_attrs; ++j) {
			if (!gck_attributes_contains (attrs, gck_attributes_at (registered->attrs, j))) {
				matched = FALSE;
				break;
			}
		}

		if (matched)
			return g_object_new (registered->renderer_type, "label", label,
			                     "attributes", attrs, NULL);
	}

	return NULL;
}

/**
 * gcr_renderer_register:
 * @renderer_type: The renderer class type
 * @attrs: The attributes to match
 *
 * Register a renderer to be created when matching attributes are passed to
 * gcr_renderer_create().
 */
void
gcr_renderer_register (GType renderer_type, GckAttributes *attrs)
{
	GcrRegistered registered;

	if (!registered_renderers)
		registered_renderers = g_array_new (FALSE, FALSE, sizeof (GcrRegistered));

	registered.renderer_type = renderer_type;
	registered.attrs = gck_attributes_ref_sink (attrs);
	g_array_append_val (registered_renderers, registered);
	registered_sorted = FALSE;
}

/**
 * gcr_renderer_register_well_known:
 *
 * Register all the well known renderers for certificates and keys known to the
 * Gcr library.
 */
void
gcr_renderer_register_well_known (void)
{
	g_type_class_unref (g_type_class_ref (GCR_TYPE_CERTIFICATE_RENDERER));
	g_type_class_unref (g_type_class_ref (GCR_TYPE_CERTIFICATE_REQUEST_RENDERER));
	g_type_class_unref (g_type_class_ref (GCR_TYPE_KEY_RENDERER));
	g_type_class_unref (g_type_class_ref (GCR_TYPE_GNUPG_RENDERER));
}