/* -*- 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>


gboolean
gedit_close_confirmation_dialog (GeditMDI *mdi, GeditMDIChild *child)
{
	GtkWidget *view;
	GtkWidget *dialog;
	gchar *name; 
	gchar *msg;
	gint ret;
	gboolean close;

	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,
	N_COLUMNS
};

gboolean
gedit_close_all_confirmation_dialog (GeditMDI *mdi)
{
	GList *children;
	GtkWidget *dialog;
	GtkListStore *store;
	GtkWidget *list_view;
	GtkTreeIter iter;
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	GList *list;
	gint ret;
	gboolean close;

	children = bonobo_mdi_get_children (BONOBO_MDI (mdi));

	if (g_list_length (children) == 1)
		return gedit_close_confirmation_dialog (mdi, GEDIT_MDI_CHILD (children->data));

	/* create and populate the model */
	store = gtk_list_store_new (N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_STRING);
	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 (store, &iter);
		gtk_list_store_set (store, &iter,
				    SAVE_COLUMN, TRUE,
				    NAME_COLUMN, name,
			            -1);

		g_free (name);
	}

	/* the view with 2 columns: a toggle and the name */
	list_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
	g_object_unref (store);

	renderer = gtk_cell_renderer_toggle_new ();
//	g_signal_connect (renderer, "toggled",
//			  G_CALLBACK (save_toggled), model);
	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 = 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,
				       _("Save the following documents?"),
				       _("If you don't save, changes will be discarded."),
				       NULL);

	gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
			    list_view, TRUE, TRUE, 0);
	gtk_widget_show (list_view);

	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);

	ret = gtk_dialog_run (GTK_DIALOG (dialog));
	switch (ret)
	{
	case GTK_RESPONSE_YES:
//		close = gedit_file_save... save all selected docs
		close = TRUE;
		break;
	case GTK_RESPONSE_NO:
		close = TRUE;
		break;
	default:
		return FALSE;
	}

	gtk_widget_destroy (dialog);

	return close;
}

