/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 *  Authors: Chenthill Palanisamy (pchenthill@novell.com)
 *
 *  Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of version 2 of the GNU General Public
 *  License as published by the Free Software Foundation.
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <e-util/e-config.h>
#include <mail/em-popup.h>
#include <mail/mail-ops.h>
#include <mail/mail-mt.h>
#include <camel/camel-vee-folder.h>
#include "e-util/e-error.h"

#define PRIMARY_TEXT \
	N_("Also mark messages in subfolders?")
#define SECONDARY_TEXT \
	N_("Do you want to mark messages as read in the current folder " \
	   "only, or in the current folder as well as all subfolders?")

void org_gnome_mark_all_read (EPlugin *ep, EMPopupTargetFolder *target);
static void mar_got_folder (char *uri, CamelFolder *folder, void *data);
static void mar_all_sub_folders (CamelStore *store, CamelFolderInfo *fi, CamelException *ex);

static void
button_clicked_cb (GtkButton *button,
                   GtkDialog *dialog)
{
	gpointer response;

	response = g_object_get_data (G_OBJECT (button), "response");
	gtk_dialog_response (dialog, GPOINTER_TO_INT (response));
}

static void
box_mapped_cb (GtkWidget *box,
               GtkWidget *label)
{
	GtkRequisition requisition;

	/* In order to get decent line wrapping we need to wait until the
	 * box containing the buttons is mapped, and then resize the label
	 * to the same width as the box. */
	gtk_widget_size_request (box, &requisition);
	gtk_widget_set_size_request (label, requisition.width, -1);
}

static gint
prompt_user (void)
{
	GtkWidget *container;
	GtkWidget *dialog;
	GtkWidget *label1;
	GtkWidget *label2;
	GtkWidget *table;
	GtkWidget *widget;
	GtkWidget *vbox;
	gchar *markup;
	gint response;

	dialog = gtk_dialog_new ();
	gtk_widget_hide (GTK_DIALOG (dialog)->action_area);
	gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
	gtk_window_set_title (GTK_WINDOW (dialog), "");
	g_signal_connect (
		dialog, "map",
		G_CALLBACK (gtk_widget_queue_resize), NULL);
	gtk_container_set_border_width (GTK_CONTAINER (dialog), 12);
	vbox = GTK_DIALOG (dialog)->vbox;

	/* Table */
	widget = gtk_table_new (3, 2, FALSE);
	gtk_table_set_row_spacings (GTK_TABLE (widget), 12);
	gtk_table_set_col_spacings (GTK_TABLE (widget), 12);
	gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
	gtk_widget_show (widget);

	table = widget;

	/* Question Icon */
	widget = gtk_image_new_from_stock (
		GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
	gtk_misc_set_alignment (GTK_MISC (widget), 0.5, 0.0);
	gtk_table_attach (
		GTK_TABLE (table), widget, 0, 1, 0, 3,
		0, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_widget_show (widget);

	/* Primary Text */
	markup = g_markup_printf_escaped (
		"<big><b>%s</b></big>", gettext (PRIMARY_TEXT));
	widget = gtk_label_new (markup);
	gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
	gtk_label_set_use_markup (GTK_LABEL (widget), TRUE);
	gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.0);
	gtk_table_attach (
		GTK_TABLE (table), widget, 1, 2, 0, 1,
		0, 0, 0, 0);
	gtk_widget_show (widget);
	g_free (markup);
	label1 = widget;

	/* Secondary Text */
	widget = gtk_label_new (gettext (SECONDARY_TEXT));
	gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
	gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.0);
	gtk_table_attach (
		GTK_TABLE (table), widget, 1, 2, 1, 2,
		0, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_widget_show (widget);
	label2 = widget;

	/* Action Area */
	widget = gtk_hbox_new (FALSE, 6);
	g_signal_connect (
		widget, "map",
		G_CALLBACK (box_mapped_cb), label1);
	g_signal_connect (
		widget, "map",
		G_CALLBACK (box_mapped_cb), label2);
	gtk_table_attach (
		GTK_TABLE (table), widget, 1, 2, 2, 3,
		GTK_EXPAND | GTK_FILL, 0, 0, 0);
	gtk_widget_show (widget);

	container = widget;

	/* Cancel button */
	widget = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
	g_object_set_data (
		G_OBJECT (widget), "response",
		GINT_TO_POINTER (GTK_RESPONSE_CANCEL));
	g_signal_connect (
		widget, "clicked",
		G_CALLBACK (button_clicked_cb), dialog);
	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
	gtk_widget_show (widget);

	/* "Current Folder and Subfolders" button */
	widget = gtk_button_new_with_mnemonic (
		_("Current Folder and _Subfolders"));
	g_object_set_data (
		G_OBJECT (widget), "response",
		GINT_TO_POINTER (GTK_RESPONSE_YES));
	g_signal_connect (
		widget, "clicked",
		G_CALLBACK (button_clicked_cb), dialog);
	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
	gtk_widget_show (widget);

	/* "Current Folder Only" button */
	widget = gtk_button_new_with_mnemonic (
		_("Current _Folder Only"));
	g_object_set_data (
		G_OBJECT (widget), "response",
		GINT_TO_POINTER (GTK_RESPONSE_NO));
	g_signal_connect (
		widget, "clicked",
		G_CALLBACK (button_clicked_cb), dialog);
	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
	gtk_widget_show (widget);

	response = gtk_dialog_run (GTK_DIALOG (dialog));

	gtk_widget_destroy (dialog);

	return response;
}

void
org_gnome_mark_all_read (EPlugin *ep, EMPopupTargetFolder *t)
{
	if (t->uri == NULL) {
		return;
	}

	mail_get_folder(t->uri, 0, mar_got_folder, NULL, mail_msg_unordered_push);
}

static void
mark_all_as_read (CamelFolder *folder)
{
	GPtrArray *uids;
	int i;

	uids =  camel_folder_get_uids (folder);
	camel_folder_freeze(folder);
	for (i=0;i<uids->len;i++)
		camel_folder_set_message_flags(folder, uids->pdata[i], CAMEL_MESSAGE_SEEN, CAMEL_MESSAGE_SEEN);
	camel_folder_thaw(folder);
	camel_folder_free_uids (folder, uids);
}

static void
mar_got_folder (char *uri, CamelFolder *folder, void *data)
{
	CamelFolderInfo *info;
	CamelStore *store;
	CamelException ex;
	gint response;
	guint32 flags = CAMEL_STORE_FOLDER_INFO_RECURSIVE | CAMEL_STORE_FOLDER_INFO_FAST;

	camel_exception_init (&ex);

	/* FIXME we have to disable the menu item */
	if (!folder)
		return;

	store = folder->parent_store;
	info = camel_store_get_folder_info (store, folder->full_name, flags, &ex);

	if (camel_exception_is_set (&ex)) {
		camel_exception_clear (&ex);
		return;
	}

	if (info && (info->child || info->next))
		response = prompt_user ();
	else
		response = GTK_RESPONSE_NO;

	if (response == GTK_RESPONSE_NO)
		mark_all_as_read (folder);
	else if (response == GTK_RESPONSE_YES)
		mar_all_sub_folders (store, info, &ex);
}

static void
mar_all_sub_folders (CamelStore *store, CamelFolderInfo *fi, CamelException *ex)
{
	while (fi) {
		CamelFolder *folder;

		if (fi->child) {
			mar_all_sub_folders (store, fi->child, ex);
			if (camel_exception_is_set (ex))
				return;
		}

		if (!(folder = camel_store_get_folder (store, fi->full_name, 0, ex)))
			return;

		if (!CAMEL_IS_VEE_FOLDER (folder)) {
			mark_all_as_read (folder);
		}

		fi = fi->next;
	}
}
itgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/p5-DBIx-Simple?h=mate-1.16&amp;id=e80f0953d18006170971019cc3936a2de46081ab'>Replace ugly "@unexec rmdir %D... 2&gt;/dev/null || true" with @dirrmtry</a></td><td>edwin</td><td><span title='2006-01-22 09:29:46 +0800'>2006-01-22</span>d>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+2</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/graphics/crw?h=gstreamer0.10-removal&amp;id=7da1cbf527091ba94734a63282875e03f1dc9dff'>Fix build with custom LOCALBASE.</a></td><td>itetcu</td><td><span title='2009-06-09 20:50:25 +0800'>2009-06-09</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+5</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/graphics/crw?h=gstreamer0.10-removal&amp;id=206915c722d239998b6f01e6c900d71cbc40c68b'>- Add SHA256</a></td><td>pav</td><td><span title='2005-11-26 09:48:16 +0800'>2005-11-26</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/graphics/crw?h=gstreamer0.10-removal&amp;id=4f8e31ecef91ccb1560dd3a17896401dcd9b3cf8'>Use PLIST_FILES.</a></td><td>trevor</td><td><span title='2004-02-06 04:38:07 +0800'>2004-02-06</span></td><td>2</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/graphics/crw?h=gstreamer0.10-removal&amp;id=6a12ba5ef35b1ee6d77dc1ddfaab71d27d03b652'>SIZEify.</a></td><td>trevor</td><td><span title='2004-01-29 15:24:56 +0800'>2004-01-29</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/graphics/crw?h=gstreamer0.10-removal&amp;id=6e94ba23a2e1cf0c7a6f7cbc0fa00b535d3d30dc'>- Use canonical form (ports@FreeBSD.org) in MAINTAINER line</a></td><td>sergei</td><td><span title='2003-12-21 08:23:25 +0800'>2003-12-21</span></td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/graphics/crw?h=gstreamer0.10-removal&amp;id=919796a4fc541dda5e83148761e916b665ea821f'>Reset maintainer on ports formerly maintained by dillon</a></td><td>kris</td><td><span title='2003-05-13 06:10:12 +0800'>2003-05-13</span></td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/graphics/crw?h=gstreamer0.10-removal&amp;id=9ddd3b82d6dc17d4f1d24406279c66dc9f23b128'>Clear moonlight beckons.</a></td><td>ade</td><td><span title='2003-03-07 14:14:21 +0800'>2003-03-07</span></td><td>2</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/graphics/crw?h=gstreamer0.10-removal&amp;id=0477daedcafab56edea72dafa282d75906ba9529'>Add missing package dependancy.</a></td><td>dillon</td><td><span title='2002-09-10 06:00:10 +0800'>2002-09-10</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+2</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/graphics/crw?h=gstreamer0.10-removal&amp;id=3f3b20aa2f956864fb122575f680817ec8f4161e'>Update to 1.03.  Source cleanups for better portability (no program flow</a></td><td>dillon</td><td><span title='2002-07-23 07:24:48 +0800'>2002-07-23</span></td><td>2</td><td><span class='deletions'>-4</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/graphics/crw?h=gstreamer0.10-removal&amp;id=4cb503f2683b71568df2cc5a0bd18386886cd9d9'>Bump to version 1.02.  Fix a bunch of bugs related to .ppm output and the</a></td><td>dillon</td><td><span title='2002-06-18 10:16:05 +0800'>2002-06-18</span></td><td>2</td><td><span class='deletions'>-2</span>/<span class='insertions'>+2</span></td></tr>