1 
2 #include "config.h"
3 
4 #include "gcr/gcr.h"
5 
6 #include <gtk/gtk.h>
7 
8 #include <unistd.h>
9 #include <string.h>
10 #include <errno.h>
11 
12 #define TEST_TYPE_COLLECTION               (test_collection_get_type ())
13 #define TEST_COLLECTION(obj)               (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_COLLECTION, TestCollection))
14 #define TEST_IS_COLLECTION(obj)            (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_COLLECTION))
15 
16 typedef struct _TestCollection TestCollection;
17 typedef struct _TestCollectionClass TestCollectionClass;
18 typedef struct _TestCollectionPrivate TestCollectionPrivate;
19 
20 struct _TestCollection {
21 	GcrSimpleCollection parent;
22 	gchar *label;
23 };
24 
25 struct _TestCollectionClass {
26 	GcrSimpleCollectionClass parent_class;
27 };
28 
29 GType test_collection_get_type (void) G_GNUC_CONST;
30 
31 enum {
32 	PROP_0,
33 	PROP_LABEL,
34 };
35 
36 G_DEFINE_TYPE (TestCollection, test_collection, GCR_TYPE_SIMPLE_COLLECTION);
37 
38 static GHashTable *all_collections = NULL;
39 
40 static void
test_collection_init(TestCollection * self)41 test_collection_init (TestCollection *self)
42 {
43 
44 }
45 
46 static void
test_collection_finalize(GObject * obj)47 test_collection_finalize (GObject *obj)
48 {
49 	TestCollection *self = TEST_COLLECTION (obj);
50 	g_free (self->label);
51 	g_hash_table_remove (all_collections, self);
52 	G_OBJECT_CLASS (test_collection_parent_class)->finalize (obj);
53 }
54 
55 static void
test_collection_get_property(GObject * obj,guint prop_id,GValue * value,GParamSpec * pspec)56 test_collection_get_property (GObject *obj,
57                               guint prop_id,
58                               GValue *value,
59                               GParamSpec *pspec)
60 {
61 	TestCollection *self = TEST_COLLECTION (obj);
62 	switch (prop_id) {
63 	case PROP_LABEL:
64 		g_value_set_string (value, self->label);
65 		break;
66 	default:
67 		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
68 		break;
69 	}
70 }
71 
72 static void
test_collection_class_init(TestCollectionClass * klass)73 test_collection_class_init (TestCollectionClass *klass)
74 {
75 	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
76 	gobject_class->get_property = test_collection_get_property;
77 	gobject_class->finalize = test_collection_finalize;
78 
79 	g_object_class_install_property (gobject_class, PROP_LABEL,
80 	           g_param_spec_string ("label", "label", "label", NULL,
81 	                                G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
82 }
83 
84 static GcrSimpleCollection *
test_collection_instance(const gchar * label)85 test_collection_instance (const gchar *label)
86 {
87 	TestCollection *collection = NULL;
88 
89 	g_assert (label);
90 
91 	if (!all_collections) {
92 		all_collections = g_hash_table_new (g_str_hash, g_str_equal);
93 	} else {
94 		collection = g_hash_table_lookup (all_collections, label);
95 		if (collection != NULL)
96 			return GCR_SIMPLE_COLLECTION (g_object_ref (collection));
97 	}
98 
99 	collection = g_object_new (TEST_TYPE_COLLECTION, NULL);
100 	collection->label = g_strdup (label);
101 	g_hash_table_insert (all_collections, collection->label, collection);
102 	return GCR_SIMPLE_COLLECTION (collection);
103 }
104 
105 static void
on_parser_parsed(GcrParser * parser,gpointer user_data)106 on_parser_parsed (GcrParser *parser, gpointer user_data)
107 {
108 	GcrSimpleCollection *collection = user_data;
109 	GcrSimpleCollection *testcol;
110 	GcrRenderer *renderer;
111 	gchar *group;
112 
113 	renderer = gcr_renderer_create (gcr_parser_get_parsed_label (parser),
114 	                                gcr_parser_get_parsed_attributes (parser));
115 	if (renderer == NULL)
116 		return;
117 
118 	if (GCR_IS_CERTIFICATE (renderer))
119 		group = gcr_certificate_get_subject_part (GCR_CERTIFICATE (renderer), "O");
120 	else
121 		group = g_strdup (G_OBJECT_TYPE_NAME (renderer));
122 
123 
124 	testcol = test_collection_instance (group);
125 	if (!gcr_collection_contains (GCR_COLLECTION (collection), G_OBJECT (testcol)))
126 		gcr_simple_collection_add (collection, G_OBJECT (testcol));
127 
128 	gcr_simple_collection_add (GCR_SIMPLE_COLLECTION (testcol), G_OBJECT (renderer));
129 	g_object_unref (renderer);
130 	g_object_unref (testcol);
131 	g_free (group);
132 }
133 
134 static void
add_to_selector(GcrParser * parser,const gchar * path)135 add_to_selector (GcrParser *parser, const gchar *path)
136 {
137 	GError *err = NULL;
138 	guchar *data;
139 	gsize n_data;
140 	GBytes *bytes;
141 
142 	if (!g_file_get_contents (path, (gchar**)&data, &n_data, NULL))
143 		g_error ("couldn't read file: %s", path);
144 
145 	bytes = g_bytes_new_take (data, n_data);
146 	if (!gcr_parser_parse_bytes (parser, bytes, &err))
147 		g_error ("couldn't parse data: %s", err->message);
148 
149 	g_bytes_unref (bytes);
150 }
151 
152 int
main(int argc,char * argv[])153 main (int argc, char *argv[])
154 {
155 	GcrCollection *collection;
156 	GcrTreeSelector *selector;
157 	GtkDialog *dialog;
158 	GcrParser *parser;
159 	GtkWidget *scroll;
160 	GList *selected, *l;
161 	int i;
162 
163 	gtk_init (&argc, &argv);
164 
165 	dialog = GTK_DIALOG (gtk_dialog_new ());
166 	g_object_ref_sink (dialog);
167 
168 	collection = gcr_simple_collection_new ();
169 	selector = gcr_tree_selector_new (collection, GCR_CERTIFICATE_COLUMNS);
170 
171 	scroll = gtk_scrolled_window_new (NULL, NULL);
172 	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll), GTK_SHADOW_ETCHED_IN);
173 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
174 	gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (selector));
175 	gtk_widget_show_all (scroll);
176 
177 	gtk_widget_show (GTK_WIDGET (selector));
178 	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (dialog)), GTK_WIDGET (scroll), TRUE, TRUE, 0);
179 
180 	gtk_window_set_default_size (GTK_WINDOW (dialog), 550, 400);
181 	gtk_container_set_border_width (GTK_CONTAINER (dialog), 20);
182 
183 	parser = gcr_parser_new ();
184 	g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), collection);
185 
186 	if (argc == 1) {
187 		add_to_selector (parser, SRCDIR "/ui/fixtures/ca-certificates.crt");
188 	} else {
189 		for (i = 1; i < argc; ++i)
190 			add_to_selector (parser, argv[i]);
191 	}
192 
193 	g_object_unref (parser);
194 	g_object_unref (collection);
195 
196 	gtk_dialog_run (dialog);
197 
198 	selected = gcr_tree_selector_get_selected (selector);
199 	for (l = selected; l; l = g_list_next (l)) {
200 		gchar *label;
201 		g_object_get (l->data, "label", &label, NULL);
202 		g_print ("selected: %s\n", label);
203 		g_free (label);
204 	}
205 	g_list_free (selected);
206 
207 	gtk_widget_destroy (GTK_WIDGET (dialog));
208 	g_object_unref (dialog);
209 
210 	return 0;
211 }
212