<commit_before>/* nbtk-icon-view.c */

#include "nbtk-icon-view.h"
#include "nbtk-cell-renderer.h"
#include "nbtk-private.h"

G_DEFINE_TYPE (NbtkIconView, nbtk_icon_view, NBTK_TYPE_GRID)

#define ICON_VIEW_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), NBTK_TYPE_ICON_VIEW, NbtkIconViewPrivate))

typedef struct
{
  gchar *name;
  gint   col;
} AttributeData;

enum
{
  PROP_0,

  PROP_MODEL,
  PROP_RENDERER
};

struct _NbtkIconViewPrivate
{
  ClutterModel      *model;
  NbtkCellRenderer  *renderer;
  GSList            *attributes;

  gulong filter_changed;
  gulong row_added;
  gulong row_changed;
  gulong row_removed;
  gulong sort_changed;
};

/* gobject implementations */

static void
nbtk_icon_view_get_property (GObject    *object,
                             guint       property_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  NbtkIconViewPrivate *priv = NBTK_ICON_VIEW (object)->priv;
  switch (property_id)
    {
    case PROP_MODEL:
      g_value_set_object (value, priv->model);
      break;
    case PROP_RENDERER:
      g_value_set_object (value, priv->renderer);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
nbtk_icon_view_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  switch (property_id)
    {
    case PROP_MODEL:
      nbtk_icon_view_set_model ((NbtkIconView*) object,
                                (ClutterModel*) g_value_get_object (value));
    case PROP_RENDERER:
      nbtk_icon_view_set_cell_renderer ((NbtkIconView*) object,
                                        (NbtkCellRenderer*)
                                          g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
nbtk_icon_view_dispose (GObject *object)
{
  NbtkIconViewPrivate *priv = NBTK_ICON_VIEW (object)->priv;

  G_OBJECT_CLASS (nbtk_icon_view_parent_class)->dispose (object);

  if (priv->model)
    {
      g_object_unref (priv->model);
      priv->model = NULL;
    }

  if (priv->renderer)
    {
      g_object_unref (priv->renderer);
      priv->renderer = NULL;
    }
}

static void
nbtk_icon_view_finalize (GObject *object)
{
  NbtkIconViewPrivate *priv = NBTK_ICON_VIEW (object)->priv;

  G_OBJECT_CLASS (nbtk_icon_view_parent_class)->finalize (object);


  if (priv->attributes)
    {
      g_slist_foreach (priv->attributes, (GFunc) g_free, NULL);
      g_slist_free (priv->attributes);
      priv->attributes = NULL;
    }
}

static void
nbtk_icon_view_class_init (NbtkIconViewClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec *pspec;

  g_type_class_add_private (klass, sizeof (NbtkIconViewPrivate));

  object_class->get_property = nbtk_icon_view_get_property;
  object_class->set_property = nbtk_icon_view_set_property;
  object_class->dispose = nbtk_icon_view_dispose;
  object_class->finalize = nbtk_icon_view_finalize;

  pspec = g_param_spec_object ("model",
                               "model",
                               "The model for the icon view",
                               CLUTTER_TYPE_MODEL,
                               NBTK_PARAM_READWRITE);
  g_object_class_install_property (object_class, PROP_MODEL, pspec);

  pspec = g_param_spec_object ("cell-renderer",
                               "cell-renderer",
                               "The renderer to use for the icon view",
                               NBTK_TYPE_CELL_RENDERER,
                               NBTK_PARAM_READWRITE);
  g_object_class_install_property (object_class, PROP_RENDERER, pspec);
}

static void
nbtk_icon_view_init (NbtkIconView *icon_view)
{
  icon_view->priv = ICON_VIEW_PRIVATE (icon_view);
}


/* model monitors */
static void
model_changed_cb (ClutterModel *model,
                  NbtkIconView *icon_view)
{
  GSList *p;
  GList *l, *children;
  NbtkIconViewPrivate *priv = icon_view->priv;
  ClutterModelIter *iter;
  gint model_n, child_n;


  /* bail out if we don't yet have a renderer */
  if (!priv->renderer)
    return;

  children = clutter_container_get_children (CLUTTER_CONTAINER (icon_view));
  child_n = g_list_length (children);

  /* set the properties on the objects */

  iter = clutter_model_get_first_iter (priv->model);
  model_n = 0;

  /* We count the number of rows in the model ourselves, as
   * clutter_list_model_get_n_rows() does not currently take into account the
   * filtered model. See Clutter bug 1562.
   */
  while (!clutter_model_iter_is_last (iter))
    {
      GObject *child;

      model_n++;

      if (child_n < model_n)
        {
          ClutterActor *new_child;

          new_child = nbtk_cell_renderer_get_actor (priv->renderer);
          if (!CLUTTER_IS_ACTOR (new_child))
            {
              g_warning ("NbtkCellRenderer of type '%s' returned NULL "<commit_msg>[NbtkIconView] check the model is not empty before iterating it<commit_after>/* nbtk-icon-view.c */

#include "nbtk-icon-view.h"
#include "nbtk-cell-renderer.h"
#include "nbtk-private.h"

G_DEFINE_TYPE (NbtkIconView, nbtk_icon_view, NBTK_TYPE_GRID)

#define ICON_VIEW_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), NBTK_TYPE_ICON_VIEW, NbtkIconViewPrivate))

typedef struct
{
  gchar *name;
  gint   col;
} AttributeData;

enum
{
  PROP_0,

  PROP_MODEL,
  PROP_RENDERER
};

struct _NbtkIconViewPrivate
{
  ClutterModel      *model;
  NbtkCellRenderer  *renderer;
  GSList            *attributes;

  gulong filter_changed;
  gulong row_added;
  gulong row_changed;
  gulong row_removed;
  gulong sort_changed;
};

/* gobject implementations */

static void
nbtk_icon_view_get_property (GObject    *object,
                             guint       property_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  NbtkIconViewPrivate *priv = NBTK_ICON_VIEW (object)->priv;
  switch (property_id)
    {
    case PROP_MODEL:
      g_value_set_object (value, priv->model);
      break;
    case PROP_RENDERER:
      g_value_set_object (value, priv->renderer);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
nbtk_icon_view_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  switch (property_id)
    {
    case PROP_MODEL:
      nbtk_icon_view_set_model ((NbtkIconView*) object,
                                (ClutterModel*) g_value_get_object (value));
    case PROP_RENDERER:
      nbtk_icon_view_set_cell_renderer ((NbtkIconView*) object,
                                        (NbtkCellRenderer*)
                                          g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
nbtk_icon_view_dispose (GObject *object)
{
  NbtkIconViewPrivate *priv = NBTK_ICON_VIEW (object)->priv;

  G_OBJECT_CLASS (nbtk_icon_view_parent_class)->dispose (object);

  if (priv->model)
    {
      g_object_unref (priv->model);
      priv->model = NULL;
    }

  if (priv->renderer)
    {
      g_object_unref (priv->renderer);
      priv->renderer = NULL;
    }
}

static void
nbtk_icon_view_finalize (GObject *object)
{
  NbtkIconViewPrivate *priv = NBTK_ICON_VIEW (object)->priv;

  G_OBJECT_CLASS (nbtk_icon_view_parent_class)->finalize (object);


  if (priv->attributes)
    {
      g_slist_foreach (priv->attributes, (GFunc) g_free, NULL);
      g_slist_free (priv->attributes);
      priv->attributes = NULL;
    }
}

static void
nbtk_icon_view_class_init (NbtkIconViewClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec *pspec;

  g_type_class_add_private (klass, sizeof (NbtkIconViewPrivate));

  object_class->get_property = nbtk_icon_view_get_property;
  object_class->set_property = nbtk_icon_view_set_property;
  object_class->dispose = nbtk_icon_view_dispose;
  object_class->finalize = nbtk_icon_view_finalize;

  pspec = g_param_spec_object ("model",
                               "model",
                               "The model for the icon view",
                               CLUTTER_TYPE_MODEL,
                               NBTK_PARAM_READWRITE);
  g_object_class_install_property (object_class, PROP_MODEL, pspec);

  pspec = g_param_spec_object ("cell-renderer",
                               "cell-renderer",
                               "The renderer to use for the icon view",
                               NBTK_TYPE_CELL_RENDERER,
                               NBTK_PARAM_READWRITE);
  g_object_class_install_property (object_class, PROP_RENDERER, pspec);
}

static void
nbtk_icon_view_init (NbtkIconView *icon_view)
{
  icon_view->priv = ICON_VIEW_PRIVATE (icon_view);
}


/* model monitors */
static void
model_changed_cb (ClutterModel *model,
                  NbtkIconView *icon_view)
{
  GSList *p;
  GList *l, *children;
  NbtkIconViewPrivate *priv = icon_view->priv;
  ClutterModelIter *iter;
  gint model_n, child_n;


  /* bail out if we don't yet have a renderer */
  if (!priv->renderer)
    return;

  children = clutter_container_get_children (CLUTTER_CONTAINER (icon_view));
  child_n = g_list_length (children);

  /* set the properties on the objects */

  iter = clutter_model_get_first_iter (priv->model);
  if (!iter)
    return;

  model_n = 0;

  /* We count the number of rows in the model ourselves, as
   * clutter_list_model_get_n_rows() does not currently take into account the
   * filtered model. See Clutter bug 1562.
   */
  while (!clutter_model_iter_is_last (iter))
    {
      GObject *child;

      model_n++;

      if (child_n < model_n)
        {
          ClutterActor *new_child;

          new_child = nbtk_cell_renderer_get_actor (priv->renderer);
          if (!CLUTTER_IS_ACTOR (new_child))
            {
              g_warning ("NbtkCellRenderer of type '%s' returned NULL "
{'fraction_non_alphanumeric': 0.08731401722787784, 'fraction_numerical': 0.0011746280344557558, 'mean_word_length': 2.5013708019191228, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '730770', 'n_tokens_mistral': 3517, 'n_tokens_neox': 3460, 'n_words': 676}