/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2001 Ximian, Inc. (www.ximian.com)
 *
 *  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 Street #330, Boston, MA 02111-1307, USA.
 *
 */

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

#include <gal/util/e-util.h>

#include "message-browser.h"

#include "mail.h"
#include "mail-callbacks.h"
#include "mail-tools.h"
#include "message-list.h"
#include "mail-ops.h"
#include "mail-vfolder.h"
#include "mail-autofilter.h"
#include "mail-mt.h"

#include "mail-local.h"
#include "mail-config.h"

#define d(x) x

#define MINIMUM_WIDTH  600
#define MINIMUM_HEIGHT 400

#define PARENT_TYPE (gnome_app_get_type ())

/* Size of the window last time it was changed.  */
static GtkAllocation last_allocation = { 0, 0 };

static GnomeAppClass *message_browser_parent_class;

static void
message_browser_destroy (GtkObject *object)
{
	MessageBrowser *message_browser;
	
	message_browser = MESSAGE_BROWSER (object);
	
	gtk_object_unref (GTK_OBJECT (message_browser->fb));
	
	gtk_widget_destroy (GTK_WIDGET (message_browser));
}

static void
message_browser_class_init (GtkObjectClass *object_class)
{
	object_class->destroy = message_browser_destroy;
	
	message_browser_parent_class = gtk_type_class (PARENT_TYPE);
}

static void
message_browser_init (GtkObject *object)
{
	
}

static void
message_browser_close (GtkWidget *menuitem, gpointer user_data)
{
	gtk_widget_destroy (GTK_WIDGET (user_data));
}

static void
message_browser_reply_to_sender (GtkWidget *widget, gpointer user_data)
{
	MessageBrowser *mb = MESSAGE_BROWSER (user_data);
	
	reply_to_sender (NULL, mb->fb);
}

static void
message_browser_reply_to_all (GtkWidget *widget, gpointer user_data)
{
	MessageBrowser *mb = MESSAGE_BROWSER (user_data);
	
	reply_to_all (NULL, mb->fb);
}

static void
message_browser_forward_msg (GtkWidget *widget, gpointer user_data)
{
	MessageBrowser *mb = MESSAGE_BROWSER (user_data);
	
	forward (NULL, mb->fb);
}

static void
message_browser_print_msg (GtkWidget *widget, gpointer user_data)
{
	MessageBrowser *mb = MESSAGE_BROWSER (user_data);
	
	mail_print_msg (mb->fb->mail_display);
}

static void
message_browser_delete_msg (GtkWidget *button, gpointer user_data)
{
	MessageBrowser *mb = MESSAGE_BROWSER (user_data);
	
	delete_msg (NULL, mb->fb);
}

static void
message_browser_next_msg (GtkWidget *button, gpointer user_data)
{
	MessageBrowser *mb = MESSAGE_BROWSER (user_data);
	int row;
	
	row = e_tree_row_of_node (mb->fb->message_list->tree,
				  e_tree_get_cursor (mb->fb->message_list->tree));
	
	message_list_select (mb->fb->message_list, row,
			     MESSAGE_LIST_SELECT_NEXT,
			     0, 0);
	
}

static void
message_browser_prev_msg (GtkWidget *button, gpointer user_data)
{
	MessageBrowser *mb = MESSAGE_BROWSER (user_data);
	int row;
	
	row = e_tree_row_of_node (mb->fb->message_list->tree,
				  e_tree_get_cursor (mb->fb->message_list->tree));
	
	message_list_select (mb->fb->message_list, row,
			     MESSAGE_LIST_SELECT_PREVIOUS,
			     0, 0);
	
}

static void
message_browser_message_loaded (FolderBrowser *fb, const char *uid, MessageBrowser *mb)
{
	CamelMimeMessage *message;
	char *subject = NULL;
	
	g_warning ("got 'message_loaded' event");
	
	message = fb->mail_display->current_message;
	
	if (message)
		subject = (char *) camel_mime_message_get_subject (message);
	
	gtk_window_set_title (GTK_WINDOW (mb), subject ? subject : "");
}

static void
message_browser_message_list_built (MessageList *ml, MessageBrowser *mb)
{
	const char *uid = gtk_object_get_data (GTK_OBJECT (mb), "uid");
	
	g_warning ("got 'message_list_built' event");
	
	message_list_select_uid (ml, uid);
}

static void
message_browser_folder_loaded (FolderBrowser *fb, const char *uri, MessageBrowser *mb)
{
	g_warning ("got 'folder_loaded' event for '%s'", uri);
	
	gtk_signal_connect (GTK_OBJECT (fb->message_list), "message_list_built",
			    message_browser_message_list_built, mb);
}

static GnomeUIInfo message_browser_toolbar [] = {
	
	/*GNOMEUIINFO_ITEM_STOCK (N_("Save"), N_("Save this message"),
	  save_msg, GNOME_STOCK_PIXMAP_SAVE),*/
	
	GNOMEUIINFO_ITEM_STOCK (N_("Reply"), N_("Reply to the sender of this message"),
				message_browser_reply_to_sender, GNOME_STOCK_PIXMAP_MAIL_RPL),
	
	GNOMEUIINFO_ITEM_STOCK (N_("Reply to All"), N_("Reply to all recipients of this message"),
				message_browser_reply_to_all, GNOME_STOCK_PIXMAP_MAIL_RPL),
	
	GNOMEUIINFO_ITEM_STOCK (N_("Forward"), N_("Forward this message"),
				message_browser_forward_msg, GNOME_STOCK_PIXMAP_MAIL_FWD),
	
	GNOMEUIINFO_SEPARATOR,
	
	GNOMEUIINFO_ITEM_STOCK (N_("Print"), N_("Print the selected message"),
				message_browser_print_msg, GNOME_STOCK_PIXMAP_PRINT),
	
	GNOMEUIINFO_ITEM_STOCK (N_("Delete"), N_("Delete this message"),
				message_browser_delete_msg, GNOME_STOCK_PIXMAP_TRASH),
	
	GNOMEUIINFO_SEPARATOR,
	
	GNOMEUIINFO_ITEM_STOCK (N_("Previous"), N_("Previous message"),
				message_browser_prev_msg, GNOME_STOCK_PIXMAP_BACK),
	
	GNOMEUIINFO_ITEM_STOCK (N_("Next"), N_("Next message"),
				message_browser_next_msg, GNOME_STOCK_PIXMAP_FORWARD),
	
	GNOMEUIINFO_END
};

static GnomeUIInfo file_menu[] = {
	/*GNOMEUIINFO_MENU_SAVE_ITEM (save, NULL),*/
	/*GNOMEUIINFO_MENU_SAVE_AS_ITEM (save_as, NULL),*/
	/*GNOMEUIINFO_SEPARATOR,*/
	GNOMEUIINFO_MENU_CLOSE_ITEM (message_browser_close, NULL),
	GNOMEUIINFO_END
};

static GnomeUIInfo view_menu[] =
{
	GNOMEUIINFO_END
};

static GnomeUIInfo message_browser_menubar[] =
{
	GNOMEUIINFO_MENU_FILE_TREE (file_menu),
	GNOMEUIINFO_MENU_VIEW_TREE (view_menu),
	GNOMEUIINFO_END
};

static void
set_default_size (GtkWidget *widget)
{
	int width, height;
	
	width  = MAX (MINIMUM_WIDTH, last_allocation.width);
	height = MAX (MINIMUM_HEIGHT, last_allocation.height);
	
	gtk_window_set_default_size (GTK_WINDOW (widget), width, height);
}

GtkWidget *
message_browser_new (const GNOME_Evolution_Shell shell, const char *uri, const char *uid)
{
	GtkWidget *vbox;
	MessageBrowser *new;
	FolderBrowser *fb;
	
	new = gtk_type_new (message_browser_get_type ());
	
	gnome_app_construct (GNOME_APP (new), "Evolution", "");
	
	gtk_object_set_data_full (GTK_OBJECT (new), "uid", g_strdup (uid), g_free);

	gnome_app_create_toolbar_with_data (GNOME_APP (new), message_browser_toolbar, new);
	gnome_app_create_menus_with_data (GNOME_APP (new), message_browser_menubar, new);
	
	fb = FOLDER_BROWSER (folder_browser_new (shell));
	new->fb = fb;
	
	/* some evil hackery action... */
	vbox = gtk_vbox_new (TRUE, 0);
	gtk_widget_ref (GTK_WIDGET (fb->mail_display));
	gtk_widget_reparent (GTK_WIDGET (fb->mail_display), vbox);
	gtk_widget_show (GTK_WIDGET (fb->mail_display));
	gtk_widget_show (vbox);
	
	gnome_app_set_contents (GNOME_APP (new), vbox);
	gtk_widget_grab_focus (GTK_WIDGET (MAIL_DISPLAY (fb->mail_display)->html));
	
	set_default_size (GTK_WIDGET (new));
	
	/* more evil hackery... */
	gtk_signal_connect (GTK_OBJECT (fb), "folder_loaded",
			    message_browser_folder_loaded, new);
	
	gtk_signal_connect (GTK_OBJECT (fb), "message_loaded",
			    message_browser_message_loaded, new);
	
	folder_browser_set_uri (fb, uri);
	
	return GTK_WIDGET (new);
}


E_MAKE_TYPE (message_browser, "MessageBrowser", MessageBrowser, message_browser_class_init,
	     message_browser_init, PARENT_TYPE);
<td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=5d4f6dbf6920e86fef1c7e3a8e60608b5d4f4764'>Convert a bunch of EXTRACT_SUFX=... into USES=tar:...</a></td><td>adamw</td><td><span title='2014-07-30 03:11:51 +0800'>2014-07-30</span></td><td>1</td><td><span class='deletions'>-2</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=c66ef665d5be7a409eb532493da02df114a64f0c'>- Update from 3.8.71 to 3.8.72</a></td><td>danilo</td><td><span title='2014-07-28 20:08:01 +0800'>2014-07-28</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=80c3e0da1a884d396b30f59581eeb4b0df92cd3d'>- Update from 3.8.70 to 3.8.71</a></td><td>danilo</td><td><span title='2014-07-27 00:59:43 +0800'>2014-07-27</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=ec91fead0b23f4386e2bef0a5ce5459c945006fd'>- Update from 3.8.69 to 3.8.70</a></td><td>danilo</td><td><span title='2014-07-08 07:53:12 +0800'>2014-07-08</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=f2cf28f69c60a14c717b18f9b5b7c386968e40d2'>- Update from 3.8.68 to 3.8.69</a></td><td>danilo</td><td><span title='2014-07-04 23:31:00 +0800'>2014-07-04</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=a65fd76bde1f003fdc65e03e1f379ebfeaacc7ba'>- Update from 3.8.67 to 3.8.68</a></td><td>danilo</td><td><span title='2014-07-02 23:22:07 +0800'>2014-07-02</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=ff8bed0138f0ba663089f7253d68d750a6ad633a'>- Update from 3.8.65 to 3.8.67</a></td><td>danilo</td><td><span title='2014-07-02 10:01:20 +0800'>2014-07-02</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=bb239f92f93cec1e5ec7534bbf65cb75fbcb1745'>- Update from 3.8.63 to 3.8.65</a></td><td>danilo</td><td><span title='2014-06-25 00:34:27 +0800'>2014-06-25</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=4f63fae2abf66f5be2d392d888ab795066c0dca8'>- Update from 3.8.61 to 3.8.63</a></td><td>danilo</td><td><span title='2014-06-06 23:41:18 +0800'>2014-06-06</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=0aaf14205f4f0d14b6f263dee08cc27d05e6a75b'>- Update from 3.8.60 to 3.8.61</a></td><td>danilo</td><td><span title='2014-05-31 12:10:40 +0800'>2014-05-31</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=6e72861e511da4a3707a9fa9c78f05f108b64e6f'>- Update from 3.8.59 to 3.8.60</a></td><td>danilo</td><td><span title='2014-05-28 23:52:22 +0800'>2014-05-28</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=ad9fa5dcfcb0cf4e227939701bbeaa463791f23e'>- Update from 3.8.58 to 3.8.59</a></td><td>danilo</td><td><span title='2014-05-28 23:15:17 +0800'>2014-05-28</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=365bc5e240f3cf18ccacb83f4305c6bb77851699'>- Update from 3.7.57 to 3.8.58</a></td><td>danilo</td><td><span title='2014-05-19 00:10:50 +0800'>2014-05-19</span></td><td>2</td><td><span class='deletions'>-5</span>/<span class='insertions'>+5</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=913b17d9cb27724626641f312872597e1171b23a'>- Update from 3.7.56 to 3.7.57</a></td><td>danilo</td><td><span title='2014-05-07 23:15:51 +0800'>2014-05-07</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/cad/xcircuit?h=gstreamer0.10-removal&amp;id=6d8a1467d4b61d19167f59aeec59e42923957c74'>- Update from 3.7.55 to 3.7.56</a></td><td>danilo</td><td><span title='2014-03-21 21:30:53 +0800'>2014-03-21</span>