[gnome-photos] base-manager: Implement adding, removing and querying items



commit 8f29c2e11818f321a70c1d152bf666e0a6f58221
Author: Debarshi Ray <debarshir gnome org>
Date:   Mon Apr 30 23:49:01 2012 +0200

    base-manager: Implement adding, removing and querying items

 src/photos-base-manager.c |  139 ++++++++++++++++++++++++++++++++++++++++++++-
 src/photos-base-manager.h |   26 ++++++++-
 2 files changed, 160 insertions(+), 5 deletions(-)
---
diff --git a/src/photos-base-manager.c b/src/photos-base-manager.c
index 96abb11..50174ae 100644
--- a/src/photos-base-manager.c
+++ b/src/photos-base-manager.c
@@ -21,11 +21,15 @@
 
 #include "config.h"
 
+#include <glib.h>
+
 #include "photos-base-manager.h"
 
 
 struct _PhotosBaseManagerPrivate
 {
+  GHashTable *items;
+  PhotosBaseItem *active_item;
 };
 
 enum
@@ -43,18 +47,42 @@ G_DEFINE_TYPE (PhotosBaseManager, photos_base_manager, G_TYPE_OBJECT);
 
 
 static void
+photos_base_manager_dispose (GObject *object)
+{
+  PhotosBaseManager *self = PHOTOS_BASE_MANAGER (object);
+  PhotosBaseManagerPrivate *priv = self->priv;
+
+  if (priv->items != NULL)
+    {
+      g_hash_table_unref (priv->items);
+      priv->items = NULL;
+    }
+
+  g_clear_object (&priv->active_item);
+
+  G_OBJECT_CLASS (photos_base_manager_parent_class)->dispose (object);
+}
+
+
+static void
 photos_base_manager_init (PhotosBaseManager *self)
 {
   PhotosBaseManagerPrivate *priv;
 
   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, PHOTOS_TYPE_BASE_MANAGER, PhotosBaseManagerPrivate);
   priv = self->priv;
+
+  priv->items = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
 }
 
 
 static void
 photos_base_manager_class_init (PhotosBaseManagerClass *class)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->dispose = photos_base_manager_dispose;
+
   signals[ACTIVE_CHANGED] = g_signal_new ("active-changed",
                                           G_TYPE_FROM_CLASS (class),
                                           G_SIGNAL_RUN_LAST,
@@ -65,7 +93,7 @@ photos_base_manager_class_init (PhotosBaseManagerClass *class)
                                           g_cclosure_marshal_VOID__OBJECT,
                                           G_TYPE_NONE,
                                           1,
-                                          G_TYPE_OBJECT);
+                                          PHOTOS_TYPE_BASE_ITEM);
 
   signals[ITEM_ADDED] = g_signal_new ("item-added",
                                       G_TYPE_FROM_CLASS (class),
@@ -77,7 +105,7 @@ photos_base_manager_class_init (PhotosBaseManagerClass *class)
                                       g_cclosure_marshal_VOID__OBJECT,
                                       G_TYPE_NONE,
                                       1,
-                                      G_TYPE_OBJECT);
+                                      PHOTOS_TYPE_BASE_ITEM);
 
   signals[ITEM_REMOVED] = g_signal_new ("item-removed",
                                         G_TYPE_FROM_CLASS (class),
@@ -89,7 +117,7 @@ photos_base_manager_class_init (PhotosBaseManagerClass *class)
                                         g_cclosure_marshal_VOID__OBJECT,
                                         G_TYPE_NONE,
                                         1,
-                                        G_TYPE_OBJECT);
+                                        PHOTOS_TYPE_BASE_ITEM);
 
   g_type_class_add_private (class, sizeof (PhotosBaseManagerPrivate));
 }
@@ -100,3 +128,108 @@ photos_base_manager_new (void)
 {
   return g_object_new (PHOTOS_TYPE_BASE_MANAGER, NULL);
 }
+
+
+void
+photos_base_manager_add_item (PhotosBaseManager *self, PhotosBaseItem *item)
+{
+  const gchar *id = photos_base_item_get_id (item);
+
+  g_object_ref (item);
+  g_hash_table_insert (self->priv->items, (gpointer) id, item);
+  g_signal_emit (self, signals[ITEM_ADDED], 0, item);
+}
+
+
+void
+photos_base_manager_clear (PhotosBaseManager *self)
+{
+  PhotosBaseManagerPrivate *priv = self->priv;
+
+  g_hash_table_remove_all (priv->items);
+  g_clear_object (&priv->active_item);
+}
+
+
+PhotosBaseItem *
+photos_base_manager_get_active_item (PhotosBaseManager *self)
+{
+  return self->priv->active_item;
+}
+
+
+PhotosBaseItem *
+photos_base_manager_get_item_by_id (PhotosBaseManager *self, const gchar *id)
+{
+  return g_hash_table_lookup (self->priv->items, id);
+}
+
+
+GHashTable *
+photos_base_manager_get_items (PhotosBaseManager *self)
+{
+  return self->priv->items;
+}
+
+
+guint
+photos_base_manager_get_items_count (PhotosBaseManager *self)
+{
+  GList *keys;
+  guint count;
+
+  keys = g_hash_table_get_keys (self->priv->items);
+  count = g_list_length (keys);
+  g_list_free (keys);
+  return count;
+}
+
+
+void
+photos_base_manager_remove_item (PhotosBaseManager *self, PhotosBaseItem *item)
+{
+  const gchar *id = photos_base_item_get_id (item);
+  photos_base_manager_remove_item_by_id (self, id);
+}
+
+
+void
+photos_base_manager_remove_item_by_id (PhotosBaseManager *self, const gchar *id)
+{
+  PhotosBaseItem *item;
+
+  item = photos_base_manager_get_item_by_id (self, id);
+  if (item == NULL)
+    return;
+
+  g_signal_emit (self, signals[ITEM_REMOVED], 0, item);
+  g_hash_table_remove (self->priv->items, id);
+}
+
+
+gboolean
+photos_base_manager_set_active_item (PhotosBaseManager *self, PhotosBaseItem *item)
+{
+  PhotosBaseManagerPrivate *priv = self->priv;
+
+  if (item == priv->active_item)
+    return FALSE;
+
+  if (priv->active_item != NULL)
+    g_object_unref (priv->active_item);
+
+  g_object_ref (item);
+  priv->active_item = item;
+  g_signal_emit (self, signals[ACTIVE_CHANGED], 0, item);
+  return TRUE;
+}
+
+
+gboolean
+photos_base_manager_set_active_item_by_id (PhotosBaseManager *self, const gchar *id)
+{
+  PhotosBaseItem *item;
+
+  item = photos_base_manager_get_item_by_id (self, id);
+  return photos_base_manager_set_active_item (self, item);
+}
diff --git a/src/photos-base-manager.h b/src/photos-base-manager.h
index 76ac5a5..7c8aaa7 100644
--- a/src/photos-base-manager.h
+++ b/src/photos-base-manager.h
@@ -23,6 +23,8 @@
 
 #include <glib-object.h>
 
+#include "photos-base-item.h"
+
 G_BEGIN_DECLS
 
 #define PHOTOS_TYPE_BASE_MANAGER (photos_base_manager_get_type ())
@@ -65,9 +67,29 @@ struct _PhotosBaseManagerClass
   void (*item_removed)   (PhotosBaseManager *self);
 };
 
-GType               photos_base_manager_get_type           (void) G_GNUC_CONST;
+GType               photos_base_manager_get_type               (void) G_GNUC_CONST;
+
+PhotosBaseManager  *photos_base_manager_new                    (void);
+
+void                photos_base_manager_add_item               (PhotosBaseManager *self, PhotosBaseItem *item);
+
+void                photos_base_manager_clear                  (PhotosBaseManager *self);
+
+PhotosBaseItem     *photos_base_manager_get_active_item        (PhotosBaseManager *self);
+
+PhotosBaseItem     *photos_base_manager_get_item_by_id         (PhotosBaseManager *self, const gchar *id);
+
+GHashTable         *photos_base_manager_get_items              (PhotosBaseManager *self);
+
+guint               photos_base_manager_get_items_count        (PhotosBaseManager *self);
+
+void                photos_base_manager_remove_item            (PhotosBaseManager *self, PhotosBaseItem *item);
+
+void                photos_base_manager_remove_item_by_id      (PhotosBaseManager *self, const gchar *id);
+
+gboolean            photos_base_manager_set_active_item        (PhotosBaseManager *self, PhotosBaseItem *item);
 
-PhotosBaseManager  *photos_base_manager_new                (void);
+gboolean            photos_base_manager_set_active_item_by_id  (PhotosBaseManager *self, const gchar *id);
 
 G_END_DECLS
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]