/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  Engrampa
 *
 *  Copyright (C) 2001 The Free Software Foundation, Inc.
 *
 *  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
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>
#include <string.h>
#include <gtk/gtk.h>
#include "dlg-long-filename.h"
#include "file-utils.h"
#include "fr-window.h"
#include "glib-utils.h"
#include "gtk-utils.h"


typedef struct {
	GtkWidget      *dialog;
	GtkWidget      *file_list_view;
	GtkListStore   *file_list_store;
	FrWindow       *window;
	gboolean        user_choice;
} LongFilenameDialogData;


/* called when the dialog is closed. */
static void
destroy_cb (GtkWidget  *widget,
	    LongFilenameDialogData *data)
{
	g_free (data);
}


static void
long_filename_dialog__response_cb (GtkWidget  *dialog,
				  int         response_id,
				  LongFilenameDialogData *data)
{
	switch (response_id) {
	case GTK_RESPONSE_OK:
		data->user_choice = TRUE;
		break;
	default:
		data->user_choice = FALSE;
		break;
	}

	gtk_widget_destroy (data->dialog);
}


static void
populate_file_list (LongFilenameDialogData *data, GList *long_files)
{
	GList *scan;
	GtkTreeIter iter;

	for (scan = long_files; scan; scan = scan->next) {
		gtk_list_store_append (data->file_list_store, &iter);
		gtk_list_store_set (data->file_list_store, &iter,
				    0, scan->data,
				    -1);
	}
}


gboolean
dlg_long_filename_show (FrWindow *window, GList *long_files)
{
	LongFilenameDialogData *data;
	GtkWidget *scrolled_window;
	GtkWidget *label;
	GtkWidget *hbox;
	GtkWidget *vbox;
	GtkWidget *image;
	GtkWidget *content_area;
	GtkTreeViewColumn *column;
	GtkCellRenderer *renderer;
	char *message;
	char *secondary_message;
	int file_count;

	data = g_new0 (LongFilenameDialogData, 1);
	data->window = window;
	data->user_choice = FALSE;

	/* Count long files */
	file_count = g_list_length (long_files);

	/* Create dialog */
	data->dialog = gtk_dialog_new_with_buttons (_("Long Filename Warning"),
						    GTK_WINDOW (window),
						    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
						    _("Continue"), GTK_RESPONSE_OK,
						    _("Cancel"), GTK_RESPONSE_CANCEL,
						    NULL);

	content_area = gtk_dialog_get_content_area (GTK_DIALOG (data->dialog));

	/* Create main message */
	message = g_strdup_printf (_("The current file contains %d long filename files (>255 characters)."), file_count);
	secondary_message = g_strdup (_("These files may not be extracted correctly. Do you want to continue?"));

	/* Create image and label */
	image = gtk_image_new_from_icon_name ("dialog-warning", GTK_ICON_SIZE_DIALOG);
	gtk_widget_set_valign (image, GTK_ALIGN_START);

	label = gtk_label_new ("");
	gtk_label_set_markup (GTK_LABEL (label),
			      g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s",
					       message, secondary_message));
	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
	gtk_label_set_selectable (GTK_LABEL (label), TRUE);

	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 24);
	gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);

	gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);

	gtk_box_pack_start (GTK_BOX (content_area), hbox, FALSE, FALSE, 0);

	/* Create file list */
	vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
	gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);

	label = gtk_label_new (_("Long filename files:"));
	gtk_label_set_xalign (GTK_LABEL (label), 0.0);
	gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

	/* Create scrolled window with tree view */
	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_min_content_height (scrolled_window, 200);
	gtk_scrolled_window_set_max_content_height (scrolled_window, 300);

	/* Create list store and tree view */
	data->file_list_store = gtk_list_store_new (1, G_TYPE_STRING);
	data->file_list_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (data->file_list_store));

	renderer = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes (_("Filename"),
							   renderer,
							   "text", 0,
							   NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (data->file_list_view), column);

	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (data->file_list_view), FALSE);

	gtk_container_add (GTK_CONTAINER (scrolled_window), data->file_list_view);
	gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);

	gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);

	/* Populate file list */
	populate_file_list (data, long_files);

	/* Set signal handlers */
	g_signal_connect (G_OBJECT (data->dialog),
			  "destroy",
			  G_CALLBACK (destroy_cb),
			  data);

	g_signal_connect (G_OBJECT (data->dialog),
			  "response",
			  G_CALLBACK (long_filename_dialog__response_cb),
			  data);

	/* Show dialog */
	gtk_widget_show_all (data->dialog);

	/* Run dialog */
	gtk_dialog_run (GTK_DIALOG (data->dialog));

	/* Clean up */
	g_free (message);
	g_free (secondary_message);

	return data->user_choice;
}