/*
This file is part of Gnome Photo Printer.

    Gnome Photo Printer 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.

    Gnome Photo Printer 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 Gnome Photo Printer; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <gnome.h>
#include <glade/glade.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libgnomeprint/gnome-print.h>
#include <libgnomeprint/gnome-print-job.h>
#include <libgnomeprintui/gnome-print-dialog.h>
#include "gpp_print.h"
#include "gpp_functions.h"
#include "gpp_widget.h"
#include "gpp_callbacks.h"
#include "gpp.h"

/* Callback for the Drag and Drop stuff */
void
drag_received (GtkWidget * widget, GdkDragContext * context, gint x, gint y,
	       GtkSelectionData * data, guint info, guint time)
{
	gchar *file_name;
	GList *list = NULL;
	GList *help = NULL;
	GtkTreeIter iter;
	GtkTreeModel *tree;
	GnomeVFSResult result;
	GnomeVFSFileInfo *file_info;
	
	tree=gpp_definition->file_names;
	
	list = gnome_vfs_uri_list_parse (data->data);
	help = list;

	while (help != NULL)
	{
		/* Get Filename from drop */
		file_name=gnome_vfs_uri_to_string ((const GnomeVFSURI*) (help->data),GNOME_VFS_URI_HIDE_TOPLEVEL_METHOD);
		
		/* Get File information and check for image !!! */
		file_info=gnome_vfs_file_info_new();
		result=gnome_vfs_get_file_info(file_name,file_info,GNOME_VFS_FILE_INFO_GET_MIME_TYPE);
	
		if(file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_TYPE)
			switch(file_info->type) {
				case GNOME_VFS_FILE_TYPE_REGULAR:
					if(file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE){
						if(g_strrstr(file_info->mime_type,"image/")!=NULL){
							/* Add File to gpp_definition->file_names_n (GtkTree) */ 
							gtk_tree_store_append(GTK_TREE_STORE(tree) ,&iter,NULL);
							gtk_tree_store_set(GTK_TREE_STORE(tree), &iter,FILE_COLUMN,file_name,-1);
						} else g_print("No Picture!\n");
					}
					
					break;
				default:
					g_print("No File!\n");	break;
			}
		
		help = help->next;
	}
	
	/* Update gpp_definition->number_of_files */
	if(gtk_tree_model_get_iter_first (gpp_definition->file_names, &iter)!=FALSE)
	{
		gpp_definition->number_of_files = gtk_tree_model_iter_n_children (gpp_definition->file_names,NULL);
	}
	
	gnome_vfs_file_info_unref (file_info);
	gnome_vfs_uri_list_free (list);
}

/* Exit the Program */
void
func_gpp_quit (GtkWidget * widget, gpointer data)
{
	gtk_main_quit ();
}

/* Removes Files from GList and GTreeStore ( gpp_definition->file_names(_n) )*/
void
func_gpp_remove (GtkWidget * widget, gpointer data)
{
	gchar *file_name;
	GtkTreeIter iter;
	GtkTreeSelection *select;
	
	select=gtk_tree_view_get_selection(gpp_definition->treeview);
	
	if(gtk_tree_selection_get_selected(select,&gpp_definition->file_names,&iter)) {
		gtk_tree_model_get(gpp_definition->file_names,&iter,FILE_COLUMN,&file_name,-1);
		
/* Debug information
		g_printf("File to remove: ->%s<-\n",file_name);
*/		
		gpp_definition->number_of_files--;
		
		gtk_tree_store_remove(GTK_TREE_STORE(gpp_definition->file_names),&iter);
		g_free(file_name);
	}		
}

/* Callback for the Preview Button,
   Closes the print job, creates the preview and after all a new print job */
void
func_gpp_preview (GtkWidget * widget, gpointer data)
{
	if (gpp_get_settings_from_libgnomeprint () != FALSE)
	{
		gpp_context = gnome_print_job_get_context (gpp_job);
		gpp_arrange_pictures ();
		gnome_print_job_close (gpp_job);
	}
	gtk_widget_show(GTK_WIDGET(gnome_print_job_preview_new(gpp_job,"Preview!")));
	
	/* Unreference the old Gnome Print Stuff */
	g_object_unref (G_OBJECT (gpp_context));
	/* Create new Print Job */
	gpp_job = gnome_print_job_new (gpp_config);
}

/* Callback for the Print Button */
void
func_gpp_print (GtkWidget * widget, gpointer data)
{	
	if (gpp_get_settings_from_libgnomeprint () != FALSE)
	{
		gpp_context = gnome_print_job_get_context (gpp_job);
		gpp_arrange_pictures ();
		gpp_start_printing ();
	}
	gtk_main_quit();
}

/* Callback for the Layout Button */
void
func_gpp_layout_1 (GtkWidget * widget, gpointer data)
{
	gpp_definition->layout = LAYOUT_1;
	gpp_definition->layout_x = gpp_calculate_size_metric_to_dpi (20);
	gpp_definition->layout_y = gpp_calculate_size_metric_to_dpi (25);
}

void
func_gpp_layout_2 (GtkWidget * widget, gpointer data)
{
	gpp_definition->layout = LAYOUT_2;
	gpp_definition->layout_x = gpp_calculate_size_metric_to_dpi (15);
	gpp_definition->layout_y = gpp_calculate_size_metric_to_dpi (20);
}

void
func_gpp_layout_3 (GtkWidget * widget, gpointer data)
{
	gpp_definition->layout = LAYOUT_3;
	gpp_definition->layout_x = gpp_calculate_size_metric_to_dpi (13);
	gpp_definition->layout_y = gpp_calculate_size_metric_to_dpi (18);
}

void
func_gpp_layout_4 (GtkWidget * widget, gpointer data)
{
	gpp_definition->layout = LAYOUT_4;
	gpp_definition->layout_x = gpp_calculate_size_metric_to_dpi (9);
	gpp_definition->layout_y = gpp_calculate_size_metric_to_dpi (13);
}

void
func_gpp_layout_5 (GtkWidget * widget, gpointer data)
{
	gpp_definition->layout = LAYOUT_5;
	gpp_definition->layout_x = gpp_calculate_size_metric_to_dpi (6);
	gpp_definition->layout_y = gpp_calculate_size_metric_to_dpi (9);
}

void
func_gpp_layout_6 (GtkWidget * widget, gpointer data)
{
	gpp_definition->layout = LAYOUT_6;
	gpp_definition->layout_x = gpp_calculate_size_metric_to_dpi (4);
	gpp_definition->layout_y = gpp_calculate_size_metric_to_dpi (5.5);
}

void func_gpp_image_1(GtkWidget *widget, gpointer data)
{
	gpp_definition->image_scale_with_ratio=TRUE;
}

void func_gpp_image_2(GtkWidget *widget, gpointer data)
{
	gpp_definition->image_scale_with_ratio=FALSE;
}

