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
|
#include "config.h"
#include "gcr/gcr.h"
#include <gtk/gtk.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define TEST_TYPE_COLLECTION (test_collection_get_type ())
#define TEST_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_COLLECTION, TestCollection))
#define TEST_IS_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_COLLECTION))
typedef struct _TestCollection TestCollection;
typedef struct _TestCollectionClass TestCollectionClass;
typedef struct _TestCollectionPrivate TestCollectionPrivate;
struct _TestCollection {
GcrSimpleCollection parent;
gchar *label;
};
struct _TestCollectionClass {
GcrSimpleCollectionClass parent_class;
};
GType test_collection_get_type (void) G_GNUC_CONST;
enum {
PROP_0,
PROP_LABEL,
};
G_DEFINE_TYPE (TestCollection, test_collection, GCR_TYPE_SIMPLE_COLLECTION);
static GHashTable *all_collections = NULL;
static void
test_collection_init (TestCollection *self)
{
}
static void
test_collection_finalize (GObject *obj)
{
TestCollection *self = TEST_COLLECTION (obj);
g_free (self->label);
g_hash_table_remove (all_collections, self);
G_OBJECT_CLASS (test_collection_parent_class)->finalize (obj);
}
static void
test_collection_get_property (GObject *obj,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TestCollection *self = TEST_COLLECTION (obj);
switch (prop_id) {
case PROP_LABEL:
g_value_set_string (value, self->label);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
break;
}
}
static void
test_collection_class_init (TestCollectionClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->get_property = test_collection_get_property;
gobject_class->finalize = test_collection_finalize;
g_object_class_install_property (gobject_class, PROP_LABEL,
g_param_spec_string ("label", "label", "label", NULL, G_PARAM_READABLE));
}
static GcrSimpleCollection *
test_collection_instance (const gchar *label)
{
TestCollection *collection = NULL;
g_assert (label);
if (!all_collections) {
all_collections = g_hash_table_new (g_str_hash, g_str_equal);
} else {
collection = g_hash_table_lookup (all_collections, label);
if (collection != NULL)
return g_object_ref (collection);
}
collection = g_object_new (TEST_TYPE_COLLECTION, NULL);
collection->label = g_strdup (label);
g_hash_table_insert (all_collections, collection->label, collection);
return GCR_SIMPLE_COLLECTION (collection);
}
static void
on_parser_parsed (GcrParser *parser, gpointer user_data)
{
GcrSimpleCollection *collection = user_data;
GcrSimpleCollection *testcol;
GcrRenderer *renderer;
gchar *group;
renderer = gcr_renderer_create (gcr_parser_get_parsed_label (parser),
gcr_parser_get_parsed_attributes (parser));
if (renderer == NULL)
return;
if (GCR_IS_CERTIFICATE (renderer))
group = gcr_certificate_get_subject_part (GCR_CERTIFICATE (renderer), "O");
else
group = g_strdup (G_OBJECT_TYPE_NAME (renderer));
testcol = test_collection_instance (group);
if (!gcr_simple_collection_contains (collection, G_OBJECT (testcol)))
gcr_simple_collection_add (collection, G_OBJECT (testcol));
gcr_simple_collection_add (GCR_SIMPLE_COLLECTION (testcol), G_OBJECT (renderer));
g_object_unref (renderer);
g_object_unref (testcol);
g_free (group);
}
static void
add_to_selector (GcrParser *parser, const gchar *path)
{
GError *err = NULL;
guchar *data;
gsize n_data;
GBytes *bytes;
if (!g_file_get_contents (path, (gchar**)&data, &n_data, NULL))
g_error ("couldn't read file: %s", path);
bytes = g_bytes_new_take (data, n_data);
if (!gcr_parser_parse_bytes (parser, bytes, &err))
g_error ("couldn't parse data: %s", err->message);
g_bytes_unref (bytes);
}
int
main (int argc, char *argv[])
{
GcrCollection *collection;
GcrTreeSelector *selector;
GtkDialog *dialog;
GcrParser *parser;
GtkWidget *scroll;
GList *selected, *l;
int i;
gtk_init (&argc, &argv);
dialog = GTK_DIALOG (gtk_dialog_new ());
g_object_ref_sink (dialog);
collection = gcr_simple_collection_new ();
selector = gcr_tree_selector_new (collection, GCR_CERTIFICATE_COLUMNS);
scroll = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll), GTK_SHADOW_ETCHED_IN);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (selector));
gtk_widget_show_all (scroll);
gtk_widget_show (GTK_WIDGET (selector));
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (dialog)), GTK_WIDGET (scroll), TRUE, TRUE, 0);
gtk_window_set_default_size (GTK_WINDOW (dialog), 550, 400);
gtk_container_set_border_width (GTK_CONTAINER (dialog), 20);
parser = gcr_parser_new ();
g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), collection);
if (argc == 1) {
add_to_selector (parser, SRCDIR "/ui/fixtures/ca-certificates.crt");
} else {
for (i = 1; i < argc; ++i)
add_to_selector (parser, argv[i]);
}
g_object_unref (parser);
g_object_unref (collection);
gtk_dialog_run (dialog);
selected = gcr_tree_selector_get_selected (selector);
for (l = selected; l; l = g_list_next (l)) {
gchar *label;
g_object_get (l->data, "label", &label, NULL);
g_print ("selected: %s\n", label);
g_free (label);
}
g_list_free (selected);
gtk_widget_destroy (GTK_WIDGET (dialog));
g_object_unref (dialog);
return 0;
}
|