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

/* 
 * Copyright (C) 2008 Jader Henrique da Silva
 * 
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301  USA.
 *
 * Author:  Jader Henrique Silva <vovozito@gmail.com>
 */

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

static GFile *burn = NULL;

static
gboolean init (NstPlugin *plugin)
{
	GVfs *gvfs;
	GFileInfo *burn_info;
	GFileType type;

	g_print ("Init nautilus burn plugin\n");

	burn = g_file_new_for_uri ("burn:/");

	burn_info = g_file_query_info (burn,
			G_FILE_ATTRIBUTE_STANDARD_TYPE,
			G_FILE_QUERY_INFO_NONE,
			NULL,
			NULL);

	if (burn_info == NULL)
		return FALSE;

	type = g_file_info_get_file_type (burn_info);

	g_object_unref (burn_info);

	if (type != G_FILE_TYPE_DIRECTORY)
		return FALSE;

	return TRUE;

}

static
GtkWidget* get_contacts_widget (NstPlugin *plugin)
{
	GtkWidget *cb;
	GtkCellRenderer *renderer;
	GtkListStore *store;
	GtkTreeModel *model;
	GtkTreeIter *iter;
	GdkPixbuf *icon;
	GtkIconTheme *it;
	GFileEnumerator *fenum;
	GFileInfo *file_info = NULL;

	it = gtk_icon_theme_get_default ();
	icon = gtk_icon_theme_load_icon (it,
					"nautilus-cd-burner",
					16,
					GTK_ICON_LOOKUP_USE_BUILTIN,
					NULL);
	iter = g_malloc (sizeof(GtkTreeIter));
	store = gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
	gtk_list_store_append (store, iter);
	gtk_list_store_set (store, iter, 0, icon, 1, _("Current CD/DVD"), -1);
	gtk_list_store_append (store, iter);
	gtk_list_store_set (store, iter, 0, icon, 1, _("New CD/DVD"), -1);
	g_free (iter);
	model = GTK_TREE_MODEL (store);
	cb = gtk_combo_box_new_with_model (model);
	renderer = gtk_cell_renderer_pixbuf_new ();
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cb),
                                    renderer,
                                    FALSE);
        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (cb), 
					renderer,
                                        "pixbuf", 0,
                                        NULL);		
        renderer = gtk_cell_renderer_text_new ();
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cb),
                                    renderer,
                                    TRUE);
        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (cb), 
					renderer,
                                        "text", 1,
                                        NULL);

	gtk_combo_box_set_active (GTK_COMBO_BOX (cb), 1);

	fenum = g_file_enumerate_children (burn,
				G_FILE_ATTRIBUTE_STANDARD_NAME,
				G_FILE_QUERY_INFO_NONE,
				NULL,
				NULL);

	if (fenum != NULL)
	{
		file_info = g_file_enumerator_next_file (fenum, NULL, NULL);
		g_object_unref (fenum);
	}

	if (file_info == NULL)
	{
		gtk_combo_box_set_active (GTK_COMBO_BOX (cb), 1);
		gtk_widget_set_sensitive (cb, FALSE);
	}
	else
	{
		gtk_combo_box_set_active (GTK_COMBO_BOX (cb), 0);
		g_object_unref (file_info);
	}

	return cb;
}

static
gboolean send_files (NstPlugin *plugin, GtkWidget *burntype_widget,
			GList *file_list)
{
	GFileEnumerator *fenum;
	GFileInfo *file_info;
	GFile *child, *destination;
	GList *l;
	char *path;

	if( gtk_combo_box_get_active (GTK_COMBO_BOX (burntype_widget)) == 1 )
	{
		fenum = g_file_enumerate_children (burn,
				G_FILE_ATTRIBUTE_STANDARD_NAME,
				G_FILE_QUERY_INFO_NONE,
				NULL,
				NULL);

		if (fenum != NULL)
		{
			while ( (file_info = g_file_enumerator_next_file
							(fenum, NULL, NULL)) )
			{
				child = g_file_get_child (burn,
					g_file_info_get_name(file_info));

				g_object_unref (file_info);
				g_file_delete (child, NULL, NULL);
				g_object_unref (child);
			}
			g_object_unref (fenum);
		}
	}

	for (l = file_list ; l; l=l->next){
		child = g_file_new_for_uri ((const char *)l->data);
		file_info = g_file_query_info (child,
					G_FILE_ATTRIBUTE_STANDARD_NAME,
					G_FILE_QUERY_INFO_NONE,
					NULL,
					NULL);

		if (file_info != NULL)
		{
			path = g_strdup_printf("burn:///%s",
					g_file_info_get_name(file_info));
			g_object_unref(file_info);

			destination = g_file_new_for_uri ((const char *)path);
			g_free(path);

			g_file_copy (child,
				destination,
				G_FILE_COPY_OVERWRITE,
				NULL,
				NULL,
				NULL,
				NULL);
			g_object_unref (destination);
		}
		g_object_unref (child);
	}
	return TRUE;
}

static 
gboolean destroy (NstPlugin *plugin){

	g_object_unref (burn);
	return TRUE;

}

static 
NstPluginInfo plugin_info = {
	"nautilus-cd-burner",
	"nautilus-burn",
	N_("CD/DVD Creator"),
	FALSE,
	init,
	get_contacts_widget,
	NULL,
	send_files,
	destroy
}; 

NST_INIT_PLUGIN (plugin_info)


