/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * PikaTreeHandler
 * Copyright (C) 2009  Michael Natterer <mitch@gimp.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "core-types.h"

#include "pikacontainer.h"
#include "pikatreehandler.h"
#include "pikaviewable.h"


static void   pika_tree_handler_dispose          (GObject         *object);

static void   pika_tree_handler_freeze           (PikaTreeHandler *handler,
                                                  PikaContainer   *container);
static void   pika_tree_handler_thaw             (PikaTreeHandler *handler,
                                                  PikaContainer   *container);

static void   pika_tree_handler_add_container    (PikaTreeHandler *handler,
                                                  PikaContainer   *container);
static void   pika_tree_handler_add_foreach      (PikaViewable    *viewable,
                                                  PikaTreeHandler *handler);
static void   pika_tree_handler_add              (PikaTreeHandler *handler,
                                                  PikaViewable    *viewable,
                                                  PikaContainer   *container);

static void   pika_tree_handler_remove_container (PikaTreeHandler *handler,
                                                  PikaContainer   *container);
static void   pika_tree_handler_remove_foreach   (PikaViewable    *viewable,
                                                  PikaTreeHandler *handler);
static void   pika_tree_handler_remove           (PikaTreeHandler *handler,
                                                  PikaViewable    *viewable,
                                                  PikaContainer   *container);


G_DEFINE_TYPE (PikaTreeHandler, pika_tree_handler, PIKA_TYPE_OBJECT)

#define parent_class pika_tree_handler_parent_class


static void
pika_tree_handler_class_init (PikaTreeHandlerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = pika_tree_handler_dispose;
}

static void
pika_tree_handler_init (PikaTreeHandler *handler)
{
}

static void
pika_tree_handler_dispose (GObject *object)
{
  PikaTreeHandler *handler = PIKA_TREE_HANDLER (object);

  if (handler->container)
    {
      g_signal_handlers_disconnect_by_func (handler->container,
                                            pika_tree_handler_freeze,
                                            handler);
      g_signal_handlers_disconnect_by_func (handler->container,
                                            pika_tree_handler_thaw,
                                            handler);

      if (! pika_container_frozen (handler->container))
        pika_tree_handler_remove_container (handler, handler->container);

      g_clear_object (&handler->container);
      g_clear_pointer (&handler->signal_name, g_free);
    }

  G_OBJECT_CLASS (parent_class)->dispose (object);
}


/*  public functions  */

PikaTreeHandler *
pika_tree_handler_connect (PikaContainer *container,
                           const gchar   *signal_name,
                           GCallback      callback,
                           gpointer       user_data)
{
  PikaTreeHandler *handler;

  g_return_val_if_fail (PIKA_IS_CONTAINER (container), NULL);
  g_return_val_if_fail (signal_name != NULL, NULL);

  handler = g_object_new (PIKA_TYPE_TREE_HANDLER, NULL);

  handler->container   = g_object_ref (container);
  handler->signal_name = g_strdup (signal_name);
  handler->callback    = callback;
  handler->user_data   = user_data;

  if (! pika_container_frozen (container))
    pika_tree_handler_add_container (handler, container);

  g_signal_connect_object (container, "freeze",
                           G_CALLBACK (pika_tree_handler_freeze),
                           handler,
                           G_CONNECT_SWAPPED);
  g_signal_connect_object (container, "thaw",
                           G_CALLBACK (pika_tree_handler_thaw),
                           handler,
                           G_CONNECT_SWAPPED);

  return handler;
}

void
pika_tree_handler_disconnect (PikaTreeHandler *handler)
{
  g_return_if_fail (PIKA_IS_TREE_HANDLER (handler));

  g_object_run_dispose (G_OBJECT (handler));
  g_object_unref (handler);
}


/*  private functions  */

static void
pika_tree_handler_freeze (PikaTreeHandler *handler,
                          PikaContainer   *container)
{
  pika_tree_handler_remove_container (handler, container);
}

static void
pika_tree_handler_thaw (PikaTreeHandler *handler,
                        PikaContainer   *container)
{
  pika_tree_handler_add_container (handler, container);
}

static void
pika_tree_handler_add_container (PikaTreeHandler *handler,
                                 PikaContainer   *container)
{
  pika_container_foreach (container,
                          (GFunc) pika_tree_handler_add_foreach,
                          handler);

  g_signal_connect_object (container, "add",
                           G_CALLBACK (pika_tree_handler_add),
                           handler,
                           G_CONNECT_SWAPPED);
  g_signal_connect_object (container, "remove",
                           G_CALLBACK (pika_tree_handler_remove),
                           handler,
                           G_CONNECT_SWAPPED);
}

static void
pika_tree_handler_add_foreach (PikaViewable    *viewable,
                               PikaTreeHandler *handler)
{
  pika_tree_handler_add (handler, viewable, NULL);
}

static void
pika_tree_handler_add (PikaTreeHandler *handler,
                       PikaViewable    *viewable,
                       PikaContainer   *unused)
{
  PikaContainer *children = pika_viewable_get_children (viewable);

  g_signal_connect (viewable,
                    handler->signal_name,
                    handler->callback,
                    handler->user_data);

  if (children)
    pika_tree_handler_add_container (handler, children);
}

static void
pika_tree_handler_remove_container (PikaTreeHandler *handler,
                                    PikaContainer   *container)
{
  g_signal_handlers_disconnect_by_func (container,
                                        pika_tree_handler_add,
                                        handler);
  g_signal_handlers_disconnect_by_func (container,
                                        pika_tree_handler_remove,
                                        handler);

  pika_container_foreach (container,
                          (GFunc) pika_tree_handler_remove_foreach,
                          handler);
}

static void
pika_tree_handler_remove_foreach (PikaViewable    *viewable,
                                  PikaTreeHandler *handler)
{
  pika_tree_handler_remove (handler, viewable, NULL);
}

static void
pika_tree_handler_remove (PikaTreeHandler *handler,
                          PikaViewable    *viewable,
                          PikaContainer   *unused)
{
  PikaContainer *children = pika_viewable_get_children (viewable);

  if (children)
    pika_tree_handler_remove_container (handler, children);

  g_signal_handlers_disconnect_by_func (viewable,
                                        handler->callback,
                                        handler->user_data);
}
