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

/* 
 * Copyright (C) 2006 Peter Enseleit
 * 
 * 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 av.
 *
 * 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:  Peter Enseleit <penseleit@gmail.com>
 *          Roberto Majadas <telemaco@openshine.com>
 * 	    Sebastian Menge <bastl@users.sf.net>
 */

#include "config.h"
#include "../nautilus-sendto-plugin.h"
#include <string.h>
#include <glib/gi18n-lib.h>

static GHashTable *hash = NULL;

static 
gboolean init (NstPlugin *plugin)
{
	gchar *a_cmd;

	printf ("Init alpine plugin\n");
	hash = g_hash_table_new (g_str_hash, g_str_equal);

	a_cmd = g_find_program_in_path ("alpine");
	if (a_cmd == NULL)
		return FALSE;
	return TRUE;
}

static
GtkWidget* get_contacts_widget (NstPlugin *plugin)
{
		
	GtkWidget *entry;
	
	entry = gtk_entry_new ();
	
	return entry;
}

static
gboolean send_files (NstPlugin *plugin, GtkWidget *contact_widget,
			GList *file_list)
{
	gchar *t_cmd, *a_cmd, *cmd, *send_to;
	GList *l;
	GString *mailto;

	send_to = (gchar *) gtk_entry_get_text (GTK_ENTRY(contact_widget));
		
	if (strlen (send_to) == 0)
	{
		mailto = g_string_new("");
	}
	else
	{
		mailto = g_string_new("");
		g_string_append_printf (mailto, " %s ", send_to);		
	}
	
	a_cmd = g_find_program_in_path ("alpine");
	if (a_cmd == NULL)
		return FALSE;
	
	t_cmd = g_find_program_in_path ("gnome-terminal");
	if (t_cmd == NULL)
		return FALSE;
	
	for (l = file_list; l; l=l->next){				
		//TODO better hack needed to strip of gnome-vfs handler "file://"
		g_string_append_printf (mailto,"-attach %s ",(l->data)+7);
	}

	cmd = g_strdup_printf ("%s -x %s %s", t_cmd, a_cmd, mailto->str);
	g_spawn_command_line_async (cmd, NULL);

	g_free (cmd);
	g_string_free (mailto, TRUE);
	g_free (a_cmd);
	return TRUE;
}

static 
gboolean destroy (NstPlugin *plugin){
	return TRUE;
}

static 
NstPluginInfo plugin_info = {
	"stock_mail",
	"alpine",
	N_("Email (Alpine)"),
	FALSE,
	init,
	get_contacts_widget,
	NULL,
	send_files,
	destroy
}; 

NST_INIT_PLUGIN (plugin_info)
