/* Jungle Monkey
 * Copyright (C) 1999-2001  The Regents of the University of Michigan
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */



#include "ggui_properties.h"
#include "ggui.h"
#include "ggui_chat.h"
#include "ggui_search.h"

#include "util/util.h"
#include "util/jmgnet.h"


typedef struct _Properties
{
  GURL*	     url;
  GtkWidget* dialog;
  GtkLabel*  label;
  GtkText*   text;

} Properties;


static GHashTable* url_to_properties = NULL;

static void shutdown_hfunc (gpointer key, gpointer value, gpointer user_data);

static Properties* properties_new (const GURL* url);
static void properties_update (Properties* props);
static void properties_delete (Properties* props);

gboolean on_properties_dialog_destroy (GtkWidget* widget, gpointer user_data);
void	 on_properties_button_clicked (GtkButton* button);


/* **************************************** */

void
ggui_properties_init (void)
{
  url_to_properties = g_hash_table_new (gnet_url_hash, gnet_url_equal);
}


void
ggui_properties_shutdown (void)
{
  g_hash_table_foreach (url_to_properties, shutdown_hfunc, NULL);
  g_hash_table_destroy (url_to_properties);
  url_to_properties = NULL;
}


static void
shutdown_hfunc (gpointer key, gpointer value, gpointer user_data)
{
  Properties* props = (Properties*) value;
  GtkWidget* dialog;

  dialog = props->dialog;
  props->dialog = NULL;
  gtk_widget_destroy (dialog);

  properties_delete (props);
}


/* **************************************** */

void
ggui_properties_show (const GURL* url)
{
  Properties* props;

  g_return_if_fail (url);

  props = g_hash_table_lookup (url_to_properties, url);
  if (!props)
    {
      props = properties_new (url);
      g_return_if_fail (props);
    }

  properties_update (props);
  gtk_widget_show (props->dialog);
}


void
ggui_properties_update (const GURL* url)
{
  Properties* props;

  g_return_if_fail (url);

  props = g_hash_table_lookup (url_to_properties, url);
  if (!props)
    return;

  properties_update (props);
}


/* **************************************** */

static Properties*
properties_new (const GURL* url)
{
  gchar*      name;
  GladeXML*   xml;
  GtkWidget*  dialog;
  GtkLabel*   label;
  GtkText*    text;
  GtkObject*  button;
  Properties* props;
  gchar*      title;

  g_return_val_if_fail (url, NULL);

# ifdef JM_ENABLE_GNOME
    name = "properties_dialog_gnome";
# else
    name = "properties_dialog";
# endif

  xml = ggui_get_glade_xml (name, "properties_dialog.glade");
  g_return_val_if_fail (xml, NULL);
  glade_xml_signal_autoconnect (xml);

  dialog = GTK_WIDGET(glade_xml_get_widget(xml, name));
  label  = GTK_LABEL( glade_xml_get_widget(xml, "properties_label"));
  text   = GTK_TEXT(  glade_xml_get_widget(xml, "properties_text"));
  button = GTK_OBJECT(glade_xml_get_widget(xml, "properties_button"));
  g_return_val_if_fail (dialog && label && text && button, NULL);

  props = g_new0 (Properties, 1);
  props->url    = gnet_url_clone (url);
  props->dialog = dialog;
  props->label  = label;
  props->text   = text;

  gtk_object_set_user_data (GTK_OBJECT(dialog), props);
  gtk_object_set_user_data (button,  props);

  title = g_strdup_printf (_("%s Properties"), gnet_url_basename(url));
  gtk_window_set_title(GTK_WINDOW(dialog), title);
  g_free (title);

  GTK_WINDOW(dialog)->position = GTK_WIN_POS_MOUSE;

  g_hash_table_insert (url_to_properties, props->url, props);

  return props;
}


static void
properties_update (Properties* props)
{
  guint length;
  gchar* str = NULL;
  gchar* temp;
  gchar* url_str;

  g_return_if_fail (props);

  /* Set the label */
  gtk_label_set_text (props->label, gnet_url_basename(props->url));

  /* Delete everything in the text box */
  gtk_text_set_point (props->text, 0);
  length = gtk_text_get_length (props->text);
  gtk_text_forward_delete (props->text, length);

  /* Set text appropriate to object */
  if (!SAFESTRCMP(props->url->protocol, "jmchat"))
    str = ggui_chat_get_properties (props->url);
  else if (!SAFESTRCMP(props->url->protocol, "jmmsp"))
    str = ggui_search_get_properties (props->url);
  else
    str = g_strdup ("");

  /* Add URL */
  url_str = gnet_url_get_nice_string (props->url);
  temp = g_strdup_printf(_("%sURL: %s\n"), str, url_str);
  g_free (url_str);
  g_free (str); str = temp;

  /* Insert text */
  gtk_text_insert (props->text, NULL, NULL, NULL, str, strlen(str));
  g_free(str);
}


static void
properties_delete (Properties* props)
{
  g_return_if_fail (props);

  gnet_url_delete (props->url);
  g_free (props);
}



/* **************************************** */

gboolean
on_properties_dialog_destroy (GtkWidget* widget, gpointer user_data)
{
  Properties* props;

  props = (Properties*) gtk_object_get_user_data(GTK_OBJECT(widget));

  /* Only delete if dialog is set.  Otherwise, props would get
     deleted twice because the other delete functions call
     widget_destory. */
  if (props->dialog)
    {
      g_hash_table_remove (url_to_properties, props->url);
      properties_delete (props);
    }

  return TRUE;
}


void
on_properties_button_clicked (GtkButton* button)
{
  Properties* props;
  GtkWidget* dialog;

  props = (Properties*) gtk_object_get_user_data(GTK_OBJECT(button));
  dialog = props->dialog;
  props->dialog = NULL;
  gtk_widget_destroy (dialog);

  g_hash_table_remove (url_to_properties, props->url);
  properties_delete (props);
}


syntax highlighted by Code2HTML, v. 0.9.1