Blame ui/gcr-combo-selector.c

Packit Service f02b19
/*
Packit Service f02b19
 * gnome-keyring
Packit Service f02b19
 *
Packit Service f02b19
 * Copyright (C) 2010 Stefan Walter
Packit Service f02b19
 *
Packit Service f02b19
 * This program is free software; you can redistribute it and/or modify
Packit Service f02b19
 * it under the terms of the GNU Lesser General Public License as
Packit Service f02b19
 * published by the Free Software Foundation; either version 2.1 of
Packit Service f02b19
 * the License, or (at your option) any later version.
Packit Service f02b19
 *
Packit Service f02b19
 * This program is distributed in the hope that it will be useful, but
Packit Service f02b19
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service f02b19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service f02b19
 * Lesser General Public License for more details.
Packit Service f02b19
 *
Packit Service f02b19
 * You should have received a copy of the GNU Lesser General Public
Packit Service f02b19
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
#include "config.h"
Packit Service f02b19
Packit Service f02b19
#include "gcr/gcr-internal.h"
Packit Service f02b19
Packit Service f02b19
#include "gcr-collection-model.h"
Packit Service f02b19
#include "gcr-combo-selector.h"
Packit Service f02b19
Packit Service f02b19
#include <glib/gi18n-lib.h>
Packit Service f02b19
Packit Service f02b19
#include <string.h>
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * SECTION:gcr-combo-selector
Packit Service f02b19
 * @title: GcrComboSelector
Packit Service f02b19
 * @short_description: A selector widget to select a single certificate or key.
Packit Service f02b19
 *
Packit Service f02b19
 * The #GcrComboSelector can be used to select a certificate or key. It allows
Packit Service f02b19
 * the user to select one object from the selector at a time.
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * GcrComboSelector:
Packit Service f02b19
 *
Packit Service f02b19
 * A combo selector widget.
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * GcrComboSelectorClass:
Packit Service f02b19
 *
Packit Service f02b19
 * The class for #GcrComboSelector.
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
enum {
Packit Service f02b19
	PROP_0,
Packit Service f02b19
	PROP_COLLECTION
Packit Service f02b19
};
Packit Service f02b19
Packit Service f02b19
struct _GcrComboSelectorPrivate {
Packit Service f02b19
	GcrCollection *collection;
Packit Service f02b19
	GcrCollectionModel *model;
Packit Service f02b19
};
Packit Service f02b19
Packit Service f02b19
G_DEFINE_TYPE (GcrComboSelector, gcr_combo_selector, GTK_TYPE_COMBO_BOX);
Packit Service f02b19
Packit Service f02b19
/* -----------------------------------------------------------------------------
Packit Service f02b19
 * INTERNAL
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
/* -----------------------------------------------------------------------------
Packit Service f02b19
 * OBJECT
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
static GObject*
Packit Service f02b19
gcr_combo_selector_constructor (GType type, guint n_props, GObjectConstructParam *props)
Packit Service f02b19
{
Packit Service f02b19
	GcrComboSelector *self = GCR_COMBO_SELECTOR (G_OBJECT_CLASS (gcr_combo_selector_parent_class)->constructor(type, n_props, props));
Packit Service f02b19
	GtkCellRenderer *cell;
Packit Service f02b19
Packit Service f02b19
	g_return_val_if_fail (self, NULL);
Packit Service f02b19
Packit Service f02b19
	self->pv->model = gcr_collection_model_new (self->pv->collection,
Packit Service f02b19
	                                            GCR_COLLECTION_MODEL_LIST,
Packit Service f02b19
	                                            "icon", G_TYPE_ICON,
Packit Service f02b19
	                                            "markup", G_TYPE_STRING,
Packit Service f02b19
	                                            NULL);
Packit Service f02b19
Packit Service f02b19
	gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (self->pv->model));
Packit Service f02b19
Packit Service f02b19
	/* The icon */
Packit Service f02b19
	cell = gtk_cell_renderer_pixbuf_new ();
Packit Service f02b19
	g_object_set (cell, "stock-size", GTK_ICON_SIZE_DND, NULL);
Packit Service f02b19
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, FALSE);
Packit Service f02b19
	gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (self), cell, "gicon", 0);
Packit Service f02b19
Packit Service f02b19
	/* The markup */
Packit Service f02b19
	cell = gtk_cell_renderer_text_new ();
Packit Service f02b19
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, TRUE);
Packit Service f02b19
	gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (self), cell, "markup", 1);
Packit Service f02b19
Packit Service f02b19
	return G_OBJECT (self);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_combo_selector_init (GcrComboSelector *self)
Packit Service f02b19
{
Packit Service f02b19
	self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_COMBO_SELECTOR, GcrComboSelectorPrivate);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_combo_selector_dispose (GObject *obj)
Packit Service f02b19
{
Packit Service f02b19
	GcrComboSelector *self = GCR_COMBO_SELECTOR (obj);
Packit Service f02b19
Packit Service f02b19
	if (self->pv->model)
Packit Service f02b19
		g_object_unref (self->pv->model);
Packit Service f02b19
	self->pv->model = NULL;
Packit Service f02b19
Packit Service f02b19
	if (self->pv->collection)
Packit Service f02b19
		g_object_unref (self->pv->collection);
Packit Service f02b19
	self->pv->collection = NULL;
Packit Service f02b19
Packit Service f02b19
	G_OBJECT_CLASS (gcr_combo_selector_parent_class)->dispose (obj);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_combo_selector_finalize (GObject *obj)
Packit Service f02b19
{
Packit Service f02b19
	GcrComboSelector *self = GCR_COMBO_SELECTOR (obj);
Packit Service f02b19
Packit Service f02b19
	g_assert (!self->pv->collection);
Packit Service f02b19
	g_assert (!self->pv->model);
Packit Service f02b19
Packit Service f02b19
	G_OBJECT_CLASS (gcr_combo_selector_parent_class)->finalize (obj);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_combo_selector_set_property (GObject *obj, guint prop_id, const GValue *value,
Packit Service f02b19
                                 GParamSpec *pspec)
Packit Service f02b19
{
Packit Service f02b19
	GcrComboSelector *self = GCR_COMBO_SELECTOR (obj);
Packit Service f02b19
Packit Service f02b19
	switch (prop_id) {
Packit Service f02b19
	case PROP_COLLECTION:
Packit Service f02b19
		g_return_if_fail (!self->pv->collection);
Packit Service f02b19
		self->pv->collection = g_value_dup_object (value);
Packit Service f02b19
		g_return_if_fail (self->pv->collection);
Packit Service f02b19
		break;
Packit Service f02b19
	default:
Packit Service f02b19
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
Packit Service f02b19
		break;
Packit Service f02b19
	}
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_combo_selector_get_property (GObject *obj, guint prop_id, GValue *value,
Packit Service f02b19
                                 GParamSpec *pspec)
Packit Service f02b19
{
Packit Service f02b19
	GcrComboSelector *self = GCR_COMBO_SELECTOR (obj);
Packit Service f02b19
Packit Service f02b19
	switch (prop_id) {
Packit Service f02b19
	case PROP_COLLECTION:
Packit Service f02b19
		g_value_set_object (value, gcr_combo_selector_get_collection (self));
Packit Service f02b19
		break;
Packit Service f02b19
	default:
Packit Service f02b19
		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
Packit Service f02b19
		break;
Packit Service f02b19
	}
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_combo_selector_class_init (GcrComboSelectorClass *klass)
Packit Service f02b19
{
Packit Service f02b19
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit Service f02b19
Packit Service f02b19
	gobject_class->constructor = gcr_combo_selector_constructor;
Packit Service f02b19
	gobject_class->dispose = gcr_combo_selector_dispose;
Packit Service f02b19
	gobject_class->finalize = gcr_combo_selector_finalize;
Packit Service f02b19
	gobject_class->set_property = gcr_combo_selector_set_property;
Packit Service f02b19
	gobject_class->get_property = gcr_combo_selector_get_property;
Packit Service f02b19
Packit Service f02b19
	g_type_class_add_private (gobject_class, sizeof (GcrComboSelectorPrivate));
Packit Service f02b19
Packit Service f02b19
	/**
Packit Service f02b19
	 * GcrComboSelector:collection:
Packit Service f02b19
	 *
Packit Service f02b19
	 * The collection which contains the objects to display in the selector.
Packit Service f02b19
	 */
Packit Service f02b19
	g_object_class_install_property (gobject_class, PROP_COLLECTION,
Packit Service f02b19
	           g_param_spec_object ("collection", "Collection", "Collection to select from",
Packit Service f02b19
	                                GCR_TYPE_COLLECTION, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/* -----------------------------------------------------------------------------
Packit Service f02b19
 * PUBLIC
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_combo_selector_new:
Packit Service f02b19
 * @collection: The collection that contains the objects to display
Packit Service f02b19
 *
Packit Service f02b19
 * Create a new #GcrTreeSelector.
Packit Service f02b19
 *
Packit Service f02b19
 * Returns: A newly allocated selector, which should be released with
Packit Service f02b19
 *     g_object_unref().
Packit Service f02b19
 */
Packit Service f02b19
GcrComboSelector*
Packit Service f02b19
gcr_combo_selector_new (GcrCollection *collection)
Packit Service f02b19
{
Packit Service f02b19
	return g_object_new (GCR_TYPE_COMBO_SELECTOR,
Packit Service f02b19
	                     "collection", collection,
Packit Service f02b19
	                     NULL);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_combo_selector_get_collection:
Packit Service f02b19
 * @self: The selector
Packit Service f02b19
 *
Packit Service f02b19
 * Get the collection that this selector is displaying objects from.
Packit Service f02b19
 *
Packit Service f02b19
 * Returns: (transfer none): The collection, owned by the selector.
Packit Service f02b19
 */
Packit Service f02b19
GcrCollection *
Packit Service f02b19
gcr_combo_selector_get_collection (GcrComboSelector *self)
Packit Service f02b19
{
Packit Service f02b19
	g_return_val_if_fail (GCR_IS_COMBO_SELECTOR (self), NULL);
Packit Service f02b19
	return self->pv->collection;
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_combo_selector_get_selected:
Packit Service f02b19
 * @self: The selector
Packit Service f02b19
 *
Packit Service f02b19
 * Get the selected object in the selector, or %NULL if nothing selected.
Packit Service f02b19
 *
Packit Service f02b19
 * Returns: (transfer none): the selected object, owned by the selector, or %NULL
Packit Service f02b19
 */
Packit Service f02b19
GObject *
Packit Service f02b19
gcr_combo_selector_get_selected (GcrComboSelector *self)
Packit Service f02b19
{
Packit Service f02b19
	GtkTreeIter iter;
Packit Service f02b19
Packit Service f02b19
	g_return_val_if_fail (GCR_IS_COMBO_SELECTOR (self), NULL);
Packit Service f02b19
	gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter);
Packit Service f02b19
Packit Service f02b19
	return gcr_collection_model_object_for_iter (self->pv->model, &iter);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_combo_selector_set_selected:
Packit Service f02b19
 * @self: The selector
Packit Service f02b19
 * @selected: (allow-none): the object to select or %NULL
Packit Service f02b19
 *
Packit Service f02b19
 * Set the currently selected object in the selector, or clear the selection
Packit Service f02b19
 * if selected is set to %NULL.
Packit Service f02b19
 */
Packit Service f02b19
void
Packit Service f02b19
gcr_combo_selector_set_selected (GcrComboSelector *self, GObject *selected)
Packit Service f02b19
{
Packit Service f02b19
	GtkTreeIter iter;
Packit Service f02b19
Packit Service f02b19
	g_return_if_fail (GCR_IS_COMBO_SELECTOR (self));
Packit Service f02b19
Packit Service f02b19
	if (selected) {
Packit Service f02b19
		if (!gcr_collection_model_iter_for_object (self->pv->model, selected, &iter))
Packit Service f02b19
			g_return_if_reached ();
Packit Service f02b19
		gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
Packit Service f02b19
	} else {
Packit Service f02b19
		gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), NULL);
Packit Service f02b19
	}
Packit Service f02b19
}