summaryrefslogtreecommitdiff
path: root/ui/frob-import-button.c
blob: d876d813c19a2158adb57f73b7b2c4f1b2dab805 (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
/*
 * Copyright (C) 2014 Stef 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, see <http://www.gnu.org/licenses/>.
 *
 * Author: Stef Walter <stefw@redhat.com>
 */

#include "config.h"

#include "ui/gcr-ui.h"

#include "gcr/gcr-importer.h"

#include <gtk/gtk.h>

#include <unistd.h>
#include <string.h>
#include <errno.h>

typedef GObject MockImporter;
typedef GObjectClass MockImporterClass;

enum {
	PROP_0,
	PROP_LABEL,
	PROP_ICON,
	PROP_URI,
	PROP_INTERACTION
};

GType mock_importer_get_type (void) G_GNUC_CONST;

static GList *
mock_importer_create_for_parsed (GcrParsed *parsed)
{
	GcrImporter *self;
	GIcon *icon;

	icon = g_themed_icon_new ("dialog-warning");
	self = g_object_new (mock_importer_get_type (),
	                     "label", gcr_parsed_get_label (parsed),
	                     "icon", icon,
	                     NULL);
	g_object_unref (icon);
	return g_list_append (NULL, self);
}

static gboolean
mock_importer_queue_for_parsed (GcrImporter *importer,
                                GcrParsed *parsed)
{
	return TRUE;
}

static void
mock_importer_import_async (GcrImporter *importer,
                            GCancellable *cancellable,
                            GAsyncReadyCallback callback,
                            gpointer user_data)
{
	GTask *task;

	task = g_task_new (importer, cancellable, callback, user_data);
	g_task_set_source_tag (task, mock_importer_import_async);

	g_printerr ("Import %p\n", importer);
	g_task_return_boolean (task, TRUE);
	g_clear_object (&task);
}


static gboolean
mock_importer_import_finish (GcrImporter *importer,
                             GAsyncResult *result,
                             GError **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}


static void
mock_importer_iface (GcrImporterIface *iface)
{
	iface->create_for_parsed = mock_importer_create_for_parsed;
	iface->queue_for_parsed = mock_importer_queue_for_parsed;
	iface->import_async = mock_importer_import_async;
	iface->import_finish = mock_importer_import_finish;
}

G_DEFINE_TYPE_WITH_CODE (MockImporter, mock_importer, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GCR_TYPE_IMPORTER, mock_importer_iface);
);

static void
value_free (gpointer value)
{
	g_boxed_free (G_TYPE_VALUE, value);
}

static void
mock_importer_init (MockImporter *self)
{

}

static void
mock_importer_set_property (GObject *obj,
                            guint prop_id,
                            const GValue *value,
                            GParamSpec *pspec)
{
	g_object_set_data_full (obj, pspec->name, g_boxed_copy (G_TYPE_VALUE, value), value_free);
}

static void
mock_importer_get_property (GObject *obj,
                            guint prop_id,
                            GValue *value,
                            GParamSpec *pspec)
{
	GValue *val = g_object_get_data (obj, pspec->name);
	g_return_if_fail (val != NULL);
	g_value_copy (val, value);
}

static void
mock_importer_class_init (MockImporterClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	gobject_class->set_property = mock_importer_set_property;
	gobject_class->get_property = mock_importer_get_property;

	g_object_class_install_property (gobject_class, PROP_LABEL,
	           g_param_spec_string ("label", "", "",
	                                NULL,
	                                G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

	g_object_class_install_property (gobject_class, PROP_ICON,
	           g_param_spec_object ("icon", "", "",
	                                G_TYPE_ICON,
	                                G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

	g_object_class_override_property (gobject_class, PROP_URI, "uri");
	g_object_class_override_property (gobject_class, PROP_INTERACTION, "interaction");
}

typedef MockImporter MockImporterTwo;
typedef MockImporterClass MockImporterTwoClass;

GType mock_importer_two_get_type (void) G_GNUC_CONST;

G_DEFINE_TYPE (MockImporterTwo, mock_importer_two, mock_importer_get_type ());

static void
mock_importer_two_init (MockImporterTwo *self)
{

}

static void
mock_importer_two_class_init (MockImporterTwoClass *klass)
{

}


static void
on_parser_parsed (GcrParser *parser,
                  gpointer user_data)
{
	gcr_import_button_add_parsed (user_data,
	                              gcr_parser_get_parsed (parser));
}

static void
parse_file (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[])
{
	GtkDialog *dialog;
	GcrParser *parser;
	GtkWidget *button;
	int i;

	gtk_init (&argc, &argv);

	gcr_importer_register (mock_importer_get_type (), gck_attributes_new (0));
	gcr_importer_register (mock_importer_two_get_type (), gck_attributes_new (0));

	dialog = GTK_DIALOG (gtk_dialog_new ());
	g_object_ref_sink (dialog);

	button = GTK_WIDGET (gcr_import_button_new ("Import Button"));
	gtk_widget_show (button);

	gtk_widget_set_halign (button, 0.5);
	gtk_widget_set_valign (button, 0.5);

	gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (dialog)), button);

	gtk_window_set_default_size (GTK_WINDOW (dialog), 200, 300);
	gtk_container_set_border_width (GTK_CONTAINER (dialog), 20);

	parser = gcr_parser_new ();
	g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), button);

	if (argc == 1) {
		parse_file (parser, SRCDIR "/ui/fixtures/ca-certificates.crt");
	} else {
		for (i = 1; i < argc; ++i)
			parse_file (parser, argv[i]);
	}

	g_object_unref (parser);

	gtk_dialog_run (dialog);

	gtk_widget_destroy (GTK_WIDGET (dialog));
	g_object_unref (dialog);

	return 0;
}