summaryrefslogtreecommitdiff
path: root/ui/gcr-combo-selector.c
blob: d225491fca035e80ca6ecbbf006cc1d601456822 (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
/*
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gcr/gcr-internal.h"

#include "gcr-collection-model.h"
#include "gcr-combo-selector.h"

#include <glib/gi18n-lib.h>

#include <string.h>

/**
 * SECTION:gcr-combo-selector
 * @title: GcrComboSelector
 * @short_description: A selector widget to select a single certificate or key.
 *
 * The #GcrComboSelector can be used to select a certificate or key. It allows
 * the user to select one object from the selector at a time.
 */

/**
 * GcrComboSelector:
 *
 * A combo selector widget.
 */

/**
 * GcrComboSelectorClass:
 *
 * The class for #GcrComboSelector.
 */

enum {
	PROP_0,
	PROP_COLLECTION
};

struct _GcrComboSelectorPrivate {
	GcrCollection *collection;
	GcrCollectionModel *model;
};

G_DEFINE_TYPE (GcrComboSelector, gcr_combo_selector, GTK_TYPE_COMBO_BOX);

/* -----------------------------------------------------------------------------
 * INTERNAL
 */

/* -----------------------------------------------------------------------------
 * OBJECT
 */

static GObject*
gcr_combo_selector_constructor (GType type, guint n_props, GObjectConstructParam *props)
{
	GcrComboSelector *self = GCR_COMBO_SELECTOR (G_OBJECT_CLASS (gcr_combo_selector_parent_class)->constructor(type, n_props, props));
	GtkCellRenderer *cell;

	g_return_val_if_fail (self, NULL);

	self->pv->model = gcr_collection_model_new (self->pv->collection,
	                                            GCR_COLLECTION_MODEL_LIST,
	                                            "icon", G_TYPE_ICON,
	                                            "markup", G_TYPE_STRING,
	                                            NULL);

	gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (self->pv->model));

	/* The icon */
	cell = gtk_cell_renderer_pixbuf_new ();
	g_object_set (cell, "stock-size", GTK_ICON_SIZE_DND, NULL);
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, FALSE);
	gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (self), cell, "gicon", 0);

	/* The markup */
	cell = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, TRUE);
	gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (self), cell, "markup", 1);

	return G_OBJECT (self);
}

static void
gcr_combo_selector_init (GcrComboSelector *self)
{
	self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_COMBO_SELECTOR, GcrComboSelectorPrivate);
}

static void
gcr_combo_selector_dispose (GObject *obj)
{
	GcrComboSelector *self = GCR_COMBO_SELECTOR (obj);

	if (self->pv->model)
		g_object_unref (self->pv->model);
	self->pv->model = NULL;

	if (self->pv->collection)
		g_object_unref (self->pv->collection);
	self->pv->collection = NULL;

	G_OBJECT_CLASS (gcr_combo_selector_parent_class)->dispose (obj);
}

static void
gcr_combo_selector_finalize (GObject *obj)
{
	GcrComboSelector *self = GCR_COMBO_SELECTOR (obj);

	g_assert (!self->pv->collection);
	g_assert (!self->pv->model);

	G_OBJECT_CLASS (gcr_combo_selector_parent_class)->finalize (obj);
}

static void
gcr_combo_selector_set_property (GObject *obj, guint prop_id, const GValue *value,
                                 GParamSpec *pspec)
{
	GcrComboSelector *self = GCR_COMBO_SELECTOR (obj);

	switch (prop_id) {
	case PROP_COLLECTION:
		g_return_if_fail (!self->pv->collection);
		self->pv->collection = g_value_dup_object (value);
		g_return_if_fail (self->pv->collection);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
		break;
	}
}

static void
gcr_combo_selector_get_property (GObject *obj, guint prop_id, GValue *value,
                                 GParamSpec *pspec)
{
	GcrComboSelector *self = GCR_COMBO_SELECTOR (obj);

	switch (prop_id) {
	case PROP_COLLECTION:
		g_value_set_object (value, gcr_combo_selector_get_collection (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
		break;
	}
}

static void
gcr_combo_selector_class_init (GcrComboSelectorClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

	gobject_class->constructor = gcr_combo_selector_constructor;
	gobject_class->dispose = gcr_combo_selector_dispose;
	gobject_class->finalize = gcr_combo_selector_finalize;
	gobject_class->set_property = gcr_combo_selector_set_property;
	gobject_class->get_property = gcr_combo_selector_get_property;

	g_type_class_add_private (gobject_class, sizeof (GcrComboSelectorPrivate));

	/**
	 * GcrComboSelector:collection:
	 *
	 * The collection which contains the objects to display in the selector.
	 */
	g_object_class_install_property (gobject_class, PROP_COLLECTION,
	           g_param_spec_object ("collection", "Collection", "Collection to select from",
	                                GCR_TYPE_COLLECTION, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}

/* -----------------------------------------------------------------------------
 * PUBLIC
 */

/**
 * gcr_combo_selector_new:
 * @collection: The collection that contains the objects to display
 *
 * Create a new #GcrTreeSelector.
 *
 * Returns: A newly allocated selector, which should be released with
 *     g_object_unref().
 */
GcrComboSelector*
gcr_combo_selector_new (GcrCollection *collection)
{
	return g_object_new (GCR_TYPE_COMBO_SELECTOR,
	                     "collection", collection,
	                     NULL);
}

/**
 * gcr_combo_selector_get_collection:
 * @self: The selector
 *
 * Get the collection that this selector is displaying objects from.
 *
 * Returns: (transfer none): The collection, owned by the selector.
 */
GcrCollection *
gcr_combo_selector_get_collection (GcrComboSelector *self)
{
	g_return_val_if_fail (GCR_IS_COMBO_SELECTOR (self), NULL);
	return self->pv->collection;
}

/**
 * gcr_combo_selector_get_selected:
 * @self: The selector
 *
 * Get the selected object in the selector, or %NULL if nothing selected.
 *
 * Returns: (transfer none): the selected object, owned by the selector, or %NULL
 */
GObject *
gcr_combo_selector_get_selected (GcrComboSelector *self)
{
	GtkTreeIter iter;

	g_return_val_if_fail (GCR_IS_COMBO_SELECTOR (self), NULL);
	gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter);

	return gcr_collection_model_object_for_iter (self->pv->model, &iter);
}

/**
 * gcr_combo_selector_set_selected:
 * @self: The selector
 * @selected: (allow-none): the object to select or %NULL
 *
 * Set the currently selected object in the selector, or clear the selection
 * if selected is set to %NULL.
 */
void
gcr_combo_selector_set_selected (GcrComboSelector *self, GObject *selected)
{
	GtkTreeIter iter;

	g_return_if_fail (GCR_IS_COMBO_SELECTOR (self));

	if (selected) {
		if (!gcr_collection_model_iter_for_object (self->pv->model, selected, &iter))
			g_return_if_reached ();
		gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
	} else {
		gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), NULL);
	}
}