/* 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-1997 Spencer Kimball and Peter Mattis
 *
 * 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 "libpikaconfig/pikaconfig.h"

#include "core-types.h"

#include "config/pikacoreconfig.h"

#include "pika.h"
#include "pikadocumentlist.h"
#include "pikaimagefile.h"


G_DEFINE_TYPE (PikaDocumentList, pika_document_list, PIKA_TYPE_LIST)


static void
pika_document_list_class_init (PikaDocumentListClass *klass)
{
}

static void
pika_document_list_init (PikaDocumentList *list)
{
}

PikaContainer *
pika_document_list_new (Pika *pika)
{
  PikaDocumentList *document_list;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  document_list = g_object_new (PIKA_TYPE_DOCUMENT_LIST,
                                "name",          "document-list",
                                "children-type", PIKA_TYPE_IMAGEFILE,
                                "policy",        PIKA_CONTAINER_POLICY_STRONG,
                                NULL);

  document_list->pika = pika;

  return PIKA_CONTAINER (document_list);
}

PikaImagefile *
pika_document_list_add_file (PikaDocumentList *document_list,
                             GFile            *file,
                             const gchar      *mime_type)
{
  Pika          *pika;
  PikaImagefile *imagefile;
  PikaContainer *container;
  gchar         *uri;

  g_return_val_if_fail (PIKA_IS_DOCUMENT_LIST (document_list), NULL);
  g_return_val_if_fail (G_IS_FILE (file), NULL);

  container = PIKA_CONTAINER (document_list);

  pika = document_list->pika;

  uri = g_file_get_uri (file);

  imagefile = (PikaImagefile *) pika_container_get_child_by_name (container,
                                                                  uri);

  g_free (uri);

  if (imagefile)
    {
      pika_container_reorder (container, PIKA_OBJECT (imagefile), 0);
    }
  else
    {
      imagefile = pika_imagefile_new (pika, file);
      pika_container_add (container, PIKA_OBJECT (imagefile));
      g_object_unref (imagefile);
    }

  pika_imagefile_set_mime_type (imagefile, mime_type);

  if (pika->config->save_document_history)
    pika_recent_list_add_file (pika, file, mime_type);

  return imagefile;
}
