diff options
author | Corentin Noël <corentin.noel@collabora.com> | 2021-12-09 11:23:02 +0100 |
---|---|---|
committer | Corentin Noël <tintou@noel.tf> | 2022-04-09 00:44:24 +0200 |
commit | d170d396c855268ef060aca4ce14130f4113ca57 (patch) | |
tree | 7fba01252bfb7fa7bfd140a709c5237d07ee6394 /gcr | |
parent | 559f852ec5c15d058e899ed2617015ff27764045 (diff) | |
download | gcr-d170d396c855268ef060aca4ce14130f4113ca57.tar.gz |
gcr: Remove the GcrCollection interface
There is now the GListModel interface in GLib that should be used for this.
Signed-off-by: Corentin Noël <corentin.noel@collabora.com>
Diffstat (limited to 'gcr')
-rw-r--r-- | gcr/gcr-callback-output-stream.h | 3 | ||||
-rw-r--r-- | gcr/gcr-collection.c | 169 | ||||
-rw-r--r-- | gcr/gcr-collection.h | 80 | ||||
-rw-r--r-- | gcr/gcr-deprecated-base.h | 5 | ||||
-rw-r--r-- | gcr/gcr-filter-collection.c | 372 | ||||
-rw-r--r-- | gcr/gcr-filter-collection.h | 76 | ||||
-rw-r--r-- | gcr/gcr-gnupg-collection.c | 738 | ||||
-rw-r--r-- | gcr/gcr-gnupg-collection.h | 69 | ||||
-rw-r--r-- | gcr/gcr-simple-collection.c | 193 | ||||
-rw-r--r-- | gcr/gcr-simple-collection.h | 65 | ||||
-rw-r--r-- | gcr/gcr-single-collection.c | 151 | ||||
-rw-r--r-- | gcr/gcr-single-collection.h | 45 | ||||
-rw-r--r-- | gcr/gcr-union-collection.c | 339 | ||||
-rw-r--r-- | gcr/gcr-union-collection.h | 77 | ||||
-rw-r--r-- | gcr/gcr.h | 3 | ||||
-rw-r--r-- | gcr/meson.build | 12 | ||||
-rw-r--r-- | gcr/test-filter-collection.c | 272 | ||||
-rw-r--r-- | gcr/test-gnupg-collection.c | 249 |
18 files changed, 1 insertions, 2917 deletions
diff --git a/gcr/gcr-callback-output-stream.h b/gcr/gcr-callback-output-stream.h index b4ba031..668ce5f 100644 --- a/gcr/gcr-callback-output-stream.h +++ b/gcr/gcr-callback-output-stream.h @@ -22,9 +22,8 @@ #ifndef GCR_CALLBACK_OUTPUT_STREAM_H #define GCR_CALLBACK_OUTPUT_STREAM_H -#include "gcr-collection.h" - #include <glib-object.h> +#include <gio/gio.h> G_BEGIN_DECLS diff --git a/gcr/gcr-collection.c b/gcr/gcr-collection.c deleted file mode 100644 index c4c05e5..0000000 --- a/gcr/gcr-collection.c +++ /dev/null @@ -1,169 +0,0 @@ -/* - * 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-collection.h" - -/** - * GcrCollection: - * - * A #GcrCollection is used to group a set of objects. - * - * This is an abstract interface which can be used to determine which objects - * show up in a selector or other user interface element. - * - * Use [ctor@SimpleCollection.new] to create a concrete implementation of this - * interface which you can add objects to. - */ - -enum { - ADDED, - REMOVED, - LAST_SIGNAL, -}; - -static guint signals[LAST_SIGNAL] = { 0 }; - - -typedef GcrCollectionIface GcrCollectionInterface; - -G_DEFINE_INTERFACE (GcrCollection, gcr_collection, G_TYPE_OBJECT); - -static void -gcr_collection_default_init (GcrCollectionIface *iface) -{ - static size_t initialized = 0; - - if (g_once_init_enter (&initialized)) { - - /** - * GcrCollection::added: - * @self: the collection - * @object: (type GObject.Object): object that was added - * - * This signal is emitted when an object is added to the collection. - */ - signals[ADDED] = g_signal_new ("added", GCR_TYPE_COLLECTION, - G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GcrCollectionIface, added), - NULL, NULL, NULL, - G_TYPE_NONE, 1, G_TYPE_OBJECT); - - /** - * GcrCollection::removed: - * @self: the collection - * @object: (type GObject.Object): object that was removed - * - * This signal is emitted when an object is removed from the collection. - */ - signals[REMOVED] = g_signal_new ("removed", GCR_TYPE_COLLECTION, - G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GcrCollectionIface, removed), - NULL, NULL, NULL, - G_TYPE_NONE, 1, G_TYPE_OBJECT); - - g_once_init_leave (&initialized, 1); - } -} - -/* ----------------------------------------------------------------------------- - * PUBLIC - */ - - -/** - * gcr_collection_get_length: - * @self: The collection - * - * Get the number of objects in this collection. - * - * Returns: The number of objects. - */ -guint -gcr_collection_get_length (GcrCollection *self) -{ - g_return_val_if_fail (GCR_IS_COLLECTION (self), 0); - g_return_val_if_fail (GCR_COLLECTION_GET_INTERFACE (self)->get_length, 0); - return GCR_COLLECTION_GET_INTERFACE (self)->get_length (self); -} - -/** - * gcr_collection_get_objects: - * @self: The collection - * - * Get a list of the objects in this collection. - * - * Returns: (transfer container) (element-type GObject.Object): a list of the objects - * in this collection, which should be freed with g_list_free() - */ -GList* -gcr_collection_get_objects (GcrCollection *self) -{ - g_return_val_if_fail (GCR_IS_COLLECTION (self), 0); - g_return_val_if_fail (GCR_COLLECTION_GET_INTERFACE (self)->get_objects, 0); - return GCR_COLLECTION_GET_INTERFACE (self)->get_objects (self); -} - -/** - * gcr_collection_contains: - * @self: the collection - * @object: object to check - * - * Check whether the collection contains an object or not. - * - * Returns: whether the collection contains this object - */ -gboolean -gcr_collection_contains (GcrCollection *self, - GObject *object) -{ - g_return_val_if_fail (GCR_IS_COLLECTION (self), FALSE); - g_return_val_if_fail (G_IS_OBJECT (object), FALSE); - g_return_val_if_fail (GCR_COLLECTION_GET_INTERFACE (self)->contains, FALSE); - return GCR_COLLECTION_GET_INTERFACE (self)->contains (self, object); -} - -/** - * gcr_collection_emit_added: - * @self: The collection - * @object: The object that was added - * - * Emit the #GcrCollection::added signal for the given object. This function - * is used by implementors of this interface. - */ -void -gcr_collection_emit_added (GcrCollection *self, GObject *object) -{ - g_return_if_fail (GCR_IS_COLLECTION (self)); - g_signal_emit (self, signals[ADDED], 0, object); -} - -/** - * gcr_collection_emit_removed: - * @self: The collection - * @object: The object that was removed - * - * Emit the #GcrCollection::removed signal for the given object. This function - * is used by implementors of this interface. - */ -void -gcr_collection_emit_removed (GcrCollection *self, GObject *object) -{ - g_return_if_fail (GCR_IS_COLLECTION (self)); - g_signal_emit (self, signals[REMOVED], 0, object); -} diff --git a/gcr/gcr-collection.h b/gcr/gcr-collection.h deleted file mode 100644 index 6a1d193..0000000 --- a/gcr/gcr-collection.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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/>. - */ - -#ifndef __GCR_COLLECTION_H__ -#define __GCR_COLLECTION_H__ - -#include "gcr-types.h" - -#include <glib-object.h> - -G_BEGIN_DECLS - -#define GCR_TYPE_COLLECTION (gcr_collection_get_type()) -#define GCR_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GCR_TYPE_COLLECTION, GcrCollection)) -#define GCR_IS_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GCR_TYPE_COLLECTION)) -#define GCR_COLLECTION_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), GCR_TYPE_COLLECTION, GcrCollectionIface)) - -typedef struct _GcrCollection GcrCollection; -typedef struct _GcrCollectionIface GcrCollectionIface; - -struct _GcrCollectionIface { - GTypeInterface parent; - - /* signals */ - void (*added) (GcrCollection *self, GObject *object); - - void (*removed) (GcrCollection *self, GObject *object); - - /* virtual */ - guint (*get_length) (GcrCollection *self); - - GList* (*get_objects) (GcrCollection *self); - - gboolean (*contains) (GcrCollection *self, - GObject *object); - - /*< private >*/ - gpointer dummy1; - gpointer dummy2; - gpointer dummy3; - gpointer dummy5; - gpointer dummy6; - gpointer dummy7; - gpointer dummy8; -}; - -GType gcr_collection_get_type (void); - -guint gcr_collection_get_length (GcrCollection *self); - -GList* gcr_collection_get_objects (GcrCollection *self); - -gboolean gcr_collection_contains (GcrCollection *self, - GObject *object); - -void gcr_collection_emit_added (GcrCollection *self, - GObject *object); - -void gcr_collection_emit_removed (GcrCollection *self, - GObject *object); - -G_END_DECLS - -#endif /* __GCR_COLLECTION_H__ */ diff --git a/gcr/gcr-deprecated-base.h b/gcr/gcr-deprecated-base.h index 55dc087..3554823 100644 --- a/gcr/gcr-deprecated-base.h +++ b/gcr/gcr-deprecated-base.h @@ -32,7 +32,6 @@ #include "gcr-importer.h" #include "gcr-parser.h" -#include "gcr-simple-collection.h" G_BEGIN_DECLS @@ -41,10 +40,6 @@ G_BEGIN_DECLS G_DEPRECATED GQuark gcr_error_get_domain (void) G_GNUC_CONST; -G_DEPRECATED_FOR(gcr_collection_contains) -gboolean gcr_simple_collection_contains (GcrSimpleCollection *self, - GObject *object); - G_DEPRECATED_FOR(gcr_importer_listen) GcrParser * gcr_importer_get_parser (GcrImporter *self); diff --git a/gcr/gcr-filter-collection.c b/gcr/gcr-filter-collection.c deleted file mode 100644 index c62abae..0000000 --- a/gcr/gcr-filter-collection.c +++ /dev/null @@ -1,372 +0,0 @@ -/* - * 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-collection.h" -#include "gcr-filter-collection.h" - -#include <string.h> - -/** - * GcrFilterCollection: - * - * A collection which filters a [iface@Collection]. - * - * An implementation of [iface@Collection] which filters objects from another - * underlying collection. Use [ctor@FilterCollection.new_with_callback] - * to create a new filter collection. - * - * The callback will determine the criteria for whether an object shows through - * the filter or not. - */ - -/** - * GcrFilterCollectionClass: - * @parent_class: the parent class - * - * The class struct for [class@FilterCollection]. - */ - -/** - * GcrFilterCollectionFunc: - * @object: object to filter - * @user_data: user data passed to the callback - * - * A function which is called by [class@FilterCollection] in order to determine - * whether an object should show through the filter or not. - * - * Returns: %TRUE if an object should be included in the filtered collection - */ - -enum { - PROP_0, - PROP_UNDERLYING -}; - -struct _GcrFilterCollectionPrivate { - GHashTable *items; - GcrCollection *underlying; - GcrFilterCollectionFunc filter_func; - gpointer user_data; - GDestroyNotify destroy_func; -}; - -static void gcr_filter_collection_iface (GcrCollectionIface *iface); - -G_DEFINE_TYPE_WITH_CODE (GcrFilterCollection, gcr_filter_collection, G_TYPE_OBJECT, - G_ADD_PRIVATE (GcrFilterCollection); - G_IMPLEMENT_INTERFACE (GCR_TYPE_COLLECTION, gcr_filter_collection_iface)); - -static void -add_object (GcrFilterCollection *self, - GObject *object) -{ - g_assert (g_hash_table_lookup (self->pv->items, object) == NULL); - g_hash_table_insert (self->pv->items, g_object_ref (object), object); - gcr_collection_emit_added (GCR_COLLECTION (self), object); -} - -static void -remove_object (GcrFilterCollection *self, - GObject *object) -{ - g_object_ref (object); - if (!g_hash_table_remove (self->pv->items, object)) - g_assert_not_reached (); - gcr_collection_emit_removed (GCR_COLLECTION (self), object); - g_object_unref (object); -} - -static gboolean -filter_object (GcrFilterCollection *self, - GObject *object) -{ - gboolean match = TRUE; - - if (self->pv->filter_func) - match = (self->pv->filter_func) (object, self->pv->user_data); - - return match; -} - -static void -on_collection_added (GcrCollection *collection, - GObject *object, - gpointer user_data) -{ - GcrFilterCollection *self = GCR_FILTER_COLLECTION (user_data); - if (filter_object (self, object)) - add_object (self, object); -} - -static void -on_collection_removed (GcrCollection *collection, - GObject *object, - gpointer user_data) -{ - GcrFilterCollection *self = GCR_FILTER_COLLECTION (user_data); - if (g_hash_table_lookup (self->pv->items, object)) - remove_object (self, object); -} - -static void -gcr_filter_collection_init (GcrFilterCollection *self) -{ - self->pv = gcr_filter_collection_get_instance_private (self); - self->pv->items = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, NULL); -} - -static void -gcr_filter_collection_set_property (GObject *obj, - guint property_id, - const GValue *value, - GParamSpec *pspec) -{ - GcrFilterCollection *self = GCR_FILTER_COLLECTION (obj); - - switch (property_id) { - case PROP_UNDERLYING: - g_return_if_fail (self->pv->underlying == NULL); - self->pv->underlying = g_value_dup_object (value); - g_return_if_fail (self->pv->underlying != NULL); - g_signal_connect (self->pv->underlying, "added", - G_CALLBACK (on_collection_added), self); - g_signal_connect (self->pv->underlying, "removed", - G_CALLBACK (on_collection_removed), self); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec); - break; - } -} - -static void -gcr_filter_collection_get_property (GObject *obj, - guint property_id, - GValue *value, - GParamSpec *pspec) -{ - GcrFilterCollection *self = GCR_FILTER_COLLECTION (obj); - - switch (property_id) { - case PROP_UNDERLYING: - g_value_set_object (value, gcr_filter_collection_get_underlying (self)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec); - break; - } -} - -static void -gcr_filter_collection_finalize (GObject *obj) -{ - GcrFilterCollection *self = GCR_FILTER_COLLECTION (obj); - - if (self->pv->underlying) { - g_signal_handlers_disconnect_by_func (self->pv->underlying, - on_collection_added, self); - g_signal_handlers_disconnect_by_func (self->pv->underlying, - on_collection_removed, self); - g_object_unref (self->pv->underlying); - } - - if (self->pv->destroy_func) - (self->pv->destroy_func) (self->pv->user_data); - - g_assert (self->pv->items); - g_hash_table_destroy (self->pv->items); - self->pv->items = NULL; - - G_OBJECT_CLASS (gcr_filter_collection_parent_class)->finalize (obj); -} - -static void -gcr_filter_collection_class_init (GcrFilterCollectionClass *klass) -{ - GObjectClass *gobject_class = G_OBJECT_CLASS (klass); - - gobject_class->get_property = gcr_filter_collection_get_property; - gobject_class->set_property = gcr_filter_collection_set_property; - gobject_class->finalize = gcr_filter_collection_finalize; - - g_object_class_install_property (gobject_class, PROP_UNDERLYING, - g_param_spec_object ("underlying", "Underlying", "Underlying collection", - GCR_TYPE_COLLECTION, G_PARAM_STATIC_STRINGS | - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); -} - -static guint -gcr_filter_collection_get_length (GcrCollection *coll) -{ - GcrFilterCollection *self = GCR_FILTER_COLLECTION (coll); - return g_hash_table_size (self->pv->items); -} - -static GList* -gcr_filter_collection_get_objects (GcrCollection *coll) -{ - GcrFilterCollection *self = GCR_FILTER_COLLECTION (coll); - return g_hash_table_get_keys (self->pv->items); -} - -static gboolean -gcr_filter_collection_contains (GcrCollection *collection, - GObject *object) -{ - GcrFilterCollection *self = GCR_FILTER_COLLECTION (collection); - return g_hash_table_lookup (self->pv->items, object) ? TRUE : FALSE; -} - -static void -gcr_filter_collection_iface (GcrCollectionIface *iface) -{ - iface->get_length = gcr_filter_collection_get_length; - iface->get_objects = gcr_filter_collection_get_objects; - iface->contains = gcr_filter_collection_contains; -} - -/** - * gcr_filter_collection_new_with_callback: - * @underlying: the underlying collection - * @callback: (nullable): function to call for each object - * @user_data: data to pass to the callback - * @destroy_func: called for user_data when it is no longer needed - * - * Create a new #GcrFilterCollection. - * - * The callback should return %TRUE if an object should appear in the - * filtered collection. - * - * If a %NULL callback is set, then all underlynig objects will appear in the - * filtered collection. - * - * Returns: (transfer full) (type Gcr.FilterCollection): a newly allocated - * filtered collection, which should be freed with g_object_unref() - */ -GcrCollection * -gcr_filter_collection_new_with_callback (GcrCollection *underlying, - GcrFilterCollectionFunc callback, - gpointer user_data, - GDestroyNotify destroy_func) -{ - GcrCollection *collection; - - collection = g_object_new (GCR_TYPE_FILTER_COLLECTION, - "underlying", underlying, - NULL); - gcr_filter_collection_set_callback (GCR_FILTER_COLLECTION (collection), - callback, user_data, destroy_func); - - return collection; -} - -/** - * gcr_filter_collection_set_callback: - * @self: a filter collection - * @callback: (nullable): function to call for each object - * @user_data: data to pass to the callback - * @destroy_func: called for user_data when it is no longer needed - * - * Set the callback used to filter the objects in the underlying collection. - * The callback should return %TRUE if an object should appear in the - * filtered collection. - * - * If a %NULL callback is set, then all underlynig objects will appear in the - * filtered collection. - * - * This will refilter the collection. - */ -void -gcr_filter_collection_set_callback (GcrFilterCollection *self, - GcrFilterCollectionFunc callback, - gpointer user_data, - GDestroyNotify destroy_func) -{ - g_return_if_fail (GCR_IS_FILTER_COLLECTION (self)); - - if (self->pv->destroy_func) - (self->pv->destroy_func) (self->pv->user_data); - self->pv->filter_func = callback; - self->pv->user_data = user_data; - self->pv->destroy_func = destroy_func; - - gcr_filter_collection_refilter (self); -} - -/** - * gcr_filter_collection_refilter: - * @self: a filter collection - * - * Refilter all objects in the underlying collection. Call this function if - * the filter callback function changes its filtering criteria. - */ -void -gcr_filter_collection_refilter (GcrFilterCollection *self) -{ - GList *objects = NULL; - GHashTable *snapshot; - GHashTableIter iter; - GObject *object; - gboolean have; - gboolean should; - GList *l; - - g_return_if_fail (GCR_IS_FILTER_COLLECTION (self)); - - snapshot = g_hash_table_new (g_direct_hash, g_direct_equal); - g_hash_table_iter_init (&iter, self->pv->items); - while (g_hash_table_iter_next (&iter, (gpointer *)&object, NULL)) - g_hash_table_insert (snapshot, object, object); - - if (self->pv->underlying) - objects = gcr_collection_get_objects (self->pv->underlying); - - for (l = objects; l != NULL; l = g_list_next (l)) { - have = g_hash_table_remove (snapshot, l->data); - should = filter_object (self, l->data); - if (have && !should) - remove_object (self, l->data); - else if (!have && should) - add_object (self, l->data); - } - - g_hash_table_iter_init (&iter, snapshot); - while (g_hash_table_iter_next (&iter, (gpointer *)&object, NULL)) - remove_object (self, object); - g_hash_table_destroy (snapshot); - - g_list_free (objects); -} - -/** - * gcr_filter_collection_get_underlying: - * @self: a filter collection - * - * Get the collection that is being filtered by this filter collection. - * - * Returns: (transfer none): the underlying collection - */ -GcrCollection * -gcr_filter_collection_get_underlying (GcrFilterCollection *self) -{ - g_return_val_if_fail (GCR_IS_FILTER_COLLECTION (self), NULL); - return self->pv->underlying; -} diff --git a/gcr/gcr-filter-collection.h b/gcr/gcr-filter-collection.h deleted file mode 100644 index 65e37db..0000000 --- a/gcr/gcr-filter-collection.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * gnome-keyring - * - * Copyright (C) 2011 Collabora Ltd. - * - * 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@collabora.co.uk> - */ - -#ifndef __GCR_FILTER_COLLECTION_H__ -#define __GCR_FILTER_COLLECTION_H__ - -#include "gcr-collection.h" - -#include <glib-object.h> - -G_BEGIN_DECLS - -#define GCR_TYPE_FILTER_COLLECTION (gcr_filter_collection_get_type ()) -#define GCR_FILTER_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GCR_TYPE_FILTER_COLLECTION, GcrFilterCollection)) -#define GCR_FILTER_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GCR_TYPE_FILTER_COLLECTION, GcrFilterCollectionClass)) -#define GCR_IS_FILTER_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GCR_TYPE_FILTER_COLLECTION)) -#define GCR_IS_FILTER_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GCR_TYPE_FILTER_COLLECTION)) -#define GCR_FILTER_COLLECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GCR_TYPE_FILTER_COLLECTION, GcrFilterCollectionClass)) - -typedef struct _GcrFilterCollection GcrFilterCollection; -typedef struct _GcrFilterCollectionClass GcrFilterCollectionClass; -typedef struct _GcrFilterCollectionPrivate GcrFilterCollectionPrivate; - -struct _GcrFilterCollection { - GObject parent; - - /*< private >*/ - GcrFilterCollectionPrivate *pv; -}; - -struct _GcrFilterCollectionClass { - GObjectClass parent_class; -}; - -GType gcr_filter_collection_get_type (void); - -typedef gboolean (* GcrFilterCollectionFunc) (GObject *object, - gpointer user_data); - -GcrCollection * gcr_filter_collection_new_with_callback (GcrCollection *underlying, - GcrFilterCollectionFunc callback, - gpointer user_data, - GDestroyNotify destroy_func); - -void gcr_filter_collection_set_callback (GcrFilterCollection *self, - GcrFilterCollectionFunc callback, - gpointer user_data, - GDestroyNotify destroy_func); - -void gcr_filter_collection_refilter (GcrFilterCollection *self); - -GcrCollection * gcr_filter_collection_get_underlying (GcrFilterCollection *self); - -G_DEFINE_AUTOPTR_CLEANUP_FUNC (GcrFilterCollection, g_object_unref) - -G_END_DECLS - -#endif /* __GCR_FILTER_COLLECTION_H__ */ diff --git a/gcr/gcr-gnupg-collection.c b/gcr/gcr-gnupg-collection.c deleted file mode 100644 index d2f90bf..0000000 --- a/gcr/gcr-gnupg-collection.c +++ /dev/null @@ -1,738 +0,0 @@ -/* - * gnome-keyring - * - * Copyright (C) 2011 Collabora Ltd. - * - * 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@collabora.co.uk> - */ - -#include "config.h" - -#include "gcr-callback-output-stream.h" -#include "gcr-collection.h" -#include "gcr-gnupg-collection.h" -#include "gcr-gnupg-key.h" -#include "gcr-gnupg-process.h" -#include "gcr-gnupg-records.h" -#include "gcr-gnupg-util.h" -#include "gcr-internal.h" -#include "gcr-record.h" -#include "gcr-util.h" - -#include <sys/wait.h> -#include <string.h> - -enum { - PROP_0, - PROP_DIRECTORY -}; - -struct _GcrGnupgCollectionPrivate { - GHashTable *items; /* char *keyid -> GcrGnupgKey* */ - gchar *directory; -}; - -/* Forward declarations */ -static void _gcr_collection_iface (GcrCollectionIface *iface); - -G_DEFINE_TYPE_WITH_CODE (GcrGnupgCollection, _gcr_gnupg_collection, G_TYPE_OBJECT, - G_ADD_PRIVATE (GcrGnupgCollection); - G_IMPLEMENT_INTERFACE (GCR_TYPE_COLLECTION, _gcr_collection_iface) -); - - -static void -_gcr_gnupg_collection_init (GcrGnupgCollection *self) -{ - self->pv = _gcr_gnupg_collection_get_instance_private (self); - - self->pv->items = g_hash_table_new_full (g_str_hash, g_str_equal, - g_free, g_object_unref); -} - -static void -_gcr_gnupg_collection_set_property (GObject *obj, guint prop_id, const GValue *value, - GParamSpec *pspec) -{ - GcrGnupgCollection *self = GCR_GNUPG_COLLECTION (obj); - - switch (prop_id) { - case PROP_DIRECTORY: - g_return_if_fail (!self->pv->directory); - self->pv->directory = g_value_dup_string (value); - if (self->pv->directory && !g_path_is_absolute (self->pv->directory)) { - g_warning ("gnupg collection directory path should be absolute: %s", - self->pv->directory); - } - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -_gcr_gnupg_collection_get_property (GObject *obj, guint prop_id, GValue *value, - GParamSpec *pspec) -{ - GcrGnupgCollection *self = GCR_GNUPG_COLLECTION (obj); - - switch (prop_id) { - case PROP_DIRECTORY: - g_value_set_string (value, self->pv->directory); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); - break; - } -} - -static void -_gcr_gnupg_collection_dispose (GObject *obj) -{ - GcrGnupgCollection *self = GCR_GNUPG_COLLECTION (obj); - - g_hash_table_remove_all (self->pv->items); - - G_OBJECT_CLASS (_gcr_gnupg_collection_parent_class)->dispose (obj); -} - -static void -_gcr_gnupg_collection_finalize (GObject *obj) -{ - GcrGnupgCollection *self = GCR_GNUPG_COLLECTION (obj); - - g_assert (self->pv->items); - g_assert (g_hash_table_size (self->pv->items) == 0); - g_hash_table_destroy (self->pv->items); - self->pv->items = NULL; - - g_free (self->pv->directory); - self->pv->directory = NULL; - - G_OBJECT_CLASS (_gcr_gnupg_collection_parent_class)->finalize (obj); -} - -static void -_gcr_gnupg_collection_class_init (GcrGnupgCollectionClass *klass) -{ - GObjectClass *gobject_class = G_OBJECT_CLASS (klass); - - gobject_class->get_property = _gcr_gnupg_collection_get_property; - gobject_class->set_property = _gcr_gnupg_collection_set_property; - gobject_class->dispose = _gcr_gnupg_collection_dispose; - gobject_class->finalize = _gcr_gnupg_collection_finalize; - - /** - * GcrGnupgCollection:directory: - * - * Directory to load the gnupg keys from, or %NULL for default - * ~/.gnupg/ directory. - */ - g_object_class_install_property (gobject_class, PROP_DIRECTORY, - g_param_spec_string ("directory", "Directory", "Gnupg Directory", - NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - - _gcr_initialize_library (); -} - -static guint -gcr_gnupg_collection_real_get_length (GcrCollection *coll) -{ - GcrGnupgCollection *self = GCR_GNUPG_COLLECTION (coll); - return g_hash_table_size (self->pv->items); -} - -static GList* -gcr_gnupg_collection_real_get_objects (GcrCollection *coll) -{ - GcrGnupgCollection *self = GCR_GNUPG_COLLECTION (coll); - return g_hash_table_get_values (self->pv->items); -} - -static gboolean -gcr_gnupg_collection_real_contains (GcrCollection *collection, - GObject *object) -{ - GcrGnupgCollection *self = GCR_GNUPG_COLLECTION (collection); - GcrGnupgKey *key; - - if (!GCR_IS_GNUPG_KEY (object)) - return FALSE; - key = g_hash_table_lookup (self->pv->items, - _gcr_gnupg_key_get_keyid (GCR_GNUPG_KEY (object))); - if (key != NULL && G_OBJECT (key) == object) - return TRUE; - return FALSE; -} - -static void -_gcr_collection_iface (GcrCollectionIface *iface) -{ - iface->get_length = gcr_gnupg_collection_real_get_length; - iface->get_objects = gcr_gnupg_collection_real_get_objects; - iface->contains = gcr_gnupg_collection_real_contains; -} - -/** - * _gcr_gnupg_collection_new: - * @directory: (nullable): The gnupg home directory. - * - * Create a new GcrGnupgCollection. - * - * The gnupg home directory is where the keyring files live. If directory is - * %NULL then the default gnupg home directory is used. - * - * Returns: (transfer full) (type Gcr.GnupgCollection): A newly allocated collection. - */ -GcrCollection* -_gcr_gnupg_collection_new (const gchar *directory) -{ - return g_object_new (GCR_TYPE_GNUPG_COLLECTION, - "directory", directory, - NULL); -} - -/* - * We have to run the gnupg process twice to list the public and then the - * secret keys. These phases are tracked by GcrLoadingPhase. If the first - * phase completes successfully (using gpg --list-keys) then we move on to - * the second phase where the secret keys are loaded (using gpg --list-secret-keys) - * - * If a key is loaded as a public key by the public phase, it can be updated by - * the secret phase. A key discovered in the secret phase must have a public - * counterpart already loaded by the public phase. - */ - -typedef enum { - GCR_LOADING_PHASE_PUBLIC = 1, - GCR_LOADING_PHASE_SECRET = 2, -} GcrLoadingPhase; - -/* - * We use @difference to track the keys that were in the collection before - * the load process, and then remove any not found, at the end of the load - * process. Strings are directly used from collection->pv->items keys. - */ - -typedef struct { - GcrGnupgCollection *collection; /* reffed pointer back to collection */ - GcrLoadingPhase loading_phase; /* Whether loading public or private */ - GPtrArray *records; /* GcrRecord* not yet made into a key */ - GcrGnupgProcess *process; /* The gnupg process itself */ - GString *out_data; /* Pending output not yet parsed into colons */ - GHashTable *difference; /* Hashset gchar *keyid -> gchar *keyid */ - - guint error_sig; - guint status_sig; - GOutputStream *output; - GOutputStream *outattr; - - GQueue *attribute_queue; /* Queue of unprocessed GcrRecord* status records */ - GByteArray *attribute_buf; /* Buffer of unprocessed attribute data received */ - GHashTable *attributes; /* Processed attributes waiting for a matching key */ -} GcrGnupgCollectionLoad; - -/* Forward declarations */ -static void spawn_gnupg_list_process (GcrGnupgCollectionLoad *load, GTask *task); - -static void -_gcr_gnupg_collection_load_free (gpointer data) -{ - GcrGnupgCollectionLoad *load = data; - g_assert (load); - - g_ptr_array_unref (load->records); - g_string_free (load->out_data, TRUE); - g_hash_table_destroy (load->difference); - g_object_unref (load->collection); - - if (load->process) { - if (load->error_sig) - g_signal_handler_disconnect (load->process, load->error_sig); - if (load->status_sig) - g_signal_handler_disconnect (load->process, load->status_sig); - g_object_unref (load->process); - } - - g_output_stream_close (load->output, NULL, NULL); - g_object_unref (load->output); - g_output_stream_close (load->outattr, NULL, NULL); - g_object_unref (load->outattr); - - if (load->attribute_queue) { - while (!g_queue_is_empty (load->attribute_queue)) - _gcr_record_free (g_queue_pop_head (load->attribute_queue)); - g_queue_free (load->attribute_queue); - } - if (load->attribute_buf) - g_byte_array_unref (load->attribute_buf); - if (load->attributes) - g_hash_table_destroy (load->attributes); - - g_slice_free (GcrGnupgCollectionLoad, load); -} - -static void -process_records_as_public_key (GcrGnupgCollectionLoad *load, GPtrArray *records, - const gchar *keyid) -{ - GPtrArray *attr_records = NULL; - const gchar *fingerprint; - gchar *orig_fingerprint; - GcrGnupgKey *key; - guint i; - - /* Add in any attributes we have loaded */ - fingerprint = _gcr_gnupg_records_get_fingerprint (records); - if (fingerprint && load->attributes) - attr_records = g_hash_table_lookup (load->attributes, fingerprint); - if (attr_records) { - g_debug ("adding %d user id attribute(s) to key/fingerprint: %s/%s", - (gint)attr_records->len, keyid, fingerprint); - - if (!g_hash_table_lookup_extended (load->attributes, fingerprint, - (gpointer*)&orig_fingerprint, NULL)) - g_assert_not_reached (); - if (!g_hash_table_steal (load->attributes, fingerprint)) - g_assert_not_reached (); - g_free (orig_fingerprint); - - /* Move all the attribute records over to main records set */ - for (i = 0; i < attr_records->len; i++) - g_ptr_array_add (records, attr_records->pdata[i]); - - /* Shallow free of attr_records array */ - g_free (g_ptr_array_free (attr_records, FALSE)); - } - - /* Note that we've seen this keyid */ - g_hash_table_remove (load->difference, keyid); - - key = g_hash_table_lookup (load->collection->pv->items, keyid); - - /* Already have this key, just update */ - if (key) { - g_debug ("updating public key: %s", keyid); - _gcr_gnupg_key_set_public_records (key, records); - - /* Add a new key */ - } else { - key = _gcr_gnupg_key_new (records, NULL); - g_debug ("creating public key: %s", keyid); - g_hash_table_insert (load->collection->pv->items, g_strdup (keyid), key); - gcr_collection_emit_added (GCR_COLLECTION (load->collection), G_OBJECT (key)); - } -} - -static void -process_records_as_secret_key (GcrGnupgCollectionLoad *load, GPtrArray *records, - const gchar *keyid) -{ - GcrGnupgKey *key; - - key = g_hash_table_lookup (load->collection->pv->items, keyid); - - /* Don't have this key */ - if (key == NULL) { - g_message ("Secret key seen but no public key for: %s", keyid); - - /* Tell the private key that it's a secret one */ - } else { - g_debug ("adding secret records to key: %s", keyid); - _gcr_gnupg_key_set_secret_records (key, records); - } -} - -static void -process_records_as_key (GcrGnupgCollectionLoad *load) -{ - GPtrArray *records; - const gchar *keyid; - GQuark schema; - - g_assert (load->records->len); - - records = load->records; - load->records = g_ptr_array_new_with_free_func (_gcr_record_free); - - keyid = _gcr_gnupg_records_get_keyid (records); - if (keyid) { - schema = _gcr_record_get_schema (records->pdata[0]); - - /* A public key */ - if (schema == GCR_RECORD_SCHEMA_PUB) - process_records_as_public_key (load, records, keyid); - - /* A secret key */ - else if (schema == GCR_RECORD_SCHEMA_SEC) - process_records_as_secret_key (load, records, keyid); - - else - g_assert_not_reached (); - - } else { - g_warning ("parsed gnupg data had no keyid"); - } - - g_ptr_array_unref (records); -} - -static gboolean -process_outstanding_attribute (GcrGnupgCollectionLoad *load, GcrRecord *record) -{ - const gchar *fingerprint; - GPtrArray *records; - GcrRecord *xa1; - guint length; - - if (!_gcr_record_get_uint (record, GCR_RECORD_ATTRIBUTE_LENGTH, &length)) - g_return_val_if_reached (FALSE); - fingerprint = _gcr_record_get_raw (record, GCR_RECORD_ATTRIBUTE_KEY_FINGERPRINT); - g_return_val_if_fail (fingerprint != NULL, FALSE); - - /* Do we have enough data for this attribute? */ - if (!load->attribute_buf || load->attribute_buf->len < length) { - g_debug ("not enough attribute data in buffer: %u", length); - return FALSE; - } - - if (!load->attributes) - load->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, - g_free, (GDestroyNotify)g_ptr_array_unref); - - records = g_hash_table_lookup (load->attributes, fingerprint); - if (!records) { - records = g_ptr_array_new_with_free_func (_gcr_record_free); - g_hash_table_insert (load->attributes, g_strdup (fingerprint), records); - } - - g_debug ("new attribute of length %d for key with fingerprint %s", - length, fingerprint); - - xa1 = _gcr_gnupg_build_xa1_record (record, load->attribute_buf->data, length); - g_ptr_array_add (records, xa1); - - /* Did we use up all the attribute data? Get rid of the buffer */ - if (length == load->attribute_buf->len) { - g_byte_array_unref (load->attribute_buf); - load->attribute_buf = NULL; - - /* Otherwise clear out the used data from buffer */ - } else { - g_byte_array_remove_range (load->attribute_buf, 0, length); - } - - return TRUE; -} - -static void -process_outstanding_attributes (GcrGnupgCollectionLoad *load) -{ - GcrRecord *record; - - if (load->attribute_queue == NULL) - return; - - g_debug ("%d outstanding attribute records", - (gint)g_queue_get_length (load->attribute_queue)); - - for (;;) { - record = g_queue_peek_head (load->attribute_queue); - if (record == NULL) - break; - if (!process_outstanding_attribute (load, record)) - break; - g_queue_pop_head (load->attribute_queue); - _gcr_record_free (record); - } -} - -static void -on_line_parse_output (const gchar *line, gpointer user_data) -{ - GcrGnupgCollectionLoad *load = user_data; - GcrRecord *record; - GQuark schema; - - g_debug ("output: %s", line); - - record = _gcr_record_parse_colons (line, -1); - if (!record) { - g_warning ("invalid gnupg output line: %s", line); - return; - } - - schema = _gcr_record_get_schema (record); - - /* - * Each time we see a line with 'pub' or 'sec' schema we assume that - * it's a new key being listed. - */ - if (schema == GCR_RECORD_SCHEMA_PUB || schema == GCR_RECORD_SCHEMA_SEC) { - g_debug ("start of new key"); - if (load->records->len) - process_records_as_key (load); - g_assert (!load->records->len); - g_ptr_array_add (load->records, record); - record = NULL; - - /* - * 'uid' and 'fpr' schema lines get added to the key that came before. - */ - } else if (schema == GCR_RECORD_SCHEMA_UID || - schema == GCR_RECORD_SCHEMA_FPR) { - if (load->records->len) { - g_ptr_array_add (load->records, record); - record = NULL; - } - } - - if (record != NULL) - _gcr_record_free (record); -} - - -static gssize -on_gnupg_process_output_data (gconstpointer buffer, - gsize count, - GCancellable *cancellable, - gpointer user_data, - GError **error) -{ - GTask *task = G_TASK (user_data); - GcrGnupgCollectionLoad *load = g_task_get_task_data (task); - - g_string_append_len (load->out_data, buffer, count); - _gcr_util_parse_lines (load->out_data, FALSE, on_line_parse_output, load); - return count; -} - -static void -on_gnupg_process_error_line (GcrGnupgProcess *process, const gchar *line, - gpointer user_data) -{ - g_printerr ("%s\n", line); -} - -static void -on_gnupg_process_status_record (GcrGnupgProcess *process, GcrRecord *record, - gpointer user_data) -{ - GTask *task = G_TASK (user_data); - GcrGnupgCollectionLoad *load = g_task_get_task_data (task); - - if (GCR_RECORD_SCHEMA_ATTRIBUTE != _gcr_record_get_schema (record)) - return; - - if (!load->attribute_queue) - load->attribute_queue = g_queue_new (); - - g_queue_push_tail (load->attribute_queue, _gcr_record_copy (record)); - process_outstanding_attributes (load); -} - -static gssize -on_gnupg_process_attribute_data (gconstpointer buffer, - gsize count, - GCancellable *cancellable, - gpointer user_data, - GError **error) -{ - GTask *task = G_TASK (user_data); - GcrGnupgCollectionLoad *load = g_task_get_task_data (task); - - /* If we don't have a buffer, just claim this one */ - if (!load->attribute_buf) - load->attribute_buf = g_byte_array_new (); - - g_byte_array_append (load->attribute_buf, buffer, count); - - process_outstanding_attributes (load); - return count; -} - -static void -on_gnupg_process_completed (GObject *source, GAsyncResult *result, gpointer user_data) -{ - GTask *task = G_TASK (user_data); - GcrGnupgCollectionLoad *load = g_task_get_task_data (task); - GHashTableIter iter; - GError *error = NULL; - GObject *object; - gpointer keyid; - - if (!_gcr_gnupg_process_run_finish (GCR_GNUPG_PROCESS (source), result, &error)) { - g_task_return_error (task, g_steal_pointer (&error)); - g_clear_object (&task); - return; - } - - /* Process any remaining output */ - _gcr_util_parse_lines (load->out_data, TRUE, on_line_parse_output, load); - - /* Process last bit as a key, if any */ - if (load->records->len) - process_records_as_key (load); - - /* If we completed loading public keys, then go and load secret */ - switch (load->loading_phase) { - case GCR_LOADING_PHASE_PUBLIC: - g_debug ("public load phase completed"); - load->loading_phase = GCR_LOADING_PHASE_SECRET; - spawn_gnupg_list_process (load, task); - g_clear_object (&task); - return; - case GCR_LOADING_PHASE_SECRET: - g_debug ("secret load phase completed"); - /* continue below */ - break; - default: - g_assert_not_reached (); - } - - /* Remove any keys that we still have in the difference */ - g_hash_table_iter_init (&iter, load->difference); - while (g_hash_table_iter_next (&iter, &keyid, NULL)) { - object = g_hash_table_lookup (load->collection->pv->items, keyid); - if (object != NULL) { - g_object_ref (object); - g_debug ("removing key no longer present in keyring: %s", (gchar*)keyid); - g_hash_table_remove (load->collection->pv->items, keyid); - gcr_collection_emit_removed (GCR_COLLECTION (load->collection), object); - g_object_unref (object); - } - } - - g_task_return_boolean (task, TRUE); - g_clear_object (&task); -} - -static void -spawn_gnupg_list_process (GcrGnupgCollectionLoad *load, GTask *task) -{ - GCancellable *cancellable = g_task_get_cancellable (task); - GcrGnupgProcessFlags flags = 0; - GPtrArray *argv; - - argv = g_ptr_array_new (); - - switch (load->loading_phase) { - case GCR_LOADING_PHASE_PUBLIC: - g_debug ("starting public load phase"); - g_ptr_array_add (argv, (gpointer)"--list-keys"); - /* Load photos in public phase */ - flags = GCR_GNUPG_PROCESS_WITH_ATTRIBUTES | - GCR_GNUPG_PROCESS_WITH_STATUS; - break; - case GCR_LOADING_PHASE_SECRET: - g_debug ("starting secret load phase"); - g_ptr_array_add (argv, (gpointer)"--list-secret-keys"); - break; - default: - g_assert_not_reached (); - } - - g_ptr_array_add (argv, (gpointer)"--fixed-list-mode"); - g_ptr_array_add (argv, (gpointer)"--with-colons"); - g_ptr_array_add (argv, (gpointer)"--with-fingerprint"); - g_ptr_array_add (argv, NULL); - - /* res is unreffed in on_gnupg_process_completed */ - _gcr_gnupg_process_run_async (load->process, (const gchar**)argv->pdata, NULL, flags, - cancellable, on_gnupg_process_completed, - g_object_ref (task)); - - g_ptr_array_unref (argv); -} - -/** - * _gcr_gnupg_collection_load_async: - * @self: The collection - * @cancellable: (nullable): Cancellation object or %NULL - * @callback: Callback to call when result is ready - * @user_data: Data for callback - * - * Start an operation to load or reload the list of gnupg keys in this - * collection. - */ -void -_gcr_gnupg_collection_load_async (GcrGnupgCollection *self, GCancellable *cancellable, - GAsyncReadyCallback callback, gpointer user_data) -{ - GTask *task; - GcrGnupgCollectionLoad *load; - GHashTableIter iter; - gpointer keyid; - - g_return_if_fail (GCR_IS_GNUPG_COLLECTION (self)); - - /* TODO: Cancellation not yet implemented */ - - task = g_task_new (self, cancellable, callback, user_data); - g_task_set_source_tag (task, _gcr_gnupg_collection_load_async); - - load = g_slice_new0 (GcrGnupgCollectionLoad); - load->records = g_ptr_array_new_with_free_func (_gcr_record_free); - load->out_data = g_string_sized_new (1024); - load->collection = g_object_ref (self); - - load->output = _gcr_callback_output_stream_new (on_gnupg_process_output_data, task, NULL); - load->outattr = _gcr_callback_output_stream_new (on_gnupg_process_attribute_data, task, NULL); - - load->process = _gcr_gnupg_process_new (self->pv->directory, NULL); - _gcr_gnupg_process_set_output_stream (load->process, load->output); - _gcr_gnupg_process_set_attribute_stream (load->process, load->outattr); - load->error_sig = g_signal_connect (load->process, "error-line", G_CALLBACK (on_gnupg_process_error_line), task); - load->status_sig = g_signal_connect (load->process, "status-record", G_CALLBACK (on_gnupg_process_status_record), task); - - /* - * Track all the keys we currently have, at end remove those that - * didn't get listed by the gpg process. - */ - load->difference = g_hash_table_new (g_str_hash, g_str_equal); - g_hash_table_iter_init (&iter, self->pv->items); - while (g_hash_table_iter_next (&iter, &keyid, NULL)) - g_hash_table_insert (load->difference, keyid, keyid); - - g_task_set_task_data (task, load, _gcr_gnupg_collection_load_free); - - load->loading_phase = GCR_LOADING_PHASE_PUBLIC; - spawn_gnupg_list_process (load, task); - - g_clear_object (&task); -} - -/** - * _gcr_gnupg_collection_load_finish: - * @self: The collection - * @result: The result passed to the callback - * @error: Location to raise an error on failure. - * - * Get the result of an operation to load or reload the list of gnupg keys - * in this collection. - */ -gboolean -_gcr_gnupg_collection_load_finish (GcrGnupgCollection *self, GAsyncResult *result, - GError **error) -{ - g_return_val_if_fail (GCR_IS_GNUPG_COLLECTION (self), FALSE); - g_return_val_if_fail (!error || !*error, FALSE); - - g_return_val_if_fail (g_task_is_valid (result, self), FALSE); - - return g_task_propagate_boolean (G_TASK (result), error); -} diff --git a/gcr/gcr-gnupg-collection.h b/gcr/gcr-gnupg-collection.h deleted file mode 100644 index ca22a9b..0000000 --- a/gcr/gcr-gnupg-collection.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * gnome-keyring - * - * Copyright (C) 2011 Collabora Ltd. - * - * 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@collabora.co.uk> - */ - -#ifndef GCR_GNUPG_COLLECTION_H -#define GCR_GNUPG_COLLECTION_H - -#include "gcr-collection.h" - -#include <glib-object.h> - -G_BEGIN_DECLS - -#define GCR_TYPE_GNUPG_COLLECTION (_gcr_gnupg_collection_get_type ()) -#define GCR_GNUPG_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GCR_TYPE_GNUPG_COLLECTION, GcrGnupgCollection)) -#define GCR_GNUPG_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GCR_TYPE_GNUPG_COLLECTION, GcrGnupgCollectionClass)) -#define GCR_IS_GNUPG_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GCR_TYPE_GNUPG_COLLECTION)) -#define GCR_IS_GNUPG_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GCR_TYPE_GNUPG_COLLECTION)) -#define GCR_GNUPG_COLLECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GCR_TYPE_GNUPG_COLLECTION, GcrGnupgCollectionClass)) - -typedef struct _GcrGnupgCollection GcrGnupgCollection; -typedef struct _GcrGnupgCollectionClass GcrGnupgCollectionClass; -typedef struct _GcrGnupgCollectionPrivate GcrGnupgCollectionPrivate; - -struct _GcrGnupgCollection { - /*< private >*/ - GObject parent; - GcrGnupgCollectionPrivate *pv; -}; - -struct _GcrGnupgCollectionClass { - GObjectClass parent_class; -}; - -GType _gcr_gnupg_collection_get_type (void); - -GcrCollection* _gcr_gnupg_collection_new (const gchar *directory); - -void _gcr_gnupg_collection_load_async (GcrGnupgCollection *self, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); - -gboolean _gcr_gnupg_collection_load_finish (GcrGnupgCollection *self, - GAsyncResult *result, - GError **error); - -G_DEFINE_AUTOPTR_CLEANUP_FUNC (GcrGnupgCollection, g_object_unref) - -G_END_DECLS - -#endif /* GCR_GNUPG_COLLECTION_H */ diff --git a/gcr/gcr-simple-collection.c b/gcr/gcr-simple-collection.c deleted file mode 100644 index d0c5a30..0000000 --- a/gcr/gcr-simple-collection.c +++ /dev/null @@ -1,193 +0,0 @@ -/* - * 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-collection.h" -#include "gcr-deprecated-base.h" -#include "gcr-internal.h" -#include "gcr-simple-collection.h" - -#include <string.h> - -/** - * GcrSimpleCollection: - * - * A simple implementation of [iface@Collection], which you can add and remove - * objects from. - * - * You can use [method@SimpleCollection.add] to add objects, and - * [method@SimpleCollection.remove] to remove them again. - */ - -struct _GcrSimpleCollectionPrivate { - GHashTable *items; -}; - -static void gcr_collection_iface (GcrCollectionIface *iface); -G_DEFINE_TYPE_WITH_CODE (GcrSimpleCollection, gcr_simple_collection, G_TYPE_OBJECT, - G_ADD_PRIVATE (GcrSimpleCollection); - G_IMPLEMENT_INTERFACE (GCR_TYPE_COLLECTION, gcr_collection_iface)); - -#define UNUSED_VALUE GUINT_TO_POINTER (1) - -/* ----------------------------------------------------------------------------- - * OBJECT - */ - -static void -gcr_simple_collection_init (GcrSimpleCollection *self) -{ - self->pv = gcr_simple_collection_get_instance_private (self); - self->pv->items = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, NULL); -} - -static void -gcr_simple_collection_dispose (GObject *obj) -{ - GcrSimpleCollection *self = GCR_SIMPLE_COLLECTION (obj); - - g_hash_table_remove_all (self->pv->items); - - G_OBJECT_CLASS (gcr_simple_collection_parent_class)->dispose (obj); -} - -static void -gcr_simple_collection_finalize (GObject *obj) -{ - GcrSimpleCollection *self = GCR_SIMPLE_COLLECTION (obj); - - g_assert (self->pv->items); - g_assert (g_hash_table_size (self->pv->items) == 0); - g_hash_table_destroy (self->pv->items); - self->pv->items = NULL; - - G_OBJECT_CLASS (gcr_simple_collection_parent_class)->finalize (obj); -} - -static void -gcr_simple_collection_class_init (GcrSimpleCollectionClass *klass) -{ - GObjectClass *gobject_class = G_OBJECT_CLASS (klass); - gobject_class->dispose = gcr_simple_collection_dispose; - gobject_class->finalize = gcr_simple_collection_finalize; -} - -static guint -gcr_simple_collection_real_get_length (GcrCollection *coll) -{ - GcrSimpleCollection *self = GCR_SIMPLE_COLLECTION (coll); - return g_hash_table_size (self->pv->items); -} - -static GList* -gcr_simple_collection_real_get_objects (GcrCollection *coll) -{ - GcrSimpleCollection *self = GCR_SIMPLE_COLLECTION (coll); - return g_hash_table_get_keys (self->pv->items); -} - -static gboolean -gcr_simple_collection_real_contains (GcrCollection *collection, - GObject *object) -{ - GcrSimpleCollection *self = GCR_SIMPLE_COLLECTION (collection); - return g_hash_table_lookup (self->pv->items, object) ? TRUE : FALSE; -} - -static void -gcr_collection_iface (GcrCollectionIface *iface) -{ - iface->get_length = gcr_simple_collection_real_get_length; - iface->get_objects = gcr_simple_collection_real_get_objects; - iface->contains = gcr_simple_collection_real_contains; -} - -/* ----------------------------------------------------------------------------- - * PUBLIC - */ - -/** - * gcr_simple_collection_new: - * - * Create a new #GcrSimpleCollection. - * - * Returns: (transfer full) (type Gcr.SimpleCollection): a newly allocated - * collection, which should be freed with g_object_unref() - */ -GcrCollection * -gcr_simple_collection_new (void) -{ - return g_object_new (GCR_TYPE_SIMPLE_COLLECTION, NULL); -} - -/** - * gcr_simple_collection_add: - * @self: The collection - * @object: The object to add - * - * Add an object to this collection - */ -void -gcr_simple_collection_add (GcrSimpleCollection *self, GObject *object) -{ - g_return_if_fail (GCR_IS_SIMPLE_COLLECTION (self)); - g_return_if_fail (G_IS_OBJECT (object)); - g_return_if_fail (!g_hash_table_lookup (self->pv->items, object)); - g_hash_table_insert (self->pv->items, g_object_ref (object), UNUSED_VALUE); - gcr_collection_emit_added (GCR_COLLECTION (self), object); -} - -/** - * gcr_simple_collection_remove: - * @self: The collection - * @object: The object to remove from the collection - * - * Remove an object from the collection. - */ -void -gcr_simple_collection_remove (GcrSimpleCollection *self, GObject *object) -{ - g_return_if_fail (GCR_IS_SIMPLE_COLLECTION (self)); - g_return_if_fail (G_IS_OBJECT (object)); - g_return_if_fail (g_hash_table_lookup (self->pv->items, object)); - g_object_ref (object); - g_hash_table_remove (self->pv->items, object); - gcr_collection_emit_removed (GCR_COLLECTION (self), object); - g_object_unref (object); -} - -/** - * gcr_simple_collection_contains: - * @self: The collection - * @object: The object to check - * - * Check if the collection contains a certain object. - * - * Deprecated: use gcr_collection_contains() instead - * - * Returns: %TRUE if the collection contains the object. - */ -gboolean -gcr_simple_collection_contains (GcrSimpleCollection *self, GObject *object) -{ - g_return_val_if_fail (GCR_IS_SIMPLE_COLLECTION (self), FALSE); - g_return_val_if_fail (G_IS_OBJECT (object), FALSE); - return gcr_collection_contains (GCR_COLLECTION (self), object); -} diff --git a/gcr/gcr-simple-collection.h b/gcr/gcr-simple-collection.h deleted file mode 100644 index 4623dae..0000000 --- a/gcr/gcr-simple-collection.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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/>. - */ - -#ifndef __GCR_SIMPLE_COLLECTION_H__ -#define __GCR_SIMPLE_COLLECTION_H__ - -#include "gcr-collection.h" - -#include <glib-object.h> - -G_BEGIN_DECLS - -#define GCR_TYPE_SIMPLE_COLLECTION (gcr_simple_collection_get_type ()) -#define GCR_SIMPLE_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GCR_TYPE_COLLECTION, GcrSimpleCollection)) -#define GCR_SIMPLE_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GCR_TYPE_COLLECTION, GcrSimpleCollectionClass)) -#define GCR_IS_SIMPLE_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GCR_TYPE_COLLECTION)) -#define GCR_IS_SIMPLE_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GCR_TYPE_COLLECTION)) -#define GCR_SIMPLE_COLLECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GCR_TYPE_COLLECTION, GcrSimpleCollectionClass)) - -typedef struct _GcrSimpleCollection GcrSimpleCollection; -typedef struct _GcrSimpleCollectionClass GcrSimpleCollectionClass; -typedef struct _GcrSimpleCollectionPrivate GcrSimpleCollectionPrivate; - -struct _GcrSimpleCollection { - GObject parent; - - /*< private >*/ - GcrSimpleCollectionPrivate *pv; -}; - -struct _GcrSimpleCollectionClass { - GObjectClass parent_class; -}; - -GType gcr_simple_collection_get_type (void); - -GcrCollection* gcr_simple_collection_new (void); - -void gcr_simple_collection_add (GcrSimpleCollection *self, - GObject *object); - -void gcr_simple_collection_remove (GcrSimpleCollection *self, - GObject *object); - -G_DEFINE_AUTOPTR_CLEANUP_FUNC (GcrSimpleCollection, g_object_unref) - -G_END_DECLS - -#endif /* __GCR_SIMPLE_COLLECTION_H__ */ diff --git a/gcr/gcr-single-collection.c b/gcr/gcr-single-collection.c deleted file mode 100644 index 478af1e..0000000 --- a/gcr/gcr-single-collection.c +++ /dev/null @@ -1,151 +0,0 @@ -/* - * 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-collection.h" -#include "gcr-single-collection.h" - -#include <string.h> - -/** - * GcrSingleCollection: - * - * A single implementation of #GcrCollection. - */ - -struct _GcrSingleCollection { - GObject parent; - GObject *object; -}; - -/** - * GcrSingleCollectionClass: - * @parent_class: The parent class - * - * The class for #GcrSingleCollection. - */ - -static void _gcr_single_collection_iface (GcrCollectionIface *iface); -G_DEFINE_TYPE_WITH_CODE (GcrSingleCollection, _gcr_single_collection, G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE (GCR_TYPE_COLLECTION, _gcr_single_collection_iface)); - -/* ----------------------------------------------------------------------------- - * OBJECT - */ - -static void -_gcr_single_collection_init (GcrSingleCollection *self) -{ - -} - -static void -_gcr_single_collection_dispose (GObject *obj) -{ - GcrSingleCollection *self = GCR_SINGLE_COLLECTION (obj); - - _gcr_single_collection_set_object (self, NULL); - - G_OBJECT_CLASS (_gcr_single_collection_parent_class)->dispose (obj); -} - -static void -_gcr_single_collection_class_init (GcrSingleCollectionClass *klass) -{ - GObjectClass *gobject_class = G_OBJECT_CLASS (klass); - gobject_class->dispose = _gcr_single_collection_dispose; -} - -static guint -_gcr_single_collection_real_get_length (GcrCollection *coll) -{ - GcrSingleCollection *self = GCR_SINGLE_COLLECTION (coll); - return self->object == NULL ? 0 : 1; -} - -static GList* -_gcr_single_collection_real_get_objects (GcrCollection *coll) -{ - GcrSingleCollection *self = GCR_SINGLE_COLLECTION (coll); - return self->object == NULL ? NULL : g_list_append (NULL, self->object); -} - -static gboolean -_gcr_single_collection_real_contains (GcrCollection *collection, - GObject *object) -{ - GcrSingleCollection *self = GCR_SINGLE_COLLECTION (collection); - return self->object == object; -} - -static void -_gcr_single_collection_iface (GcrCollectionIface *iface) -{ - iface->get_length = _gcr_single_collection_real_get_length; - iface->get_objects = _gcr_single_collection_real_get_objects; - iface->contains = _gcr_single_collection_real_contains; -} - -/* ----------------------------------------------------------------------------- - * PUBLIC - */ - -GcrCollection * -_gcr_single_collection_new (GObject *object) -{ - GcrSingleCollection *self; - - self = g_object_new (GCR_TYPE_SINGLE_COLLECTION, NULL); - _gcr_single_collection_set_object (self, object); - - return GCR_COLLECTION (self); -} - -GObject * -_gcr_single_collection_get_object (GcrSingleCollection *self) -{ - g_return_val_if_fail (GCR_IS_SINGLE_COLLECTION (self), NULL); - return self->object; -} - -void -_gcr_single_collection_set_object (GcrSingleCollection *self, - GObject *object) -{ - GObject *obj; - - g_return_if_fail (GCR_IS_SINGLE_COLLECTION (self)); - g_return_if_fail (object == NULL || G_IS_OBJECT (object)); - - if (object == self->object) - return; - - if (self->object) { - obj = self->object; - self->object = NULL; - gcr_collection_emit_removed (GCR_COLLECTION (self), obj); - g_object_unref (obj); - } - - if (object) { - self->object = g_object_ref (object); - gcr_collection_emit_added (GCR_COLLECTION (self), self->object); - } -} diff --git a/gcr/gcr-single-collection.h b/gcr/gcr-single-collection.h deleted file mode 100644 index 7dee26e..0000000 --- a/gcr/gcr-single-collection.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * gnome-keyring - * - * Copyright (C) 2011 Collabora Ltd. - * - * 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@collabora.co.uk> - */ - -#ifndef __GCR_SINGLE_COLLECTION_H__ -#define __GCR_SINGLE_COLLECTION_H__ - -#include "gcr-collection.h" - -#include <glib-object.h> - -G_BEGIN_DECLS - -#define GCR_TYPE_SINGLE_COLLECTION (_gcr_single_collection_get_type ()) -G_DECLARE_FINAL_TYPE (GcrSingleCollection, _gcr_single_collection, - GCR, SINGLE_COLLECTION, - GObject) - -GcrCollection * _gcr_single_collection_new (GObject *object); - -GObject * _gcr_single_collection_get_object (GcrSingleCollection *self); - -void _gcr_single_collection_set_object (GcrSingleCollection *self, - GObject *object); - -G_END_DECLS - -#endif /* __GCR_SINGLE_COLLECTION_H__ */ diff --git a/gcr/gcr-union-collection.c b/gcr/gcr-union-collection.c deleted file mode 100644 index 846b226..0000000 --- a/gcr/gcr-union-collection.c +++ /dev/null @@ -1,339 +0,0 @@ -/* - * gnome-keyring - * - * Copyright (C) 2011 Collabora Ltd. - * - * 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@collabora.co.uk> - */ - -#include "config.h" - -#include "gcr-collection.h" -#include "gcr-internal.h" -#include "gcr-union-collection.h" - -#include <string.h> - -/** - * GcrUnionCollection: - * - * An implementation of #GcrCollection, which combines the objects in - * other [iface@Collection]s. Use [method@UnionCollection.add] to add and - * [method@UnionCollection.remove] to remove them. - */ - -struct _GcrUnionCollectionPrivate { - GHashTable *items; - GHashTable *collections; -}; - -static void gcr_collection_iface (GcrCollectionIface *iface); - -G_DEFINE_TYPE_WITH_CODE (GcrUnionCollection, gcr_union_collection, G_TYPE_OBJECT, - G_ADD_PRIVATE (GcrUnionCollection); - G_IMPLEMENT_INTERFACE (GCR_TYPE_COLLECTION, gcr_collection_iface)); - -static void -on_collection_added (GcrCollection *collection, - GObject *object, - gpointer user_data) -{ - GcrUnionCollection *self = GCR_UNION_COLLECTION (user_data); - gint *count; - - g_object_ref (object); - - count = g_hash_table_lookup (self->pv->items, object); - if (count == NULL) { - count = g_new0 (gint, 1); - *count = 1; - g_hash_table_insert (self->pv->items, object, count); - gcr_collection_emit_added (GCR_COLLECTION (self), object); - } else { - g_assert (*count > 0); - (*count)++; - } - - g_object_unref (object); -} - -static void -on_collection_removed (GcrCollection *collection, - GObject *object, - gpointer user_data) -{ - GcrUnionCollection *self = GCR_UNION_COLLECTION (user_data); - gint *count; - - g_object_ref (object); - - count = g_hash_table_lookup (self->pv->items, object); - if (count != NULL) { - g_assert (*count > 0); - (*count)--; - - if (*count == 0) { - g_hash_table_remove (self->pv->items, object); - gcr_collection_emit_removed (GCR_COLLECTION (self), object); - } - } else { - g_warning ("Object of type %s that exists in an underlying " - "collection of a GcrUnionCollection appeared without " - "emitting 'added' signal.", G_OBJECT_TYPE_NAME (object)); - } - - g_object_unref (object); - -} - -static void -connect_to_collection (GcrUnionCollection *self, - GcrCollection *collection) -{ - g_signal_connect (collection, "added", G_CALLBACK (on_collection_added), self); - g_signal_connect (collection, "removed", G_CALLBACK (on_collection_removed), self); -} - -static void -disconnect_from_collection (GcrUnionCollection *self, - GcrCollection *collection) -{ - g_signal_handlers_disconnect_by_func (collection, on_collection_added, self); - g_signal_handlers_disconnect_by_func (collection, on_collection_removed, self); -} - -static void -gcr_union_collection_init (GcrUnionCollection *self) -{ - self->pv = gcr_union_collection_get_instance_private (self); - self->pv->items = g_hash_table_new_full (g_direct_hash, g_direct_equal, - NULL, g_free); - self->pv->collections = g_hash_table_new_full (g_direct_hash, g_direct_equal, - g_object_unref, NULL); -} - -static void -gcr_union_collection_dispose (GObject *obj) -{ - GcrUnionCollection *self = GCR_UNION_COLLECTION (obj); - GHashTableIter iter; - GcrCollection *collection; - - g_hash_table_iter_init (&iter, self->pv->collections); - while (g_hash_table_iter_next (&iter, (gpointer *)&collection, NULL)) - disconnect_from_collection (self, collection); - g_hash_table_remove_all (self->pv->collections); - g_hash_table_remove_all (self->pv->items); - - G_OBJECT_CLASS (gcr_union_collection_parent_class)->dispose (obj); -} - -static void -gcr_union_collection_finalize (GObject *obj) -{ - GcrUnionCollection *self = GCR_UNION_COLLECTION (obj); - - g_assert (g_hash_table_size (self->pv->items) == 0); - g_hash_table_destroy (self->pv->items); - - g_assert (g_hash_table_size (self->pv->collections) == 0); - g_hash_table_destroy (self->pv->collections); - - G_OBJECT_CLASS (gcr_union_collection_parent_class)->finalize (obj); -} - -static void -gcr_union_collection_class_init (GcrUnionCollectionClass *klass) -{ - GObjectClass *gobject_class = G_OBJECT_CLASS (klass); - gobject_class->dispose = gcr_union_collection_dispose; - gobject_class->finalize = gcr_union_collection_finalize; -} - -static guint -gcr_union_collection_real_get_length (GcrCollection *coll) -{ - GcrUnionCollection *self = GCR_UNION_COLLECTION (coll); - return g_hash_table_size (self->pv->items); -} - -static GList* -gcr_union_collection_real_get_objects (GcrCollection *coll) -{ - GcrUnionCollection *self = GCR_UNION_COLLECTION (coll); - return g_hash_table_get_keys (self->pv->items); -} - -static gboolean -gcr_union_collection_real_contains (GcrCollection *collection, - GObject *object) -{ - GcrUnionCollection *self = GCR_UNION_COLLECTION (collection); - return g_hash_table_lookup (self->pv->items, object) ? TRUE : FALSE; -} - -static void -gcr_collection_iface (GcrCollectionIface *iface) -{ - iface->get_length = gcr_union_collection_real_get_length; - iface->get_objects = gcr_union_collection_real_get_objects; - iface->contains = gcr_union_collection_real_contains; -} - -/* ----------------------------------------------------------------------------- - * PUBLIC - */ - -/** - * gcr_union_collection_new: - * - * Create a new #GcrUnionCollection. - * - * Returns: (transfer full) (type Gcr.UnionCollection): a newly allocated - * collection, which should be freed with g_object_unref() - */ -GcrCollection * -gcr_union_collection_new (void) -{ - return g_object_new (GCR_TYPE_UNION_COLLECTION, NULL); -} - -/** - * gcr_union_collection_add: - * @self: The union collection - * @collection: The collection whose objects to add - * - * Add objects from this collection to the union - */ -void -gcr_union_collection_add (GcrUnionCollection *self, - GcrCollection *collection) -{ - g_return_if_fail (GCR_IS_UNION_COLLECTION (self)); - g_return_if_fail (GCR_IS_COLLECTION (collection)); - gcr_union_collection_take (self, g_object_ref (collection)); -} - -/** - * gcr_union_collection_take: - * @self: The union collection - * @collection: The collection whose objects to add - * - * Add objects from this collection to the union. Do not add an additional - * reference to the collection. - */ -void -gcr_union_collection_take (GcrUnionCollection *self, - GcrCollection *collection) -{ - GList *objects, *l; - - g_return_if_fail (GCR_IS_UNION_COLLECTION (self)); - g_return_if_fail (GCR_IS_COLLECTION (collection)); - g_return_if_fail (!g_hash_table_lookup (self->pv->collections, collection)); - - g_object_ref (collection); - - g_hash_table_insert (self->pv->collections, collection, collection); - connect_to_collection (self, collection); - - objects = gcr_collection_get_objects (collection); - for (l = objects; l != NULL; l = g_list_next (l)) - on_collection_added (collection, l->data, self); - g_list_free (objects); - - g_object_unref (collection); -} - -/** - * gcr_union_collection_remove: - * @self: The collection - * @collection: The collection whose objects to remove - * - * Remove an object from the collection. - */ -void -gcr_union_collection_remove (GcrUnionCollection *self, - GcrCollection *collection) -{ - GList *objects, *l; - - g_return_if_fail (GCR_IS_UNION_COLLECTION (self)); - g_return_if_fail (GCR_IS_COLLECTION (collection)); - g_return_if_fail (g_hash_table_lookup (self->pv->collections, collection)); - - g_object_ref (collection); - - g_hash_table_remove (self->pv->collections, collection); - disconnect_from_collection (self, collection); - - objects = gcr_collection_get_objects (collection); - for (l = objects; l != NULL; l = g_list_next (l)) - on_collection_removed (collection, l->data, self); - g_list_free (objects); - - g_object_unref (collection); -} - -/** - * gcr_union_collection_have: - * @self: the union collection - * @collection: the collection to check - * - * Check whether the collection is present in the union. - * - * Returns: whether present or not - */ -gboolean -gcr_union_collection_have (GcrUnionCollection *self, - GcrCollection *collection) -{ - g_return_val_if_fail (GCR_IS_UNION_COLLECTION (self), FALSE); - g_return_val_if_fail (GCR_IS_COLLECTION (collection), FALSE); - return g_hash_table_lookup (self->pv->collections, collection) != NULL; -} - -/** - * gcr_union_collection_size: - * @self: the union collection - * - * Return the number of collections in this union. This does not reflect - * the number of objects in the combined collection. - * - * Returns: number of collections inlcuded - */ -guint -gcr_union_collection_size (GcrUnionCollection *self) -{ - g_return_val_if_fail (GCR_IS_UNION_COLLECTION (self), FALSE); - return g_hash_table_size (self->pv->collections); -} - -/** - * gcr_union_collection_elements: - * @self: the union collection - * - * Get the collections that have been added to this union. - * - * Returns: (element-type Gcr.Collection) (transfer container): collections - * added to the union - */ -GList * -gcr_union_collection_elements (GcrUnionCollection *self) -{ - g_return_val_if_fail (GCR_IS_UNION_COLLECTION (self), NULL); - return g_hash_table_get_values (self->pv->collections); -} diff --git a/gcr/gcr-union-collection.h b/gcr/gcr-union-collection.h deleted file mode 100644 index 2ca740c..0000000 --- a/gcr/gcr-union-collection.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * gnome-keyring - * - * Copyright (C) 2011 Collabora Ltd. - * - * 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@collabora.co.uk> - */ - -#ifndef __GCR_UNION_COLLECTION_H__ -#define __GCR_UNION_COLLECTION_H__ - -#include "gcr-collection.h" - -#include <glib-object.h> - -G_BEGIN_DECLS - -#define GCR_TYPE_UNION_COLLECTION (gcr_union_collection_get_type ()) -#define GCR_UNION_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GCR_TYPE_COLLECTION, GcrUnionCollection)) -#define GCR_UNION_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GCR_TYPE_COLLECTION, GcrUnionCollectionClass)) -#define GCR_IS_UNION_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GCR_TYPE_COLLECTION)) -#define GCR_IS_UNION_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GCR_TYPE_COLLECTION)) -#define GCR_UNION_COLLECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GCR_TYPE_COLLECTION, GcrUnionCollectionClass)) - -typedef struct _GcrUnionCollection GcrUnionCollection; -typedef struct _GcrUnionCollectionClass GcrUnionCollectionClass; -typedef struct _GcrUnionCollectionPrivate GcrUnionCollectionPrivate; - -struct _GcrUnionCollection { - GObject parent; - - /*< private >*/ - GcrUnionCollectionPrivate *pv; -}; - -struct _GcrUnionCollectionClass { - GObjectClass parent_class; -}; - -GType gcr_union_collection_get_type (void); - -GcrCollection* gcr_union_collection_new (void); - -void gcr_union_collection_add (GcrUnionCollection *self, - GcrCollection *collection); - -void gcr_union_collection_take (GcrUnionCollection *self, - GcrCollection *collection); - -void gcr_union_collection_remove (GcrUnionCollection *self, - GcrCollection *collection); - -gboolean gcr_union_collection_have (GcrUnionCollection *self, - GcrCollection *collection); - -guint gcr_union_collection_size (GcrUnionCollection *self); - -GList * gcr_union_collection_elements (GcrUnionCollection *self); - -G_DEFINE_AUTOPTR_CLEANUP_FUNC (GcrUnionCollection, g_object_unref) - -G_END_DECLS - -#endif /* __GCR_UNION_COLLECTION_H__ */ @@ -38,7 +38,6 @@ #include <gcr/gcr-column.h> #include <gcr/gcr-deprecated-base.h> #include <gcr/gcr-enum-types.h> -#include <gcr/gcr-filter-collection.h> #include <gcr/gcr-fingerprint.h> #include <gcr/gcr-icons.h> #include <gcr/gcr-importer.h> @@ -50,12 +49,10 @@ #include <gcr/gcr-secret-exchange.h> #include <gcr/gcr-secure-memory.h> #include <gcr/gcr-simple-certificate.h> -#include <gcr/gcr-simple-collection.h> #include <gcr/gcr-ssh-askpass.h> #include <gcr/gcr-system-prompt.h> #include <gcr/gcr-system-prompter.h> #include <gcr/gcr-trust.h> -#include <gcr/gcr-union-collection.h> #include <gcr/gcr-unlock-options.h> #include <gcr/gcr-version.h> diff --git a/gcr/meson.build b/gcr/meson.build index 24074e5..8b1448d 100644 --- a/gcr/meson.build +++ b/gcr/meson.build @@ -5,9 +5,7 @@ gcr_public_sources = files( 'gcr-certificate.c', 'gcr-certificate-chain.c', 'gcr-certificate-request.c', - 'gcr-collection.c', 'gcr-comparable.c', - 'gcr-filter-collection.c', 'gcr-fingerprint.c', 'gcr-icons.c', 'gcr-importer.c', @@ -20,19 +18,16 @@ gcr_public_sources = files( 'gcr-secret-exchange.c', 'gcr-secure-memory.c', 'gcr-simple-certificate.c', - 'gcr-simple-collection.c', 'gcr-ssh-askpass.c', 'gcr-system-prompt.c', 'gcr-system-prompter.c', 'gcr-trust.c', - 'gcr-union-collection.c', ) gcr_private_sources = files( 'gcr-callback-output-stream.c', 'gcr-certificate-extensions.c', 'gcr-column.c', - 'gcr-gnupg-collection.c', 'gcr-gnupg-importer.c', 'gcr-gnupg-key.c', 'gcr-gnupg-process.c', @@ -44,7 +39,6 @@ gcr_private_sources = files( 'gcr-openssh.c', 'gcr-pkcs11-importer.c', 'gcr-record.c', - 'gcr-single-collection.c', 'gcr-subject-public-key.c', 'gcr-util.c', ) @@ -54,12 +48,10 @@ gcr_headers = files( 'gcr-certificate.h', 'gcr-certificate-chain.h', 'gcr-certificate-request.h', - 'gcr-collection.h', 'gcr-column.h', 'gcr-comparable.h', 'gcr-deprecated-base.h', 'gcr-fingerprint.h', - 'gcr-filter-collection.h', 'gcr-icons.h', 'gcr-importer.h', 'gcr-import-interaction.h', @@ -71,13 +63,11 @@ gcr_headers = files( 'gcr-secret-exchange.h', 'gcr-secure-memory.h', 'gcr-simple-certificate.h', - 'gcr-simple-collection.h', 'gcr-ssh-askpass.h', 'gcr-system-prompt.h', 'gcr-system-prompter.h', 'gcr-trust.h', 'gcr-types.h', - 'gcr-union-collection.h', 'gcr-unlock-options.h', ) @@ -327,7 +317,6 @@ endforeach # Tests gcr_test_names = [ 'util', - 'filter-collection', 'secret-exchange', 'simple-certificate', 'certificate', @@ -343,7 +332,6 @@ gcr_test_names = [ 'record', 'memory-icon', 'gnupg-key', - 'gnupg-collection', 'gnupg-process', 'system-prompt', 'ssh-askpass', diff --git a/gcr/test-filter-collection.c b/gcr/test-filter-collection.c deleted file mode 100644 index 952dbd3..0000000 --- a/gcr/test-filter-collection.c +++ /dev/null @@ -1,272 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* - Copyright (C) 2011 Collabora Ltd. - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - see <http://www.gnu.org/licenses/>. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gcr/gcr-simple-collection.h" -#include "gcr/gcr-filter-collection.h" - -#include "egg/egg-testing.h" - -#include <glib.h> - -#define NUM_OBJECTS 10 - -typedef struct { - GcrCollection *underlying; - GObject *objects[NUM_OBJECTS]; -} Test; - -static guint -mock_object_value (GObject *object) -{ - return GPOINTER_TO_UINT (g_object_get_data (object, "value")); -} - -static void -setup (Test *test, - gconstpointer unused) -{ - GcrSimpleCollection *collection; - guint i; - - test->underlying = gcr_simple_collection_new (); - collection = GCR_SIMPLE_COLLECTION (test->underlying); - - for (i = 0; i < NUM_OBJECTS; i++) { - test->objects[i] = g_object_new (G_TYPE_OBJECT, NULL); - g_object_set_data (test->objects[i], "value", GUINT_TO_POINTER (i)); - gcr_simple_collection_add (GCR_SIMPLE_COLLECTION (collection), test->objects[i]); - } -} - -static void -teardown (Test *test, - gconstpointer unused) -{ - guint i; - - for (i = 0; i < NUM_OBJECTS; i++) - g_object_unref (test->objects[i]); - - g_object_unref (test->underlying); -} - -static void -test_create (Test *test, - gconstpointer unused) -{ - GcrFilterCollection *filter; - GcrCollection *collection; - GcrCollection *underlying; - GList *objects; - guint i; - - collection = gcr_filter_collection_new_with_callback (test->underlying, - NULL, NULL, NULL); - filter = GCR_FILTER_COLLECTION (collection); - - g_assert (test->underlying == gcr_filter_collection_get_underlying (filter)); - g_object_get (collection, "underlying", &underlying, NULL); - g_assert (test->underlying == underlying); - g_object_unref (underlying); - - g_assert_cmpuint (gcr_collection_get_length (collection), ==, NUM_OBJECTS); - for (i = 0; i < NUM_OBJECTS; i++) - gcr_collection_contains (collection, test->objects[i]); - objects = gcr_collection_get_objects (collection); - g_assert_cmpuint (g_list_length (objects), ==, NUM_OBJECTS); - for (i = 0; i < NUM_OBJECTS; i++) - g_assert (g_list_find (objects, test->objects[i]) != NULL); - g_list_free (objects); - - g_object_unref (collection); -} - -static gboolean -on_filter_increment_value (GObject *object, - gpointer user_data) -{ - guint *value = user_data; - - g_assert_cmpuint (*value, >=, 0); - g_assert_cmpuint (*value, <=, 20); - (*value)++; - - return TRUE; -} - -static void -destroy_change_value (gpointer data) -{ - guint *value = data; - g_assert (value != NULL); - *value = 0; -} - -static void -test_callbacks (Test *test, - gconstpointer unused) -{ - GcrCollection *collection; - GcrFilterCollection *filter; - guint value = 4; - - collection = gcr_filter_collection_new_with_callback (test->underlying, - on_filter_increment_value, - &value, destroy_change_value); - g_assert_cmpuint (value, ==, 4 + NUM_OBJECTS); - - filter = GCR_FILTER_COLLECTION (collection); - - /* This should call destroy (value -> 0), and then refilter all values (value -> 10) */ - gcr_filter_collection_set_callback (filter, on_filter_increment_value, - &value, destroy_change_value); - g_assert_cmpuint (value, ==, NUM_OBJECTS); - - g_object_unref (collection); - g_assert_cmpuint (value, ==, 0); -} - -static gboolean -on_filter_modulo (GObject *object, - gpointer user_data) -{ - guint value = mock_object_value (object); - guint *modulo = user_data; - - g_assert (modulo != NULL); - g_assert_cmpuint (value, >=, 0); - g_assert_cmpuint (value, <, NUM_OBJECTS); - - return (value % *modulo) == 0; -} - -static void -test_filtering (Test *test, - gconstpointer unused) -{ - GcrFilterCollection *filter; - GcrCollection *collection; - GList *objects; - guint modulo; - guint i; - - modulo = 2; - collection = gcr_filter_collection_new_with_callback (test->underlying, - on_filter_modulo, &modulo, NULL); - filter = GCR_FILTER_COLLECTION (collection); - - g_assert_cmpuint (gcr_collection_get_length (collection), ==, NUM_OBJECTS / modulo); - for (i = 0; i < NUM_OBJECTS; i += modulo) - gcr_collection_contains (collection, test->objects[i]); - objects = gcr_collection_get_objects (collection); - g_assert_cmpuint (g_list_length (objects), ==, NUM_OBJECTS / modulo); - for (i = 0; i < NUM_OBJECTS; i += modulo) - g_assert (g_list_find (objects, test->objects[i]) != NULL); - g_list_free (objects); - - modulo = 5; - gcr_filter_collection_refilter (filter); - - g_assert_cmpuint (gcr_collection_get_length (collection), ==, NUM_OBJECTS / modulo); - for (i = 0; i < NUM_OBJECTS; i += modulo) - gcr_collection_contains (collection, test->objects[i]); - objects = gcr_collection_get_objects (collection); - g_assert_cmpuint (g_list_length (objects), ==, NUM_OBJECTS / modulo); - for (i = 0; i < NUM_OBJECTS; i += modulo) - g_assert (g_list_find (objects, test->objects[i]) != NULL); - g_list_free (objects); - - g_object_unref (collection); -} - -static void -on_filter_added (GcrCollection *collection, - GObject *object, - gpointer user_data) -{ - guint *added = user_data; - g_assert (added != NULL); - (*added)++; -} - -static void -on_filter_removed (GcrCollection *collection, - GObject *object, - gpointer user_data) -{ - guint *removed = user_data; - g_assert (removed != NULL); - (*removed)++; -} - -static void -test_add_remove (Test *test, - gconstpointer unused) -{ - GcrCollection *collection; - guint modulo; - guint added = 0; - guint removed = 0; - guint i; - - modulo = 2; - collection = gcr_filter_collection_new_with_callback (test->underlying, - on_filter_modulo, &modulo, NULL); - - g_assert_cmpuint (gcr_collection_get_length (collection), ==, NUM_OBJECTS / modulo); - - g_signal_connect (collection, "added", G_CALLBACK (on_filter_added), &added); - g_signal_connect (collection, "removed", G_CALLBACK (on_filter_removed), &removed); - - for (i = 0; i < NUM_OBJECTS; i++) - gcr_simple_collection_remove (GCR_SIMPLE_COLLECTION (test->underlying), - test->objects[i]); - - g_assert_cmpuint (gcr_collection_get_length (collection), ==, 0); - g_assert_cmpuint (added, ==, 0); - g_assert_cmpuint (removed, ==, NUM_OBJECTS / modulo); - - for (i = 0; i < NUM_OBJECTS; i++) - gcr_simple_collection_add (GCR_SIMPLE_COLLECTION (test->underlying), - test->objects[i]); - - g_assert_cmpuint (gcr_collection_get_length (collection), ==, NUM_OBJECTS / modulo); - g_assert_cmpuint (added, ==, NUM_OBJECTS / modulo); - g_assert_cmpuint (removed, ==, NUM_OBJECTS / modulo); - - g_object_unref (collection); -} - -int -main (int argc, char **argv) -{ - g_test_init (&argc, &argv, NULL); - g_set_prgname ("test-filter-collection"); - - g_test_add ("/gcr/filter-collection/create", Test, NULL, setup, test_create, teardown); - g_test_add ("/gcr/filter-collection/callbacks", Test, NULL, setup, test_callbacks, teardown); - g_test_add ("/gcr/filter-collection/filtering", Test, NULL, setup, test_filtering, teardown); - g_test_add ("/gcr/filter-collection/add-remove", Test, NULL, setup, test_add_remove, teardown); - - return g_test_run (); -} diff --git a/gcr/test-gnupg-collection.c b/gcr/test-gnupg-collection.c deleted file mode 100644 index b12c399..0000000 --- a/gcr/test-gnupg-collection.c +++ /dev/null @@ -1,249 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ -/* - Copyright (C) 2010 Collabora Ltd - - The Gnome Keyring Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The Gnome Keyring Library 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 - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the Gnome Library; see the file COPYING.LIB. If not, - see <http://www.gnu.org/licenses/>. - - Author: Stef Walter <stefw@collabora.co.uk> -*/ - -#include "config.h" - -#include "gcr/gcr.h" -#include "gcr/gcr-gnupg-collection.h" -#include "gcr/gcr-gnupg-key.h" -#include "gcr/gcr-record.h" - -#include "egg/egg-testing.h" - -#include <glib.h> -#include <glib/gstdio.h> - -#include <errno.h> -#include <stdlib.h> -#include <string.h> - -typedef struct { - GcrGnupgCollection *collection; - gchar *directory; - GHashTable *keys; - GAsyncResult *result; -} Test; - -static void -on_collection_added (GcrCollection *collection, GObject *object, gpointer user_data) -{ - Test *test = user_data; - GcrGnupgKey *key; - const gchar *keyid; - - g_assert (GCR_COLLECTION (test->collection) == collection); - - g_assert (GCR_IS_GNUPG_KEY (object)); - key = GCR_GNUPG_KEY (object); - - keyid = _gcr_gnupg_key_get_keyid (key); - g_assert (keyid); - g_assert (!g_hash_table_lookup (test->keys, keyid)); - - g_hash_table_insert (test->keys, g_strdup (keyid), key); -} - -static void -on_collection_removed (GcrCollection *collection, GObject *object, gpointer user_data) -{ - Test *test = user_data; - GcrGnupgKey *key; - const gchar *keyid; - - g_assert (GCR_COLLECTION (test->collection) == collection); - g_assert (GCR_IS_GNUPG_KEY (object)); - - keyid = _gcr_gnupg_key_get_keyid (GCR_GNUPG_KEY (object)); - key = g_hash_table_lookup (test->keys, keyid); - g_assert (key == GCR_GNUPG_KEY (object)); - - if (!g_hash_table_remove (test->keys, keyid)) - g_assert_not_reached (); -} - -static void -setup (Test *test, gconstpointer unused) -{ - GcrCollection *collection; - GError *error = NULL; - gchar *cmd; - - test->directory = g_build_filename ("/tmp/gcr-tests.XXXXXX", NULL); - g_assert (g_mkdtemp_full (test->directory, 0700) != NULL); - - cmd = g_strdup_printf ("cp -p " SRCDIR "/gcr/fixtures/gnupg-homedir/* %s", test->directory); - g_spawn_check_exit_status (system (cmd), &error); - g_assert_no_error (error); - g_free (cmd); - - collection = _gcr_gnupg_collection_new (test->directory); - test->collection = GCR_GNUPG_COLLECTION (collection); - - test->keys = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); - g_signal_connect (collection, "added", G_CALLBACK (on_collection_added), test); - g_signal_connect (collection, "removed", G_CALLBACK (on_collection_removed), test); -} - -static void -teardown (Test *test, gconstpointer unused) -{ - GError *error = NULL; - gchar *cmd; - - g_hash_table_destroy (test->keys); - - if (test->result) - g_object_unref (test->result); - - g_object_unref (test->collection); - - /* remove potential gpg 2.1 extras, ignore any errors. */ - cmd = g_strdup_printf ("rm -rf %s/*.d", test->directory); - system (cmd); - g_free (cmd); - cmd = g_strdup_printf ("rm -f %s/.gpg-v21-migrated", test->directory); - system (cmd); - g_free (cmd); - - cmd = g_strdup_printf ("rm -f %s/*", test->directory); - g_spawn_check_exit_status (system (cmd), &error); - g_assert_no_error (error); - g_free (cmd); - - if (g_rmdir (test->directory) < 0) - g_critical ("couldn't remove %s: %s", test->directory, g_strerror (errno)); - g_free (test->directory); -} - -static void -on_async_ready (GObject *source, GAsyncResult *res, gpointer user_data) -{ - Test *test = user_data; - g_assert (G_OBJECT (test->collection) == source); - g_assert (test->result == NULL); - test->result = g_object_ref (res); - egg_test_wait_stop (); -} - -static void -test_properties (Test *test, gconstpointer unused) -{ - gchar *directory; - g_object_get (test->collection, "directory", &directory, NULL); - g_assert_cmpstr (directory, ==, test->directory); - g_free (directory); -} - -static void -test_load (Test *test, gconstpointer unused) -{ - GError *error = NULL; - GcrGnupgKey *key; - GList *l, *objects; - GcrRecord *record; - GHashTable *check; - - _gcr_gnupg_collection_load_async (test->collection, NULL, on_async_ready, test); - egg_test_wait_until (500000); - - g_assert (test->result); - _gcr_gnupg_collection_load_finish (test->collection, test->result, &error); - g_assert_no_error (error); - - /* Werner Koch (a public key) */ - key = g_hash_table_lookup (test->keys, "5DE249965B0358A2"); - g_assert (GCR_IS_GNUPG_KEY (key)); - g_assert (_gcr_gnupg_key_get_secret_records (key) == NULL); - - /* Test Number 2 (a secret key)*/ - key = g_hash_table_lookup (test->keys, "268FEE686262C395"); - g_assert (GCR_IS_GNUPG_KEY (key)); - g_assert (_gcr_gnupg_key_get_secret_records (key)); - - /* The length of collection should be correct */ - g_assert_cmpuint (g_hash_table_size (test->keys), ==, - gcr_collection_get_length (GCR_COLLECTION (test->collection))); - - /* The list of objects should be correct */ - objects = gcr_collection_get_objects (GCR_COLLECTION (test->collection)); - g_assert_cmpuint (g_hash_table_size (test->keys), ==, g_list_length (objects)); - check = g_hash_table_new (g_str_hash, g_str_equal); - for (l = objects; l != NULL; l = g_list_next (l)) { - g_assert (GCR_IS_GNUPG_KEY (l->data)); - key = g_hash_table_lookup (test->keys, _gcr_gnupg_key_get_keyid (l->data)); - g_assert (key == l->data); - g_hash_table_replace (check, (gchar*)_gcr_gnupg_key_get_keyid (l->data), ""); - } - g_assert_cmpuint (g_hash_table_size (check), ==, g_hash_table_size (test->keys)); - g_hash_table_destroy (check); - g_list_free (objects); - - /* Phillip R. Zimmerman's key should have a photo */ - key = g_hash_table_lookup (test->keys, "C7463639B2D7795E"); - g_assert (GCR_IS_GNUPG_KEY (key)); - record = _gcr_records_find (_gcr_gnupg_key_get_public_records (key), GCR_RECORD_SCHEMA_XA1); - g_assert (record); -} - -static void -test_reload (Test *test, gconstpointer unused) -{ - GError *error = NULL; - GcrGnupgKey *key; - - _gcr_gnupg_collection_load_async (test->collection, NULL, on_async_ready, test); - egg_test_wait_until (500000); - g_assert (test->result); - _gcr_gnupg_collection_load_finish (test->collection, test->result, &error); - g_assert_no_error (error); - - g_object_unref (test->result); - test->result = NULL; - - _gcr_gnupg_collection_load_async (test->collection, NULL, on_async_ready, test); - egg_test_wait_until (500000); - g_assert (test->result); - _gcr_gnupg_collection_load_finish (test->collection, test->result, &error); - g_assert_no_error (error); - - /* Werner Koch (a public key) */ - key = g_hash_table_lookup (test->keys, "5DE249965B0358A2"); - g_assert (GCR_IS_GNUPG_KEY (key)); - g_assert (_gcr_gnupg_key_get_secret_records (key) == NULL); - - /* Test Number 2 (a secret key)*/ - key = g_hash_table_lookup (test->keys, "268FEE686262C395"); - g_assert (GCR_IS_GNUPG_KEY (key)); - g_assert (_gcr_gnupg_key_get_secret_records (key)); -} - -int -main (int argc, char **argv) -{ - g_test_init (&argc, &argv, NULL); - g_set_prgname ("test-gnupg-collection"); - - g_test_add ("/gcr/gnupg-collection/properties", Test, NULL, setup, test_properties, teardown); - g_test_add ("/gcr/gnupg-collection/load", Test, NULL, setup, test_load, teardown); - g_test_add ("/gcr/gnupg-collection/reload", Test, NULL, setup, test_reload, teardown); - - return egg_tests_run_with_loop (); -} |