/*
     This file is part of GNUnet.
     Copyright (C) 2010, 2024 GNUnet e.V.

     GNUnet 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, or (at your
     option) any later version.

     GNUnet 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 GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file src/util/builder.c
 * @brief code for integration with GtkBuilder
 * @author Christian Grothoff
 */
#include "anastasis_gtk_config.h"
#include "anastasis_gtk_util.h"


void
ANASTASIS_GTK_set_icon_search_path (const struct GNUNET_OS_ProjectData *pd)
{
  char *buf;

  buf = GNUNET_OS_installation_get_path (pd,
                                         GNUNET_OS_IPK_ICONDIR);
  gtk_icon_theme_add_search_path (
    gtk_icon_theme_get_for_display (gdk_display_get_default ()),
    buf);
  GNUNET_free (buf);
}


const char *
ANASTASIS_GTK_get_data_dir (const struct GNUNET_OS_ProjectData *pd)
{
  static char *dd;

  if (NULL == dd)
    dd = GNUNET_OS_installation_get_path (pd,
                                          GNUNET_OS_IPK_DATADIR);
  return dd;
}


void
ANASTASIS_GTK_load_css (const struct GNUNET_OS_ProjectData *pd)
{
  GtkCssProvider *css;
  char *path;

  GNUNET_asprintf (&path,
                   "%s%s",
                   ANASTASIS_GTK_get_data_dir (pd),
                   "anastasis-gtk.css");
  css = gtk_css_provider_new ();
  gtk_css_provider_load_from_path (css,
                                   path);
  gtk_style_context_add_provider_for_display (
    gdk_display_get_default (),
    GTK_STYLE_PROVIDER (css),
    GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  GNUNET_free (path);
  g_object_unref (css);
}


GtkBuilder *
ANASTASIS_GTK_get_new_builder (const struct GNUNET_OS_ProjectData *pd,
                               const char *filename,
                               GObject *user_data)
{
  char *ui_path;
  GtkBuilder *ret;
  GError *error;

  ret = gtk_builder_new ();
  /* GtkBuilder connects the signals named in the file while it parses it, and
     passes the "current object" to the handlers as their user data, so this
     has to be set before anything is loaded. */
  gtk_builder_set_current_object (ret,
                                  (NULL != user_data)
                                  ? user_data
                                  : G_OBJECT (ret));
  GNUNET_asprintf (&ui_path,
                   "%s%s",
                   ANASTASIS_GTK_get_data_dir (pd),
                   filename);
  error = NULL;
  if (0 == gtk_builder_add_from_file (ret,
                                      ui_path,
                                      &error))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _ ("Failed to load `%s': %s\n"),
                ui_path,
                error->message);
    g_error_free (error);
    GNUNET_free (ui_path);
    g_object_unref (G_OBJECT (ret));
    return NULL;
  }
  GNUNET_free (ui_path);
  return ret;
}


/* end of builder.c */
