/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2003 The GNOME Foundation.
 *
 * 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.
 *
 * Author(s):
 *      Paolo Borelli <pborelli@katamail.com>
 *
 */

/*
 * Modified by the gedit Team, 2003. See the AUTHORS file for a 
 * list of people on the gedit Team.  
 * See the ChangeLog files for a list of changes. 
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <libgnome/libgnome.h>
#include <glade/glade-xml.h>

#include "gedit2.h"
#include "gedit-mdi.h"
#include "gedit-file.h"
#include "gedit-dialogs.h"
#include "gedit-document.h"
#include "gedit-view.h"
#include "gedit-debug.h"
#include "gedit-utils.h"

#include <eel/eel-alert-dialog.h>

/*
 * Simple alert dialog displayed when closing a single unsaved document.
 */
gboolean
gedit_close_confirmation_dialog (GeditMDI *mdi, GeditMDIChild *child)
{
	GtkWidget *view;
	GtkWidget *dialog;
	gchar *name; 
	gchar *msg;
	gint ret;
	gboolean close;

	g_return_val_if_fail (GEDIT_IS_MDI (mdi), FALSE);
	g_return_val_if_fail (GEDIT_IS_MDI_CHILD (child), FALSE);

	view = GTK_WIDGET (g_list_nth_data (bonobo_mdi_child_get_views (BONOBO_MDI_CHILD (child)), 0));
	if (view)
	{
		GtkWindow *window;

		window = GTK_WINDOW (bonobo_mdi_get_window_from_view (view));
		gtk_window_present (window);
			
		bonobo_mdi_set_active_view (BONOBO_MDI (mdi), view);
	}

	name = gedit_document_get_short_name (child->document);
	msg = g_strdup_printf (_("Save changes to the document \"%s\" before closing?"),
				name);

	dialog = eel_alert_dialog_new (GTK_WINDOW (bonobo_mdi_get_active_window (BONOBO_MDI(mdi))),
				       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				       GTK_MESSAGE_WARNING,
				       GTK_BUTTONS_NONE,
				       msg,
				       _("Your changes will be lost if you don't save them."),
				       NULL);
	g_free (msg);
	g_free (name);

	gedit_dialog_add_button (GTK_DIALOG (dialog),
				 _("Do_n't save"), GTK_STOCK_NO, GTK_RESPONSE_NO);

	gtk_dialog_add_buttons (GTK_DIALOG (dialog),
				GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL);

	gtk_dialog_set_default_response	(GTK_DIALOG (dialog), GTK_RESPONSE_YES);

	ret = gtk_dialog_run (GTK_DIALOG (dialog));
	switch (ret)
	{
	case GTK_RESPONSE_YES:
		close = gedit_file_save (GEDIT_MDI_CHILD (child), TRUE);
		break;
	case GTK_RESPONSE_NO:
		close = TRUE;
		break;
	default:
		close = FALSE;
	}

	gtk_widget_destroy (dialog);

	return close;
}

enum
{
	SAVE_COLUMN,
	NAME_COLUMN,
	CHILD_COLUMN, /* a pointer to the gedit mdi child to easily save it */
	N_COLUMNS
};

static void
populate_model (GtkTreeModel *store, GList *children)
{
	GList *list;
	GtkTreeIter iter;

	for (list = children; list; list = list->next)
	{
		GeditDocument *doc;
		gchar *name;

		doc = GEDIT_MDI_CHILD (list->data)->document;
		name = gedit_document_get_short_name (doc);
		if (!name)
		{
			g_warning ("Can't get name from doc");
			continue;
		}

		gtk_list_store_append (GTK_LIST_STORE (store), &iter);
		gtk_list_store_set (GTK_LIST_STORE (store), &iter,
				    SAVE_COLUMN, TRUE,
				    NAME_COLUMN, name,
				    CHILD_COLUMN, list->data,
			            -1);

		g_free (name);
	}
}

static void
save_toggled (GtkCellRendererToggle *renderer, gchar *path_str, GtkTreeModel *store)
{
	GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
	GtkTreeIter iter;
	gboolean active;

	gtk_tree_model_get_iter (store, &iter, path);
	gtk_tree_model_get (store, &iter, SAVE_COLUMN, &active, -1);

	active ^= 1;

	gtk_list_store_set (GTK_LIST_STORE (store), &iter,
			    SAVE_COLUMN, active, -1);

	gtk_tree_path_free (path);
}

static gboolean
save_selected_children (GtkTreeModel *store)
{
	gboolean ret = TRUE;
	gboolean valid;
	GtkTreeIter iter;

	valid = gtk_tree_model_get_iter_first (store, &iter);

	while (valid)
	{
		gboolean saved = TRUE;
		gboolean to_save;
		GeditMDIChild *child;

		gtk_tree_model_get (store, &iter, 
				    SAVE_COLUMN, &to_save,
				    CHILD_COLUMN, &child,
				    -1);

		if (to_save)
			saved = gedit_file_save (child, TRUE);

		ret &= saved;

		valid = gtk_tree_model_iter_next (store, &iter);
	}

	return ret;
}

/*
 * Alert dialog with a list of unsaved documents.
 */
gboolean
gedit_close_all_confirmation_dialog (GeditMDI *mdi)
{
	GList *unsaved_children;
	GtkWidget *dialog;
	GladeXML *gui;
	GtkWidget *content;
	GtkListStore *store;
	GtkWidget *list_view;
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	gint ret;
	gboolean close;

	g_return_val_if_fail (GEDIT_IS_MDI (mdi), FALSE);

	unsaved_children = gedit_mdi_get_unsaved_children (mdi);
	if (!unsaved_children)
		return FALSE;

	/* If only one document, go for the simple alert dialog */
	if (g_list_length (unsaved_children) == 1)
		return gedit_close_confirmation_dialog (mdi,
				GEDIT_MDI_CHILD (unsaved_children->data));

	/* create and populate the model */
	store = gtk_list_store_new (N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_POINTER);
	populate_model (GTK_TREE_MODEL (store), unsaved_children);

	gui = glade_xml_new (GEDIT_GLADEDIR "close-confirmation.glade2",
			     "content", NULL);
	if (!gui)
	{
		gedit_warning (GTK_WINDOW (gedit_get_active_window ()),
			       MISSING_FILE,
			       GEDIT_GLADEDIR "close-confirmation.glade2");
		return FALSE;
	}

	content = glade_xml_get_widget (gui, "content");
	list_view = glade_xml_get_widget (gui, "list_view");
	if (!content || !list_view)
	{
		gedit_warning (GTK_WINDOW (gedit_get_active_window ()),
			       MISSING_WIDGETS,
			       GEDIT_GLADEDIR "close-confirmation.glade2");
		g_object_unref (gui);
		return FALSE;
	}

	/* the list view with 2 columns: a toggle and the name */
	gtk_tree_view_set_model (GTK_TREE_VIEW (list_view), GTK_TREE_MODEL (store));
	g_object_unref (store);

	renderer = gtk_cell_renderer_toggle_new ();
	g_signal_connect (renderer, "toggled",
			  G_CALLBACK (save_toggled), store);
	column = gtk_tree_view_column_new_with_attributes ("Save?",
							   renderer,
							   "active",
							   SAVE_COLUMN,
							   NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (list_view), column);

	renderer = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes ("Name",
							   renderer,
							   "text",
							   NAME_COLUMN,
							   NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (list_view), column);

	dialog = gtk_dialog_new ();
	gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
	gtk_window_set_transient_for (GTK_WINDOW (dialog),
				      GTK_WINDOW (gedit_get_active_window ()));

	gedit_dialog_add_button (GTK_DIALOG (dialog),
				 _("Save None"), GTK_STOCK_NO, GTK_RESPONSE_NO);

	gtk_dialog_add_button (GTK_DIALOG (dialog),
			       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);

	gedit_dialog_add_button (GTK_DIALOG (dialog),
				 _("Save Selected"), GTK_STOCK_SAVE, GTK_RESPONSE_YES);

	gtk_dialog_set_default_response	(GTK_DIALOG (dialog), GTK_RESPONSE_YES);

	gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), content);

	ret = gtk_dialog_run (GTK_DIALOG (dialog));
	switch (ret)
	{
	case GTK_RESPONSE_YES:
		close = save_selected_children (GTK_TREE_MODEL (store));
		break;
	case GTK_RESPONSE_NO:
		close = TRUE;
		break;
	default:
		close = FALSE;
	}

	gtk_widget_destroy (dialog);
	g_object_unref (gui);

	return close;
}

